Make WordPress Core


Ignore:
Timestamp:
12/07/2017 02:19:11 AM (9 years ago)
Author:
obenland
Message:

Widgets: Always try restoring widget assignments

For code-historic reasons, restoring widget assignments would not be attempted if there were no sidebars to map.
Restoring previous assignments is something unrelated to sidebar mappings however, so now it will be attempted on every theme switch.

See #42719.

File:
1 edited

Legend:

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

    r42362 r42374  
    12201220 *
    12211221 * @since 4.9.0
     1222 * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.
    12221223 *
    12231224 * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs.
     
    12621263        }
    12631264
    1264         // If there are no old sidebars left, then we're done.
    1265         if ( empty( $existing_sidebars_widgets ) ) {
    1266                 return $new_sidebars_widgets;
    1267         }
    1268 
    1269         /*
    1270          * If old and new theme both have sidebars that contain phrases
    1271          * from within the same group, make an educated guess and map it.
    1272          */
    1273         $common_slug_groups = array(
    1274                 array( 'sidebar', 'primary', 'main', 'right' ),
    1275                 array( 'second', 'left' ),
    1276                 array( 'sidebar-2', 'footer', 'bottom' ),
    1277                 array( 'header', 'top' ),
    1278         );
    1279 
    1280         // Go through each group...
    1281         foreach ( $common_slug_groups as $slug_group ) {
    1282 
    1283                 // ...and see if any of these slugs...
    1284                 foreach ( $slug_group as $slug ) {
    1285 
    1286                         // ...and any of the new sidebars...
    1287                         foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
    1288 
    1289                                 // ...actually match!
    1290                                 if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
    1291                                         continue;
    1292                                 }
    1293 
    1294                                 // Then see if any of the existing sidebars...
    1295                                 foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
    1296 
    1297                                         // ...and any slug in the same group...
    1298                                         foreach ( $slug_group as $slug ) {
    1299 
    1300                                                 // ... have a match as well.
    1301                                                 if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
    1302                                                         continue;
    1303                                                 }
    1304 
    1305                                                 // Make sure this sidebar wasn't mapped and removed previously.
    1306                                                 if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
    1307 
    1308                                                         // We have a match that can be mapped!
    1309                                                         $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
    1310 
    1311                                                         // Remove the mapped sidebar so it can't be mapped again.
    1312                                                         unset( $existing_sidebars_widgets[ $sidebar ] );
    1313 
    1314                                                         // Go back and check the next new sidebar.
    1315                                                         continue 3;
    1316                                                 }
    1317                                         } // endforeach ( $slug_group as $slug )
    1318                                 } // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets )
    1319                         } // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args )
    1320                 } // endforeach ( $slug_group as $slug )
    1321         } // endforeach ( $common_slug_groups as $slug_group )
     1265        // If there are more sidebars, try to map them.
     1266        if ( ! empty( $existing_sidebars_widgets ) ) {
     1267
     1268                /*
     1269                 * If old and new theme both have sidebars that contain phrases
     1270                 * from within the same group, make an educated guess and map it.
     1271                 */
     1272                $common_slug_groups = array(
     1273                        array( 'sidebar', 'primary', 'main', 'right' ),
     1274                        array( 'second', 'left' ),
     1275                        array( 'sidebar-2', 'footer', 'bottom' ),
     1276                        array( 'header', 'top' ),
     1277                );
     1278
     1279                // Go through each group...
     1280                foreach ( $common_slug_groups as $slug_group ) {
     1281
     1282                        // ...and see if any of these slugs...
     1283                        foreach ( $slug_group as $slug ) {
     1284
     1285                                // ...and any of the new sidebars...
     1286                                foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
     1287
     1288                                        // ...actually match!
     1289                                        if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
     1290                                                continue;
     1291                                        }
     1292
     1293                                        // Then see if any of the existing sidebars...
     1294                                        foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
     1295
     1296                                                // ...and any slug in the same group...
     1297                                                foreach ( $slug_group as $slug ) {
     1298
     1299                                                        // ... have a match as well.
     1300                                                        if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
     1301                                                                continue;
     1302                                                        }
     1303
     1304                                                        // Make sure this sidebar wasn't mapped and removed previously.
     1305                                                        if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
     1306
     1307                                                                // We have a match that can be mapped!
     1308                                                                $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
     1309
     1310                                                                // Remove the mapped sidebar so it can't be mapped again.
     1311                                                                unset( $existing_sidebars_widgets[ $sidebar ] );
     1312
     1313                                                                // Go back and check the next new sidebar.
     1314                                                                continue 3;
     1315                                                        }
     1316                                                } // endforeach ( $slug_group as $slug )
     1317                                        } // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets )
     1318                                } // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args )
     1319                        } // endforeach ( $slug_group as $slug )
     1320                } // endforeach ( $common_slug_groups as $slug_group )
     1321        }
    13221322
    13231323        // Move any left over widgets to inactive sidebar.
     
    13301330        // Sidebars_widgets settings from when this theme was previously active.
    13311331        $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
    1332         $old_sidebars_widgets = $old_sidebars_widgets['data'];
     1332        $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false;
    13331333
    13341334        if ( is_array( $old_sidebars_widgets ) ) {
Note: See TracChangeset for help on using the changeset viewer.