
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php
/* -----------------------------------------------------------------------------
 * Menu Functions
 * -------------------------------------------------------------------------- */

/**
 * Show Posts
 */
add_action( 'presso_action_mega_menu_render__latest', 'presso_mega_menu_render__posts', 10, 4 );
add_action( 'presso_action_mega_menu_render__featured', 'presso_mega_menu_render__posts', 10, 4 );
add_action( 'presso_action_mega_menu_render__latest_gallery', 'presso_mega_menu_render__posts', 10, 4 );
add_action( 'presso_action_mega_menu_render__latest_video', 'presso_mega_menu_render__posts', 10, 4 );
add_action( 'presso_action_mega_menu_render__latest_audio', 'presso_mega_menu_render__posts', 10, 4 );
add_action( 'presso_action_mega_menu_render__latest_reviews', 'presso_mega_menu_render__posts', 10, 4 );
if ( ! function_exists( 'presso_mega_menu_render__posts' ) ) {
	function presso_mega_menu_render__posts( $item, $depth, $args, $current_object_id ) {
		global $presso_secondary_query;

		$sub_template = '4';
		$query_args = array(
			'paged' => 1,
			'post_order' => 'latest',
			'posts_per_page' => 4,
		);

		if ( ! empty( $item->classes ) && in_array( 'menu-item-has-children', $item->classes ) ) {
			$query_args[ 'posts_per_page' ] = 3;
			$sub_template = '3';
		}

		if ( $item->type == 'taxonomy' ) {
			$query_args[ 'tax_query' ] = array(
				array(
					'taxonomy' => $item->object,
					'field'    => 'term_id',
					'terms'    => $item->object_id,
				)
			);
		
		} else {
			return; // No mega menu
		}

		if ( in_array( $item->vw_menu_type, array( 'latest', 'featured', 'latest_gallery', 'latest_video', 'latest_audio', 'latest_reviews' ) ) ) {
			$query_args[ 'post_order' ] = $item->vw_menu_type;
		}

		// Build query
		$query_args = presso_build_query( $query_args );
		$presso_secondary_query = new WP_Query( apply_filters( 'presso_filter_mega_menu_'.$item->vw_menu_type.'_query', $query_args, $item, $depth, $args, $current_object_id ) );
		get_template_part( 'templates/menu/mega-menu-post', $sub_template );
		wp_reset_postdata();
	}
}

add_action( 'presso_action_mega_menu_render__latest_products', 'presso_mega_menu_render__products', 10, 4 );
if ( ! function_exists( 'presso_mega_menu_render__products' ) ) {
	function presso_mega_menu_render__products( $item, $depth, $args, $current_object_id ) {
		global $presso_secondary_query;

		if ( ! presso_has_woocommerce() ) return;

		$query_args = array(
			'paged' => 1,
			'post_type' => 'product',
			'post_order' => 'latest',
			'posts_per_page' => 4,
		);

		if ( ! empty( $item->classes ) && in_array( 'menu-item-has-children', $item->classes ) ) {
			$query_args[ 'posts_per_page' ] = 3;
		}

		if ( $item->object == 'product_cat' ) {
			$query_args['tax_query'] = array(
				array(
					'taxonomy' => 'product_cat',
					'field'    => 'term_id',
					'terms'    => $item->object_id,
				),
			);
		}

		// Build query
		$query_args = presso_build_query( $query_args );
		$presso_secondary_query = new WP_Query( apply_filters( 'presso_filter_mega_menu_latest_products_query', $query_args, $item, $depth, $args, $current_object_id ) );

		get_template_part( 'templates/menu/mega-menu-product' );

		wp_reset_postdata();
	}
}