connection_manager = AIVectorSearch_Connection_Manager::instance(); $this->analytics = AIVectorSearch_Analytics::instance(); $this->init_hooks(); } private function init_hooks() { if (!$this->is_search_enabled()) { return; } add_action('wp_ajax_aivesese_autocomplete', [$this, 'handle_search_autocomplete']); add_action('wp_ajax_nopriv_aivesese_autocomplete', [$this, 'handle_search_autocomplete']); add_action('wp_enqueue_scripts', [$this, 'enqueue_search_nonce']); add_action('wp_footer', [$this, 'render_search_templates']); // Regular WordPress search interception add_action('pre_get_posts', [$this, 'intercept_product_search'], 9999); // Woodmart AJAX search integration $this->init_woodmart_integration(); // Track clicks on search results add_action('template_redirect', [$this, 'maybe_track_click']); } /** * Initialize Woodmart AJAX search integration */ private function init_woodmart_integration() { // Only initialize if Woodmart integration is enabled if (get_option('aivesese_enable_woodmart_integration', '0') !== '1') { return; } // Register AJAX handlers for both our custom endpoint and original Woodmart endpoint add_action('wp_ajax_aivs_woodmart_search', [$this, 'handle_woodmart_ajax_search']); add_action('wp_ajax_nopriv_aivs_woodmart_search', [$this, 'handle_woodmart_ajax_search']); // Keep support for original Woodmart endpoint for backward compatibility add_action('wp_ajax_woodmart_ajax_search', [$this, 'handle_woodmart_ajax_search']); add_action('wp_ajax_nopriv_woodmart_ajax_search', [$this, 'handle_woodmart_ajax_search']); // Remove original Woodmart handlers add_action('wp_loaded', [$this, 'override_woodmart_ajax'], 99); // Fallback method using pre_get_posts add_action('pre_get_posts', [$this, 'intercept_woodmart_query'], 1); } /** * Override Woodmart's original AJAX search handlers */ public function override_woodmart_ajax() { // Remove original Woodmart handlers if they exist remove_action('wp_ajax_woodmart_ajax_search', 'woodmart_ajax_search'); remove_action('wp_ajax_nopriv_woodmart_ajax_search', 'woodmart_ajax_search'); } /** * Enqueue search nonce for frontend */ public function enqueue_search_nonce() { wp_localize_script('jquery', 'aivs_search_data', [ 'nonce' => wp_create_nonce('aivesese_search_nonce'), 'ajax_url' => admin_url('admin-ajax.php') ]); $autocomplete_enabled = $this->is_autocomplete_enabled(); $woodmart_enabled = get_option('aivesese_enable_woodmart_integration', '0') === '1'; if (!$autocomplete_enabled && !$woodmart_enabled) { return; } wp_enqueue_style( 'aivesese-search-autocomplete', AIVESESE_PLUGIN_URL . 'assets/css/search-autocomplete.css', [], AIVESESE_PLUGIN_VERSION ); wp_enqueue_script( 'aivesese-search-autocomplete', AIVESESE_PLUGIN_URL . 'assets/js/search-autocomplete.js', ['jquery'], AIVESESE_PLUGIN_VERSION, true ); wp_localize_script('aivesese-search-autocomplete', 'aivesese_search', [ 'ajax_url' => admin_url('admin-ajax.php'), 'search_nonce' => wp_create_nonce('aivesese_search_nonce'), 'tracking_nonce' => wp_create_nonce('aivs_tracking_nonce'), 'enabled' => '1', 'autocomplete_enabled' => $autocomplete_enabled ? '1' : '0', ]); if (!$woodmart_enabled) { return; } wp_enqueue_script( 'aivesese-woodmart-integration', AIVESESE_PLUGIN_URL . 'assets/js/woodmart-integration.js', ['jquery', 'aivesese-search-autocomplete'], AIVESESE_PLUGIN_VERSION, true ); wp_localize_script('aivesese-woodmart-integration', 'aivesese_woodmart', [ 'ajax_url' => admin_url('admin-ajax.php'), 'search_nonce' => wp_create_nonce('aivesese_search_nonce'), 'tracking_nonce' => wp_create_nonce('aivs_tracking_nonce'), 'enabled' => '1', 'autocomplete_enabled' => $autocomplete_enabled ? '1' : '0', ]); } /** * Output the HTML