<?php

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

/**
 * Check whether ajax search is enabled
 */





function shrk_ajax_search_enabled() {
	if ( class_exists( 'SearchWP_Live_Search' ) ) {
		return apply_filters( 'shrk_ajax_search_enabled', true );
	}

	return false;
}


/**
 * Add theme options & modules
 * scripts & styles, if plugin is active
 */
if ( class_exists( 'SearchWP_Live_Search' ) ) {
	add_filter( 'redux/options/shrk_theme_options/sections', 'shrk_searchwp_live_search_add_options' );
	add_action( 'wp_enqueue_scripts', 'shrk_searchwp_live_search_theme_css', 12 );
}


/**
 * Dequeue plugin style and enqueue module script
 * @return none
 */
function shrk_searchwp_live_search_theme_css() {
	global $shrk_theme_info;
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	wp_dequeue_style( 'searchwp-live-search' );
	wp_enqueue_script( 'shrk-searchwp-live-search',  SHRK_THEME_MODULES_URI . 'searchwp-live-ajax-search/js/searchwp-live-frontend' . $suffix . '.js', array( 'jquery', 'shrk-theme-js' ), $shrk_theme_info['Version'], true );
}


/**
 * Disable plugin's base styles
 */
add_filter( 'searchwp_live_search_base_styles', '__return_false' );


/**
 * Modify plugins template directory
 * @param  string $dir 
 * @return string
 */
function shrk_searchwp_live_search_template_dir( $dir ) {
	return str_replace( trailingslashit( get_template_directory() ), '', SHRK_THEME_MODULES ) . '/searchwp-live-ajax-search/templates';
}
add_filter( 'searchwp_live_search_template_dir', 'shrk_searchwp_live_search_template_dir' );


/**
 * Set searchwp default configuration
 * @param  array $configs Search WP Configurations to filter
 * @return array          Filtered configurations
 */
function shrk_searchwp_live_search_configs( $configs ) {
	// override some defaults
	$configs['default'] = array(
		'engine' => 'default',
		'input' => array(
			'delay'     => 200,
			'min_chars' => 3,
		),
		'results' => array(
			'position'  => 'bottom',
			'width'     => 'css',
			'offset'    => array(
				'x' => 0,
				'y' => 0
			),
		),
		'spinner' => false,
	);
	
	return $configs;
}
add_filter( 'searchwp_live_search_configs', 'shrk_searchwp_live_search_configs' );


/**
 * Set number of returned results
 * @param  integer number of posts per page
 * @return integer number of posts per page
 */
function shrk_searchwp_live_search_posts_per_page( $ppp ) {
	$ppp = shrk_get_theme_option( 'ajax_search_posts_per_page', $ppp );
	return $ppp;
}
add_filter( 'searchwp_live_search_posts_per_page', 'shrk_searchwp_live_search_posts_per_page' );


/**
 * Modify query args based on theme options
 * @param  array $args Query args
 * @return array       Query args
 */
function shrk_searchwp_live_search_query_args( $args ) {
	if ( shrk_get_theme_option( 'ajax_search_globally' ) == false ) {
		$args['post_type'] = 'product';
	}
	return $args;
}
add_filter( 'searchwp_live_search_query_args', 'shrk_searchwp_live_search_query_args' );


/**
 * Add shrk_searchwp_live_search options
 * @param  array $sections - redux sections
 * @return array modified sections
 */
function shrk_searchwp_live_search_add_options( $sections ) {
	$sections[] = array(
		'title' => esc_html__( 'Ajax search', 'ux-shop' ),
		'icon' => 'el-icon-search',
		'fields' => array(
			array(
				'id'      => 'ajax_search_globally',
				'on'      => esc_html__( 'Yes', 'ux-shop' ),
				'off'     => esc_html__( 'No', 'ux-shop' ),
				'type'    => 'switch',
				'title'   => esc_html__( 'Search globally', 'ux-shop' ),
				'desc'    => esc_html__( 'If set to yes, the search is performed on all public post types. Otherwise only products are searched', 'ux-shop' ),
				'default' => 0,
				),
			array(
				'id'      => 'ajax_search_posts_per_page',
				'min'     => '1',
				'step'    => '1',
				'max'     => '20',
				'type'    => 'slider',
				'title'   => esc_html__( 'Returned results', 'ux-shop'),
				'desc'    => esc_html__( 'Set the (max) number of returned results.', 'ux-shop'),
				'default' => '5',
			),
		),
	);
	return $sections;
}



?>