| 176 | |
| 177 | /** |
| 178 | * Import menus without menu item from source site to current site. |
| 179 | * Create default `Home` menu item for each menu. |
| 180 | * Also set menu locations as per source site. |
| 181 | * |
| 182 | * @param type $source_site_id ID of source site. |
| 183 | */ |
| 184 | protected function import_menus( $source_site_id ) { |
| 185 | switch_to_blog( $source_site_id ); |
| 186 | |
| 187 | // Get all menus from source site. |
| 188 | $menus = wp_get_nav_menus(); |
| 189 | |
| 190 | if ( empty( $menus ) || is_wp_error( $menus ) ) { |
| 191 | restore_current_blog(); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | // Get menu locations from source site. |
| 196 | $menu_locations = get_nav_menu_locations(); |
| 197 | |
| 198 | restore_current_blog(); |
| 199 | |
| 200 | $ref = array(); |
| 201 | |
| 202 | // Create menu and add one menu item to that menu. |
| 203 | foreach ( $menus as $menu ) { |
| 204 | $new_menu_id = wp_create_nav_menu( $menu->name ); |
| 205 | |
| 206 | if ( is_wp_error( $new_menu_id ) ) { |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | // save new menu id reference for old menu id. Use to set menu location. |
| 211 | $ref[ $menu->term_id ] = $new_menu_id; |
| 212 | |
| 213 | $args = array( |
| 214 | 'menu-item-position' => 1, |
| 215 | 'menu-item-type' => 'custom', |
| 216 | 'menu-item-title' => __( 'Home', 'wordcamporg' ), |
| 217 | 'menu-item-attr-title' => __( 'Home', 'wordcamporg' ), |
| 218 | 'menu-item-url' => home_url( '/' ), |
| 219 | 'menu-item-status' => 'publish', |
| 220 | ); |
| 221 | |
| 222 | wp_update_nav_menu_item( $new_menu_id, 0, $args ); |
| 223 | } |
| 224 | |
| 225 | // Set menu to same menu location as source site. |
| 226 | if ( ! empty( $menu_locations ) ) { |
| 227 | foreach ( $menu_locations as $menu_location => $menu_id ) { |
| 228 | $menu_locations[ $menu_location ] = $ref[ $menu_id ]; |
| 229 | } |
| 230 | set_theme_mod( 'nav_menu_locations', array_map( 'absint', $menu_locations ) ); |
| 231 | } |
| 232 | } |