<?php
/**
* Menu Functions
*
* @package GridMini WordPress Theme
* @copyright Copyright (C) 2025 ThemesDNA
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
* @author ThemesDNA <themesdna@gmail.com>
*/

// Get our wp_nav_menu() fallback, wp_page_menu(), to show a "Home" link as the first item
function gridmini_page_menu_args( $args ) {
    $args['show_home'] = true;
    return $args;
}
add_filter( 'wp_page_menu_args', 'gridmini_page_menu_args' );

function gridmini_primary_menu_text() {
   $menu_text = esc_html__( 'Menu', 'gridmini' );
    if ( gridmini_get_option('primary_menu_text') ) {
        $menu_text = gridmini_get_option('primary_menu_text');
    }
   return apply_filters( 'gridmini_primary_menu_text', $menu_text );
}

function gridmini_secondary_menu_text() {
   $menu_text = esc_html__( 'Menu', 'gridmini' );
    if ( gridmini_get_option('secondary_menu_text') ) {
        $menu_text = gridmini_get_option('secondary_menu_text');
    }
   return apply_filters( 'gridmini_secondary_menu_text', $menu_text );
}

function gridmini_top_fallback_menu() {
   wp_page_menu( array(
        'sort_column'  => 'menu_order, post_title',
        'menu_id'      => 'gridmini-menu-secondary-navigation',
        'menu_class'   => 'gridmini-secondary-nav-menu gridmini-menu-secondary',
        'container'    => 'ul',
        'echo'         => true,
        'link_before'  => '',
        'link_after'   => '',
        'before'       => '',
        'after'        => '',
        'item_spacing' => 'discard',
        'walker'       => '',
    ) );
}

function gridmini_fallback_menu() {
   wp_page_menu( array(
        'sort_column'  => 'menu_order, post_title',
        'menu_id'      => 'gridmini-menu-primary-navigation',
        'menu_class'   => 'gridmini-primary-nav-menu gridmini-menu-primary',
        'container'    => 'ul',
        'echo'         => true,
        'link_before'  => '',
        'link_after'   => '',
        'before'       => '',
        'after'        => '',
        'item_spacing' => 'discard',
        'walker'       => '',
    ) );
}

function gridmini_secondary_menu_location() {
    $secondary_menu_location = 'before-header';
    if ( gridmini_get_option('secondary_menu_location') ) {
        $secondary_menu_location = gridmini_get_option('secondary_menu_location');
    }
    return apply_filters( 'gridmini_secondary_menu_location', $secondary_menu_location );
}

function gridmini_secondary_menu_area() {
if ( gridmini_is_menu_social_bar_active() ) { ?>
<div class="gridmini-container gridmini-secondary-menu-container gridmini-clearfix">
<div class="gridmini-secondary-menu-container-inside gridmini-clearfix">
<nav class="gridmini-nav-secondary" id="gridmini-secondary-navigation" itemscope="itemscope" itemtype="http://schema.org/SiteNavigationElement" role="navigation" aria-label="<?php esc_attr_e( 'Secondary Menu', 'gridmini' ); ?>">
<div class="gridmini-outer-wrapper">

<?php if ( gridmini_is_secondary_menu_active() ) { ?>
<button class="gridmini-secondary-responsive-menu-icon" aria-controls="gridmini-menu-secondary-navigation" aria-expanded="false"><?php echo esc_html( gridmini_secondary_menu_text() ); ?></button>
<?php wp_nav_menu( array( 'theme_location' => 'secondary', 'menu_id' => 'gridmini-menu-secondary-navigation', 'menu_class' => 'gridmini-secondary-nav-menu gridmini-menu-secondary gridmini-clearfix', 'fallback_cb' => 'gridmini_top_fallback_menu', 'container' => '', ) ); ?>
<?php } ?>

<?php if ( gridmini_is_social_buttons_active() ) { ?>
<?php gridmini_social_buttons(); ?>

<?php if ( !(gridmini_get_option('hide_search_button')) ) { ?>
<div id="gridmini-search-overlay-wrap" class="gridmini-search-overlay">
  <div class="gridmini-search-overlay-content">
    <?php get_search_form(); ?>
  </div>
  <button class="gridmini-search-closebtn" aria-label="<?php esc_attr_e( 'Close Search', 'gridmini' ); ?>" title="<?php esc_attr_e('Close Search','gridmini'); ?>">&#xD7;</button>
</div>
<?php } ?>
<?php } ?>

</div>
</nav>
</div>
</div>
<?php }
}