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 #36389. git-svn-id: https://develop.svn.wordpress.org/trunk@37166 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
f1c3ec9010
commit
ca5db69c31
@ -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.
|
||||
*
|
||||
|
@ -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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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()
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user