init_components(); $this->init_hooks(); } private function init_components() { $this->encryption_manager = AIVectorSearch_Encryption_Manager::instance(); $this->connection_manager = AIVectorSearch_Connection_Manager::instance(); $this->supabase_client = AIVectorSearch_Supabase_Client::instance(); $this->api_client = AIVectorSearch_API_Client::instance(); $this->openai_client = AIVectorSearch_OpenAI_Client::instance(); $this->analytics = AIVectorSearch_Analytics::instance(); $this->product_sync = AIVectorSearch_Product_Sync::instance(); $this->search_handler = AIVectorSearch_Search_Handler::instance(); $this->recommendations = AIVectorSearch_Recommendations::instance(); $this->recommendations_integrations = AIVectorSearch_Recommendations_Integrations::instance(); $this->admin_interface = AIVectorSearch_Admin_Interface::instance(); } private function init_hooks() { add_action('plugins_loaded', [$this, 'ensure_store_id'], 11); // Plugin lifecycle hooks - must use main plugin file path, not this class file if (defined('AIVESESE_PLUGIN_PATH')) { register_activation_hook(AIVESESE_PLUGIN_PATH . 'ai-supabase-search.php', [$this, 'on_activation']); register_deactivation_hook(AIVESESE_PLUGIN_PATH . 'ai-supabase-search.php', [$this, 'on_deactivation']); } // Add admin notices for mode switching add_action('admin_notices', [$this, 'show_mode_switch_notices']); add_action('admin_notices', [$this, 'show_analytics_notices']); add_action('admin_enqueue_scripts', [$this, 'enqueue_analytics_notice_script']); } public function ensure_store_id() { $store = get_option('aivesese_store', ''); // Basic UUID v4 format check $is_uuid = preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i', (string) $store ); if (!$is_uuid) { $new = function_exists('wp_generate_uuid4') ? wp_generate_uuid4() : wp_generate_password(36, false); // fallback update_option('aivesese_store', $new, false); // autoload = false } } public function on_activation() { // Ensure store ID is generated on activation $this->ensure_store_id(); // Set default options $defaults = [ 'aivesese_connection_mode' => 'lite', // Default to lite mode for instant search 'aivesese_enable_search' => '1', 'aivesese_semantic_toggle' => '0', 'aivesese_auto_sync' => '0', 'aivesese_enable_pdp_similar' => '0', 'aivesese_enable_cart_below' => '0', 'aivesese_enable_woodmart_integration' => '0', ]; foreach ($defaults as $option => $default_value) { if (get_option($option) === false) { update_option($option, $default_value); } } $this->analytics->create_table(); // Clear any existing caches $this->clear_plugin_caches(); // Show welcome notice update_option('aivesese_show_welcome_notice', '1'); } public function on_deactivation() { // Clear plugin caches $this->clear_plugin_caches(); wp_clear_scheduled_hook('aivs_cleanup_analytics'); } private function clear_plugin_caches() { global $wpdb; // Delete all transients that start with our prefixes $prefixes = ['fts_', 'sem_', 'recs_', 'aivesese_sim_']; foreach ($prefixes as $prefix) { $escaped_prefix = $wpdb->esc_like($prefix) . '%'; $wpdb->query($wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", '_transient_' . $escaped_prefix )); $wpdb->query($wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", '_transient_timeout_' . $escaped_prefix )); } } /** * Show admin notices for mode switching and welcome */ public function show_mode_switch_notices() { if (!current_user_can('manage_options')) { return; } // Welcome notice for new installs if (get_option('aivesese_show_welcome_notice') === '1') { $this->show_welcome_notice(); } // Connection mode notices $connection_mode = get_option('aivesese_connection_mode', 'lite'); if ($connection_mode === 'api' && empty(get_option('aivesese_license_key'))) { $this->show_api_setup_notice(); } if ($connection_mode === 'self_hosted' && (!get_option('aivesese_url') || !get_option('aivesese_key'))) { $this->show_self_hosted_setup_notice(); } } /** * Show analytics-related notices */ public function show_analytics_notices() { if (!current_user_can('manage_options')) { return; } $screen = get_current_screen(); if (!$screen || !in_array($screen->id, ['dashboard', 'edit-product'], true)) { return; } // Check if analytics has interesting data to show $stats = $this->analytics->get_search_stats(7); // Last 7 days // Show analytics discovery notice if they have search activity if ($stats['total_searches'] >= 10 && !get_option('aivesese_analytics_discovered', false)) { echo '
Great news! Your store has ' . $stats['total_searches'] . ' searches this week. '; echo 'We\'ve been collecting analytics data that can help you boost sales!
'; echo ''; echo 'View Analytics Dashboard '; echo 'Maybe Later'; echo '
'; echo 'Customers searched for "' . esc_html($term) . '" ' . $count . ' times this week but found no products. '; echo 'This could be a significant sales opportunity!
'; echo ''; echo 'Add Product for "' . esc_html($term) . '" '; echo 'View All Opportunities'; echo '
'; echo 'Thank you for installing AI Vector Search. You have two ways to get started:
'; echo 'We\'re working on a hosted service that will eliminate setup complexity.
'; echo 'AI Vector Search: API mode is selected but no license key is configured. '; echo 'Configure your license key or '; echo 'get a license.
'; echo 'AI Vector Search: Self-hosted mode requires Supabase configuration. '; echo 'Complete your setup.
'; echo '