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_
:
:
'; esc_html_e( 'No searches yet this week. Insights appear here once visitors use your search.', 'ai-search' ); echo '
'; return; } ?>