Make WordPress Core


Ignore:
Timestamp:
02/03/2023 12:13:52 AM (2 years ago)
Author:
hellofromTonya
Message:

Widgets: Preserve classic sidebars when switching to a block theme.

When switching to a block theme, classic sidebars were orphaned and their widgets remapping to the 'wp_inactive_widgets' sidebar . This changeset preserves the sidebars and their widgets, providing a migration path to a block theme without losing the widgets.

Classic sidebars are now:

  • Stored in a new theme mod called 'wp_classic_sidebars';
  • Restored to the $wp_registered_sidebars global variable when the 'widgets_init' action fires (via a new internal function called _wp_block_theme_register_classic_sidebars());
  • And marked as 'inactive' when interacting with sidebars REST API endpoint.

References:

Follow-up to [50995], [6334].

Props mamaduka, audrasjb, hellofromTonya, ironprogrammer, jameskoster, joen, matveb, mukesh27, noisysocks, poena, youknowriad.
Fixes #57531.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-sidebars-controller.php

    r54896 r55200  
    862862
    863863    /**
     864     * @ticket 57531
     865     * @covers WP_Test_REST_Sidebars_Controller::prepare_item_for_response
     866     */
     867    public function test_prepare_item_for_response_to_set_inactive_on_theme_switch() {
     868        $request = new WP_REST_Request( 'GET', '/wp/v2/sidebars/sidebar-1' );
     869
     870        // Set up the test.
     871        wp_widgets_init();
     872        $this->setup_widget(
     873            'widget_rss',
     874            1,
     875            array(
     876                'title' => 'RSS test',
     877            )
     878        );
     879        $this->setup_widget(
     880            'widget_text',
     881            1,
     882            array(
     883                'text' => 'Custom text test',
     884            )
     885        );
     886        $this->setup_sidebar(
     887            'sidebar-1',
     888            array(
     889                'name' => 'Sidebar 1',
     890            ),
     891            array( 'text-1', 'rss-1' )
     892        );
     893
     894        // Validate the state before a theme switch.
     895        $response = rest_get_server()->dispatch( $request );
     896        $data     = $response->get_data();
     897        $data     = $this->remove_links( $data );
     898
     899        $this->assertSame( 'active', $data['status'] );
     900        $this->assertFalse(
     901            get_theme_mod( 'wp_classic_sidebars' ),
     902            'wp_classic_sidebars theme mod should not exist before switching to block theme'
     903        );
     904
     905        switch_theme( 'block-theme' );
     906        wp_widgets_init();
     907
     908        // Validate the state after a theme switch.
     909        $response = rest_get_server()->dispatch( $request );
     910        $data     = $response->get_data();
     911        $data     = $this->remove_links( $data );
     912
     913        $this->assertSame(
     914            'inactive',
     915            $data['status'],
     916            'Sidebar status should have changed to inactive'
     917        );
     918        $this->assertSame(
     919            array( 'text-1', 'rss-1' ),
     920            $data['widgets'],
     921            'The text and rss widgets should still in sidebar-1'
     922        );
     923        $this->assertArrayHasKey(
     924            'sidebar-1',
     925            get_theme_mod( 'wp_classic_sidebars' ),
     926            'sidebar-1 should be in "wp_classic_sidebars" theme mod'
     927        );
     928    }
     929
     930    /**
    864931     * @ticket 41683
    865932     */
Note: See TracChangeset for help on using the changeset viewer.