array(type, default, section, extra) * Types: bool, int (extra: min, max), enum (extra: allowed values), text, secret, cidr_list, email_list, list (extra: allowed values) */ public static function schema() { return array( // General and scanning 'external_wporg' => array('bool', false, 'general'), 'scan_schedule' => array('enum', 'off', 'general', array('off', 'daily', 'weekly')), 'scan_schedule_profile' => array('enum', 'full', 'general', array('quick', 'full')), 'scan_time_budget' => array('int', 10, 'general', array(3, 60)), 'scan_max_files' => array('int', 100000, 'general', array(1000, 1000000)), 'delete_data_on_uninstall' => array('bool', false, 'general'), // Vulnerability intelligence 'vuln_provider' => array('enum', 'none', 'intel', array('none', 'wordfence')), 'wordfence_api_key' => array('secret', '', 'intel'), // Monitoring and privacy 'traffic_monitoring' => array('bool', false, 'monitoring'), 'count_all_requests' => array('bool', false, 'monitoring'), 'anonymize_ip' => array('bool', true, 'monitoring'), 'log_retention_days' => array('int', 30, 'monitoring', array(1, 365)), 'log_admin_logins' => array('bool', true, 'monitoring'), 'verify_crawlers' => array('bool', false, 'monitoring'), 'proxy_header' => array('enum', 'none', 'monitoring', array('none', 'cf-connecting-ip', 'x-forwarded-for', 'x-real-ip')), 'trusted_proxies' => array('cidr_list', '', 'monitoring'), 'proxy_cloudflare' => array('bool', false, 'monitoring'), 'proxy_none' => array('bool', false, 'monitoring'), // Protection (every protective feature is off by default) 'login_protection' => array('bool', false, 'protection'), 'login_max_failures' => array('int', 5, 'protection', array(3, 50)), 'login_window_minutes' => array('int', 15, 'protection', array(1, 1440)), 'login_block_minutes' => array('int', 15, 'protection', array(1, 1440)), 'login_generic_errors' => array('bool', false, 'protection'), 'xmlrpc_mode' => array('enum', 'off', 'protection', array('off', 'pingbacks', 'disable', 'server')), 'xmlrpc_block_multicall' => array('bool', false, 'protection'), 'rest_user_enumeration' => array('bool', false, 'protection'), 'hide_generator' => array('bool', false, 'protection'), 'probe_protection' => array('bool', false, 'protection'), 'probe_threshold' => array('int', 10, 'protection', array(3, 500)), 'probe_block_minutes' => array('int', 60, 'protection', array(1, 1440)), 'throttle_404' => array('bool', false, 'protection'), 'throttle_404_threshold' => array('int', 60, 'protection', array(10, 5000)), 'block_scope' => array('enum', 'login', 'protection', array('login', 'site')), 'block_scanner_agents' => array('bool', false, 'protection'), 'block_fake_crawlers' => array('bool', false, 'protection'), 'allowlist' => array('cidr_list', '', 'protection'), 'disable_file_edit' => array('bool', false, 'protection'), 'hide_errors_runtime' => array('bool', false, 'protection'), // Security headers 'headers_enabled' => array('bool', false, 'headers'), 'headers_delivery' => array('enum', 'php', 'headers', array('php', 'htaccess')), 'header_nosniff' => array('bool', true, 'headers'), 'header_frame' => array('enum', 'sameorigin', 'headers', array('off', 'sameorigin')), 'header_referrer' => array('enum', 'strict-origin-when-cross-origin', 'headers', array('off', 'no-referrer', 'same-origin', 'strict-origin', 'strict-origin-when-cross-origin')), 'header_hsts' => array('enum', 'off', 'headers', array('off', 'test', 'year', 'year_subdomains')), 'header_permissions' => array('bool', false, 'headers'), 'header_permissions_value' => array('text', 'camera=(), microphone=(), geolocation=()', 'headers'), 'header_csp_mode' => array('enum', 'off', 'headers', array('off', 'report-only', 'enforce')), 'header_csp_policy' => array('text', "object-src 'none'; base-uri 'self'; frame-ancestors 'self'", 'headers'), // Notifications 'notify_email' => array('bool', false, 'notifications'), 'notify_recipients' => array('email_list', '', 'notifications'), 'notify_min_severity' => array('enum', 'high', 'notifications', array('critical', 'high', 'medium', 'low')), 'notify_events' => array('list', array('findings', 'admin_created', 'login_attacks', 'scan_failed'), 'notifications', array('findings', 'admin_created', 'login_attacks', 'scan_complete', 'scan_failed')), 'digest' => array('enum', 'off', 'notifications', array('off', 'daily', 'weekly')), 'notify_max_per_hour' => array('int', 10, 'notifications', array(1, 100)), 'dashboard_alerts' => array('bool', true, 'notifications'), ); } public static function defaults() { $defaults = array(); foreach (self::schema() as $key => $def) { $defaults[$key] = $def[1]; } return $defaults; } public static function all() { if (self::$cache === null) { $stored = AOWPCS_Plugin::get_option(self::OPTION, array()); self::$cache = array_merge(self::defaults(), is_array($stored) ? $stored : array()); } return self::$cache; } public static function get($key) { $all = self::all(); return array_key_exists($key, $all) ? $all[$key] : null; } /** * Validate and store values. Unknown keys are ignored. Returns the stored settings. */ public static function update($values) { $schema = self::schema(); $current = self::all(); foreach ($values as $key => $value) { if (!isset($schema[$key])) { continue; } $current[$key] = self::sanitize_value($schema[$key], $value, $current[$key]); } AOWPCS_Plugin::update_option(self::OPTION, $current, true); self::$cache = $current; do_action('aowpcs_settings_updated', $current); return $current; } /** * Values for one settings section taken from a submitted form. Unchecked boxes are absent from * a form submission, so every bool of the section is read explicitly. */ public static function from_form($section, $input) { $values = array(); foreach (self::schema() as $key => $def) { if ($def[2] !== $section) { continue; } if ($def[0] === 'bool') { $values[$key] = !empty($input[$key]); } elseif ($def[0] === 'list') { $values[$key] = isset($input[$key]) ? (array) $input[$key] : array(); } elseif ($def[0] === 'secret') { // An empty secret field means "keep the stored value"; the clear checkbox removes it if (!empty($input[$key . '_clear'])) { $values[$key] = ''; } elseif (isset($input[$key]) && trim($input[$key]) !== '') { $values[$key] = $input[$key]; } } elseif (isset($input[$key])) { $values[$key] = $input[$key]; } } return $values; } public static function sanitize_value($def, $value, $previous) { switch ($def[0]) { case 'bool': return (bool) $value; case 'int': $value = (int) $value; return max($def[3][0], min($def[3][1], $value)); case 'enum': $value = is_string($value) ? $value : ''; return in_array($value, $def[3], true) ? $value : $previous; case 'list': $value = array_values(array_intersect(array_map('strval', (array) $value), $def[3])); return $value; case 'secret': return preg_replace('/[^A-Za-z0-9._\-]/', '', (string) $value); case 'cidr_list': return self::sanitize_cidr_list($value); case 'email_list': $emails = array(); foreach (preg_split('/[\s,;]+/', (string) $value) as $email) { $email = sanitize_email($email); if ($email !== '' && is_email($email)) { $emails[] = $email; } } return implode(', ', array_unique($emails)); case 'text': default: return sanitize_text_field((string) $value); } } /** * One IP address or CIDR range per line; invalid entries are dropped */ public static function sanitize_cidr_list($value) { $valid = array(); foreach (preg_split('/[\s,]+/', (string) $value) as $entry) { $entry = trim($entry); if ($entry !== '' && AOWPCS_IP::is_valid_range($entry)) { $valid[] = $entry; } } return implode("\n", array_unique($valid)); } public static function list_value($key) { $value = self::get($key); if (!is_string($value) || $value === '') { return array(); } return array_values(array_filter(array_map('trim', explode("\n", $value)))); } /** * The original content protection option (per site) */ public static function legacy() { $opts = get_option(self::LEGACY_OPTION); return is_array($opts) ? $opts : array(); } public static function legacy_on($key) { $opts = self::legacy(); return isset($opts[$key]) && $opts[$key] === 'on'; } public static function update_legacy($key, $on) { $opts = self::legacy(); // An empty option means a new installation, where editors and administrators are exempt if ($opts === array() && $key !== 'exempt_editors') { $opts['exempt_editors'] = 'on'; } $opts[$key] = $on ? 'on' : 'off'; update_option(self::LEGACY_OPTION, $opts); } /** * Internal state (baselines, last scan, onboarding) — not autoloaded */ public static function state($key, $default = null) { if (self::$state === null) { $stored = AOWPCS_Plugin::get_option(self::STATE_OPTION, array()); self::$state = is_array($stored) ? $stored : array(); } return array_key_exists($key, self::$state) ? self::$state[$key] : $default; } public static function set_state($key, $value) { self::state('__load'); if ($value === null) { unset(self::$state[$key]); } else { self::$state[$key] = $value; } AOWPCS_Plugin::update_option(self::STATE_OPTION, self::$state, false); } /** * Forget cached values (used after another process may have changed them) */ public static function flush() { self::$cache = null; self::$state = null; } }