Пример #1
0
 function ajax_validate_save_post()
 {
     // validate
     if (!isset($_POST['_acfnonce'])) {
         // ignore validation, this form $_POST was not correctly configured
         die;
     }
     // success
     if (acf_validate_save_post()) {
         $json = array('result' => 1, 'message' => __('Validation successful', 'acf'), 'errors' => 0);
         die(json_encode($json));
     }
     // fail
     $json = array('result' => 0, 'message' => __('Validation failed', 'acf'), 'errors' => acf_get_validation_errors());
     // update message
     $i = count($json['errors']);
     $json['message'] .= '. ' . sprintf(_n('1 required field below is empty', '%s required fields below are empty', $i, 'acf'), $i);
     // return
     die(json_encode($json));
 }
Пример #2
0
 function validate_save_post($show_errors = false)
 {
     // action
     do_action('acf/validate_save_post');
     // vars
     $errors = acf_get_validation_errors();
     // bail ealry if no errors
     if (!$errors) {
         return true;
     }
     // show errors
     if ($show_errors) {
         $message = '<h2>' . __('Validation failed', 'acf') . '</h2>';
         $message .= '<ul>';
         foreach ($errors as $error) {
             $message .= '<li>' . $error['message'] . '</li>';
         }
         $message .= '</ul>';
         // die
         wp_die($message, 'Validation failed');
     }
     // return
     return false;
 }
Пример #3
0
 function ajax_validate_save_post()
 {
     // bail early if _acfnonce is missing
     if (!isset($_POST['_acfnonce'])) {
         wp_send_json_error();
     }
     // vars
     $json = array('valid' => 1, 'errors' => 0);
     // success
     if (acf_validate_save_post()) {
         wp_send_json_success($json);
     }
     // update vars
     $json['valid'] = 0;
     $json['errors'] = acf_get_validation_errors();
     // return
     wp_send_json_success($json);
 }
Пример #4
0
 function validate_save_post($show_errors = false)
 {
     // validate fields
     if (!empty($_POST['acf'])) {
         // loop
         foreach ($_POST['acf'] as $field_key => $value) {
             // get field
             $field = acf_get_field($field_key);
             $input = 'acf[' . $field_key . ']';
             // bail early if not found
             if (!$field) {
                 continue;
             }
             // validate
             acf_validate_value($value, $field, $input);
         }
     }
     // action for 3rd party customization
     do_action('acf/validate_save_post');
     // vars
     $errors = acf_get_validation_errors();
     // bail ealry if no errors
     if (!$errors) {
         return true;
     }
     // show errors
     if ($show_errors) {
         $message = '<h2>Validation failed</h2>';
         $message .= '<ul>';
         foreach ($errors as $error) {
             $message .= '<li>' . $error['message'] . '</li>';
         }
         $message .= '</ul>';
         // die
         wp_die($message, 'Validation failed');
     }
     // return
     return false;
 }
Пример #5
0
 function customizer_widget_update_callback($instance, $new_instance, $old_instance, $widget)
 {
     // bail early if not valid
     if (!acf_validate_save_post()) {
         // vars
         $errors = acf_get_validation_errors();
         $total = count($errors);
         // message
         $message = __('Validation failed', 'acf') . '. ';
         $message .= sprintf(_n('1 field requires attention.', '%d fields require attention.', $total, 'acf'), $total);
         // add missing WP JS to remove spinning
         // also set state to 'saved' which disables the save button
         $message .= '<script type="text/javascript">';
         $message .= '(function($) {';
         $message .= '$(".customize-control.previewer-loading").removeClass("previewer-loading"); ';
         $message .= 'wp.customize.state("saved").set( true ); ';
         $message .= '})(jQuery);';
         $message .= '</script>';
         // return JSON error
         wp_send_json_error(array('message' => $message));
     } else {
         $instance['acf'] = array('ID' => 'widget_' . $widget->id, 'values' => false, 'reference' => array());
         // append acf $_POST data to instance
         // this allows preview JS data to contain acf values
         $instance['acf']['values'] = $_POST['acf'];
         // backup name => key reference
         // this will allow the customizer preview to correctly load the field when attempting to run acf_load_value and acf_format_value functions on newly added widgets
         foreach ($_POST['acf'] as $k => $v) {
             // get field
             $field = acf_get_field($k);
             // continue if no field
             if (!$field) {
                 continue;
             }
             // update
             $instance['acf']['reference'][$field['name']] = $field['key'];
         }
     }
     // return
     return $instance;
 }
Пример #6
0
 function ajax_validate_save_post()
 {
     // validate
     if (!isset($_POST['_acfnonce'])) {
         // ignore validation, this form $_POST was not correctly configured
         die;
     }
     // success
     if (acf_validate_save_post()) {
         $json = array('result' => 1, 'message' => __('Validation successful', 'acf'), 'errors' => 0);
         die(json_encode($json));
     }
     // fail
     $json = array('result' => 0, 'message' => __('Validation failed', 'acf'), 'errors' => acf_get_validation_errors());
     // update message
     $i = count($json['errors']);
     $json['message'] .= '. ' . sprintf(_n('1 field below is invalid.', '%s fields below are invalid.', $i, 'acf_vf'), $i) . ' ' . __('Please check your values and submit again.', 'acf_vf');
     die(json_encode($json));
 }
Пример #7
0
 function customizer_widget_update_callback($instance, $new_instance, $old_instance, $widget)
 {
     // add preview_values reference for later use in load_value filter
     // WP will re-generate the widget form HTML, and we need to load the $_POST data, not the DB data
     $this->preview_values["widget_{$widget->id}"] = $_POST['acf'];
     // add filter
     add_filter('acf/load_value', array($this, 'load_value'), 10, 3);
     // bail early if not valid
     if (!acf_validate_save_post()) {
         // vars
         $errors = acf_get_validation_errors();
         $total = count($errors);
         // message
         $message = __('Validation failed', 'acf') . '. ';
         $message .= sprintf(_n('1 field requires attention.', '%d fields require attention.', $total, 'acf'), $total);
         // add missing WP JS to remove spinning
         // also set state to 'saved' which disables the save button
         $message .= '<script type="text/javascript">';
         $message .= '(function($) {';
         $message .= '$(".customize-control.previewer-loading").removeClass("previewer-loading"); ';
         $message .= 'wp.customize.state("saved").set( true ); ';
         $message .= '})(jQuery);';
         $message .= '</script>';
         // return JSON error
         wp_send_json_error(array('message' => $message));
     } else {
         // append acf $_POST data to instance
         // this allows preview JS data to contain acf values
         $instance['acf'] = $_POST['acf'];
     }
     // return
     return $instance;
 }
Пример #8
0
 function ajax_validate_save_post()
 {
     // bail early if _acfnonce is missing
     if (!isset($_POST['_acfnonce'])) {
         wp_send_json_error();
     }
     // vars
     $json = array('message' => __('Validation successful', 'acf'), 'valid' => 1, 'errors' => 0);
     // success
     if (acf_validate_save_post()) {
         wp_send_json_success($json);
     }
     // update vars
     $json['message'] = __('Validation failed', 'acf');
     $json['valid'] = 0;
     $json['errors'] = acf_get_validation_errors();
     // update message
     if (count($json['errors']) == 1) {
         $json['message'] .= '. ' . __('1 required field below is empty', 'acf');
     } else {
         $json['message'] .= '. ' . sprintf(__('%s required fields below are empty', 'acf'), count($json['errors']));
     }
     // return
     wp_send_json_success($json);
 }