<?php
/**
* Menu Functions
*
* @package GridFeel 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 gridfeel_page_menu_args( $args ) {
    $args['show_home'] = true;
    return $args;
}
add_filter( 'wp_page_menu_args', 'gridfeel_page_menu_args' );

function gridfeel_primary_menu_text() {
   $menu_text = esc_html__( 'Menu', 'gridfeel' );
    if ( gridfeel_get_option('primary_menu_text') ) {
        $menu_text = gridfeel_get_option('primary_menu_text');
    }
   return apply_filters( 'gridfeel_primary_menu_text', $menu_text );
}

function gridfeel_secondary_menu_text() {
   $menu_text = esc_html__( 'Menu', 'gridfeel' );
    if ( gridfeel_get_option('secondary_menu_text') ) {
        $menu_text = gridfeel_get_option('secondary_menu_text');
    }
   return apply_filters( 'gridfeel_secondary_menu_text', $menu_text );
}

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

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

function gridfeel_primary_menu_area() {
if ( (gridfeel_is_primary_menu_active()) || (('primary-menu' === gridfeel_social_buttons_location()) && gridfeel_is_social_buttons_active()) ) { ?>
<div class="gridfeel-container gridfeel-primary-menu-container gridfeel-clearfix">
<div class="gridfeel-primary-menu-container-inside gridfeel-clearfix">
<nav class="gridfeel-nav-primary" id="gridfeel-primary-navigation" itemscope="itemscope" itemtype="http://schema.org/SiteNavigationElement" role="navigation" aria-label="<?php esc_attr_e( 'Primary Menu', 'gridfeel' ); ?>">
<div class="gridfeel-outer-wrapper">

<?php if ( gridfeel_is_primary_menu_active() ) { ?>
<button class="gridfeel-primary-responsive-menu-icon" aria-controls="gridfeel-menu-primary-navigation" aria-expanded="false"><?php echo esc_html( gridfeel_primary_menu_text() ); ?></button>
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_id' => 'gridfeel-menu-primary-navigation', 'menu_class' => 'gridfeel-primary-nav-menu gridfeel-menu-primary gridfeel-clearfix', 'fallback_cb' => 'gridfeel_primary_fallback_menu', 'container' => '', ) ); ?>
<?php } ?>

<?php if ( 'primary-menu' === gridfeel_social_buttons_location() ) { ?>
    <?php if ( gridfeel_is_social_buttons_active() ) { ?>
        <?php gridfeel_header_social_buttons(); ?>
        <div id="gridfeel-search-overlay-wrap" class="gridfeel-search-overlay">
          <div class="gridfeel-search-overlay-content">
            <?php get_search_form(); ?>
          </div>
          <button class="gridfeel-search-closebtn" aria-label="<?php esc_attr_e( 'Close Search', 'gridfeel' ); ?>" title="<?php esc_attr_e('Close Search','gridfeel'); ?>">&#xD7;</button>
        </div>
    <?php } ?>
<?php } ?>

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

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

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

<?php if ( 'secondary-menu' === gridfeel_social_buttons_location() ) { ?>
    <?php if ( gridfeel_is_social_buttons_active() ) { ?>
        <?php gridfeel_header_social_buttons(); ?>
        <div id="gridfeel-search-overlay-wrap" class="gridfeel-search-overlay">
          <div class="gridfeel-search-overlay-content">
            <?php get_search_form(); ?>
          </div>
          <button class="gridfeel-search-closebtn" aria-label="<?php esc_attr_e( 'Close Search', 'gridfeel' ); ?>" title="<?php esc_attr_e('Close Search','gridfeel'); ?>">&#xD7;</button>
        </div>
    <?php } ?>
<?php } ?>

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