Make WordPress Core

Changeset 31329


Ignore:
Timestamp:
02/03/2015 10:14:28 AM (12 years ago)
Author:
ocean90
Message:

Ensure that WP_Customize_Setting::value() returns default value for setting if not dirty.

There was regression introduced by #28580 where only changed (dirty) settings now are POST'ed to the Customizer preview.

  • Allow WP_Customize_Manager::post_value() to accept a second $default argument.
  • Introduce WP_Customize_Manager::unsanitized_post_values() for accessing previously-private member variable _post_values.
  • Do require_once instead of require for Customizer classes.
  • Add unit tests for WP_Customize_Manager and WP_Customize_Setting.

props westonruter.
fixes #30988.

Location:
trunk
Files:
3 added
2 edited

Legend:

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

    r31234 r31329  
    6464
    6565        /**
    66          * $_POST values for Customize Settings.
    67          *
    68          * @var array
     66         * Unsanitized values for Customize Settings parsed from $_POST['customized'].
     67         *
     68         * @var array|false
    6969         */
    7070        private $_post_values;
     
    7676         */
    7777        public function __construct() {
    78                 require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
    79                 require( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
    80                 require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
    81                 require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
    82                 require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
     78                require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
     79                require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
     80                require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
     81                require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
     82                require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
    8383
    8484                $this->widgets = new WP_Customize_Widgets( $this );
     
    400400
    401401        /**
    402          * Decode the $_POST['customized'] values for a specific Customize Setting.
     402         * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
     403         * settings for subsequent post_value() lookups.
     404         *
     405         * @since 4.1.1
     406         *
     407         * @return array
     408         */
     409        public function unsanitized_post_values() {
     410                if ( ! isset( $this->_post_values ) ) {
     411                        if ( isset( $_POST['customized'] ) ) {
     412                                $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
     413                        }
     414                        if ( empty( $this->_post_values ) ) { // if not isset or of JSON error
     415                                $this->_post_values = false;
     416                        }
     417                }
     418                if ( empty( $this->_post_values ) ) {
     419                        return array();
     420                } else {
     421                        return $this->_post_values;
     422                }
     423        }
     424
     425        /**
     426         * Return the sanitized value for a given setting from the request's POST data.
    403427         *
    404428         * @since 3.4.0
    405429         *
    406430         * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
    407          * @return string $post_value Sanitized value
    408          */
    409         public function post_value( $setting ) {
    410                 if ( ! isset( $this->_post_values ) ) {
    411                         if ( isset( $_POST['customized'] ) )
    412                                 $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
    413                         else
    414                                 $this->_post_values = false;
    415                 }
    416 
    417                 if ( isset( $this->_post_values[ $setting->id ] ) )
    418                         return $setting->sanitize( $this->_post_values[ $setting->id ] );
     431         * @param mixed $default value returned $setting has no post value (added in 4.2.0).
     432         * @return string|mixed $post_value Sanitized value or the $default provided
     433         */
     434        public function post_value( $setting, $default = null ) {
     435                $post_values = $this->unsanitized_post_values();
     436                if ( array_key_exists( $setting->id, $post_values ) ) {
     437                        return $setting->sanitize( $post_values[ $setting->id ] );
     438                } else {
     439                        return $default;
     440                }
    419441        }
    420442
  • trunk/src/wp-includes/class-wp-customize-setting.php

    r31126 r31329  
    101101        }
    102102
     103        protected $_original_value;
     104
    103105        /**
    104106         * Handle previewing the setting.
     
    107109         */
    108110        public function preview() {
     111                if ( ! isset( $this->_original_value ) ) {
     112                        $this->_original_value = $this->value();
     113                }
     114
    109115                switch( $this->type ) {
    110116                        case 'theme_mod' :
     
    157163         */
    158164        public function _preview_filter( $original ) {
    159                 return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
     165                $undefined = new stdClass(); // symbol hack
     166                $post_value = $this->manager->post_value( $this, $undefined );
     167                if ( $undefined === $post_value ) {
     168                        $value = $this->_original_value;
     169                } else {
     170                        $value = $post_value;
     171                }
     172
     173                return $this->multidimensional_replace( $original, $this->id_data['keys'], $value );
    160174        }
    161175
     
    423437                }
    424438
    425                 if ( $create && ! isset( $node[ $last ] ) )
    426                         $node[ $last ] = array();
     439                if ( $create ) {
     440                        if ( ! is_array( $node ) ) {
     441                                // account for an array overriding a string or object value
     442                                $node = array();
     443                        }
     444                        if ( ! isset( $node[ $last ] ) ) {
     445                                $node[ $last ] = array();
     446                        }
     447                }
    427448
    428449                if ( ! isset( $node[ $last ] ) )
Note: See TracChangeset for help on using the changeset viewer.