WordPress.org

Make WordPress Core

Changeset 10927


Ignore:
Timestamp:
04/14/2009 01:17:57 AM (10 years ago)
Author:
azaozz
Message:

Match old/new sidebars by index when switching themes, see #9511

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/widgets.php

    r10916 r10927  
    3939    $sidebars_widgets = wp_get_widget_defaults();
    4040
    41 // look for "lost" widgets, perhaps run only after changin themes
     41// look for "lost" widgets, this has to run at each theme change
    4242function retrieve_widgets() {
    4343    global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets;
    4444
    45     $key_sidebars = array_keys( (array) $wp_registered_sidebars);
    46     $key_widgets = array_keys($sidebars_widgets);
    47     if ( count($key_widgets) > count($key_sidebars) ) {
    48         $changed_sidebars = array_diff( $key_widgets, $key_sidebars );
    49         foreach ( $changed_sidebars as $lost ) {
    50             if ( is_array($sidebars_widgets[$lost]) )
    51                 $sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $sidebars_widgets['wp_inactive_widgets'], $sidebars_widgets[$lost] );
    52             unset($sidebars_widgets[$lost]);
     45    $_sidebars_widgets = array();
     46    $sidebars = array_keys($wp_registered_sidebars);
     47
     48    $diff = array_diff( array_keys($sidebars_widgets), $sidebars );
     49    if ( empty($diff) )
     50        return;
     51
     52    unset( $sidebars_widgets['array_version'] );
     53
     54    // Move the known-good ones first
     55    foreach ( $sidebars as $id ) {
     56        if ( array_key_exists( $id, $sidebars_widgets ) ) {
     57            $_sidebars_widgets[$id] = $sidebars_widgets[$id];
     58            unset($sidebars_widgets[$id], $sidebars[$id]);
    5359        }
    5460    }
    5561
     62    // Assign to each unmatched registered sidebar the first available orphan
     63    while ( ( $sidebar = array_shift( $sidebars ) ) && $widgets = array_shift( $sidebars_widgets ) )
     64        $_sidebars_widgets[ $sidebar ] = $widgets;
     65
     66    // if new theme has less sidebars than the old theme
     67    if ( !empty($sidebars_widgets) ) {
     68        foreach ( $sidebars_widgets as $lost => $val ) {
     69            if ( is_array($val) )
     70                $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
     71        }
     72    }
     73
     74    $sidebars_widgets = $_sidebars_widgets;
     75    unset($_sidebars_widgets);
     76
     77    // find hidden/lost multi-widget instances
    5678    $shown_widgets = array();
    5779    foreach ( $sidebars_widgets as $sidebar ) {
     
    84106
    85107    $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
     108    $sidebars_widgets['array_version'] = 3;
    86109    wp_set_sidebars_widgets($sidebars_widgets);
    87110}
     
    134157<div id="message" class="updated fade"><p><?php echo $messages[$_GET['message']]; ?></p></div>
    135158<?php endif; ?>
    136 
    137159
    138160<!--
Note: See TracChangeset for help on using the changeset viewer.