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, boonebgorges.
Merges [31329] [31339] [31342] [31360] to the 4.1 branch.
Fixes #30988.

Built from https://develop.svn.wordpress.org/branches/4.1@31410


git-svn-id: http://core.svn.wordpress.org/branches/4.1@31391 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dion Hulse 2015-02-11 06:25:23 +00:00
parent a8ac7d3dc1
commit d349b9fdaf
2 changed files with 65 additions and 21 deletions

View File

@ -63,9 +63,9 @@ final class WP_Customize_Manager {
protected $registered_control_types = array(); protected $registered_control_types = array();
/** /**
* $_POST values for Customize Settings. * Unsanitized values for Customize Settings parsed from $_POST['customized'].
* *
* @var array * @var array|false
*/ */
private $_post_values; private $_post_values;
@ -75,11 +75,11 @@ final class WP_Customize_Manager {
* @since 3.4.0 * @since 3.4.0
*/ */
public function __construct() { public function __construct() {
require( ABSPATH . WPINC . '/class-wp-customize-setting.php' ); require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
require( ABSPATH . WPINC . '/class-wp-customize-panel.php' ); require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
require( ABSPATH . WPINC . '/class-wp-customize-section.php' ); require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
require( ABSPATH . WPINC . '/class-wp-customize-control.php' ); require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' ); require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
$this->widgets = new WP_Customize_Widgets( $this ); $this->widgets = new WP_Customize_Widgets( $this );
@ -399,23 +399,46 @@ final class WP_Customize_Manager {
} }
/** /**
* Decode the $_POST['customized'] values for a specific Customize Setting. * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
* settings for subsequent post_value() lookups.
*
* @since 4.1.1
*
* @return array
*/
public function unsanitized_post_values() {
if ( ! isset( $this->_post_values ) ) {
if ( isset( $_POST['customized'] ) ) {
$this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
}
if ( empty( $this->_post_values ) ) { // if not isset or of JSON error
$this->_post_values = false;
}
}
if ( empty( $this->_post_values ) ) {
return array();
} else {
return $this->_post_values;
}
}
/**
* Return the sanitized value for a given setting from the request's POST data.
* *
* @since 3.4.0 * @since 3.4.0
* @since 4.1.1 Introduced 'default' parameter.
* *
* @param WP_Customize_Setting $setting A WP_Customize_Setting derived object * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
* @return string $post_value Sanitized value * @param mixed $default value returned $setting has no post value (added in 4.2.0).
* @return string|mixed $post_value Sanitized value or the $default provided
*/ */
public function post_value( $setting ) { public function post_value( $setting, $default = null ) {
if ( ! isset( $this->_post_values ) ) { $post_values = $this->unsanitized_post_values();
if ( isset( $_POST['customized'] ) ) if ( array_key_exists( $setting->id, $post_values ) ) {
$this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true ); return $setting->sanitize( $post_values[ $setting->id ] );
else } else {
$this->_post_values = false; return $default;
} }
if ( isset( $this->_post_values[ $setting->id ] ) )
return $setting->sanitize( $this->_post_values[ $setting->id ] );
} }
/** /**

View File

@ -103,12 +103,18 @@ class WP_Customize_Setting {
return $this; return $this;
} }
protected $_original_value;
/** /**
* Handle previewing the setting. * Handle previewing the setting.
* *
* @since 3.4.0 * @since 3.4.0
*/ */
public function preview() { public function preview() {
if ( ! isset( $this->_original_value ) ) {
$this->_original_value = $this->value();
}
switch( $this->type ) { switch( $this->type ) {
case 'theme_mod' : case 'theme_mod' :
add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) ); add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
@ -159,7 +165,15 @@ class WP_Customize_Setting {
* @return mixed New or old value. * @return mixed New or old value.
*/ */
public function _preview_filter( $original ) { public function _preview_filter( $original ) {
return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() ); $undefined = new stdClass(); // symbol hack
$post_value = $this->manager->post_value( $this, $undefined );
if ( $undefined === $post_value ) {
$value = $this->_original_value;
} else {
$value = $post_value;
}
return $this->multidimensional_replace( $original, $this->id_data['keys'], $value );
} }
/** /**
@ -425,8 +439,15 @@ class WP_Customize_Setting {
$node = &$node[ $key ]; $node = &$node[ $key ];
} }
if ( $create && ! isset( $node[ $last ] ) ) if ( $create ) {
$node[ $last ] = array(); if ( ! is_array( $node ) ) {
// account for an array overriding a string or object value
$node = array();
}
if ( ! isset( $node[ $last ] ) ) {
$node[ $last ] = array();
}
}
if ( ! isset( $node[ $last ] ) ) if ( ! isset( $node[ $last ] ) )
return; return;