load_dependencies(); $this->init_components(); $this->register_hooks(); } /** * Load required files */ private function load_dependencies() { require_once plugin_dir_path( __FILE__ ) . 'class-settings-pages.php'; require_once plugin_dir_path( __FILE__ ) . 'class-setup-wizard.php'; require_once plugin_dir_path( __FILE__ ) . 'class-post-meta-box.php'; require_once plugin_dir_path( __FILE__ ) . 'class-deactivation-feedback.php'; } /** * Initialize components */ private function init_components() { $this->settings_pages = new AI_Search_Settings_Pages(); $this->setup_wizard = new AI_Search_Setup_Wizard(); $this->post_meta_box = new AI_Search_Post_Meta_Box(); new AI_Search_Deactivation_Feedback(); } /** * Register admin hooks */ private function register_hooks() { add_action( 'admin_menu', [ $this, 'register_settings_menu' ] ); // v1.49.0: insights at a glance for everyone with the dashboard. add_action( 'wp_dashboard_setup', [ $this, 'register_dashboard_widget' ] ); // v1.47.0: entering any AI Search admin screen invalidates the plan / // quota transients, so a plan change made elsewhere (claim key, Stripe // webhook, support toggle) is reflected on the very next pageview // without the merchant having to press "Refresh quota". The next // $qm->get_quota_data() call in the render fetches fresh. add_action( 'current_screen', [ $this, 'maybe_refresh_plan_cache' ] ); add_action( 'admin_notices', [ $this->setup_wizard, 'show_setup_notice' ] ); add_action( 'admin_notices', [ $this->setup_wizard, 'show_setup_success_notice' ] ); add_action( 'admin_notices', [ $this, 'show_domain_mismatch_notice' ] ); add_action( 'admin_notices', [ $this, 'show_woocommerce_welcome_notice' ] ); add_action( 'admin_notices', [ $this, 'show_quota_warning_banner' ] ); add_action( 'admin_notices', [ $this, 'show_pro_upgrade_banner' ] ); add_action( 'admin_post_ai_search_snooze_quota_banner', [ $this, 'snooze_quota_banner' ] ); add_action( 'admin_post_ai_search_dismiss_quota_banner_forever', [ $this, 'dismiss_quota_banner_forever' ] ); add_action( 'admin_post_ai_search_dismiss_pro_banner_forever', [ $this, 'dismiss_pro_banner_forever' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] ); add_action( 'admin_enqueue_scripts', [ $this->setup_wizard, 'enqueue_scripts' ] ); add_action( 'admin_head', [ $this, 'add_menu_icon_styles' ] ); add_filter( 'parent_file', [ $this, 'fix_admin_parent_file' ] ); add_filter( 'submenu_file', [ $this, 'fix_admin_submenu_file' ] ); // Admin post actions // v1.42.0: the `admin_post_ai_search_generate_embeddings` form action // is gone — Indexing tab drives wp_ajax_ai_search_generate_batch now. add_action( 'admin_post_ai_search_generate_term_embeddings', [ $this->settings_pages, 'generate_term_embeddings_bulk' ] ); add_action( 'admin_post_ai_search_clear_cache', [ $this->settings_pages, 'clear_embeddings_cache' ] ); add_action( 'admin_post_ai_search_clear_cache_filtered', [ $this->settings_pages, 'clear_embeddings_cache_filtered' ] ); add_action( 'admin_post_ai_search_validate_token', [ $this->settings_pages, 'validate_service_token' ] ); add_action( 'admin_post_ai_search_recover_token', [ $this->settings_pages, 'recover_service_token' ] ); add_action( 'admin_post_ai_search_save_custom_fields', [ $this->settings_pages, 'save_custom_fields_settings' ] ); add_action( 'admin_post_ai_search_save_woocommerce_fields', [ $this->settings_pages, 'save_woocommerce_fields_settings' ] ); add_action( 'admin_post_ai_search_refresh_quota', [ $this->settings_pages, 'refresh_quota_data' ] ); add_action( 'admin_post_ai_search_claim_subscription', [ $this->settings_pages, 'handle_claim_subscription' ] ); add_action( 'admin_post_ai_search_release_slot', [ $this->settings_pages, 'handle_release_slot' ] ); add_action( 'admin_post_ai_search_dismiss_reindex_notice', [ $this->settings_pages, 'handle_dismiss_reindex_notice' ] ); // v1.49.0: native Upgrade page CTA → server-to-server Stripe Session. add_action( 'admin_post_ai_search_start_trial', [ $this->settings_pages, 'handle_start_trial' ] ); // v1.44.1 security: the legacy `admin_post_ai_search_complete_setup` // handler was CSRF-free and accepted `api_key` from POST — a // vulnerability that let a forged cross-site POST swap the site's // OpenAI key for an attacker's. The handler had no live UI caller // since v1.40.0 (step-final.php was removed). Registration dropped. add_action( 'admin_post_ai_search_reconnect_service', [ $this, 'reconnect_service' ] ); // AJAX actions add_action( 'wp_ajax_ai_search_validate_token', [ $this->settings_pages, 'validate_service_token' ] ); add_action( 'wp_ajax_ai_search_get_taxonomies', [ $this, 'ajax_get_taxonomies' ] ); add_action( 'wp_ajax_ai_search_get_terms', [ $this, 'ajax_get_terms' ] ); add_action( 'wp_ajax_ai_search_dismiss_woo_welcome', [ $this, 'ajax_dismiss_woo_welcome' ] ); add_action( 'admin_post_ai_search_dismiss_blocks_notice', [ $this, 'dismiss_blocks_notice' ] ); add_action( 'admin_post_ai_search_snooze_pro_banner', [ $this, 'snooze_pro_banner' ] ); add_action( 'wp_ajax_ai_search_wizard_generate_batch', [ $this->setup_wizard, 'ajax_generate_batch' ] ); // v1.42.0: generic-named alias for the Indexing tab and any future // surface. Same handler, same nonce, so both callers stay in sync. add_action( 'wp_ajax_ai_search_generate_batch', [ $this->setup_wizard, 'ajax_generate_batch' ] ); add_action( 'wp_ajax_ai_search_fast_backfill_batch', [ 'AI_Search_Fast_Search', 'ajax_backfill_batch' ] ); // v1.45.0 — vector format migration (v1 JSON → v2 packed binary). add_action( 'wp_ajax_ai_search_vector_migration_start', [ 'AI_Search_Vector_Migration', 'ajax_start' ] ); add_action( 'wp_ajax_ai_search_vector_migration_batch', [ 'AI_Search_Vector_Migration', 'ajax_batch' ] ); add_action( 'wp_ajax_ai_search_vector_migration_finalize', [ 'AI_Search_Vector_Migration', 'ajax_finalize' ] ); add_action( 'wp_ajax_ai_search_vector_migration_dismiss', [ 'AI_Search_Vector_Migration', 'ajax_dismiss' ] ); add_action( 'admin_notices', [ 'AI_Search_Vector_Migration', 'maybe_render_done_notice' ] ); add_action( 'wp_ajax_ai_search_wizard_prepare_sync', [ $this->setup_wizard, 'ajax_prepare_sync' ] ); add_action( 'wp_ajax_ai_search_dismiss_wizard', [ $this->setup_wizard, 'ajax_dismiss_wizard' ] ); add_action( 'wp_ajax_ai_search_dismiss_setup_success', [ $this->setup_wizard, 'ajax_dismiss_setup_success' ] ); add_action( 'wp_ajax_ai_search_test_query', [ $this->settings_pages, 'ajax_test_query' ] ); } /** * AJAX handler to get taxonomies for a post type. */ public function ajax_get_taxonomies() { check_ajax_referer( 'ai_search_ajax' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Unauthorized' ); } $post_type = isset( $_POST['post_type'] ) ? sanitize_text_field( $_POST['post_type'] ) : 'post'; $taxonomies = get_object_taxonomies( $post_type, 'objects' ); $result = []; foreach ( $taxonomies as $taxonomy ) { if ( $taxonomy->public && $taxonomy->show_ui ) { $result[ $taxonomy->name ] = [ 'label' => $taxonomy->label, 'name' => $taxonomy->name, ]; } } wp_send_json_success( $result ); } /** * AJAX handler to get terms for a taxonomy. */ public function ajax_get_terms() { check_ajax_referer( 'ai_search_ajax' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( 'Unauthorized' ); } $taxonomy = isset( $_POST['taxonomy'] ) ? sanitize_text_field( $_POST['taxonomy'] ) : ''; if ( empty( $taxonomy ) || ! taxonomy_exists( $taxonomy ) ) { wp_send_json_error( 'Invalid taxonomy' ); } $terms = get_terms( [ 'taxonomy' => $taxonomy, 'hide_empty' => false, 'number' => 100, ] ); if ( is_wp_error( $terms ) ) { wp_send_json_error( $terms->get_error_message() ); } $result = []; foreach ( $terms as $term ) { $result[] = [ 'id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'count' => $term->count, ]; } wp_send_json_success( $result ); } /** * Enqueue admin CSS and JS */ public function enqueue_admin_assets( $hook ) { // v1.49.0: the Upgrade submenu has its own stylesheet and is hooked // under `ai-search_page_ai-search-upgrade`. Load it there only. if ( 'ai-search_page_ai-search-upgrade' === $hook ) { wp_enqueue_style( 'ai-search-admin-upgrade', plugins_url( 'css/admin-upgrade.css', __FILE__ ), [], AI_SEARCH_VERSION ); return; } // Only load the main settings CSS on our top-level menu page. if ( 'toplevel_page_ai-search' !== $hook ) { return; } // Enqueue WordPress pointer for compatibility with other plugins wp_enqueue_script( 'wp-pointer' ); wp_enqueue_style( 'wp-pointer' ); wp_enqueue_style( 'ai-search-admin-settings', plugins_url( 'css/admin-settings.css', __FILE__ ), [], AI_SEARCH_VERSION ); } /** * Drop the plan / quota transients when the admin lands on any AI Search * screen. Keeps the merchant's view of their subscription in lockstep * with the service without them having to hit "Refresh quota" — the * cache was built for frontend hot-path reads, not admin. * * The 6h transient still protects frontend calls (search, suggest, Fast * Search gate) because this hook only runs inside wp-admin. * * @since 1.47.0 */ public function maybe_refresh_plan_cache() { if ( ! function_exists( 'get_current_screen' ) ) return; $screen = get_current_screen(); if ( ! $screen || empty( $screen->id ) ) return; // Match the top-level "AI Search" page and every submenu underneath // it. WordPress builds submenu screen IDs as "ai-search_page_", // and the top-level as "toplevel_page_ai-search" - both match this // substring, and no non-plugin screen uses "ai-search" as a token. if ( false === strpos( $screen->id, 'ai-search' ) ) return; delete_transient( 'ai_search_quota_data' ); delete_transient( 'ai_search_validation_data' ); } /** * Register settings menu in admin */ public function register_settings_menu() { // Main menu page add_menu_page( 'AI Search', // Page title 'AI Search', // Menu title 'manage_options', // Capability 'ai-search', // Menu slug [ $this->settings_pages, 'display_settings_page' ], // Callback AI_SEARCH_URL . 'assets/icon.svg', // Icon 58 // Position (before Settings) ); // Submenu pages add_submenu_page( 'ai-search', // Parent slug 'General Settings', // Page title 'General Settings', // Menu title 'manage_options', // Capability 'ai-search', // Menu slug (same as parent for first item) [ $this->settings_pages, 'display_settings_page' ] // Callback ); // The Setup Wizard submenu only makes sense until the user has // walked through it. After that it's noise: the same settings live // in the tabs, and a permanent "Setup Wizard" item next to the // General/Search/Indexing surface reads like the plugin still // isn't configured. The wizard URL keeps working for anyone who // navigates to it manually. if ( ! get_option( 'ai_search_setup_completed', false ) ) { add_submenu_page( 'ai-search', 'Setup Wizard', 'Setup Wizard', 'manage_options', 'ai-search&setup=wizard', [ $this->settings_pages, 'display_settings_page' ] ); } // Upgrade submenu — Free plan only, AI Service provider only. // * Paid customers already got what they were pitched, a permanent // "Upgrade" item after checkout reads as unresolved status. // * OpenAI (BYO key) users pay OpenAI directly and have no plan // concept in our service — showing them "Upgrade — 14-day trial" // would be misleading, so hide it. if ( 'ai_service' === get_option( 'ai_search_provider', 'ai_service' ) && $this->is_on_free_plan() ) { add_submenu_page( 'ai-search', __( 'Upgrade', 'ai-search' ), __( 'Upgrade: 14-day trial', 'ai-search' ), 'manage_options', 'ai-search-upgrade', [ $this, 'display_upgrade_page' ] ); } add_submenu_page( 'ai-search', 'Ask for help! Support', 'Ask for help! Support', 'manage_options', 'https://wordpress.org/support/plugin/ai-search/', '' ); } /** * Whether this site is currently on the Free plan. * * Reads from the quota transient the Quota Manager keeps (6h cache) — * NEVER makes an HTTP call from here, because admin_menu fires on * every wp-admin page load and blocking the sidebar on a 20s request * to Railway would tank the whole dashboard. If the transient is * empty, default to Free: worst case the paid customer sees an * unnecessary "Upgrade" item until the next quota refresh — mildly * annoying, not broken. * * @return bool */ private function is_on_free_plan() { $quota = get_transient( 'ai_search_quota_data' ); if ( ! is_array( $quota ) ) { return true; } $plan = isset( $quota['plan'] ) ? $quota['plan'] : null; $sub = isset( $quota['subscription'] ) ? $quota['subscription'] : null; $key = is_array( $plan ) && isset( $plan['key'] ) ? (string) $plan['key'] : 'free'; return ! ( 'free' !== $key && is_array( $sub ) ); } /** * Render the Upgrade page — a full-height iframe of the pricing page * in embed mode so the customer never leaves wp-admin. */ public function display_upgrade_page() { // v1.49.0: native Upgrade page replaces the iframe to // wp-search.ai/pricing. Two plan cards, each with a trial CTA that // posts to admin-post → server-to-server authenticated call to the // service → Stripe Checkout with prefilled email + origin=wp_admin // metadata. Pricing numbers are hardcoded here on purpose: the // marketing page keeps canonical pricing for the public, this // screen keeps canonical pricing for the plugin's own funnel. // Read the quota transient directly — the 6h cache is kept fresh by // maybe_refresh_plan_cache() entering this screen, and we do not // want to make a blocking HTTP call on every page render. $quota = get_transient( 'ai_search_quota_data' ); $usage_line = ''; if ( is_array( $quota ) && isset( $quota['usage']['used'], $quota['usage']['limit'] ) ) { $used = (int) $quota['usage']['used']; $limit = (int) $quota['usage']['limit']; $usage_line = sprintf( /* translators: 1: embeddings used this month, 2: monthly limit */ __( 'You have used %1$s of %2$s embeddings this month.', 'ai-search' ), '' . esc_html( number_format_i18n( $used ) ) . '', '' . esc_html( number_format_i18n( $limit ) ) . '' ); } $error = isset( $_GET['error'] ) ? sanitize_text_field( wp_unslash( $_GET['error'] ) ) : ''; $start_action_url = admin_url( 'admin-post.php' ); ?>

[] ] ); ?>

€19/year

€39/year

settings_pages; } /** * Get setup wizard instance */ public function get_setup_wizard() { return $this->setup_wizard; } /** * Add custom CSS for menu icon sizing */ public function add_menu_icon_styles() { ?>

:

:

check_origin_match() ) { return; } $registered_origin = $service->get_registered_origin(); $current_origin = get_site_url(); $reconnect_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_reconnect_service' ), 'ai_search_reconnect_service' ); ?>

' . esc_html( $registered_origin ) . '', '' . esc_html( $current_origin ) . '' ); ?>

register_origin(); if ( $new_token ) { // v1.48.4: clear the background-retry state so the self-heal path // rearms if this install ever loses the token again. delete_option( 'ai_search_register_retry_count' ); delete_transient( 'ai_search_register_retry_lock' ); wp_redirect( admin_url( 'admin.php?page=ai-search&ai_search_reconnected=success' ) ); } else { wp_redirect( admin_url( 'admin.php?page=ai-search&ai_search_reconnected=error' ) ); } exit; } /** * Show a one-time welcome notice when WooCommerce is active. */ public function show_woocommerce_welcome_notice() { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( ! class_exists( 'WooCommerce' ) ) { return; } if ( ! get_option( 'ai_search_setup_completed', false ) ) { return; } if ( get_option( 'ai_search_woo_welcome_dismissed', false ) ) { return; } $settings_url = admin_url( 'admin.php?page=ai-search&tab=woocommerce' ); $nonce = wp_create_nonce( 'ai_search_dismiss_woo_welcome' ); ?>

:

id, 'ai-search' ) ) { return; } // Do not stack on top of the Subscription hero after a fresh claim, // or the reindex reminder card - both live on the General tab and // deserve to be the star for that first load. if ( get_transient( 'ai_search_claim_success' ) ) { return; } if ( ! get_option( 'ai_search_setup_completed', false ) ) { return; } // v1.35.0: if the quota banner is going to render this request, don't // stack another purple upsell above/below it - the quota story is // more urgent and specific, let it stand alone. if ( $this->resolve_quota_banner_variant() ) { return; } // v1.35.0: permanent opt-out. The 60-day snooze is fine when the // user is undecided; anyone who clicks "Don't show again" has // decided, and we owe them silence. if ( get_option( 'ai_search_pro_banner_dismissed_forever', false ) ) { return; } $snoozed_until = (int) get_option( 'ai_search_pro_banner_snoozed_until', 0 ); if ( $snoozed_until > time() ) { return; } // Skip paid customers. $quota = get_transient( 'ai_search_quota_data' ); if ( is_array( $quota ) && isset( $quota['plan']['key'] ) && 'free' !== $quota['plan']['key'] ) { return; } $pricing_url = 'https://wp-search.ai/pricing/?utm_source=wp-admin&utm_medium=pro-banner'; $dismiss_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_snooze_pro_banner' ), 'ai_search_snooze_pro_banner' ); $forever_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_dismiss_pro_banner_forever' ), 'ai_search_dismiss_pro_banner_forever' ); ?>
= 80% used), 'quota_exhausted' * (>= 100% used), or null (do not show). Also honours the preview * query var so the design can be reviewed on any account state. * * The 80/100 thresholds compare the WORSE of the daily and monthly * fractions - a store that has burned 100% of today's throttle wants * the same red banner as one that used up its monthly cap. * * @since 1.35.0 * * @return string|null 'quota_warning' | 'quota_exhausted' | null */ private function resolve_quota_banner_variant() { // Preview mode: any admin can force a variant with ?ai_search_preview=... // on a plugin page to review the design. This bypasses plan/quota // checks so paid customers or fresh installs can see it too. $preview = isset( $_GET['ai_search_preview'] ) ? sanitize_key( (string) $_GET['ai_search_preview'] ) : ''; if ( in_array( $preview, [ 'quota_warning', 'quota_exhausted' ], true ) ) { return $preview; } $quota = get_transient( 'ai_search_quota_data' ); if ( ! is_array( $quota ) ) { return null; } // Only nag Free plans. Paid customers already have room. $plan_key = isset( $quota['plan']['key'] ) ? (string) $quota['plan']['key'] : 'free'; if ( 'free' !== $plan_key ) { return null; } $used_daily = (int) ( $quota['daily_usage'] ?? 0 ); $limit_daily = (int) ( $quota['daily_limit'] ?? 0 ); $used_total = (int) ( $quota['total_embeddings_used'] ?? 0 ); $limit_total = (int) ( $quota['total_embeddings_limit'] ?? 0 ); $daily_pct = $limit_daily > 0 ? ( $used_daily / $limit_daily ) : 0; $total_pct = $limit_total > 0 ? ( $used_total / $limit_total ) : 0; $worst = max( $daily_pct, $total_pct ); if ( $worst >= 1.0 ) { return 'quota_exhausted'; } if ( $worst >= 0.8 ) { return 'quota_warning'; } return null; } /** * Render the quota-warning banner when Free-plan usage approaches or * hits the cap. Two variants (see resolve_quota_banner_variant()), * both dismissable via a short (3-day) snooze because the underlying * situation is dynamic - a purchase reset or a fresh month can flip * the state on its own. * * @since 1.35.0 */ public function show_quota_warning_banner() { if ( ! current_user_can( 'manage_options' ) ) { return; } $screen = get_current_screen(); if ( ! $screen || false === strpos( (string) $screen->id, 'ai-search' ) ) { return; } // Do not stack on the claim-success hero or a fresh install without // setup. Same reasoning as show_pro_upgrade_banner(). if ( get_transient( 'ai_search_claim_success' ) ) { return; } if ( ! get_option( 'ai_search_setup_completed', false ) ) { return; } // Snooze / permanent-dismiss respect - both ignored when previewing // so the design can be reviewed even after the user opted out. $preview = isset( $_GET['ai_search_preview'] ) ? sanitize_key( (string) $_GET['ai_search_preview'] ) : ''; if ( ! $preview && get_option( 'ai_search_quota_banner_dismissed_forever', false ) ) { return; } $snoozed_until = (int) get_option( 'ai_search_quota_banner_snoozed_until', 0 ); if ( ! $preview && $snoozed_until > time() ) { return; } $variant = $this->resolve_quota_banner_variant(); if ( ! $variant ) { return; } $pricing_url = 'https://wp-search.ai/pricing/?utm_source=wp-admin&utm_medium=quota-banner&utm_content=' . rawurlencode( $variant ); $dismiss_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_snooze_quota_banner' ), 'ai_search_snooze_quota_banner' ); $forever_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_dismiss_quota_banner_forever' ), 'ai_search_dismiss_quota_banner_forever' ); if ( 'quota_exhausted' === $variant ) { $title = __( 'Monthly cap reached - new content is no longer being synced with AI', 'ai-search' ); $lede = __( 'You have used every embedding on the Free plan. Existing search keeps working, but new posts and updates will not be searchable until you upgrade or the monthly counter resets.', 'ai-search' ); $cta = __( 'Upgrade to keep syncing', 'ai-search' ); } else { $title = __( 'You are running low on Free-plan embeddings', 'ai-search' ); $lede = __( 'At the current pace you will hit the monthly cap soon. Standard raises it to 25,000/month, runs on the more precise text-embedding-3-large model, and starts with a 14-day free trial on annual plans.', 'ai-search' ); $cta = __( 'See Standard', 'ai-search' ); } ?>
'; printf( /* translators: %s: link to Insights tab */ esc_html__( 'Search logging is off. %s to see what shoppers search for.', 'ai-search' ), '' . esc_html__( 'Turn it on', 'ai-search' ) . '' ); echo '

'; return; } $summary = AI_Search::logs_summary( 7 ); $top_three = AI_Search::top_searches_since( 7, 3 ); $top_zero = AI_Search::top_no_results_since( 7, 3 ); if ( 0 === $summary['total'] ) { echo '

'; esc_html_e( 'No searches yet this week. Insights appear here once visitors use your search.', 'ai-search' ); echo '

'; return; } ?>