Make WordPress Core


Ignore:
Timestamp:
08/02/2017 05:34:32 AM (8 years ago)
Author:
westonruter
Message:

Customize: Introduce settings_previewed arg and getter on WP_Customize_Manager which controls whether WP_Customize_Setting::preview() should be called on settings.

The settings_previewed property eliminates the need for the Customizer components from having to look at global doing_ajax state. This is in particular needed when saving settings, as some settings will short-circuit the update operation if they detect no changes are introduced. This is also needed for low-level integrations with the Customizer, such as in REST API endpoints under development.

Props stubgo, westonruter, utkarshpatel for testing.
See #38900.
Fixes #39221.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/manager.php

    r41012 r41205  
    214214        $wp_customize->setup_theme();
    215215        $this->assertTrue( $show_admin_bar );
     216    }
     217
     218    /**
     219     * Test WP_Customize_Manager::settings_previewed().
     220     *
     221     * @ticket 39221
     222     * @covers WP_Customize_Manager::settings_previewed()
     223     */
     224    function test_settings_previewed() {
     225        $wp_customize = new WP_Customize_Manager( array( 'settings_previewed' => false ) );
     226        $this->assertSame( false, $wp_customize->settings_previewed() );
     227
     228        $wp_customize = new WP_Customize_Manager();
     229        $this->assertSame( true, $wp_customize->settings_previewed() );
    216230    }
    217231
     
    12931307
    12941308    /**
     1309     * Test saving settings by publishing a changeset outside of Customizer entirely.
     1310     *
     1311     * Widgets get their settings registered and previewed early in the admin,
     1312     * so this ensures that the previewing is bypassed when in the context of
     1313     * publishing
     1314     *
     1315     * @ticket 39221
     1316     * @covers _wp_customize_publish_changeset()
     1317     * @see WP_Customize_Widgets::schedule_customize_register()
     1318     * @see WP_Customize_Widgets::customize_register()
     1319     */
     1320    function test_wp_customize_publish_changeset() {
     1321        global $wp_customize;
     1322        $wp_customize = null;
     1323
     1324        // Set the admin current screen to cause WP_Customize_Widgets::schedule_customize_register() to do early setting registration.
     1325        set_current_screen( 'edit' );
     1326        $this->assertTrue( is_admin() );
     1327
     1328        $old_sidebars_widgets = get_option( 'sidebars_widgets' );
     1329        $new_sidebars_widgets = $old_sidebars_widgets;
     1330        $this->assertGreaterThan( 2, count( $new_sidebars_widgets['sidebar-1'] ) );
     1331        $new_sidebar_1 = array_reverse( $new_sidebars_widgets['sidebar-1'] );
     1332
     1333        $post_id = $this->factory()->post->create( array(
     1334            'post_type' => 'customize_changeset',
     1335            'post_status' => 'draft',
     1336            'post_name' => wp_generate_uuid4(),
     1337            'post_content' => wp_json_encode( array(
     1338                'sidebars_widgets[sidebar-1]' => array(
     1339                    'value' => $new_sidebar_1,
     1340                ),
     1341            ) ),
     1342        ) );
     1343
     1344        // Save the updated sidebar widgets into the options table by publishing the changeset.
     1345        wp_publish_post( $post_id );
     1346
     1347        // Make sure previewing filters were never added, since WP_Customize_Manager should be constructed with settings_previewed=false.
     1348        $this->assertFalse( has_filter( 'option_sidebars_widgets' ) );
     1349        $this->assertFalse( has_filter( 'default_option_sidebars_widgets' ) );
     1350
     1351        // Ensure that the value has actually been written to the DB.
     1352        $updated_sidebars_widgets = get_option( 'sidebars_widgets' );
     1353        $this->assertEquals( $new_sidebar_1, $updated_sidebars_widgets['sidebar-1'] );
     1354    }
     1355
     1356    /**
    12951357     * Ensure that save_changeset_post method bails updating an underlying changeset which is invalid.
    12961358     *
Note: See TracChangeset for help on using the changeset viewer.