diff --git a/src/wp-includes/class-wp-customize-setting.php b/src/wp-includes/class-wp-customize-setting.php index 9f72966734..d176d6aa2a 100644 --- a/src/wp-includes/class-wp-customize-setting.php +++ b/src/wp-includes/class-wp-customize-setting.php @@ -206,6 +206,19 @@ class WP_Customize_Setting { } } + /** + * 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(); + } + /** * The ID for the current site when the preview() method was called. * diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index 0b7b7ceac2..0a73953167 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -99,7 +99,7 @@ final class WP_Customize_Widgets { } add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 ); - add_action( 'after_setup_theme', array( $this, 'register_settings' ) ); + add_action( 'widgets_init', array( $this, 'register_settings' ), 95 ); add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); @@ -376,6 +376,8 @@ final class WP_Customize_Widgets { public function customize_register() { global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars; + add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); + $sidebars_widgets = array_merge( array( 'wp_inactive_widgets' => array() ), array_fill_keys( array_keys( $wp_registered_sidebars ), array() ), @@ -509,8 +511,6 @@ final class WP_Customize_Widgets { $this->manager->get_setting( $new_setting_id )->preview(); } } - - add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); } /** diff --git a/tests/phpunit/tests/customize/widgets.php b/tests/phpunit/tests/customize/widgets.php index 28392fdf59..721f804cf6 100644 --- a/tests/phpunit/tests/customize/widgets.php +++ b/tests/phpunit/tests/customize/widgets.php @@ -46,6 +46,9 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase { remove_action( 'customize_register', 'twentysixteen_customize_register', 11 ); $this->backup_registered_sidebars = $GLOBALS['wp_registered_sidebars']; + + // Reset protected static var on class. + WP_Customize_Setting::reset_aggregated_multidimensionals(); } function clean_up_global_scope() { @@ -70,6 +73,11 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase { function set_customized_post_data( $customized ) { $_POST['customized'] = wp_slash( wp_json_encode( $customized ) ); + if ( $this->manager ) { + foreach ( $customized as $id => $value ) { + $this->manager->set_post_value( $id, $value ); + } + } } function do_customize_boot_actions() { @@ -150,11 +158,13 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase { } /** - * Test WP_Customize_Widgets::register_settings() + * Test WP_Customize_Widgets::register_settings() with selective refresh enabled. * * @ticket 30988 + * @ticket 36389 */ function test_register_settings() { + add_theme_support( 'customize-selective-refresh-widgets' ); $raw_widget_customized = array( 'widget_categories[2]' => array( @@ -176,14 +186,58 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase { $this->do_customize_boot_actions(); $this->assertTrue( is_customize_preview() ); - $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.' ); + 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() ); + } + + $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 ); $widget_categories = get_option( 'widget_categories' ); $this->assertEquals( $raw_widget_customized['widget_categories[2]'], $widget_categories[2], 'Expected $wp_customize->get_setting(widget_categories[2])->preview() to have been called.' ); } + /** + * 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' ); + } + /** * Test WP_Customize_Widgets::get_setting_args() */