admin_url( 'admin-ajax.php' ), 'testApiNonce' => wp_create_nonce( 'aeo_bob_lite_test_api' ), 'i18n' => array( 'enterApiKeyFirst' => __( 'Please enter an API key first.', 'aeo-bob-lite' ), 'testing' => __( 'Testing...', 'aeo-bob-lite' ), 'connecting' => __( 'Connecting to OpenAI...', 'aeo-bob-lite' ), 'connectionFailed' => __( 'Connection failed. Please try again.', 'aeo-bob-lite' ), 'testConnection' => __( 'Test Connection', 'aeo-bob-lite' ), ), ) ); // Enqueue admin styles wp_enqueue_style( 'aeo-bob-lite-admin' ); } /** * Register settings */ public function register_settings() { register_setting('aeo_bob_lite_settings', 'aeo_bob_lite_openai_key', array( 'type' => 'string', 'sanitize_callback' => array($this, 'sanitize_api_key'), 'default' => '', )); add_settings_section( 'aeo_bob_lite_api_section', esc_html__('OpenAI API Configuration', 'aeo-bob-lite'), array($this, 'render_section_description'), 'aeo_bob_lite_settings' ); add_settings_field( 'openai_api_key', esc_html__('OpenAI API Key', 'aeo-bob-lite'), array($this, 'render_api_key_field'), 'aeo_bob_lite_settings', 'aeo_bob_lite_api_section' ); } /** * Sanitize API key */ public function sanitize_api_key($value) { return sanitize_text_field(trim($value)); } /** * Render section description */ public function render_section_description() { ?>

  1. platform.openai.com/api-keys

gpt-4o-mini

esc_html__('Permission denied', 'aeo-bob-lite'))); } $api_key = isset($_POST['api_key']) ? sanitize_text_field(wp_unslash($_POST['api_key'])) : ''; if (empty($api_key)) { wp_send_json_error(array('message' => esc_html__('API key is required', 'aeo-bob-lite'))); } // Test the API key with a simple request $response = wp_remote_post('https://api.openai.com/v1/chat/completions', array( 'headers' => array( 'Authorization' => 'Bearer ' . $api_key, 'Content-Type' => 'application/json', ), 'body' => json_encode(array( 'model' => 'gpt-4o-mini', 'messages' => array( array('role' => 'user', 'content' => 'Say "OK"') ), 'max_tokens' => 5, )), 'timeout' => 15, )); if (is_wp_error($response)) { wp_send_json_error(array('message' => esc_html__('Connection failed: ', 'aeo-bob-lite') . $response->get_error_message())); } $status_code = wp_remote_retrieve_response_code($response); if ($status_code === 200) { wp_send_json_success(array('message' => esc_html__('API key is valid and working!', 'aeo-bob-lite'))); } elseif ($status_code === 401) { wp_send_json_error(array('message' => esc_html__('Invalid API key. Please check your key.', 'aeo-bob-lite'))); } elseif ($status_code === 429) { wp_send_json_error(array('message' => esc_html__('Rate limit exceeded. Your key works but you need to add billing.', 'aeo-bob-lite'))); } else { $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); // Validate JSON parsing succeeded if (json_last_error() !== JSON_ERROR_NONE) { wp_send_json_error(array('message' => esc_html__('Invalid response from API', 'aeo-bob-lite'))); return; } $error_msg = isset($data['error']['message']) ? $data['error']['message'] : esc_html__('Unknown error', 'aeo-bob-lite'); wp_send_json_error(array('message' => $error_msg)); } } /** * Render settings page */ public function render_settings_page() { if (!current_user_can('manage_options')) { return; } // Save settings if (isset($_POST['submit']) && check_admin_referer('aeo_bob_lite_settings_save', 'aeo_bob_lite_settings_nonce')) { $api_key = isset($_POST['aeo_bob_lite_openai_key']) ? sanitize_text_field(wp_unslash($_POST['aeo_bob_lite_openai_key'])) : ''; update_option('aeo_bob_lite_openai_key', $api_key); echo '

' . esc_html__('Settings saved!', 'aeo-bob-lite') . '

'; } ?>