config = $config; } /** * Initialize settings * * @return void */ public function init() { add_action('admin_init', [$this, 'register_settings']); } /** * Register settings * * @return void */ public function register_settings() { register_setting( $this->settings_group, $this->option_name, [ 'sanitize_callback' => [$this, 'sanitize_settings'] ] ); $this->add_settings_sections(); $this->add_settings_fields(); } /** * Add settings sections * * @return void */ private function add_settings_sections() { // AI Provider Section add_settings_section( 'aicog_provider_section', __('AI Provider Configuration', 'ai-comment-guard'), [$this, 'render_provider_section'], 'ai-comment-guard' ); // Processing Settings Section add_settings_section( 'aicog_processing_section', __('Processing Settings', 'ai-comment-guard'), [$this, 'render_processing_section'], 'ai-comment-guard' ); // Logging Section add_settings_section( 'aicog_logging_section', __('Logging Settings', 'ai-comment-guard'), [$this, 'render_logging_section'], 'ai-comment-guard' ); // Prompt Customization Section add_settings_section( 'aicog_prompt_section', __('AI Prompt Customization', 'ai-comment-guard'), [$this, 'render_prompt_section'], 'ai-comment-guard-prompt' ); } /** * Add settings fields * * @return void */ private function add_settings_fields() { // Provider fields add_settings_field( 'aicog_provider', esc_html__('AI Provider', 'ai-comment-guard'), [$this, 'render_provider_field'], 'ai-comment-guard', 'aicog_provider_section' ); add_settings_field( 'aicog_provider_token', esc_html__('API Token', 'ai-comment-guard'), [$this, 'render_token_field'], 'ai-comment-guard', 'aicog_provider_section' ); // Processing fields add_settings_field( 'aicog_auto_process', esc_html__('Auto Process Comments', 'ai-comment-guard'), [$this, 'render_auto_process_field'], 'ai-comment-guard', 'aicog_processing_section' ); add_settings_field( 'aicog_spam_threshold', esc_html__('Spam Threshold', 'ai-comment-guard'), [$this, 'render_spam_threshold_field'], 'ai-comment-guard', 'aicog_processing_section' ); add_settings_field( 'aicog_approval_threshold', esc_html__('Approval Threshold', 'ai-comment-guard'), [$this, 'render_approval_threshold_field'], 'ai-comment-guard', 'aicog_processing_section' ); add_settings_field( 'aicog_disable_email_notifications', esc_html__('Disable Email Notifications', 'ai-comment-guard'), [$this, 'render_disable_email_notifications_field'], 'ai-comment-guard', 'aicog_processing_section' ); add_settings_field( 'aicog_log_enabled', esc_html__('Enable logging', 'ai-comment-guard'), [$this, 'render_log_enabled_field'], 'ai-comment-guard', 'aicog_logging_section' ); add_settings_field( 'aicog_log_retention_days', esc_html__('Log retention (days)', 'ai-comment-guard'), [$this, 'render_log_retention_field'], 'ai-comment-guard', 'aicog_logging_section' ); // Prompt fields add_settings_field( 'aicog_custom_system_message', esc_html__('System Message', 'ai-comment-guard'), [$this, 'render_system_message_field'], 'ai-comment-guard-prompt', 'aicog_prompt_section' ); } /** * Render provider section * * @return void */ public function render_provider_section() { echo '
' . esc_html__('Select your AI provider and enter your API credentials.', 'ai-comment-guard') . '
'; } /** * Render processing section * * @return void */ public function render_processing_section() { echo '' . esc_html__('Configure how comments are processed and thresholds for automatic actions.', 'ai-comment-guard') . '
'; } /** * Render logging section * * @return void */ public function render_logging_section() { echo '' . esc_html__('Configure logging and data retention settings.', 'ai-comment-guard') . '
'; } /** * Render prompt section * * @return void */ public function render_prompt_section() { echo '' . esc_html__('Customize the prompt sent to the AI for comment analysis.', 'ai-comment-guard') . '
'; } /** * Render provider field * * @return void */ public function render_provider_field() { $value = $this->config->get('ai_provider', ''); $providers = AIManager::get_available_providers(); ?> config->has_sensitive_value('ai_provider_token'); $masked_token = $has_token ? $this->config->get_masked('ai_provider_token') : ''; if ($has_token) { ?>config->get('auto_process', true); ?> config->get('spam_threshold', 0.7); ?> config->get('approval_threshold', 0.3); ?> config->get('disable_email_notifications', false); ?> config->get('log_enabled', false); ?> config->get('log_retention_days', 30); ?>
config->get('custom_system_message', ''); $placeholder = __('You are an expert comment moderator. Analyze comments and respond only with valid JSON.', 'ai-comment-guard'); ?> config->get(); // Detect if this is a prompt-only submission $is_prompt_only = array_key_exists('custom_system_message', $input) && !isset($input['ai_provider']) && !isset($input['ai_provider_token']) && !array_key_exists('auto_process', $input) && !array_key_exists('log_enabled', $input); if ($is_prompt_only) { // This is from the prompt tab - only process prompt field // Use array_key_exists so an explicit empty string can be saved (clearing the prompt). if (array_key_exists('custom_system_message', $input)) { $sanitized['custom_system_message'] = sanitize_textarea_field($input['custom_system_message']); } } else { // This is from the main settings tab - process all fields // Use SecurityHelper for consistent sanitization foreach ($input as $key => $value) { // Special handling for empty token with existing token if ($key === 'ai_provider_token' && empty($value) && !empty($current['ai_provider_token'])) { continue; // Keep existing token } $sanitized_value = \AICOG\Utils\SecurityHelper::sanitize_config_value($key, $value); if ($sanitized_value !== false) { $sanitized[$key] = $sanitized_value; } } } // Only clear connection test flag if provider or token changed $provider_changed = isset($sanitized['ai_provider']) && $sanitized['ai_provider'] !== $current['ai_provider']; $token_changed = isset($sanitized['ai_provider_token']) && !empty($sanitized['ai_provider_token']); if ($provider_changed || $token_changed) { delete_transient('aicog_connection_tested'); } // Clear configuration cache since settings are being updated $this->config->clear_cache(); return array_merge($current, $sanitized); } }