<!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>[37166] trunk: Customize: Harden assignment of Customizer settings transports for selective refreshable widgets</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/37166">37166</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/37166","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>2016-04-07 20:58:56 +0000 (Thu, 07 Apr 2016)</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'>Customize: Harden assignment of Customizer settings transports for selective refreshable widgets

Theme support for `customize-selective-refresh-widgets` can be added _after_ the logic for registering the settings for incoming widgets that have been changed. This is due to themes adding the theme support in `after_setup_theme` which is also the action where `WP_Customize_Widgets::register_settings()` is called. If these both happen at priority 10, which one is called first depends on which one was added first. The other issue is that at the time that `WP_Customize_Widgets::register_settings()` is called at `after_setup_theme`, it is called before `widgets_init` and thus no widgets are yet registered. This means that any settings registered at this point will always have a `refresh` transport even if the theme supports `customize-selective-refresh-widgets`, since the `WP_Widget` instance is not visible yet to see if it supports selective refresh.

The fix: Defer `WP_Customize_Widgets::register_settings()` from `after_setup_theme` to `widgets_init` at priority 95 when the widget objects have all been registered. Also, ensure that the preview filter for `sidebars_widgets` is added before the sidebars are iterated for adding the controls.

Props westonruter.
Fixes <a href="https://core.trac.wordpress.org/ticket/36389">#36389</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizesettingphp">trunk/src/wp-includes/class-wp-customize-setting.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
<li><a href="#trunktestsphpunittestscustomizewidgetsphp">trunk/tests/phpunit/tests/customize/widgets.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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      2016-04-07 19:21:02 UTC (rev 37165)
+++ trunk/src/wp-includes/class-wp-customize-setting.php        2016-04-07 20:58:56 UTC (rev 37166)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -207,6 +207,19 @@
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Reset `$aggregated_multidimensionals` static variable.
+        *
+        * This is intended only for use by unit tests.
+        *
+        * @since 4.5.0
+        * @access public
+        * @ignore
+        */
+       static public function reset_aggregated_multidimensionals() {
+               self::$aggregated_multidimensionals = array();
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * The ID for the current site when the preview() method was called.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.2.0
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></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-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-widgets.php      2016-04-07 19:21:02 UTC (rev 37165)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php        2016-04-07 20:58:56 UTC (rev 37166)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -99,7 +99,7 @@
</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">                add_filter( 'customize_dynamic_setting_args',          array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'after_setup_theme',                       array( $this, 'register_settings' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'widgets_init',                            array( $this, 'register_settings' ), 95 );
</ins><span class="cx" style="display: block; padding: 0 10px">                 add_action( 'wp_loaded',                               array( $this, 'override_sidebars_widgets_for_theme_switch' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_controls_init',                 array( $this, 'customize_controls_init' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register',                      array( $this, 'schedule_customize_register' ), 1 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -376,6 +376,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function customize_register() {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $sidebars_widgets = array_merge(
</span><span class="cx" style="display: block; padding: 0 10px">                        array( 'wp_inactive_widgets' => array() ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array_fill_keys( array_keys( $wp_registered_sidebars ), array() ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -509,8 +511,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->manager->get_setting( $new_setting_id )->preview();
</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">-
-               add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 );
</del><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="trunktestsphpunittestscustomizewidgetsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/widgets.php   2016-04-07 19:21:02 UTC (rev 37165)
+++ trunk/tests/phpunit/tests/customize/widgets.php     2016-04-07 20:58:56 UTC (rev 37166)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -46,6 +46,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                remove_action( 'customize_register', 'twentysixteen_customize_register', 11 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->backup_registered_sidebars = $GLOBALS['wp_registered_sidebars'];
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Reset protected static var on class.
+               WP_Customize_Setting::reset_aggregated_multidimensionals();
</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">        function clean_up_global_scope() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -70,6 +73,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        function set_customized_post_data( $customized ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $_POST['customized'] = wp_slash( wp_json_encode( $customized ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $this->manager ) {
+                       foreach ( $customized as $id => $value ) {
+                               $this->manager->set_post_value( $id, $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">        function do_customize_boot_actions() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -150,11 +158,13 @@
</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">-         * Test WP_Customize_Widgets::register_settings()
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Test WP_Customize_Widgets::register_settings() with selective refresh enabled.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 30988
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @ticket 36389
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_register_settings() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_theme_support( 'customize-selective-refresh-widgets' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $raw_widget_customized = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'widget_categories[2]' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -176,15 +186,59 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->do_customize_boot_actions();
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( is_customize_preview() );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertNotEmpty( $this->manager->get_setting( 'widget_categories[2]' ), 'Expected setting for pre-existing widget category-2, being customized.' );
-               $this->assertNotEmpty( $this->manager->get_setting( 'widget_search[2]' ), 'Expected setting for pre-existing widget search-2, not being customized.' );
-               $this->assertNotEmpty( $this->manager->get_setting( 'widget_search[3]' ), 'Expected dynamic setting for non-existing widget search-3, being customized.' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( current_theme_supports( 'customize-selective-refresh-widgets' ) ) {
+                       $expected_transport = 'postMessage';
+                       $this->assertNotEmpty( $this->manager->widgets->get_selective_refreshable_widgets() );
+               } else {
+                       $expected_transport = 'refresh';
+                       $this->assertEmpty( $this->manager->widgets->get_selective_refreshable_widgets() );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $setting = $this->manager->get_setting( 'widget_categories[2]' );
+               $this->assertNotEmpty( $setting, 'Expected setting for pre-existing widget category-2, being customized.' );
+               $this->assertEquals( $expected_transport, $setting->transport );
+
+               $setting = $this->manager->get_setting( 'widget_search[2]' );
+               $this->assertNotEmpty( $setting, 'Expected setting for pre-existing widget search-2, not being customized.' );
+               $this->assertEquals( $expected_transport, $setting->transport );
+
+               $setting = $this->manager->get_setting( 'widget_search[3]' );
+               $this->assertNotEmpty( $setting, 'Expected dynamic setting for non-existing widget search-3, being customized.' );
+               $this->assertEquals( $expected_transport, $setting->transport );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $widget_categories = get_option( 'widget_categories' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' );
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test registering settings without selective refresh enabled.
+        *
+        * @ticket 36389
+        */
+       function test_register_settings_without_selective_refresh() {
+               remove_theme_support( 'customize-selective-refresh-widgets' );
+               $this->test_register_settings();
+       }
+
+       /**
+        * Test registering settings with selective refresh enabled at a late after_setup_theme action.
+        *
+        * @ticket 36389
+        */
+       function test_register_settings_with_late_theme_support_added() {
+               remove_theme_support( 'customize-selective-refresh-widgets' );
+               add_action( 'after_setup_theme', array( $this, 'add_customize_selective_refresh_theme_support' ), 100 );
+               $this->test_register_settings();
+       }
+
+       /**
+        * Add customize-selective-refresh-widgets theme support.
+        */
+       function add_customize_selective_refresh_theme_support() {
+               add_theme_support( 'customize-selective-refresh-widgets' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test WP_Customize_Widgets::get_setting_args()
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        function test_get_setting_args() {
</span></span></pre>
</div>
</div>

</body>
</html>