' . esc_html__( 'Token recovered!', 'ai-search' ) . ' ' . esc_html__( 'A fresh API token was issued for this site. Your existing usage limits and quotas were preserved.', 'ai-search' ) . '

'; } elseif ( $recovery_status === 'failed' ) { echo '

' . esc_html__( 'Token recovery failed.', 'ai-search' ) . ' ' . esc_html__( 'Could not issue a new token. Please try again in a few minutes or contact support.', 'ai-search' ) . '

'; } } // Handle connection check messages if ( isset( $_GET['token_validation'] ) ) { $validation_status = sanitize_text_field( $_GET['token_validation'] ); if ( $validation_status === 'success' ) { $validation_data = get_transient( 'ai_search_validation_data' ); if ( $validation_data ) { echo '

' . esc_html__( 'Connection successful!', 'ai-search' ) . ' ' . esc_html__( 'AI Search Service is working correctly.', 'ai-search' ) . '

'; if ( isset( $validation_data['client'] ) ) { $client = $validation_data['client']; $used_total = intval( $client['used_total'] ?? 0 ); $total_limit = intval( $client['total_limit'] ?? 10000 ); $remaining = $total_limit - $used_total; echo '

' . sprintf( esc_html__( 'Embeddings used: %1$s / %2$s (%3$s remaining)', 'ai-search' ), '' . esc_html( number_format( $used_total ) ) . '', esc_html( number_format( $total_limit ) ), esc_html( number_format( $remaining ) ) ) . '

'; } echo '
'; delete_transient( 'ai_search_validation_data' ); } } elseif ( $validation_status === 'invalid' ) { $error_data = get_transient( 'ai_search_validation_error' ); echo '

' . esc_html__( 'Connection failed!', 'ai-search' ) . '

'; if ( $error_data && isset( $error_data['details'] ) ) { echo '

' . esc_html( $error_data['details'] ) . '

'; } // The service returns error: "Invalid token" specifically when the // token isn't found in its DB (vs. "Origin mismatch" which has its // own dedicated reconnect flow). Offer one-click recovery only for // that case so we don't show a misleading button for other errors. $is_invalid_token = $error_data && isset( $error_data['error'] ) && $error_data['error'] === 'Invalid token'; if ( $is_invalid_token ) { $recover_url = wp_nonce_url( admin_url( 'admin-post.php?action=ai_search_recover_token' ), 'ai_search_recover_token' ); echo '

' . esc_html__( 'Your token is no longer recognized by the service. You can reconnect by issuing a fresh token — your usage limits and quotas will be preserved.', 'ai-search' ) . '

'; echo '

' . esc_html__( 'Reconnect with new token', 'ai-search' ) . '

'; } echo '
'; delete_transient( 'ai_search_validation_error' ); } elseif ( $validation_status === 'error' ) { echo '

' . esc_html__( 'Connection failed!', 'ai-search' ) . ' ' . esc_html__( 'Unable to reach AI Search Service. Please try again later.', 'ai-search' ) . '

'; } elseif ( $validation_status === 'no_token' ) { echo '

' . esc_html__( 'Not registered.', 'ai-search' ) . ' ' . esc_html__( 'Please save your settings to register with the AI Search Service.', 'ai-search' ) . '

'; } } // Handle form submission if ( isset( $_POST['provider'], $_POST['api_key'] ) ) { check_admin_referer( 'ai_search_save_settings' ); $new_provider = sanitize_text_field( $_POST['provider'] ); $current_provider = get_option( 'ai_search_provider', '' ); // Save the basic settings update_option( 'ai_search_provider', $new_provider ); update_option( 'ai_search_api_key', sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) ); // Save badge public visibility option update_option( 'ai_search_badge_public', isset( $_POST['badge_public'] ) && $_POST['badge_public'] === '1' ); // Save OpenAI model selection $allowed_models = [ 'text-embedding-3-small', 'text-embedding-3-large' ]; $selected_model = isset( $_POST['openai_model'] ) ? sanitize_text_field( $_POST['openai_model'] ) : 'text-embedding-3-small'; if ( in_array( $selected_model, $allowed_models, true ) ) { update_option( 'ai_search_openai_model', $selected_model ); } // Handle AI service registration $service_client = new AI_Search_Service(); if ( $new_provider === 'ai_service' && empty( get_option( 'ai_search_service_token', '' ) ) ) { $service_token = $service_client->register_origin(); if ( $service_token ) { update_option( 'ai_search_service_token', $service_token ); echo '

' . esc_html__( 'Settings saved successfully! AI Search Service registered.', 'ai-search' ) . '

'; } else { update_option( 'ai_search_provider', $current_provider ); echo '

' . esc_html__( 'AI Search Service is temporarily unavailable.', 'ai-search' ) . ' ' . esc_html__( 'Please try again later or use your own OpenAI API key instead.', 'ai-search' ) . '

'; } } else { echo '

' . esc_html__( 'Settings saved successfully!', 'ai-search' ) . '

'; } // Reload settings to display updated values $provider = get_option( 'ai_search_provider', 'ai_service' ); $api_key = get_option( 'ai_search_api_key', '' ); $service_token = get_option( 'ai_search_service_token', '' ); $badge_public = get_option( 'ai_search_badge_public', false ); $openai_model = get_option( 'ai_search_openai_model', 'text-embedding-3-small' ); } ?>

get_quota_info(); $sub_plan = is_array( $sub_quota ) && isset( $sub_quota['plan'] ) ? $sub_quota['plan'] : null; $sub_sub = is_array( $sub_quota ) && isset( $sub_quota['subscription'] ) ? $sub_quota['subscription'] : null; $plan_key = is_array( $sub_plan ) && isset( $sub_plan['key'] ) ? (string) $sub_plan['key'] : 'free'; $plan_label = is_array( $sub_plan ) && isset( $sub_plan['label'] ) ? (string) $sub_plan['label'] : ucfirst( $plan_key ); $is_paid = ( 'free' !== $plan_key ) && is_array( $sub_sub ); // ------------------------------------------------------------------- // Persistent reindex reminder. Set true by the claim handler; the // customer sees it every time they load the General tab until they // dismiss it (or ideally regenerate). // // The technical reason: 3-small (Free) and 3-large (paid) produce // vectors with different dimensions (1536 vs 3072). We do NOT re-embed // existing posts on upgrade - the vectors already stored are still // compared with themselves for now, but new queries under -large will // dominate them. Regeneration is the fix and it is worth being loud // about, because the customer is paying for a precision they will not // feel until they trigger it. // // Belt-and-braces guard on top of the flag: if the current plan is // somehow back to Free (release-slot cleared the flag, or the flag // lingered from a past subscription that ended), the card is no // longer relevant - re-indexing on the small model gains nothing. if ( $is_paid && get_option( 'ai_search_upgrade_pending_reindex', false ) ) : ?>

' . esc_html__( 'This site is back on the Free plan. Paste the same activation key on another WordPress install to use it there.', 'ai-search' ) . '

'; } $claim_error = get_transient( 'ai_search_claim_error' ); if ( $claim_error && is_array( $claim_error ) ) { delete_transient( 'ai_search_claim_error' ); echo '

' . esc_html__( 'Activation failed.', 'ai-search' ) . ' ' . esc_html( $claim_error['message'] ) . '

'; } ?>
__( 'Trial', 'ai-search' ), 'active' => __( 'Active', 'ai-search' ), 'past_due' => __( 'Past due', 'ai-search' ), ]; $cycle_labels = [ 'monthly' => __( 'monthly', 'ai-search' ), 'annual' => __( 'annual', 'ai-search' ), ]; ?>

' . esc_html( $period_human ) . '' ); elseif ( $will_cancel && $period_human ) : printf( /* translators: %s: end of billing period */ esc_html__( 'Cancelling - active until %s, then this site drops back to the Free plan.', 'ai-search' ), '' . esc_html( $period_human ) . '' ); elseif ( 'past_due' === $status_key ) : esc_html_e( 'Payment failed on the last charge. Update your card in the billing portal to keep the plan active.', 'ai-search' ); elseif ( $period_human ) : printf( /* translators: 1: 'monthly' or 'annual', 2: next renewal date */ esc_html__( 'Renews %1$s on %2$s.', 'ai-search' ), esc_html( isset( $cycle_labels[ $cycle_key ] ) ? $cycle_labels[ $cycle_key ] : $cycle_key ), '' . esc_html( $period_human ) . '' ); endif; ?>

', '' ); ?>