<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

// note - the default menu is handled in aweaver_page_menu() in filters.php

function aweaver_menu_primary( ) {

	$menuw = apply_filters( 'aweaver_menu_name', 'primary' );

	if ( aweaver_getopt( 'm_primary_hide' ) != 'hide' ) {


		aweaver_clear_both( 'menu-primary' );

		$class = aweaver_menu_class( 'm_primary' );


		$logo = '';

		if ( aweaver_getopt( 'm_primary_logo_left' ) ) {
			$custom_logo_url = aweaver_get_wp_custom_logo_url();
			// We have a logo. Logo is go.
			if ( $custom_logo_url ) {
				if ( aweaver_getopt( 'm_primary_logo_home_link' ) ) {
					$logo = apply_filters( 'aweaver_menu_logo', '<span class="custom-logo-on-menu"><a href="' . get_home_url() . '" alt="' . esc_attr__('Site Home', 'absolute-weaver') . '"><img src="' . $custom_logo_url . '" alt="logo"/></a></span>', $custom_logo_url );    // +since: 3.1.10: add alt=
				} else {
					$logo = apply_filters( 'aweaver_menu_logo', '<span class="custom-logo-on-menu"><img src="' . $custom_logo_url . '" alt="' . esc_attr__('logo', 'absolute-weaver') . '"/></span>', $custom_logo_url );
				}    // +since: 3.1.10: add alt=

			}
		}

		$site_title = '';

		if ( aweaver_getopt( 'm_primary_site_title_left' ) ) {

			$classt = 'site-title-on-menu wvrx-menu-html wvrx-menu-left';

			// font-family
			$val = aweaver_getopt( 'site_title_font_family' );
			if ( $val && $val != 'default' ) {
				$classt .= ' font-' . $val;
			}

			$classt .= aweaver_get_bold_italic( 'site_title', 'bold' );
			$classt .= aweaver_get_bold_italic( 'site_title', 'italic' );

			$site_title = '<span class="' . $classt . '"><a href="' . get_home_url() . '" alt="' . esc_attr__('Site Home', 'absolute-weaver') . '">' . get_bloginfo( 'name' ) . '</a></span>';
		}

		$left = aweaver_getopt( 'm_primary_html_left' );
		$right = aweaver_getopt( 'm_primary_html_right' );


		if ( $left ) {
			$hide = ' ' . aweaver_getopt( 'm_primary_hide_left' );
			$left = '<span class="wvrx-menu-html wvrx-menu-left' . $hide . '">' . do_shortcode( $left ) . '</span>';
			$left = str_replace( '%', '%%', $left );    // wp_nav_menu uses sprintf! This will almost always fix the issue.
		} elseif ( is_customize_preview() ) {
			$hide = ' ' . aweaver_getopt( 'm_primary_hide_left' );
			$left = '<span class="wvrx-menu-html wvrx-menu-left' . $hide . '"></span>';
		}

		if ( $right ) {
			$hide = ' ' . aweaver_getopt( 'm_primary_hide_right' );
			$right = '<span class="wvrx-menu-html wvrx-menu-right ' . $hide . '">' . do_shortcode( $right ) . '</span>';
			$right = str_replace( '%', '%%', $right );    // wp_nav_menu uses sprintf!
		} elseif ( is_customize_preview() ) {
			$hide = ' ' . aweaver_getopt( 'm_primary_hide_right' );
			$right = '<span class="wvrx-menu-html wvrx-menu-right ' . $hide . '"></span>';
		}

		if ( aweaver_getopt_checked( 'm_primary_search' ) ) {
			$right = '<span class="menu-search">&nbsp;' . get_search_form( false ) . '&nbsp;</span>' . $right;
		}

		$left = $logo . $site_title . $left;

		if ( aweaver_getopt( 'use_smartmenus' ) ) {                            // ==================  SMART MENUS ( make any changes in default menu version, too in filters.php )
			$hamburger = apply_filters( 'aweaver_mobile_menu_name', aweaver_getopt( 'm_primary_hamburger' ) );


			if ( $hamburger == '' ) {
				$alt = aweaver_getopt( 'mobile_alt_label' );
				if ( $alt == '' ) {
					$hamburger = '<span class="genericon genericon-menu"></span>';
				} else {
					$hamburger = '<span class="menu-toggle-menu">' . $alt . '</span>';
				}
			}
			$left = '<span class="wvrx-menu-button">' . "{$hamburger}</span>{$left}";            // +since: 3.1.10: remove empty href=""
		}

		$menu_class = apply_filters( 'aweaver_menu_class', 'weaverx-theme-menu wvrx-menu menu-hover', 'primary' );

		$align = aweaver_getopt( 'm_primary_align' );

		switch ( $align ) {        // add classes for alignment and fixed top
			case 'left':
			case 'alignwide left':
			case 'alignfull left':
				$menu_class .= ' menu-alignleft';
				break;
			case 'center':
			case 'alignwide center':
			case 'alignfull center':
				$menu_class .= ' wvrx-center-menu';
				break;
			case 'right':
			case 'alignwide right':
			case 'alignfull right':
				$menu_class .= ' menu-alignright';
				break;
			default:
				$menu_class .= ' menu-alignleft';
		}

		if ( aweaver_getopt( 'm_primary_move' ) ) {
			$nav_class = 'menu-primary menu-primary-moved menu-type-standard';
		} else {
			$nav_class = 'menu-primary menu-primary-standard menu-type-standard';
		}

		if ( aweaver_getopt( 'm_primary_fixedtop' ) == 'fixed-top' ) {    // really is a drop-down value, so need to check for === for backward compat.
			$class .= ' wvrx-fixedtop';
			$nav_class .= ' wvrx-primary-fixedtop';
		}

		echo "\n\n" . '<div id="nav-primary" class="' . $nav_class . '"';
		aweaver_schema( 'menu' );
		echo ">\n";

		$args = array(
			'fallback_cb'     => 'aweaver_page_menu',
			'theme_location'  => $menuw,
			'menu_class'      => $menu_class,
			'container'       => 'div',
			'container_class' => 'wvrx-menu-container ' . $class,
			'items_wrap'      => $left . $right .
			                     '<div class="wvrx-menu-clear"></div><ul id="%1$s" class="%2$s">%3$s</ul><div style="clear:both;"></div>',
		);


		$locations = get_nav_menu_locations();                            // note - the default menu is handled in aweaver_page_menu() in filters.php
		if ( isset( $locations[ $menuw ] ) ) {
			$the_menu = wp_get_nav_menu_object( $locations[ $menuw ] );
			if ( ! empty( $the_menu ) ) {
				$args['fallback_cb'] = '';
				$args['walker'] = new aweaver_Walker_Nav_Menu();
			}
		}


		wp_nav_menu( $args );

		echo "</div><div class='clear-menu-primary-end' style='clear:both;'></div><!-- /.menu-primary -->\n\n";

		if ( aweaver_getopt( 'use_smartmenus' ) ) {
			if ( function_exists( 'aweaverplus_plugin_installed' ) ) {
				do_action( 'aweaver_plus_smartmenu', 'nav-primary', 'm_primary' );
			}    // emit required JS to invoke smartmenu
			else {        // use theme "action"
				aweaver_smartmenu( 'nav-primary', 'm_primary' );
			}
		}
	}
}

aweaver_menu_primary();

