Make WordPress Core

Changeset 51232


Ignore:
Timestamp:
06/25/2021 12:07:15 AM (3 years ago)
Author:
desrosj
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-widgets.php

    r51228 r51232  
    14141414
    14151415        if ( array() === $value ) {
    1416             return;
     1416            return $value;
    14171417        }
    14181418
  • trunk/tests/phpunit/tests/customize/widgets.php

    r51228 r51232  
    482482
    483483    /**
     484     * There should be a 'raw_instance' key when the block editor is enabled and
     485     * the widget supports them via `show_instance_in_rest`.
     486     *
     487     * @ticket 53489
     488     */
     489    function test_sanitize_widget_instance_raw_instance() {
     490        remove_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) );
     491        $this->do_customize_boot_actions();
     492
     493        $block_instance = array(
     494            'content' => '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
     495        );
     496
     497        $sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $block_instance, 'block' );
     498        $this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js );
     499        $this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) );
     500        $this->assertSame( '', $sanitized_for_js['title'] );
     501        $this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
     502        $this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
     503        $this->assertEquals( (object) $block_instance, $sanitized_for_js['raw_instance'] );
     504
     505        $unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );
     506        $this->assertSame( $unsanitized_from_js, $block_instance );
     507    }
     508
     509    /**
     510     * There should NOT be a 'raw_instance' key when the block editor is enabled
     511     * but the widget does not support them because `show_instance_in_rest` on
     512     * the widget is set to false.
     513     *
     514     * @ticket 53489
     515     */
     516    function test_sanitize_widget_instance_with_no_show_instance_in_rest() {
     517        global $wp_widget_factory;
     518
     519        remove_action( 'widgets_init', array( $this, 'remove_widgets_block_editor' ) );
     520        $this->do_customize_boot_actions();
     521
     522        $widget_object = $wp_widget_factory->get_widget_object( 'block' );
     523        $widget_object->widget_options['show_instance_in_rest'] = false;
     524
     525        $block_instance = array(
     526            'content' => '<!-- wp:paragraph --><p>Hello</p><!-- /wp:paragraph -->',
     527        );
     528
     529        $sanitized_for_js = $this->manager->widgets->sanitize_widget_js_instance( $block_instance, 'block' );
     530        $this->assertArrayHasKey( 'encoded_serialized_instance', $sanitized_for_js );
     531        $this->assertTrue( is_serialized( base64_decode( $sanitized_for_js['encoded_serialized_instance'] ), true ) );
     532        $this->assertSame( '', $sanitized_for_js['title'] );
     533        $this->assertTrue( $sanitized_for_js['is_widget_customizer_js_value'] );
     534        $this->assertArrayHasKey( 'instance_hash_key', $sanitized_for_js );
     535        $this->assertArrayNotHasKey( 'raw_instance', $sanitized_for_js );
     536
     537        $unsanitized_from_js = $this->manager->widgets->sanitize_widget_instance( $sanitized_for_js );
     538        $this->assertSame( $unsanitized_from_js, $block_instance );
     539    }
     540
     541    /**
     542     * Empty instances, seen when inserting a new widget, should be left alone
     543     * when sanitized.
     544     *
     545     * @ticket 53479
     546     */
     547    function test_sanitize_widget_instance_empty_instance() {
     548        $this->do_customize_boot_actions();
     549        $this->assertSame( $this->manager->widgets->sanitize_widget_instance( array() ), array() );
     550    }
     551
     552    /**
    484553     * Get the widget control args for tests.
    485554     *
Note: See TracChangeset for help on using the changeset viewer.