Widgets: Fix an “Invalid value” warning when adding a new widget in the Customizer.

This fixes a regression introduced in [50996] where sites that have been opted-out of the block-based widget editor experienced an “Invalid value.” error when adding a new widget to a sidebar in the Customizer.

This was caused by the early return value was changed to `null` from `$value` when set to an empty `array`, resulting in the widget being evaluated as invalid elsewhere.

Props jamesros161, caseymilne, naoki0h, ixkaito, zieladam, noisysocks, hellofromTonya.
Fixes #53479.

git-svn-id: https://develop.svn.wordpress.org/trunk@51232 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2021-06-25 00:07:15 +00:00
parent 5def66daa0
commit fc0c7a01f2
2 changed files with 70 additions and 1 deletions

View File

@ -1413,7 +1413,7 @@ final class WP_Customize_Widgets {
global $wp_widget_factory;
if ( array() === $value ) {
return;
return $value;
}
if ( isset( $value['raw_instance'] ) && $id_base && wp_use_widgets_block_editor() ) {

View File

@ -480,6 +480,75 @@ class Tests_WP_Customize_Widgets extends WP_UnitTestCase {
$this->assertSame( $unsanitized_from_js, $new_categories_instance );
}
/**
* There should be a 'raw_instance' key when the block editor is enabled and
* the widget supports them via `show_instance_in_rest`.
*
* @ticket 53489
*/
function test_sanitize_widget_instance_raw_instance() {
remove_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) );
$this->do_customize_boot_actions();
$block_instance = array(
'content' => '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
);
$sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $block_instance, 'block' );
$this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js );
$this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) );
$this->assertSame( '', $sanitized_for_js['title'] );
$this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
$this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
$this->assertEquals( (object) $block_instance, $sanitized_for_js['raw_instance'] );
$unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );
$this->assertSame( $unsanitized_from_js, $block_instance );
}
/**
* There should NOT be a 'raw_instance' key when the block editor is enabled
* but the widget does not support them because `show_instance_in_rest` on
* the widget is set to false.
*
* @ticket 53489
*/
function test_sanitize_widget_instance_with_no_show_instance_in_rest() {
global $wp_widget_factory;
remove_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) );
$this->do_customize_boot_actions();
$widget_object = $wp_widget_factory->get_widget_object( 'block' );
$widget_object->widget_options['show_instance_in_rest'] = false;
$block_instance = array(
'content' => '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
);
$sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $block_instance, 'block' );
$this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js );
$this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) );
$this->assertSame( '', $sanitized_for_js['title'] );
$this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
$this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
$this->assertArrayNotHasKey( 'raw_instance', $sanitized_for_js );
$unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );
$this->assertSame( $unsanitized_from_js, $block_instance );
}
/**
* Empty instances, seen when inserting a new widget, should be left alone
* when sanitized.
*
* @ticket 53479
*/
function test_sanitize_widget_instance_empty_instance() {
$this->do_customize_boot_actions();
$this->assertSame( $this->manager->widgets->sanitize_widget_instance( array() ), array() );
}
/**
* Get the widget control args for tests.
*