' . 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' ); } ?>