diff --git a/includes/admin/assets/js/um-admin-field.js b/includes/admin/assets/js/um-admin-field.js index 3e9591a8..0ff7ef15 100644 --- a/includes/admin/assets/js/um-admin-field.js +++ b/includes/admin/assets/js/um-admin-field.js @@ -35,22 +35,20 @@ jQuery(document).ready(function() { /* Add a Field */ jQuery(document.body).on('submit', 'form.um_add_field', function(e){ - e.preventDefault(); - var conditions = jQuery('.um-admin-cur-condition'); - //need fields refactor - jQuery(conditions).each( function ( i ) { - - if ( jQuery( this ).find('[id^="_conditional_action"]').val() === '' || - jQuery( this ).find('[id^="_conditional_field"]').val() === '' || - jQuery( this ).find('[id^="_conditional_operator"]').val() ==='' ) - { - jQuery(conditions[i]).find('.um-admin-remove-condition').trigger('click'); - } - } ); - conditions = jQuery('.um-admin-cur-condition'); - jQuery(conditions).each( function ( i ) { - var id = i === 0 ? '' : i; + var conditions = jQuery('.um-admin-cur-condition'); + //need fields refactor + jQuery(conditions).each( function ( i ) { + if ( jQuery( this ).find('[id^="_conditional_action"]').val() === '' || + jQuery( this ).find('[id^="_conditional_field"]').val() === '' || + jQuery( this ).find('[id^="_conditional_operator"]').val() ==='' ) + { + jQuery(conditions[i]).find('.um-admin-remove-condition').trigger('click'); + } + } ); + conditions = jQuery('.um-admin-cur-condition'); + jQuery(conditions).each( function ( i ) { + var id = i === 0 ? '' : i; jQuery( this ).find('[id^="_conditional_action"]').attr('name', '_conditional_action' + id); jQuery( this ).find('[id^="_conditional_action"]').attr('id', '_conditional_action' + id); @@ -60,8 +58,7 @@ jQuery(document).ready(function() { jQuery( this ).find('[id^="_conditional_operator"]').attr('id', '_conditional_operator' + id); jQuery( this ).find('[id^="_conditional_value"]').attr('name', '_conditional_value' + id); jQuery( this ).find('[id^="_conditional_value"]').attr('id', '_conditional_value' + id); - - } ); + } ); var form = jQuery(this); jQuery.ajax({ @@ -78,40 +75,34 @@ jQuery(document).ready(function() { complete: function(){ form.css({'opacity': 1}); }, - success: function(data){ - if (data.error){ - - c = 0; - jQuery.each(data.error, function(i, v){ - c++; - if ( c == 1 ) { - form.find('#'+i).addClass('um-admin-error').trigger('focus'); - form.find('.um-admin-error-block').show().html(v); - } - }); - - um_admin_modal_responsive(); + success: function( response ){ + if ( response.success ) { + let data = response.data; + if ( data.error ) { + let c = 0; + jQuery.each( data.error, function(i, v){ + c++; + if ( 1 === c ) { + form.find('#' + i).addClass('um-admin-error').trigger('focus'); + form.find('.um-admin-error-block').show().html(v); + } + }); + um_admin_modal_responsive(); + } else { + jQuery('.um-col-demon-settings').data('in_row', '').data('in_sub_row', '').data('in_column', '').data('in_group', ''); + um_admin_remove_modal(); + um_admin_update_builder(); + } } else { - - jQuery('.um-col-demon-settings').data('in_row', ''); - jQuery('.um-col-demon-settings').data('in_sub_row', ''); - jQuery('.um-col-demon-settings').data('in_column', ''); - jQuery('.um-col-demon-settings').data('in_group', ''); - - um_admin_remove_modal(); - um_admin_update_builder(); - + console.log( response ); } - }, - error: function(data){ - console.log(data); + error: function( response ){ + console.log( response ); } }); - return false; - }); -}); \ No newline at end of file +}); diff --git a/includes/admin/core/class-admin-builder.php b/includes/admin/core/class-admin-builder.php index d48ec3db..5283fa49 100644 --- a/includes/admin/core/class-admin-builder.php +++ b/includes/admin/core/class-admin-builder.php @@ -583,12 +583,14 @@ if ( ! class_exists( 'um\admin\core\Admin_Builder' ) ) { $output['error'] = null; + // phpcs:disable WordPress.Security.NonceVerification -- Already verified by `UM()->admin()->check_ajax_nonce()` $array = array( 'field_type' => sanitize_key( $_POST['_type'] ), 'form_id' => absint( $_POST['post_id'] ), 'args' => UM()->builtin()->get_core_field_attrs( sanitize_key( $_POST['_type'] ) ), 'post' => UM()->admin()->sanitize_builder_field_meta( $_POST ), ); + // phpcs:enable WordPress.Security.NonceVerification -- Already verified by `UM()->admin()->check_ajax_nonce()` /** * Filters the field data before save in Form Builder. @@ -628,110 +630,82 @@ if ( ! class_exists( 'um\admin\core\Admin_Builder' ) ) { * add_filter( 'um_admin_field_update_error_handling', 'my_custom_um_admin_field_update_error_handling', 10, 2 ); */ $output['error'] = apply_filters( 'um_admin_field_update_error_handling', $output['error'], $array ); - - /** - * @var $_metakey - * @var $post_id - */ - extract( $array['post'] ); - if ( empty( $output['error'] ) ) { - - $save = array(); - $save[ $_metakey ] = null; + $save = array(); + $field_id = $array['post']['_metakey']; // Set field ID as it's metakey. + $save[ $field_id ] = null; foreach ( $array['post'] as $key => $val ) { + if ( '' !== $val && '_' === substr( $key, 0, 1 ) ) { // field attribute + $new_key = ltrim( $key, '_' ); - if ( substr( $key, 0, 1 ) === '_' && $val !== '' ) { // field attribute - $new_key = ltrim ( $key, '_' ); - - if ( $new_key == 'options' ) { - //$save[ $_metakey ][$new_key] = explode(PHP_EOL, $val); - $save[ $_metakey ][ $new_key ] = preg_split( '/[\r\n]+/', $val, -1, PREG_SPLIT_NO_EMPTY ); + if ( 'options' === $new_key ) { + $save[ $field_id ][ $new_key ] = preg_split( '/[\r\n]+/', $val, -1, PREG_SPLIT_NO_EMPTY ); } else { - $save[ $_metakey ][ $new_key ] = $val; + $save[ $field_id ][ $new_key ] = $val; } - - } elseif ( strstr( $key, 'um_editor' ) ) { - + } elseif ( false !== strpos( $key, 'um_editor' ) ) { if ( 'block' === $array['post']['_type'] ) { - $save[ $_metakey ]['content'] = wp_kses_post( $val ); + $save[ $field_id ]['content'] = wp_kses_post( $val ); } else { - $save[ $_metakey ]['content'] = sanitize_textarea_field( $val ); + $save[ $field_id ]['content'] = sanitize_textarea_field( $val ); } } - } - $field_ID = $_metakey; - $field_args = $save[ $_metakey ]; - /** - * UM hook + * Filters the field options before save to form on the update field in Form Builder. * - * @type filter - * @title um_admin_pre_save_field_to_form - * @description Change field options before save to form - * @input_vars - * [{"var":"$field_args","type":"array","desc":"Field Options"}] - * @change_log - * ["Since: 2.0"] - * @usage add_filter( 'um_admin_pre_save_field_to_form', 'function_name', 10, 1 ); - * @example - * Force change the field's metakey when store it to DB for the form. + * function my_custom_um_admin_pre_save_field_to_form( $field_args ) { + * $field_args['metakey'] = {new_metakey}; * return $field_args; * } - * ?> + * add_filter( 'um_admin_pre_save_field_to_form', 'my_custom_um_admin_pre_save_field_to_form' ); */ - $field_args = apply_filters( 'um_admin_pre_save_field_to_form', $field_args ); + $field_args = apply_filters( 'um_admin_pre_save_field_to_form', $save[ $field_id ] ); - UM()->fields()->update_field( $field_ID, $field_args, $post_id ); + UM()->fields()->update_field( $field_id, $field_args, $array['post']['post_id'] ); /** - * UM hook + * Filters the field options before save to DB (globally) on the update field in Form Builder. * - * @type filter - * @title um_admin_pre_save_field_to_db - * @description Change field options before save to DB - * @input_vars - * [{"var":"$field_args","type":"array","desc":"Field Options"}] - * @change_log - * ["Since: 2.0"] - * @usage add_filter( 'um_admin_pre_save_field_to_db', 'function_name', 10, 1 ); - * @example - * Force change the field's metakey when store it to DB globally. + * function my_custom_um_admin_pre_save_field_to_db( $field_args ) { + * $field_args['metakey'] = {new_metakey}; * return $field_args; * } - * ?> + * add_filter( 'um_admin_pre_save_field_to_db', 'my_custom_um_admin_pre_save_field_to_db' ); */ $field_args = apply_filters( 'um_admin_pre_save_field_to_db', $field_args ); if ( ! isset( $array['args']['form_only'] ) ) { - if ( ! isset( UM()->builtin()->predefined_fields[ $field_ID ] ) ) { - UM()->fields()->globally_update_field( $field_ID, $field_args ); + if ( ! isset( UM()->builtin()->predefined_fields[ $field_id ] ) ) { + UM()->fields()->globally_update_field( $field_id, $field_args ); } } } - $output = json_encode( $output ); - if ( is_array( $output ) ) { - print_r( $output ); - } else { - echo $output; - } - die; + wp_send_json_success( $output ); } - /** * */ - function dynamic_modal_content() { + public function dynamic_modal_content() { UM()->admin()->check_ajax_nonce(); if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) {