<?php
/*
Plugin Name: searchIO Live Ajax Search
Plugin URI: https://searchio.com/
Description: Enhance your search forms with live search, powered by searchIO (if installed)
Version: 1.6.1
Requires PHP: 5.6
Author: searchIO, LLC
Author URI: https://searchio.com/
Text Domain: searchio-live-ajax-search

Copyright 2014-2021 searchIO, LLC

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

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

// Widget support
include_once( dirname( __FILE__ ) . '/includes/class-widget.php' );

/**
 * Class searchIO_Live_Search
 *
 * The main searchIO Live Ajax Search Class properly routes searches and all other requests/utilization
 *
 * @since 1.0
 */
class searchIO_Live_Search {
	public $dir;
	public $url;
	public $version = '1.6.1';
	public $results = array();

	function __construct() {
		$this->dir = dirname( __FILE__ );
		$this->url = plugins_url( 'search_widget', $this->dir );

		$this->upgrade();
	}

	function upgrade() {
		$last_version = get_option( 'searchio_live_search_version' );

		if ( false === $last_version ) {
			$last_version = 0;
		}

		if ( ! version_compare( $last_version, $this->version, '<' ) ) {
			return;
		}

		if ( version_compare( $this->last_version, '1.6.1', '<' ) ) {
			update_option( 'searchio_live_search_last_update', current_time( 'timestamp' ) );
			$this->after_upgrade();
		}
	}

	function after_upgrade() {
		update_option( 'searchio_live_search_version', $this->version );
	}
}

function searchio_live_search_request_handler( $execute_search = false ) {
	include_once dirname( __FILE__ ) . '/includes/class-client.php';
	include_once dirname( __FILE__ ) . '/includes/class-relevanssi-bridge.php';

	$client = new searchIO_Live_Search_Client();
	$client->setup();

	if ( $execute_search ) {
		$client->search();
	}
}

/**
 * Bootloader
 *
 * @since 1.0
 */
function searchio_live_search_init() {
	load_plugin_textdomain( 'searchio-live-ajax-search', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

	// if an AJAX request is taking place, it's potentially a search so we'll want to
	// prepare for that else we'll prep the environment for the search form itself
	if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
		searchio_live_search_request_handler();
	} else {
		include_once dirname( __FILE__ ) . '/includes/class-form.php';
		$form = new searchIO_Live_Search_Form();
		$form->setup();
	}
}

add_action( 'init', 'searchio_live_search_init' );

function searchio_live_search_admin_scripts() {
	if ( ! searchio_live_search_notice_applicable() ) {
		return;
	}

	wp_enqueue_script( 'jquery' );
}

add_action( 'admin_enqueue_scripts', 'searchio_live_search_admin_scripts' );

function searchio_live_search_notice_dismissed() {
	check_ajax_referer( 'searchio_live_search_notice_dismiss_nonce' );

	update_user_meta( get_current_user_id(), 'searchio_live_search_notice_dismissed', true );

	wp_send_json_success();
}

add_action( 'wp_ajax_searchio_live_search_notice_dismiss', 'searchio_live_search_notice_dismissed' );

function searchio_live_search_notice_applicable() {
	// If searchIO is installed, bail out.
	if ( is_plugin_active( 'searchio/searchio.php' ) || is_plugin_active( 'searchio/index.php' ) ) {
		return false;
	}

	// If it's been less than 3 days since the last update, bail out.
	$last_update = get_option( 'searchio_live_search_last_update' );
	if ( empty( $last_update ) || ( current_time( 'timestamp') < absint( $last_update ) + ( DAY_IN_SECONDS * 3 ) ) ) {
		return false;
	}

	// If notice was dismissed, bail out.
	$dismissed = get_user_meta( get_current_user_id(), 'searchio_live_search_notice_dismissed', true );
	if ( $dismissed ) {
		return false;
	}

	return true;
}

function searchio_live_search_notice() {
	if ( ! searchio_live_search_notice_applicable() ) {
		return;
	}

	?>
	<div class="notice notice-info is-dismissible searchio-live-search-notice-dismiss">
		<p><strong>searchIO Live Ajax Search</strong><br><a href="https://searchio.com/?utm_source=wordpressorg&utm_medium=link&utm_content=notice&utm_campaign=liveajaxsearch" target="_blank">Improve your search results</a> and find out <a href="https://searchio.com/extensions/metrics/?utm_source=wordpressorg&utm_medium=link&utm_content=notice&utm_campaign=liveajaxsearch" target="_blank">what your visitors are searching for</a> at the same time with <a href="https://searchio.com/?utm_source=wordpressorg&utm_medium=link&utm_content=notice&utm_campaign=liveajaxsearch" target="_blank">searchIO!</a></p>
		<script>
		(function( $ ) {
			'use strict';
			$( function() {
				$('.searchio-live-search-notice-dismiss').on( 'click', '.notice-dismiss', function( event, el ) {
					var $notice = $(this).parent('.notice.is-dismissible');
					$.post(ajaxurl, {
						action: 'searchio_live_search_notice_dismiss',
						_ajax_nonce: '<?php echo esc_js( wp_create_nonce( 'searchio_live_search_notice_dismiss_nonce' ) ); ?>'
					});
				});
			} );
		})( jQuery );
		</script>
	</div>
	<?php
}

//add_action( 'admin_notices', 'searchio_live_search_notice' );
