WordPress.org

Make WordPress Core

Ticket #9695: 9695-retrieve_widgets.diff

File 9695-retrieve_widgets.diff, 2.2 KB (added by Denis-de-Bernardy, 9 years ago)
  • wp-admin/widgets.php

     
    4040
    4141// look for "lost" widgets, this has to run at least on each theme change
    4242function retrieve_widgets() {
    43         global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets;
     43        global $wp_registered_widget_updates, $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets;
    4444
    4545        $_sidebars_widgets = array();
    4646        $sidebars = array_keys($wp_registered_sidebars);
    4747
     48        unset( $sidebars_widgets['array_version'] );
     49
    4850        $diff = array_diff( array_keys($sidebars_widgets), $sidebars );
    4951        if ( empty($diff) )
    5052                return;
    5153
    52         unset( $sidebars_widgets['array_version'] );
    53 
    5454        // Move the known-good ones first
    5555        foreach ( $sidebars as $id ) {
    5656                if ( array_key_exists( $id, $sidebars_widgets ) ) {
     
    5959                }
    6060        }
    6161
    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 
    6662        // if new theme has less sidebars than the old theme
    6763        if ( !empty($sidebars_widgets) ) {
    6864                foreach ( $sidebars_widgets as $lost => $val ) {
     
    7066                                $_sidebars_widgets['wp_inactive_widgets'] = array_merge( (array) $_sidebars_widgets['wp_inactive_widgets'], $val );
    7167                }
    7268        }
     69       
     70        // discard invalid, theme-specific widgets from sidebars
     71        foreach ( $_sidebars_widgets as $sidebar => $widgets ) {
     72                $_widgets = array();
     73                foreach ( $widgets as $widget ) {
     74                        if ( isset($wp_registered_widgets[$widget]) )
     75                                $_widgets[] = $widget;
     76                }
     77                $_sidebars_widgets[$sidebar] = $_widgets;
     78        }
    7379
    7480        $sidebars_widgets = $_sidebars_widgets;
    75         unset($_sidebars_widgets);
     81        unset($_sidebars_widgets, $_widgets);
    7682
    7783        // find hidden/lost multi-widget instances
    7884        $shown_widgets = array();
     
    105111        }
    106112
    107113        $sidebars_widgets['wp_inactive_widgets'] = array_merge($lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets']);
    108         $sidebars_widgets['array_version'] = 3;
    109114        wp_set_sidebars_widgets($sidebars_widgets);
    110115}
    111116retrieve_widgets();