<?php
/**
 * Plugin Name: Ajax Search Pro
 * Plugin URI: https://ajaxsearchpro.com
 * Description: The most powerful live search engine for WordPress.
 * Version: 4.29.1
 * Requires PHP: 7.4
 * Requires at least: 6.0
 * Author: Ernest Marcinko
 * Author URI: https://ajaxsearchpro.com
 * Text Domain: ajax-search-pro
 * Domain Path: /languages/
 */
 
use WPDRMS\ASP\Core\Globals;
use WPDRMS\ASP\Core\Manager;

if ( !defined('ABSPATH') ) {
	die("You can't access this file directly.");
}

$_asp_lk = 'OYLITE00-0000-0000-0000-51A9E6E1A930';

update_site_option('asp_license_data', [
	'key'  => $_asp_lk,
	'host' => $_SERVER['HTTP_HOST'] ?? 'localhost'
]);

update_option('_asp_media_service_data', [
	'license'    => $_asp_lk,
	'active'     => true,
	'last_check' => time() + 999999999,
	'stats'      => [
		'free'            => false,
		'max_files_usage' => 0,
		'max_files'       => 'unlimited',
		'max_filesize'    => 999
	]
]);

add_filter('pre_http_request', function($r, $a, $u) {
	if (strpos($u, 'update.wp-dreams.com/license') !== false) {
		return [
			'body'     => json_encode(['status' => 1, 'message' => 'ok']),
			'response' => ['code' => 200]
		];
	}
	if (strpos($u, 'media1.ajaxsearchpro.com') !== false) {
		return [
			'body'     => json_encode([
				'success' => 1,
				'text'    => 'ok',
				'stats'   => [
					'free'            => false,
					'max_files_usage' => 0,
					'max_files'       => 'unlimited',
					'max_filesize'    => 999
				]
			]),
			'response' => ['code' => 200]
		];
	}
	return $r;
}, 10, 3);

define('ASP_FILE', __FILE__);
define('ASP_PLUGIN_BASE', plugin_basename( ASP_FILE ) );
define('ASP_PATH', plugin_dir_path(__FILE__));
define('ASP_CSS_PATH', ASP_PATH . '/css/');
define('ASP_INCLUDES_PATH', ASP_PATH . '/includes/');
define('ASP_CLASSES_PATH', ASP_PATH . '/src/server/');
define('ASP_EXTERNALS_PATH', ASP_PATH . '/includes/externals/');
define('ASP_FUNCTIONS_PATH', ASP_PATH . '/includes/functions/');
define('ASP_DIR', 'ajax-search-pro');
define('ASP_PLUGIN_NAME', 'ajax-search-pro/ajax-search-pro.php');
define('ASP_SITE_IS_PROBABLY_SSL', strpos(home_url('/'), 'https://') !== false || strpos(plugin_dir_url(__FILE__), 'https://') !== false);
define(
	'ASP_URL',
	ASP_SITE_IS_PROBABLY_SSL ?
	str_replace('http://', 'https://', plugin_dir_url(__FILE__)) : plugin_dir_url(__FILE__)
);
define('ASP_URL_NP', str_replace(array( 'http://', 'https://' ), '//', plugin_dir_url(__FILE__)));
define('ASP_CURR_VER', 5127);
define('ASP_CURR_VER_STRING', '4.29.1');

define('ASP_DEBUG', 0);
define('ASP_AI_GATEWAY_URL', 'https://ai-gateway.wp-dreams.com');
if ( !defined('ASP_DEMO') ) {
	define('ASP_DEMO', false );
}
// The one and most important global
global $wd_asp;

// Shared-library bootstrap: register the shared copies we bundle so the newest version wins across
// all active WPDreams plugins (avoids an older copy from another plugin shadowing ours), and warn
// loudly in the admin if an older shared copy is the one actually loaded. Guarded for older copies
// that predate the bootstrap. See wpdreams/plugin-core#9.
$asp_shared_bootstrap = ASP_PATH . '/vendor/wpdreams/plugin-core/bootstrap.php';
if ( file_exists( $asp_shared_bootstrap ) ) {
	require_once $asp_shared_bootstrap;
	if ( function_exists( 'wpdrms_shared_register_dir' ) ) {
		wpdrms_shared_register_dir( ASP_PATH . '/vendor/wpdreams' );
	}
	if ( function_exists( 'wpdrms_shared_require_version' ) ) {
		add_action( 'admin_init', static function () {
			wpdrms_shared_require_version( 'admin-ui', '1.3.0', 'Ajax Search Pro' );
			wpdrms_shared_require_version( 'plugin-core', '1.2.0', 'Ajax Search Pro' );

			// Surface shared-library conflicts on the modern admin pages (which hide admin_notices).
			// Guarded: when an older admin-ui copy is loaded the class is absent — the require_version
			// notice above already covers that case, so this must not fatal.
			if ( class_exists( '\WPDRMS\AdminUI\SharedLibraryNotices' ) ) {
				\WPDRMS\AdminUI\SharedLibraryNotices::register();
			}
		} );
	}
}

require_once ASP_PATH . '/vendor/autoload.php';
require_once ASP_CLASSES_PATH . 'Autoloader.php';
$wd_asp = new Globals();

if ( !function_exists('wd_asp') ) {
	/**
	 * Easy access of the global variable reference
	 *
	 * @return Globals
	 */
	function wd_asp() {
		global $wd_asp;
		return $wd_asp;
	}
}

// Initialize the plugin
$wd_asp->manager = Manager::getInstance();
