diff --git a/src/wp-includes/class-wp-customize-widgets.php b/src/wp-includes/class-wp-customize-widgets.php index 7f1be455a3..c1ed9c8e90 100644 --- a/src/wp-includes/class-wp-customize-widgets.php +++ b/src/wp-includes/class-wp-customize-widgets.php @@ -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() ) { diff --git a/tests/phpunit/tests/customize/widgets.php b/tests/phpunit/tests/customize/widgets.php index f872ff12ee..d157d52261 100644 --- a/tests/phpunit/tests/customize/widgets.php +++ b/tests/phpunit/tests/customize/widgets.php @@ -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' => '

Hello

', + ); + + $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' => '

Hello

', + ); + + $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. *