__('Permission denied.', 'ai-scrape-protect')], 403); } if (!check_ajax_referer('aisp_nonce', 'nonce', false)) { wp_send_json_error(['message' => __('Security check failed.', 'ai-scrape-protect')], 403); } } // Save global options (toggles). public function save_options() { $this->verify_request(); $options = get_option('aisp_options', []); $fields = [ 'enabled', 'robots_txt_enabled', 'meta_tags_enabled', 'meta_content_enabled', 'meta_images_enabled', ]; foreach ($fields as $field) { $options[$field] = isset($_POST[$field]) && $_POST[$field] === '1' ? '1' : '0'; } update_option('aisp_options', $options); wp_send_json_success(['message' => __('Settings saved.', 'ai-scrape-protect')]); } // Save individual bot enabled/disabled state. public function save_bot() { $this->verify_request(); $key = isset($_POST['bot_key']) ? sanitize_key($_POST['bot_key']) : ''; $enabled = isset($_POST['enabled']) && $_POST['enabled'] === '1' ? '1' : '0'; if (empty($key)) { wp_send_json_error(['message' => __('Invalid bot key.', 'ai-scrape-protect')]); } $saved = get_option('aisp_bot_settings', []); $saved[$key] = $enabled; update_option('aisp_bot_settings', $saved); wp_send_json_success(['message' => __('Bot setting saved.', 'ai-scrape-protect')]); } // Add a custom user agent string. public function add_custom_bot() { $this->verify_request(); $agent = isset($_POST['agent']) ? sanitize_text_field($_POST['agent']) : ''; // Validate: no spaces, no slashes, no quotes, not empty, max 100 chars. if (empty($agent) || strlen($agent) > 100 || preg_match('/[\s\/\'"\\\\]/', $agent)) { wp_send_json_error(['message' => __('Invalid user agent string. No spaces, slashes, or quotes allowed.', 'ai-scrape-protect')]); } $custom = get_option('aisp_custom_bots', []); if (in_array($agent, $custom, true)) { wp_send_json_error(['message' => __('This bot is already in your list.', 'ai-scrape-protect')]); } $custom[] = $agent; update_option('aisp_custom_bots', $custom); wp_send_json_success([ 'message' => __('Custom bot added.', 'ai-scrape-protect'), 'agent' => $agent, ]); } // Remove a custom user agent string. public function remove_custom_bot() { $this->verify_request(); $agent = isset($_POST['agent']) ? sanitize_text_field($_POST['agent']) : ''; $custom = get_option('aisp_custom_bots', []); $custom = array_values(array_filter($custom, function($item) use ($agent) { return $item !== $agent; })); update_option('aisp_custom_bots', $custom); wp_send_json_success(['message' => __('Custom bot removed.', 'ai-scrape-protect')]); } // Reset all settings to defaults. public function reset_defaults() { $this->verify_request(); delete_option('aisp_options'); delete_option('aisp_bot_settings'); delete_option('aisp_custom_bots'); // Re-install defaults. aisp_install_defaults(); wp_send_json_success(['message' => __('Settings reset to defaults.', 'ai-scrape-protect')]); } }