Skip to content

Commit

Permalink
Introduce heuristic for identifying "primary" nav location in a theme.
Browse files Browse the repository at this point in the history
This is important when creating default nav menu items in site
templates, and then copying those values into sites created from
those templates.

See cuny-academic-commons/commons-in-a-box#462.
  • Loading branch information
boonebgorges committed Jul 17, 2024
1 parent 5ad29d1 commit 526d089
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
5 changes: 3 additions & 2 deletions classes/Upgrades/NavMenus.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ public function process( $item ) {

switch_to_blog( $site_id );

$locations = get_theme_mod( 'nav_menu_locations' );
$menu_id = isset( $locations['primary'] ) ? (int) $locations['primary'] : 0;
$locations = get_theme_mod( 'nav_menu_locations' );
$primary_nav_key = cboxol_get_theme_primary_nav_menu_location();
$menu_id = isset( $locations[ $primary_nav_key ] ) ? (int) $locations[ $primary_nav_key ] : 0;

if ( ! $menu_id ) {
restore_current_blog();
Expand Down
33 changes: 30 additions & 3 deletions includes/group-sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -1820,9 +1820,10 @@ function( $v ) use ( $source_site_url, $source_site_upload_dir, $dest_site_url,
}

// Updated custom nav menu items
$locations = get_theme_mod( 'nav_menu_locations' );
$menu_id = isset( $locations['primary'] ) ? (int) $locations['primary'] : 0;
$nav_items = get_term_meta( $menu_id, 'cboxol_custom_menus', true );
$primary_nav_key = cboxol_get_theme_primary_nav_menu_location();
$locations = get_theme_mod( 'nav_menu_locations' );
$menu_id = isset( $locations[ $primary_nav_key ] ) ? (int) $locations[ $primary_nav_key ] : 0;
$nav_items = get_term_meta( $menu_id, 'cboxol_custom_menus', true );

if ( $menu_id && ! empty( $nav_items ) ) {
$group_type = cboxol_get_group_group_type( $group_id );
Expand Down Expand Up @@ -2127,6 +2128,32 @@ function cboxol_get_nav_menu_items() {
return $items;
}

/**
* Determine a theme's primary nav menu location.
*
* Uses a heuristic based on naming conventions, and falls back on the first available.
*
* @since 1.6.0
*
* @return string|null
*/
function cboxol_get_theme_primary_nav_menu_location() {
$keys_to_check = [ 'primary', 'main', 'header', 'top' ];

$locations = get_nav_menu_locations();
if ( ! $locations ) {
return null;
}

foreach ( $keys_to_check as $key ) {
if ( isset( $locations[ $key ] ) ) {
return $key;
}
}

return key( $locations );
}

/**
* Register meta box for CBOX OpenLab nav menu.
*
Expand Down
5 changes: 3 additions & 2 deletions includes/site-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,9 @@ function cboxol_create_site_for_template( $template_id, $slug, $name ) {
true
);

$locations = get_theme_mod( 'nav_menu_locations' );
$locations['primary'] = $menu_id;
$primary_nav_key = cboxol_get_theme_primary_nav_menu_location();
$locations = get_theme_mod( 'nav_menu_locations' );
$locations[ $primary_nav_key ] = $menu_id;
set_theme_mod( 'nav_menu_locations', $locations );

restore_current_blog();
Expand Down

0 comments on commit 526d089

Please sign in to comment.