Make WordPress Core

Changeset 42375 for branches/4.9


Ignore:
Timestamp:
12/07/2017 02:22:55 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.

Fixes #42719.

Merges [42374] to the 4.9 branch.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/widgets.php

    r42363 r42375  
    11811181 *
    11821182 * @since 4.9.0
     1183 * @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping.
    11831184 *
    11841185 * @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs.
    … …  
    12231224        }
    12241225
    1225         // If there are no old sidebars left, then we're done.
    1226         if ( empty( $existing_sidebars_widgets ) ) {
    1227                 return $new_sidebars_widgets;
    1228         }
    1229 
    1230         /*
    1231          * If old and new theme both have sidebars that contain phrases
    1232          * from within the same group, make an educated guess and map it.
    1233          */
    1234         $common_slug_groups = array(
    1235                 array( 'sidebar', 'primary', 'main', 'right' ),
    1236                 array( 'second', 'left' ),
    1237                 array( 'sidebar-2', 'footer', 'bottom' ),
    1238                 array( 'header', 'top' ),
    1239         );
    1240 
    1241         // Go through each group...
    1242         foreach ( $common_slug_groups as $slug_group ) {
    1243 
    1244                 // ...and see if any of these slugs...
    1245                 foreach ( $slug_group as $slug ) {
    1246 
    1247                         // ...and any of the new sidebars...
    1248                         foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
    1249 
    1250                                 // ...actually match!
    1251                                 if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
    1252                                         continue;
    1253                                 }
    1254 
    1255                                 // Then see if any of the existing sidebars...
    1256                                 foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
    1257 
    1258                                         // ...and any slug in the same group...
    1259                                         foreach ( $slug_group as $slug ) {
    1260 
    1261                                                 // ... have a match as well.
    1262                                                 if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
    1263                                                         continue;
    1264                                                 }
    1265 
    1266                                                 // Make sure this sidebar wasn't mapped and removed previously.
    1267                                                 if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
    1268 
    1269                                                         // We have a match that can be mapped!
    1270                                                         $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
    1271 
    1272                                                         // Remove the mapped sidebar so it can't be mapped again.
    1273                                                         unset( $existing_sidebars_widgets[ $sidebar ] );
    1274 
    1275                                                         // Go back and check the next new sidebar.
    1276                                                         continue 3;
    1277                                                 }
    1278                                         } // endforeach ( $slug_group as $slug )
    1279                                 } // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets )
    1280                         } // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args )
    1281                 } // endforeach ( $slug_group as $slug )
    1282         } // endforeach ( $common_slug_groups as $slug_group )
     1226        // If there are more sidebars, try to map them.
     1227        if ( ! empty( $existing_sidebars_widgets ) ) {
     1228
     1229                /*
     1230                 * If old and new theme both have sidebars that contain phrases
     1231                 * from within the same group, make an educated guess and map it.
     1232                 */
     1233                $common_slug_groups = array(
     1234                        array( 'sidebar', 'primary', 'main', 'right' ),
     1235                        array( 'second', 'left' ),
     1236                        array( 'sidebar-2', 'footer', 'bottom' ),
     1237                        array( 'header', 'top' ),
     1238                );
     1239
     1240                // Go through each group...
     1241                foreach ( $common_slug_groups as $slug_group ) {
     1242
     1243                        // ...and see if any of these slugs...
     1244                        foreach ( $slug_group as $slug ) {
     1245
     1246                                // ...and any of the new sidebars...
     1247                                foreach ( $wp_registered_sidebars as $new_sidebar => $args ) {
     1248
     1249                                        // ...actually match!
     1250                                        if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) {
     1251                                                continue;
     1252                                        }
     1253
     1254                                        // Then see if any of the existing sidebars...
     1255                                        foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) {
     1256
     1257                                                // ...and any slug in the same group...
     1258                                                foreach ( $slug_group as $slug ) {
     1259
     1260                                                        // ... have a match as well.
     1261                                                        if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) {
     1262                                                                continue;
     1263                                                        }
     1264
     1265                                                        // Make sure this sidebar wasn't mapped and removed previously.
     1266                                                        if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) {
     1267
     1268                                                                // We have a match that can be mapped!
     1269                                                                $new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] );
     1270
     1271                                                                // Remove the mapped sidebar so it can't be mapped again.
     1272                                                                unset( $existing_sidebars_widgets[ $sidebar ] );
     1273
     1274                                                                // Go back and check the next new sidebar.
     1275                                                                continue 3;
     1276                                                        }
     1277                                                } // endforeach ( $slug_group as $slug )
     1278                                        } // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets )
     1279                                } // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args )
     1280                        } // endforeach ( $slug_group as $slug )
     1281                } // endforeach ( $common_slug_groups as $slug_group )
     1282        }
    12831283
    12841284        // Move any left over widgets to inactive sidebar.
    … …  
    12911291        // Sidebars_widgets settings from when this theme was previously active.
    12921292        $old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' );
    1293         $old_sidebars_widgets = $old_sidebars_widgets['data'];
     1293        $old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false;
    12941294
    12951295        if ( is_array( $old_sidebars_widgets ) ) {
Note: See TracChangeset for help on using the changeset viewer.