config = $config; $this->database = $database; } /** * Initialize admin components * * @return void */ public function init() { // Initialize settings manager $this->settings_manager = new SettingsManager($this->config); $this->settings_manager->init(); // Initialize pages $this->settings_page = new SettingsPage($this->config, $this->settings_manager); $this->logs_page = new LogsPage($this->database); $this->preview_page = new PreviewPage($this->config); // Register hooks add_action('admin_menu', [$this, 'add_admin_menu']); add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']); // Register AJAX handlers $this->register_ajax_handlers(); // Add plugin action links add_filter('plugin_action_links_' . AICOG_PLUGIN_BASENAME, [$this, 'add_plugin_action_links']); } /** * Add admin menu * * @return void */ public function add_admin_menu() { // Main settings page add_options_page( __('AI Comment Guard Settings', 'ai-comment-guard'), __('AI Comment Guard', 'ai-comment-guard'), 'manage_options', 'ai-comment-guard', [$this, 'render_admin_page'] ); } /** * Render admin page * * @return void */ public function render_admin_page() { // Get current tab (nonce verification not required for read-only GET parameters in admin context) $current_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'settings'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $log_enabled = $this->config->is_enabled('logging'); // Available tabs $tabs = [ 'settings' => __('Settings', 'ai-comment-guard'), 'prompt' => __('Prompt', 'ai-comment-guard'), 'preview' => __('Preview', 'ai-comment-guard') ]; if ($log_enabled) { $tabs['logs'] = __('Logs', 'ai-comment-guard'); } ?>

render_header(); ?> render_tabs($tabs, $current_tab); ?>
settings_page->render_prompt_tab(); break; case 'preview': $this->preview_page->render(); break; case 'logs': if ($log_enabled) { $this->logs_page->render(); } break; case 'settings': default: $this->settings_page->render_settings_tab(); break; } ?>

wp_create_nonce('aicog_nonce'), 'ajaxurl' => admin_url('admin-ajax.php'), 'strings' => $this->get_js_strings() ] ); } /** * Get JavaScript strings for localization * * @return array */ private function get_js_strings() { return [ 'testing' => __('Testing connection...', 'ai-comment-guard'), 'test_success' => __('Connection successful!', 'ai-comment-guard'), 'test_error' => __('Connection failed:', 'ai-comment-guard'), 'test_button' => __('Test AI Connection', 'ai-comment-guard'), 'confirm_delete_logs' => __('Are you sure you want to delete all logs?', 'ai-comment-guard'), 'unsaved_changes' => __('You have unsaved changes. Are you sure you want to leave?', 'ai-comment-guard'), 'provider_required' => __('Please select a provider and enter the API token', 'ai-comment-guard'), 'test_required' => __('Please test the connection before saving', 'ai-comment-guard'), 'threshold_error' => __('Thresholds must be between 0.0 and 1.0', 'ai-comment-guard'), 'log_tab_note' => __('Note: After saving, a "Logs" tab will appear on this page.', 'ai-comment-guard'), 'unsaved_changes_notice' => __('Unsaved changes: Remember to save your settings.', 'ai-comment-guard'), 'token_validated' => __('🔒 Validated', 'ai-comment-guard'), 'token_validated_tooltip' => __('Token validated. Change provider to modify.', 'ai-comment-guard'), 'provider_changed_warning' => __('Provider changed. Please enter your new API token and test the connection.', 'ai-comment-guard'), // Preview strings 'analyzing' => __('Analyzing...', 'ai-comment-guard'), 'analyze_button' => __('Analyze Comment', 'ai-comment-guard'), 'analysis_error' => __('Analysis failed:', 'ai-comment-guard'), 'analysis_error_title' => __('Analysis Error', 'ai-comment-guard'), 'analysis_results' => __('Analysis Results', 'ai-comment-guard'), 'status_label' => __('Status', 'ai-comment-guard'), 'status_approved' => __('Approved', 'ai-comment-guard'), 'status_rejected' => __('Rejected', 'ai-comment-guard'), 'status_spam' => __('Spam', 'ai-comment-guard'), 'status_pending' => __('Pending Review', 'ai-comment-guard'), 'confidence_label' => __('Confidence Score', 'ai-comment-guard'), 'reasoning_label' => __('AI Reasoning', 'ai-comment-guard'), 'prompt_label' => __('Complete Prompt Used', 'ai-comment-guard'), 'show_prompt' => __('Show/Hide Complete Prompt', 'ai-comment-guard'), 'system_message_label' => __('System Message', 'ai-comment-guard'), 'user_prompt_label' => __('User Prompt', 'ai-comment-guard') ]; } /** * Register AJAX handlers * * @return void */ private function register_ajax_handlers() { // Test AI connection add_action('wp_ajax_aicog_test_connection', [$this, 'handle_test_connection']); // Delete logs add_action('wp_ajax_aicog_delete_logs', [$this->logs_page, 'handle_delete_logs']); // Preview comment analysis add_action('wp_ajax_aicog_analyze_comment', [$this->preview_page, 'handle_analyze_comment']); } /** * Handle test connection AJAX request * * @since 1.1.0 * @return void */ public function handle_test_connection() { // Validate nonce and capability first check_ajax_referer('aicog_nonce', 'nonce'); if (!current_user_can('manage_options')) { wp_die('Unauthorized', 403); } // Now safe to access $_POST $provider = isset($_POST['ai_provider']) ? sanitize_text_field(wp_unslash($_POST['ai_provider'])) : ''; $token = isset($_POST['ai_provider_token']) ? sanitize_text_field(wp_unslash($_POST['ai_provider_token'])) : ''; if (empty($provider) || empty($token)) { wp_send_json_error(__('Please select a provider and provide the token', 'ai-comment-guard')); } try { $ai_manager = new \AICOG\AI\AIManager($provider, $token); if ($ai_manager->test_connection()) { wp_send_json_success([ 'message' => __('Connection successful with AI provider', 'ai-comment-guard'), 'provider' => $provider ]); } else { wp_send_json_error(__('Connection test failed', 'ai-comment-guard')); } } catch (\Exception $e) { wp_send_json_error(sprintf( /* translators: %s is the error message from the connection attempt */ __('Connection error: %s', 'ai-comment-guard'), $e->getMessage() )); } } /** * Add plugin action links * * @param array $links Existing plugin action links * @return array Modified plugin action links */ public function add_plugin_action_links($links) { $settings_link = sprintf( '%s', admin_url('options-general.php?page=ai-comment-guard'), esc_html__('Settings', 'ai-comment-guard') ); // Add settings link at the beginning array_unshift($links, $settings_link); return $links; } }