| 981 | |
| 982 | /** |
| 983 | * Handle menu config after theme change |
| 984 | * |
| 985 | * @access private |
| 986 | * @since 4.8.0 |
| 987 | */ |
| 988 | function _wp_menus_changed() { |
| 989 | $old_nav_menu_locations = get_option( 'theme_switch_menu_locations' ); |
| 990 | $new_nav_menu_locations = get_nav_menu_locations(); |
| 991 | $registered_nav_menus = get_registered_nav_menus(); |
| 992 | |
| 993 | // If old and new theme have just one location, map it. |
| 994 | if ( 1 === count( $old_nav_menu_locations ) && 1 === count( $registered_nav_menus ) ) { |
| 995 | $new_nav_menu_locations[ key( $registered_nav_menus ) ] = array_pop( $old_nav_menu_locations ); |
| 996 | } else { |
| 997 | $old_locations = array_keys( $old_nav_menu_locations ); |
| 998 | |
| 999 | // Map locations with the same slug. |
| 1000 | foreach ( $registered_nav_menus as $location => $name ) { |
| 1001 | if ( in_array( $location, $old_locations ) ) { |
| 1002 | $new_nav_menu_locations[ $location ] = $old_nav_menu_locations[ $location ]; |
| 1003 | unset( $old_nav_menu_locations[ $location ] ); |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | if ( ! empty( $old_nav_menu_locations ) ) { |
| 1008 | /* |
| 1009 | * If old and new theme both have locations that contain phrases |
| 1010 | * from within the same group, make an educated guess and map it. |
| 1011 | */ |
| 1012 | $common_slug_groups = array( |
| 1013 | array( 'main', 'primary', 'navigation', 'top' ), |
| 1014 | array( 'secondary', 'footer', 'subsidiary', 'bottom' ), |
| 1015 | array( 'social' ), |
| 1016 | ); |
| 1017 | |
| 1018 | foreach( $common_slug_groups as $slug_group ) { |
| 1019 | foreach( $old_nav_menu_locations as $location => $menu_id ) { |
| 1020 | foreach ( $slug_group as $slug ) { |
| 1021 | if ( strpos( $location, $slug ) || strpos( $slug, $location ) ) { |
| 1022 | foreach( $registered_nav_menus as $new_location => $name ) { |
| 1023 | if ( strpos( $new_location, $slug ) || strpos( $slug, $new_location ) ) { |
| 1024 | $new_nav_menu_locations[ $new_location ] = $old_nav_menu_locations[ $location ]; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | set_theme_mod( 'nav_menu_locations', $new_nav_menu_locations ); |
| 1035 | delete_option( 'theme_switch_menu_locations' ); |
| 1036 | } |