WordPress.org

Make WordPress Core

Changeset 41594


Ignore:
Timestamp:
09/25/2017 04:18:31 PM (8 months ago)
Author:
obenland
Message:

Widgets: Merge orphaned widgets on theme switch

Merges orphaned widgets into the inactive sidebar when switching themes.

Props bpayton.
See #39693.

Location:
trunk
Files:
2 edited

Legend:

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

    r41590 r41594  
    11481148    }
    11491149
    1150 
    11511150    // Discard invalid, theme-specific widgets from sidebars.
    11521151    $sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids );
     
    12151214            $new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ];
    12161215            unset( $existing_sidebars_widgets[ $sidebar ] );
    1217         } else {
     1216        } else if ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) {
    12181217            $new_sidebars_widgets[ $sidebar ] = array();
    12191218        }
  • trunk/tests/phpunit/tests/widgets.php

    r41555 r41594  
    963963
    964964    /**
     965     * Tests for orphaned widgets being moved into inactive widgets.
     966     *
     967     * @covers retrieve_widgets()
     968     */
     969    function test_retrieve_widgets_move_orphaned_widgets_to_inactive() {
     970        global $sidebars_widgets;
     971
     972        wp_widgets_init();
     973        $this->register_sidebars( array( 'sidebar-1', 'sidebar-2', 'sidebar-3', 'wp_inactive_widgets' ) );
     974
     975        $sidebars_widgets = array(
     976            'sidebar-1' => array( 'tag_cloud-1' ),
     977            'sidebar-2' => array( 'text-1' ),
     978            'wp_inactive_widgets' => array( 'search-2', 'archives-2' ),
     979            'orphaned_widgets_1'  => array( 'calendar-1' ),
     980        );
     981
     982        retrieve_widgets();
     983
     984        $this->assertInternalType( 'array', $sidebars_widgets );
     985
     986        foreach ( $sidebars_widgets as $widgets ) {
     987            $this->assertInternalType( 'array', $widgets );
     988        }
     989
     990        // 6 default widgets + 1 orphaned calendar widget = 7.
     991        $this->assertCount( 7, $sidebars_widgets['wp_inactive_widgets'] );
     992        $this->assertContains( 'calendar-1', $sidebars_widgets['wp_inactive_widgets'] );
     993        $this->assertArrayNotHasKey( 'orphaned_widgets_1', $sidebars_widgets );
     994
     995        // Sidebar_widgets option was updated.
     996        $this->assertEquals( $sidebars_widgets, wp_get_sidebars_widgets() );
     997    }
     998
     999    /**
    9651000     * Test _wp_remove_unregistered_widgets.
    9661001     *
Note: See TracChangeset for help on using the changeset viewer.