<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[31410] branches/4.1: Ensure that `WP_Customize_Setting::value()` returns default value for setting if not dirty.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/31410">31410</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/31410","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dd32</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-02-11 06:24:01 +0000 (Wed, 11 Feb 2015)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Ensure that `WP_Customize_Setting::value()` returns default value for setting if not dirty.
There was regression introduced by <a href="https://core.trac.wordpress.org/ticket/28580">#28580</a> 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 <a href="https://core.trac.wordpress.org/changeset/31329">[31329]</a> <a href="https://core.trac.wordpress.org/changeset/31339">[31339]</a> <a href="https://core.trac.wordpress.org/changeset/31342">[31342]</a> <a href="https://core.trac.wordpress.org/changeset/31360">[31360]</a> to the 4.1 branch.
Fixes <a href="https://core.trac.wordpress.org/ticket/30988">#30988</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#branches41srcwpincludesclasswpcustomizemanagerphp">branches/4.1/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#branches41srcwpincludesclasswpcustomizesettingphp">branches/4.1/src/wp-includes/class-wp-customize-setting.php</a></li>
<li><a href="#branches41testsphpunittestscustomizesettingphp">branches/4.1/tests/phpunit/tests/customize/setting.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>branches/4.1/tests/phpunit/tests/customize/</li>
</ul>
<h3>Property Changed</h3>
<ul>
<li><a href="#branches41">branches/4.1/</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<span class="cx" style="display: block; padding: 0 10px">Index: branches/4.1
</span><span class="cx" style="display: block; padding: 0 10px">===================================================================
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">--- branches/4.1 2015-02-11 05:05:13 UTC (rev 31409)
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+++ branches/4.1 2015-02-11 06:24:01 UTC (rev 31410)
</ins><a id="branches41"></a>
<div class="propset"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Property changes: branches/4.1</h4>
<pre class="diff"><span>
</span></pre></div>
<a id="svnmergeinfo"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: svn:mergeinfo</h4></div>
<span class="cx" style="display: block; padding: 0 10px"> /branches/3.1:18031
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.3:20543
</span><span class="cx" style="display: block; padding: 0 10px"> /branches/3.4:21757
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-/trunk:18512,30801-30813,30817,30821,30823-30824,30828-30847,30849,30852-30857,30860,30865-30869,30871-30874,30879,30884-30891,30893,30896-30897,30899-30901,30903,30905,30907,30910,30912,30921,30929,30932,30944-30946,30951-30956,30961,30967,30971,30985,30991,30993,31010,31054,31071,31073,31140,31179,31197,31251,31273,31386,31390
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/trunk:18512,30801-30813,30817,30821,30823-30824,30828-30847,30849,30852-30857,30860,30865-30869,30871-30874,30879,30884-30891,30893,30896-30897,30899-30901,30903,30905,30907,30910,30912,30921,30929,30932,30944-30946,30951-30956,30961,30967,30971,30985,30991,30993,31010,31054,31071,31073,31140,31179,31197,31251,31273,31329,31339,31342,31360,31386,31390
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="branches41srcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.1/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.1/src/wp-includes/class-wp-customize-manager.php 2015-02-11 05:05:13 UTC (rev 31409)
+++ branches/4.1/src/wp-includes/class-wp-customize-manager.php 2015-02-11 06:24:01 UTC (rev 31410)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -63,9 +63,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> protected $registered_control_types = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * $_POST values for Customize Settings.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Unsanitized values for Customize Settings parsed from $_POST['customized'].
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @var array
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @var array|false
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private $_post_values;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -75,11 +75,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function __construct() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- require( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
- require( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
- require( ABSPATH . WPINC . '/class-wp-customize-section.php' );
- require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
- require( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ require_once( ABSPATH . WPINC . '/class-wp-customize-setting.php' );
+ require_once( ABSPATH . WPINC . '/class-wp-customize-panel.php' );
+ require_once( ABSPATH . WPINC . '/class-wp-customize-section.php' );
+ require_once( ABSPATH . WPINC . '/class-wp-customize-control.php' );
+ require_once( ABSPATH . WPINC . '/class-wp-customize-widgets.php' );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->widgets = new WP_Customize_Widgets( $this );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -399,23 +399,46 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Decode the $_POST['customized'] values for a specific Customize Setting.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Parse the incoming $_POST['customized'] JSON data and store the unsanitized
+ * settings for subsequent post_value() lookups.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @since 3.4.0
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 4.1.1
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
- * @return string $post_value Sanitized value
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return array
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function post_value( $setting ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function unsanitized_post_values() {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $this->_post_values ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $_POST['customized'] ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( isset( $_POST['customized'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_post_values = json_decode( wp_unslash( $_POST['customized'] ), true );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- else
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
+ if ( empty( $this->_post_values ) ) { // if not isset or of JSON error
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->_post_values = false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $this->_post_values ) ) {
+ return array();
+ } else {
+ return $this->_post_values;
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $this->_post_values[ $setting->id ] ) )
- return $setting->sanitize( $this->_post_values[ $setting->id ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Return the sanitized value for a given setting from the request's POST data.
+ *
+ * @since 3.4.0
+ * @since 4.1.1 Introduced 'default' parameter.
+ *
+ * @param WP_Customize_Setting $setting A WP_Customize_Setting derived object
+ * @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, $default = null ) {
+ $post_values = $this->unsanitized_post_values();
+ if ( array_key_exists( $setting->id, $post_values ) ) {
+ return $setting->sanitize( $post_values[ $setting->id ] );
+ } else {
+ return $default;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="branches41srcwpincludesclasswpcustomizesettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.1/src/wp-includes/class-wp-customize-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- branches/4.1/src/wp-includes/class-wp-customize-setting.php 2015-02-11 05:05:13 UTC (rev 31409)
+++ branches/4.1/src/wp-includes/class-wp-customize-setting.php 2015-02-11 06:24:01 UTC (rev 31410)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,12 +103,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return $this;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ protected $_original_value;
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Handle previewing the setting.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function preview() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! isset( $this->_original_value ) ) {
+ $this->_original_value = $this->value();
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> switch( $this->type ) {
</span><span class="cx" style="display: block; padding: 0 10px"> case 'theme_mod' :
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'theme_mod_' . $this->id_data[ 'base' ], array( $this, '_preview_filter' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -159,7 +165,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return mixed New or old value.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function _preview_filter( $original ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return $this->multidimensional_replace( $original, $this->id_data[ 'keys' ], $this->post_value() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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 );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -425,8 +439,15 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $node = &$node[ $key ];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $create && ! isset( $node[ $last ] ) )
- $node[ $last ] = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $create ) {
+ if ( ! is_array( $node ) ) {
+ // account for an array overriding a string or object value
+ $node = array();
+ }
+ if ( ! isset( $node[ $last ] ) ) {
+ $node[ $last ] = array();
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $node[ $last ] ) )
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span></span></pre></div>
<a id="branches41testsphpunittestscustomizesettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: branches/4.1/tests/phpunit/tests/customize/setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/setting.php 2015-02-03 10:14:28 UTC (rev 31329)
+++ branches/4.1/tests/phpunit/tests/customize/setting.php 2015-02-11 06:24:01 UTC (rev 31410)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -215,7 +215,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->arrayHasKey( 'foo', $base_value );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->arrayHasKey( 'bar', $base_value );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $base_initial_value['foo'], $base_value['foo'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $base_initial_value['bar'], call_user_func( $type_options['getter'], $base_name, $this->undefined )['bar'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $getter = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+ $this->assertEquals( $base_initial_value['bar'], $getter['bar'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $initial_value, $setting->value() );
</span><span class="cx" style="display: block; padding: 0 10px"> $setting->preview();
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -224,7 +226,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertArrayHasKey( 'foo', $base_value );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $this->post_data_overrides[ $name ], $base_value['foo'] );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->arrayHasKey( 'bar', call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertEquals( $base_initial_value['bar'], call_user_func( $type_options['getter'], $base_name, $this->undefined )['bar'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ $getter = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+ $this->assertEquals( $base_initial_value['bar'], $getter['bar'] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>