Make WordPress Core


Ignore:
Timestamp:
06/14/2016 07:16:54 PM (9 years ago)
Author:
westonruter
Message:

Customize: Update server-sent setting validation notifications as changes are entered.

Send back setting validities with full refreshes and selective refreshes so that invalid settings can have notifications displayed immediately before attempting save, and so that these notifications can be cleared as soon as the input is corrected.

  • Splits out JS logic for listing controls into separate methods wp.customize.Setting.prototype.findControls() and wp.customize.findControlsForSettings().
  • Adds a setting property to the data on notifications added to controls that are synced from their settings.
  • Adds selective-refresh-setting-validities message sent from preview to pane.
  • Changes WP_Customize_Manager::validate_setting_values() to return when settings are valid as well as invalid.
  • Adds WP_Customize_Manager::prepare_setting_validity_for_js().
  • Add setting validities to data exported to JS in Customizer Preview and in selective refresh responses.

Fixes #36944.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r37539 r37700  
    826826     */
    827827    public function customize_preview_settings() {
     828        $setting_validities = $this->validate_setting_values( $this->unsanitized_post_values() );
     829        $exported_setting_validities = array_map( array( $this, 'prepare_setting_validity_for_js' ), $setting_validities );
     830
    828831        $settings = array(
    829832            'theme' => array(
     
    838841            'activeSections' => array(),
    839842            'activeControls' => array(),
     843            'settingValidities' => $exported_setting_validities,
    840844            'nonce' => $this->get_nonces(),
    841845            'l10n' => array(
     
    992996     * @access public
    993997     * @see WP_REST_Request::has_valid_params()
     998     * @see WP_Customize_Setting::validate()
    994999     *
    9951000     * @param array $setting_values Mapping of setting IDs to values to sanitize and validate.
    996      * @return array Empty array if all settings were valid. One or more instances of `WP_Error` if any were invalid.
     1001     * @return array Mapping of setting IDs to return value of validate method calls, either `true` or `WP_Error`.
    9971002     */
    9981003    public function validate_setting_values( $setting_values ) {
    999         $validity_errors = array();
     1004        $validities = array();
    10001005        foreach ( $setting_values as $setting_id => $unsanitized_value ) {
    10011006            $setting = $this->get_setting( $setting_id );
     
    10071012                $validity = new WP_Error( 'invalid_value', __( 'Invalid value.' ) );
    10081013            }
    1009             if ( is_wp_error( $validity ) ) {
    1010                 $validity_errors[ $setting_id ] = $validity;
    1011             }
    1012         }
    1013         return $validity_errors;
     1014            $validities[ $setting_id ] = $validity;
     1015        }
     1016        return $validities;
     1017    }
     1018
     1019    /**
     1020     * Prepare setting validity for exporting to the client (JS).
     1021     *
     1022     * Converts `WP_Error` instance into array suitable for passing into the
     1023     * `wp.customize.Notification` JS model.
     1024     *
     1025     * @since 4.6.0
     1026     * @access public
     1027     *
     1028     * @param true|WP_Error $validity Setting validity.
     1029     * @return true|array If `$validity` was `WP_Error` then array mapping the error
     1030     *                    codes to their respective `message` and `data` to pass
     1031     *                    into the `wp.customize.Notification` JS model.
     1032     */
     1033    public function prepare_setting_validity_for_js( $validity ) {
     1034        if ( is_wp_error( $validity ) ) {
     1035            $notification = array();
     1036            foreach ( $validity->errors as $error_code => $error_messages ) {
     1037                $error_data = $validity->get_error_data( $error_code );
     1038                if ( is_null( $error_data ) ) {
     1039                    $error_data = array();
     1040                }
     1041                $error_data = array_merge(
     1042                    $error_data,
     1043                    array( 'from_server' => true )
     1044                );
     1045                $notification[ $error_code ] = array(
     1046                    'message' => join( ' ', $error_messages ),
     1047                    'data' => $error_data,
     1048                );
     1049            }
     1050            return $notification;
     1051        } else {
     1052            return true;
     1053        }
    10141054    }
    10151055
     
    10421082
    10431083        // Validate settings.
    1044         $validity_errors = $this->validate_setting_values( $this->unsanitized_post_values() );
    1045         $invalid_count = count( $validity_errors );
    1046         if ( $invalid_count > 0 ) {
    1047             $settings_errors = array();
    1048             foreach ( $validity_errors as $setting_id => $validity_error ) {
    1049                 $settings_errors[ $setting_id ] = array();
    1050                 foreach ( $validity_error->errors as $error_code => $error_messages ) {
    1051                     $settings_errors[ $setting_id ][ $error_code ] = array(
    1052                         'message' => join( ' ', $error_messages ),
    1053                         'data' => $validity_error->get_error_data( $error_code ),
    1054                     );
    1055                 }
    1056             }
     1084        $setting_validities = $this->validate_setting_values( $this->unsanitized_post_values() );
     1085        $invalid_setting_count = count( array_filter( $setting_validities, 'is_wp_error' ) );
     1086        $exported_setting_validities = array_map( array( $this, 'prepare_setting_validity_for_js' ), $setting_validities );
     1087        if ( $invalid_setting_count > 0 ) {
    10571088            $response = array(
    1058                 'invalid_settings' => $settings_errors,
    1059                 'message' => sprintf( _n( 'There is %s invalid setting.', 'There are %s invalid settings.', $invalid_count ), number_format_i18n( $invalid_count ) ),
     1089                'setting_validities' => $exported_setting_validities,
     1090                'message' => sprintf( _n( 'There is %s invalid setting.', 'There are %s invalid settings.', $invalid_setting_count ), number_format_i18n( $invalid_setting_count ) ),
    10601091            );
    10611092
     
    10981129        do_action( 'customize_save_after', $this );
    10991130
     1131        $data = array(
     1132            'setting_validities' => $exported_setting_validities,
     1133        );
     1134
    11001135        /**
    11011136         * Filters response data for a successful customize_save AJAX request.
     
    11091144         * @param WP_Customize_Manager $this WP_Customize_Manager instance.
    11101145         */
    1111         $response = apply_filters( 'customize_save_response', array(), $this );
     1146        $response = apply_filters( 'customize_save_response', $data, $this );
    11121147        wp_send_json_success( $response );
    11131148    }
Note: See TracChangeset for help on using the changeset viewer.