
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

define( 'INSTANT_SEARCH_PLUGIN_DIR', get_template_directory().'/inc/instant-search' );
defined( 'INSTANT_SEARCH_PLUGIN_URL' ) || define( 'INSTANT_SEARCH_PLUGIN_URL', get_template_directory_uri().'/inc/instant-search' );

/**
 * Do search
 */
add_action( 'wp_ajax_presso_instant_search', 'presso_instant_search' );
add_action( 'wp_ajax_nopriv_presso_instant_search', 'presso_instant_search' );
if ( ! function_exists( 'presso_instant_search' ) ) {
	function presso_instant_search() {
		global $presso_secondary_query;

		if ( isset( $_GET[ 's' ] ) ){
			$q = htmlspecialchars( $_GET[ 's' ] );
			$q = sanitize_text_field( $q );
			$q = esc_sql( $q );
		} else {
			die();
		}

		$query_args = array(
			's' => $q,
			'posts_per_page' => 6,
			// 'post_status' => 'publish',
			// 'order' => 'DESC',
			// 'orderby' => 'post_date',
			// 'update_post_term_cache' => false,
			// 'update_post_meta_cache' => false,
		);

		$presso_secondary_query = new WP_Query( apply_filters( 'presso_filter_instant_search_query_args', $query_args ) );

		// Check if any posts were found.
		if ( ! $presso_secondary_query->have_posts() ) : ?>

			<li class="vw-instant-search__no-result">
				<span class="vw-instant-search__result-title"><?php esc_html_e( 'No result was found.', 'presso' ); ?></span>
			</li>

		<?php else:

			while ( $presso_secondary_query->have_posts() ) { $presso_secondary_query->the_post();
				get_template_part( 'templates/instant-search/instant-search-result' );
			} ?>

			<li class="vw-instant-search__all-result">
				<a href="<?php echo esc_url( get_search_link( $q ) ); ?>" class="vw-button vw-button--small vw-button--full-width"><span class="vw-instant-search__result-title"><?php esc_html_e( 'View all results', 'presso' ); ?></span></a>
			</li>

		<?php endif;

		wp_reset_postdata();
		die();
	}
}

/**
 * Register scripts
 */
add_action( 'wp_enqueue_scripts', 'presso_instant_search_localize' );
if ( ! function_exists( 'presso_instant_search_localize' ) ) {
	function presso_instant_search_localize(){
		wp_enqueue_script( 'instant-search', INSTANT_SEARCH_PLUGIN_URL.'/instant-search.js', array( 'jquery' ), PRESSO_THEME_VERSION, true );
		wp_localize_script( 'instant-search', 'instant_search', array(
			'blog_url' => home_url('/'),
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'placeholder' => esc_html__( 'Search', 'presso' ),
		));
	}
}




/*//////////////////////////////////////
// The Instant Search Icon
//////////////////////////////////////*/

if ( ! function_exists( 'presso_the_instant_search_button' ) ) {
	function presso_the_instant_search_button() {
		?>
			<div class="vw-instant-search">
				<a class="vw-instant-search__button" href="<?php echo esc_url( get_search_link() ); ?>"><i class="vw-icon icon-entypo-search"></i></a>
			</div>
		<?php
	}
}

// add_filter( 'presso_filter_add_mobile_menu_buttons', 'presso_instant_search_add_icon_to_mobile_nav' );
if ( ! function_exists( 'presso_instant_search_add_icon_to_mobile_nav' ) ) {
	function presso_instant_search_add_icon_to_mobile_nav( $content ) {
		ob_start();
		?>
		<span class="vw-search-buton main-menu-item">
			<a class="vw-search-buton" href="<?php echo esc_url( get_search_link() ); ?>"><i class="vw-icon icon-iconic-search"></i></a>
		</span>
		<?php

		return $content.ob_get_clean();
	}
}