<!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>[31329] trunk: 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/31329">31329</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/31329","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-02-03 10:14:28 +0000 (Tue, 03 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.
fixes <a href="https://core.trac.wordpress.org/ticket/30988">#30988</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizesettingphp">trunk/src/wp-includes/class-wp-customize-setting.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li>trunk/tests/phpunit/tests/customize/</li>
<li><a href="#trunktestsphpunittestscustomizemanagerphp">trunk/tests/phpunit/tests/customize/manager.php</a></li>
<li><a href="#trunktestsphpunittestscustomizesettingphp">trunk/tests/phpunit/tests/customize/setting.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php      2015-02-03 07:46:21 UTC (rev 31328)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2015-02-03 10:14:28 UTC (rev 31329)
</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,45 @@
</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
+        *
+        * @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="trunksrcwpincludesclasswpcustomizesettingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-setting.php      2015-02-03 07:46:21 UTC (rev 31328)
+++ trunk/src/wp-includes/class-wp-customize-setting.php        2015-02-03 10:14:28 UTC (rev 31329)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,12 +100,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        add_filter( "customize_sanitize_js_{$this->id}", $this->sanitize_js_callback, 10, 2 );
</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">@@ -156,7 +162,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">@@ -422,8 +436,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="trunktestsphpunittestscustomizemanagerphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/customize/manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/manager.php                           (rev 0)
+++ trunk/tests/phpunit/tests/customize/manager.php     2015-02-03 10:14:28 UTC (rev 31329)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,75 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for the WP_Customize_Manager class.
+ *
+ * @group customize
+ */
+class Tests_WP_Customize_Manager extends WP_UnitTestCase {
+
+       function setUp() {
+               parent::setUp();
+               require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
+               $GLOBALS['wp_customize'] = new WP_Customize_Manager();
+               $this->manager = $GLOBALS['wp_customize'];
+               $this->undefined = new stdClass();
+       }
+
+       function tearDown() {
+               parent::tearDown();
+               $this->manager = null;
+               unset( $GLOBALS['wp_customize'] );
+       }
+
+       /**
+        * Instantiate class, set global $wp_customize, and return instance.
+        *
+        * @return WP_Customize_Manager
+        */
+       function instantiate() {
+               $GLOBALS['wp_customize'] = new WP_Customize_Manager();
+               return $GLOBALS['wp_customize'];
+       }
+
+       /**
+        * Test WP_Customize_Manager::unsanitized_post_values()
+        *
+        * @ticket 30988
+        */
+       function test_unsanitized_post_values() {
+               $manager = $this->instantiate();
+
+               $customized = array(
+                       'foo' => 'bar',
+                       'baz[quux]' => 123,
+               );
+               $_POST['customized'] = wp_slash( wp_json_encode( $customized ) );
+               $post_values = $manager->unsanitized_post_values();
+               $this->assertEquals( $customized, $post_values );
+       }
+
+       /**
+        * Test the WP_Customize_Manager::post_value() method
+        *
+        * @ticket 30988
+        */
+       function test_post_value() {
+               $posted_settings = array(
+                       'foo' => 'OOF',
+               );
+               $_POST['customized'] = wp_slash( wp_json_encode( $posted_settings ) );
+
+               $manager = $this->instantiate();
+
+               $manager->add_setting( 'foo', array( 'default' => 'foo_default' ) );
+               $foo_setting = $manager->get_setting( 'foo' );
+               $this->assertEquals( 'foo_default', $manager->get_setting( 'foo' )->value(), 'Expected non-previewed setting to return default when value() method called.' );
+               $this->assertEquals( $posted_settings['foo'], $manager->post_value( $foo_setting, 'post_value_foo_default' ), 'Expected post_value($foo_setting) to return value supplied in $_POST[customized][foo]' );
+
+               $manager->add_setting( 'bar', array( 'default' => 'bar_default' ) );
+               $bar_setting = $manager->get_setting( 'bar' );
+               $this->assertEquals( 'post_value_bar_default', $manager->post_value( $bar_setting, 'post_value_bar_default' ), 'Expected post_value($bar_setting, $default) to return $default since no value supplied in $_POST[customized][bar]' );
+       }
+
+}
+
</ins></span></pre></div>
<a id="trunktestsphpunittestscustomizesettingphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/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                           (rev 0)
+++ trunk/tests/phpunit/tests/customize/setting.php     2015-02-03 10:14:28 UTC (rev 31329)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,364 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for the WP_Customize_Setting class.
+ *
+ * @group customize
+ */
+class Tests_WP_Customize_Setting extends WP_UnitTestCase {
+
+       /**
+        * @var WP_Customize_Manager
+        */
+       protected $manager;
+
+       /**
+        * @var stdClass an instance which serves as a symbol to do identity checks with
+        */
+       public $undefined;
+
+       function setUp() {
+               parent::setUp();
+               require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
+               $GLOBALS['wp_customize'] = new WP_Customize_Manager();
+               $this->manager = $GLOBALS['wp_customize'];
+               $this->undefined = new stdClass();
+       }
+
+       function tearDown() {
+               parent::tearDown();
+               $this->manager = null;
+               unset( $GLOBALS['wp_customize'] );
+       }
+
+       function test_constructor_without_args() {
+               $setting = new WP_Customize_Setting( $this->manager, 'foo' );
+               $this->assertEquals( $this->manager, $setting->manager );
+               $this->assertEquals( 'foo', $setting->id );
+               $this->assertEquals( 'theme_mod', $setting->type );
+               $this->assertEquals( 'edit_theme_options', $setting->capability );
+               $this->assertEquals( '', $setting->theme_supports );
+               $this->assertEquals( '', $setting->default );
+               $this->assertEquals( 'refresh', $setting->transport );
+               $this->assertEquals( '', $setting->sanitize_callback );
+               $this->assertEquals( '', $setting->sanitize_js_callback );
+               $this->assertFalse( has_filter( "customize_sanitize_{$setting->id}" ) );
+               $this->assertFalse( has_filter( "customize_sanitize_js_{$setting->id}" ) );
+       }
+
+       function test_constructor_with_args() {
+               $args = array(
+                       'type' => 'option',
+                       'capability' => 'edit_posts',
+                       'theme_supports' => 'widgets',
+                       'default' => 'barbar',
+                       'transport' => 'postMessage',
+                       'sanitize_callback' => create_function( '$value', 'return $value . ":sanitize_callback";' ),
+                       'sanitize_js_callback' => create_function( '$value', 'return $value . ":sanitize_js_callback";' ),
+               );
+               $setting = new WP_Customize_Setting( $this->manager, 'bar', $args );
+               $this->assertEquals( 'bar', $setting->id );
+               foreach ( $args as $key => $value ) {
+                       $this->assertEquals( $value, $setting->$key );
+               }
+               $this->assertEquals( 10, has_filter( "customize_sanitize_{$setting->id}", $args['sanitize_callback'] ) );
+               $this->assertEquals( 10, has_filter( "customize_sanitize_js_{$setting->id}" ), $args['sanitize_js_callback'] );
+       }
+
+       public $post_data_overrides = array(
+               'unset_option_overridden' => 'unset_option_post_override_value',
+               'unset_theme_mod_overridden' => 'unset_theme_mod_post_override_value',
+               'set_option_overridden' => 'set_option_post_override_value',
+               'set_theme_mod_overridden' => 'set_theme_mod_post_override_value',
+               'unset_option_multi_overridden[foo]' => 'unset_option_multi_overridden[foo]_post_override_value',
+               'unset_theme_mod_multi_overridden[foo]' => 'unset_theme_mod_multi_overridden[foo]_post_override_value',
+               'set_option_multi_overridden[foo]' => 'set_option_multi_overridden[foo]_post_override_value',
+               'set_theme_mod_multi_overridden[foo]' => 'set_theme_mod_multi_overridden[foo]_post_override_value',
+       );
+
+       public $standard_type_configs = array(
+               'option' => array(
+                       'getter' => 'get_option',
+                       'setter' => 'update_option',
+               ),
+               'theme_mod' => array(
+                       'getter' => 'get_theme_mod',
+                       'setter' => 'set_theme_mod',
+               ),
+       );
+
+       /**
+        * Run assertions on non-multidimensional standard settings.
+        */
+       function test_preview_standard_types_non_multidimensional() {
+               $_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
+
+               // Try non-multidimensional settings
+               foreach ( $this->standard_type_configs as $type => $type_options ) {
+                       // Non-multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen)
+                       $name = "unset_{$type}_without_post_value";
+                       $default = "default_value_{$name}";
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $name, $this->undefined ) );
+                       $this->assertEquals( $default, $setting->value() );
+                       $setting->preview();
+                       $this->assertEquals( $default, call_user_func( $type_options['getter'], $name, $this->undefined ), sprintf( 'Expected %s(%s) to return setting default: %s.', $type_options['getter'], $name, $default ) );
+                       $this->assertEquals( $default, $setting->value() );
+
+                       // Non-multidimensional: See what effect the preview has on an extant setting (default value should not be seen)
+                       $name = "set_{$type}_without_post_value";
+                       $default = "default_value_{$name}";
+                       $initial_value = "initial_value_{$name}";
+                       call_user_func( $type_options['setter'], $name, $initial_value );
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
+                       $this->assertEquals( $initial_value, $setting->value() );
+                       $setting->preview();
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
+                       $this->assertEquals( $initial_value, $setting->value() );
+
+                       // @todo What if we call the setter after preview() is called? If no post_value, should the new set value be stored? If that happens, then the following 3 assertions should be inverted
+                       $overridden_value = "overridden_value_$name";
+                       call_user_func( $type_options['setter'], $name, $overridden_value );
+                       $this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name ) );
+                       $this->assertEquals( $initial_value, $setting->value() );
+                       $this->assertNotEquals( $overridden_value, $setting->value() );
+
+                       // Non-multidimensional: Test unset setting being overridden by a post value
+                       $name = "unset_{$type}_overridden";
+                       $default = "default_value_{$name}";
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $name, $this->undefined ) );
+                       $this->assertEquals( $default, $setting->value() );
+                       $setting->preview(); // activate post_data
+                       $this->assertEquals( $this->post_data_overrides[ $name ], call_user_func( $type_options['getter'], $name, $this->undefined ) );
+                       $this->assertEquals( $this->post_data_overrides[ $name ], $setting->value() );
+
+                       // Non-multidimensional: Test set setting being overridden by a post value
+                       $name = "set_{$type}_overridden";
+                       $default = "default_value_{$name}";
+                       $initial_value = "initial_value_{$name}";
+                       call_user_func( $type_options['setter'], $name, $initial_value );
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $initial_value, call_user_func( $type_options['getter'], $name, $this->undefined ) );
+                       $this->assertEquals( $initial_value, $setting->value() );
+                       $setting->preview(); // activate post_data
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( $this->post_data_overrides[ $name ], call_user_func( $type_options['getter'], $name, $this->undefined ) );
+                       $this->assertEquals( $this->post_data_overrides[ $name ], $setting->value() );
+               }
+       }
+
+       /**
+        * Run assertions on multidimensional standard settings.
+        */
+       function test_preview_standard_types_multidimensional() {
+               $_POST['customized'] = wp_slash( wp_json_encode( $this->post_data_overrides ) );
+
+               foreach ( $this->standard_type_configs as $type => $type_options ) {
+                       // Multidimensional: See what effect the preview filter has on a non-existent setting (default value should be seen)
+                       $base_name = "unset_{$type}_multi";
+                       $name = $base_name . '[foo]';
+                       $default = "default_value_{$name}";
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
+                       $this->assertEquals( $default, $setting->value() );
+                       $setting->preview();
+                       $base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+                       $this->assertArrayHasKey( 'foo', $base_value );
+                       $this->assertEquals( $default, $base_value['foo'] );
+
+                       // Multidimensional: See what effect the preview has on an extant setting (default value should not be seen)
+                       $base_name = "set_{$type}_multi";
+                       $name = $base_name . '[foo]';
+                       $default = "default_value_{$name}";
+                       $initial_value = "initial_value_{$name}";
+                       $base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' );
+                       call_user_func( $type_options['setter'], $base_name, $base_initial_value );
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $base_value = call_user_func( $type_options['getter'], $base_name, array() );
+                       $this->assertEquals( $initial_value, $base_value['foo'] );
+                       $this->assertEquals( $initial_value, $setting->value() );
+                       $setting->preview();
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $base_value = call_user_func( $type_options['getter'], $base_name, array() );
+                       $this->assertEquals( $initial_value, $base_value['foo'] );
+                       $this->assertEquals( $initial_value, $setting->value() );
+
+                       // Multidimensional: Test unset setting being overridden by a post value
+                       $base_name = "unset_{$type}_multi_overridden";
+                       $name = $base_name . '[foo]';
+                       $default = "default_value_{$name}";
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $this->assertEquals( $this->undefined, call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
+                       $this->assertEquals( $default, $setting->value() );
+                       $setting->preview();
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+                       $this->assertArrayHasKey( 'foo', $base_value );
+                       $this->assertEquals( $this->post_data_overrides[ $name ], $base_value['foo'] );
+
+                       // Multidimemsional: Test set setting being overridden by a post value
+                       $base_name = "set_{$type}_multi_overridden";
+                       $name = $base_name . '[foo]';
+                       $default = "default_value_{$name}";
+                       $initial_value = "initial_value_{$name}";
+                       $base_initial_value = array( 'foo' => $initial_value, 'bar' => 'persisted' );
+                       call_user_func( $type_options['setter'], $base_name, $base_initial_value );
+                       $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+                       $base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+                       $this->arrayHasKey( 'foo', $base_value );
+                       $this->arrayHasKey( 'bar', $base_value );
+                       $this->assertEquals( $base_initial_value['foo'], $base_value['foo'] );
+                       $this->assertEquals( $base_initial_value['bar'], call_user_func( $type_options['getter'], $base_name, $this->undefined )['bar'] );
+                       $this->assertEquals( $initial_value, $setting->value() );
+                       $setting->preview();
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->id}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $this->assertEquals( 0, did_action( "customize_preview_{$setting->type}" ) ); // only applicable for custom types (not options or theme_mods)
+                       $base_value = call_user_func( $type_options['getter'], $base_name, $this->undefined );
+                       $this->assertArrayHasKey( 'foo', $base_value );
+                       $this->assertEquals( $this->post_data_overrides[ $name ], $base_value['foo'] );
+                       $this->arrayHasKey( 'bar', call_user_func( $type_options['getter'], $base_name, $this->undefined ) );
+                       $this->assertEquals( $base_initial_value['bar'], call_user_func( $type_options['getter'], $base_name, $this->undefined )['bar'] );
+               }
+       }
+
+       /**
+        * @var array storage for saved custom type data that are tested in self::test_preview_custom_type()
+        */
+       protected $custom_type_data_saved;
+
+       /**
+        * @var array storage for previewed custom type data that are tested in self::test_preview_custom_type()
+        */
+       protected $custom_type_data_previewed;
+
+       function custom_type_getter( $name, $default = null ) {
+               if ( did_action( "customize_preview_{$name}" ) && array_key_exists( $name, $this->custom_type_data_previewed ) ) {
+                       $value = $this->custom_type_data_previewed[ $name ];
+               } else if ( array_key_exists( $name, $this->custom_type_data_saved ) ) {
+                       $value = $this->custom_type_data_saved[ $name ];
+               } else {
+                       $value = $default;
+               }
+               return $value;
+       }
+
+       function custom_type_setter( $name, $value ) {
+               $this->custom_type_data_saved[ $name ] = $value;
+       }
+
+       function custom_type_value_filter( $default ) {
+               $name = preg_replace( '/^customize_value_/', '', current_filter() );
+               return $this->custom_type_getter( $name, $default );
+       }
+
+       /**
+        * @param WP_Customize_Setting $setting
+        */
+       function custom_type_preview( $setting ) {
+               $previewed_value = $setting->post_value( $this->undefined );
+               if ( $this->undefined !== $previewed_value ) {
+                       $this->custom_type_data_previewed[ $setting->id ] = $previewed_value;
+               }
+       }
+
+       function test_preview_custom_type() {
+               $type = 'custom_type';
+               $post_data_overrides = array(
+                       "unset_{$type}_with_post_value" => "unset_{$type}_without_post_value",
+                       "set_{$type}_with_post_value" => "set_{$type}_without_post_value",
+               );
+               $_POST['customized'] = wp_slash( wp_json_encode( $post_data_overrides ) );
+
+               $this->custom_type_data_saved = array();
+               $this->custom_type_data_previewed = array();
+
+               add_action( "customize_preview_{$type}", array( $this, 'custom_type_preview' ) );
+
+               // Custom type not existing and no post value override
+               $name = "unset_{$type}_without_post_value";
+               $default = "default_value_{$name}";
+               $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+               // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
+               add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
+               $this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $default, $setting->value() );
+               $setting->preview();
+               $this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
+               $this->assertEquals( 1, did_action( "customize_preview_{$setting->type}" ) );
+               $this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) ); // Note: for a non-custom type this is $default
+               $this->assertEquals( $default, $setting->value() ); // should be same as above
+
+               // Custom type existing and no post value override
+               $name = "set_{$type}_without_post_value";
+               $default = "default_value_{$name}";
+               $initial_value = "initial_value_{$name}";
+               $this->custom_type_setter( $name, $initial_value );
+               $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+               // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
+               add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
+               $this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $initial_value, $setting->value() );
+               $setting->preview();
+               $this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
+               $this->assertEquals( 2, did_action( "customize_preview_{$setting->type}" ) );
+               $this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) ); // should be same as above
+               $this->assertEquals( $initial_value, $setting->value() ); // should be same as above
+
+               // Custom type not existing and with a post value override
+               $name = "unset_{$type}_with_post_value";
+               $default = "default_value_{$name}";
+               $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+               // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
+               add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
+               $this->assertEquals( $this->undefined, $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $default, $setting->value() );
+               $setting->preview();
+               $this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
+               $this->assertEquals( 3, did_action( "customize_preview_{$setting->type}" ) );
+               $this->assertEquals( $post_data_overrides[ $name ], $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $post_data_overrides[ $name ], $setting->value() );
+
+               // Custom type not existing and with a post value override
+               $name = "set_{$type}_with_post_value";
+               $default = "default_value_{$name}";
+               $initial_value = "initial_value_{$name}";
+               $this->custom_type_setter( $name, $initial_value );
+               $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+               // Note: #29316 will allow us to have one filter for all settings of a given type, which is what we need
+               add_filter( "customize_value_{$name}", array( $this, 'custom_type_value_filter' ) );
+               $this->assertEquals( $initial_value, $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $initial_value, $setting->value() );
+               $setting->preview();
+               $this->assertEquals( 1, did_action( "customize_preview_{$setting->id}" ) );
+               $this->assertEquals( 4, did_action( "customize_preview_{$setting->type}" ) );
+               $this->assertEquals( $post_data_overrides[ $name ], $this->custom_type_getter( $name, $this->undefined ) );
+               $this->assertEquals( $post_data_overrides[ $name ], $setting->value() );
+
+               unset( $this->custom_type_data_previewed, $this->custom_type_data_saved );
+       }
+
+       /**
+        * Test specific fix for setting's default value not applying on preview window
+        *
+        * @ticket 30988
+        */
+       function test_non_posted_setting_applying_default_value_in_preview() {
+               $type = 'option';
+               $name = 'unset_option_without_post_value';
+               $default = "default_value_{$name}";
+               $setting = new WP_Customize_Setting( $this->manager, $name, compact( 'type', 'default' ) );
+               $this->assertEquals( $this->undefined, get_option( $name, $this->undefined ) );
+               $this->assertEquals( $default, $setting->value() );
+               $setting->preview();
+               $this->assertEquals( $default, get_option( $name, $this->undefined ), sprintf( 'Expected get_option(%s) to return setting default: %s.', $name, $default ) );
+               $this->assertEquals( $default, $setting->value() );
+       }
+}
+
</ins></span></pre>
</div>
</div>

</body>
</html>