set_defaults(); $this->load_settings(); } /** * Set default settings. * * This is the single source of truth for plugin defaults — both * activation (Snaggly::activate()) and reset (self::reset()) pull * from here rather than keeping their own copies. * * @since 1.0.0 */ private function set_defaults() { $this->defaults = array( 'allowed_extensions' => array_values(array_unique(explode('|', implode('|', array_keys(get_allowed_mime_types()))))), 'max_file_size' => 10485760, // 10MB 'download_timeout' => 30, // 30 seconds 'concurrent_downloads' => 3, 'auto_update_content' => true, 'group_duplicate_urls' => true, 'scan_post_types' => array('post', 'page'), 'preserve_original_urls' => false, 'retry_attempts' => 2, 'file_naming_convention' => 'original', // original, sanitized, hash 'user_agent' => 'default', // default, browser_chrome, browser_firefox, browser_safari, custom 'custom_user_agent' => '', 'logging_level' => 'errors', // none, errors, all // Rate limiting settings 'rate_limiting_enabled' => true, 'rate_limit_requests_per_minute' => 60, 'rate_limit_burst_requests' => 10, 'rate_limit_burst_window' => 10, 'rate_limit_block_duration' => 300, 'rate_limit_whitelist_ips' => array(), 'rate_limit_whitelist_users' => array(), // Enhanced scanning options 'scan_private_posts' => false, 'batch_size' => 20, 'enable_progressive_scan' => true, // Large site optimization 'large_site_threshold' => 500, 'auto_progressive_for_large_sites' => true, 'memory_threshold' => 0.8, 'max_scan_time' => 300, // Performance monitoring 'log_performance_metrics' => false, ); $this->defaults = apply_filters('snaggly_default_settings', $this->defaults); } /** * Get the full default settings array (used by Snaggly::activate() * so activation defaults and runtime defaults can never drift apart). * * @since 1.6.0 * @return array */ public function get_defaults() { return SnagglyFreeEdition::constrain_settings($this->defaults); } /** * Load settings from database * * @since 1.0.0 */ private function load_settings() { $saved_settings = get_option($this->option_name, array()); $this->settings = wp_parse_args($saved_settings, $this->defaults); } /** * Reload settings from database * * @since 1.0.0 */ public function reload_settings() { $this->load_settings(); } /** * Get a setting value * * @since 1.0.0 * @param string $key Setting key to retrieve. * @param mixed $default Optional. Default value if setting doesn't exist. Default null. * @return mixed Setting value or default. */ public function get($key, $default = null) { if (isset($this->settings[$key])) { return SnagglyFreeEdition::constrain_settings($this->settings)[$key]; } if (null !== $default) { return $default; } return isset($this->defaults[$key]) ? $this->defaults[$key] : null; } /** * Get all settings * * @since 1.0.0 * @return array All current settings. */ public function get_all() { return SnagglyFreeEdition::constrain_settings($this->settings); } /** * Update a setting * * @since 1.0.0 * @param string $key Setting key to update. * @param mixed $value Setting value. * @return bool True if setting was saved successfully. */ public function set($key, $value) { $this->settings[$key] = $value; return $this->save(); } /** * Update multiple settings * * @since 1.0.0 * @param array $settings Associative array of settings to update. * @return bool True if settings were saved successfully. */ public function set_multiple($settings) { foreach ($settings as $key => $value) { $this->settings[$key] = $value; } return $this->save(); } /** * Save settings to database * * @since 1.0.0 * @return bool True if settings were saved successfully. */ public function save() { return update_option($this->option_name, $this->settings); } /** * Reset settings to defaults * * @since 1.0.0 * @return bool True if settings were reset successfully. */ public function reset() { $this->settings = $this->defaults; return $this->save() || get_option($this->option_name) === $this->settings; } /** * Get allowed file extensions * * @since 1.0.0 * @return array */ public function get_allowed_extensions() { return array_values(array_filter(array_map('sanitize_key', (array) apply_filters('snaggly_allowed_extensions', $this->get('allowed_extensions', $this->defaults['allowed_extensions']))))); } /** * Check if file extension is allowed * * @since 1.0.0 * @param string $extension * @return bool */ public function is_allowed_extension($extension) { $allowed = $this->get_allowed_extensions(); $extension_lower = strtolower($extension); $allowed_lower = array_map('strtolower', $allowed); $is_allowed = in_array($extension_lower, $allowed_lower, true); $logging_level = $this->get_logging_level(); if ($logging_level === 'all' || ((defined('WP_DEBUG') && WP_DEBUG) && $logging_level !== 'none')) { $logger = SnagglyLogger::get_instance(); $logger->debug("Checking extension '$extension' against allowed: " . implode(', ', $allowed) . " - Result: " . ($is_allowed ? 'ALLOWED' : 'NOT ALLOWED')); } return $is_allowed; } /** * Get maximum file size * * @since 1.0.0 * @return int */ public function get_max_file_size() { return $this->get('max_file_size', $this->defaults['max_file_size']); } /** * Get download timeout * * @since 1.0.0 * @return int */ public function get_download_timeout() { return $this->get('download_timeout', $this->defaults['download_timeout']); } /** * Get concurrent downloads limit * * @since 1.0.0 * @return int */ public function get_concurrent_downloads() { return $this->get('concurrent_downloads', $this->defaults['concurrent_downloads']); } /** * Check if content should be auto-updated * * @since 1.0.0 * @return bool */ public function should_auto_update_content() { return $this->get('auto_update_content', $this->defaults['auto_update_content']); } /** * Check if duplicate URLs should be grouped for efficient downloading * * @since 1.0.0 * @return bool */ public function should_group_duplicate_urls() { return $this->get('group_duplicate_urls', $this->defaults['group_duplicate_urls']); } /** * Get post types to scan * * @since 1.0.0 * @return array */ public function get_scan_post_types() { return array_values(array_filter(array_map('sanitize_key', (array) apply_filters('snaggly_scan_post_types', $this->get('scan_post_types', $this->defaults['scan_post_types']))))); } /** * Check if original URLs should be preserved * * @since 1.0.0 * @return bool */ public function should_preserve_original_urls() { return $this->get('preserve_original_urls', $this->defaults['preserve_original_urls']); } /** * Get retry attempts * * @since 1.0.0 * @return int */ public function get_retry_attempts() { return $this->get('retry_attempts', $this->defaults['retry_attempts']); } /** * Get file naming convention * * @since 1.0.0 * @return string */ public function get_file_naming_convention() { return $this->get('file_naming_convention', $this->defaults['file_naming_convention']); } /** * Get logging level * * @since 1.0.0 * @return string */ public function get_logging_level() { return $this->get('logging_level', $this->defaults['logging_level']); } /** * Get user agent string for HTTP requests * * @since 1.0.0 * @return string */ public function get_user_agent() { $user_agent_type = $this->get('user_agent', $this->defaults['user_agent']); $user_agents = array( 'default' => 'Snaggly Plugin/' . SNAGGLY_VERSION . ' (WordPress)', 'browser_chrome' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', 'browser_firefox' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', 'browser_safari' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Safari/605.1.15', 'browser_edge' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', 'googlebot' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 'curl' => 'curl/8.4.0', ); if ($user_agent_type === 'custom') { $custom = $this->get('custom_user_agent', ''); return !empty($custom) ? $custom : $user_agents['default']; } return isset($user_agents[$user_agent_type]) ? $user_agents[$user_agent_type] : $user_agents['default']; } /** * Get available user agent options for settings UI * * @since 1.0.0 * @return array */ public function get_user_agent_options() { return array( 'default' => __('Snaggly Plugin (default)', 'snaggly'), 'browser_chrome' => __('Chrome Browser', 'snaggly'), 'browser_firefox' => __('Firefox Browser', 'snaggly'), 'browser_safari' => __('Safari Browser', 'snaggly'), 'browser_edge' => __('Edge Browser', 'snaggly'), 'googlebot' => __('Googlebot', 'snaggly'), 'curl' => __('cURL', 'snaggly'), 'custom' => __('Custom User Agent', 'snaggly'), ); } /** * Check if excerpts should be scanned * * @since 1.0.0 * @return bool */ public function should_scan_excerpts() { return $this->get('scan_excerpts', false); } /** * Check if custom fields should be scanned * * @since 1.0.0 * @return bool */ public function should_scan_custom_fields() { return $this->get('scan_custom_fields', false); } /** * Check if private posts should be scanned * * @since 1.0.0 * @return bool */ public function should_scan_private_posts() { return $this->get('scan_private_posts', $this->defaults['scan_private_posts']); } /** * Get excluded custom fields * * @since 1.0.0 * @return array */ public function get_excluded_custom_fields() { return $this->get('excluded_custom_fields', array()); } /** * Get batch size * * @since 1.0.0 * @return int */ public function get_batch_size() { return $this->get('batch_size', $this->defaults['batch_size']); } /** * Check if progressive scan is enabled * * @since 1.0.0 * @return bool */ public function is_progressive_scan_enabled() { return $this->get('enable_progressive_scan', $this->defaults['enable_progressive_scan']); } /** * Check if rate limiting is enabled * * @since 1.6.0 * @return bool */ public function is_rate_limiting_enabled() { return $this->get('rate_limiting_enabled', $this->defaults['rate_limiting_enabled']); } /** * Get setting (alias for backward compatibility) * * @since 1.0.0 * @param string $key Setting key to retrieve. * @param mixed $default Optional. Default value if setting doesn't exist. Default null. * @return mixed */ public function get_setting($key, $default = null) { return $this->get($key, $default); } /** * Validate settings * * @since 1.0.0 * @param array $input Raw settings input. * @return array Validated settings. */ public static function sanitize_input($input) { if (!is_array($input)) { return array(); } $clean = array(); foreach ($input as $key => $value) { if (!is_string($key) || sanitize_key($key) !== $key) { continue; } if (is_array($value)) { if (!in_array($key, array('allowed_extensions', 'scan_post_types', 'excluded_custom_fields', 'rate_limit_whitelist_ips', 'rate_limit_whitelist_users'), true)) { continue; } $clean[$key] = array_map('sanitize_text_field', array_values(array_filter($value, 'is_scalar'))); } elseif (is_scalar($value)) { $clean[$key] = sanitize_text_field((string) $value); } } return $clean; } public function validate_settings($input) { $input = self::sanitize_input($input); $validated = array(); $this->validate_allowed_extensions($input, $validated); $this->validate_numeric_settings($input, $validated); $this->validate_enum_settings($input, $validated); $this->validate_text_settings($input, $validated); $this->validate_boolean_settings($input, $validated); $this->validate_list_settings($input, $validated); // Merge with existing settings so fields not present in $input // (e.g. advanced settings without a UI control) are preserved. return apply_filters('snaggly_validate_settings', wp_parse_args($validated, $this->settings), $input); } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_allowed_extensions($input, array &$validated) { if (!isset($input['allowed_extensions'])) { return; } $raw = $input['allowed_extensions']; $raw_list = is_string($raw) ? explode(',', $raw) : (is_array($raw) ? $raw : array()); $extensions = array_map(function ($ext) { $ext = is_string($ext) ? $ext : ''; return strtolower(preg_replace('/[^a-z0-9]/i', '', $ext)); }, $raw_list); $extensions = array_values(array_unique(array_filter($extensions))); $validated['allowed_extensions'] = !empty($extensions) ? $extensions : $this->defaults['allowed_extensions']; } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_numeric_settings($input, array &$validated) { $ranges = array( 'max_file_size' => array(0, 104857600), // up to 100MB 'download_timeout' => array(5, 300), 'concurrent_downloads' => array(1, 10), 'retry_attempts' => array(0, 5), 'batch_size' => array(1, 100), 'large_site_threshold' => array(10, 100000), 'rate_limit_requests_per_minute' => array(1, 6000), ); foreach ($ranges as $key => list($min, $max)) { if (isset($input[$key])) { $value = absint($input[$key]); $validated[$key] = max($min, min($value, $max)); } } } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_enum_settings($input, array &$validated) { $enums = array( 'file_naming_convention' => array('original', 'sanitized', 'hash'), 'user_agent' => array('default', 'browser_chrome', 'browser_firefox', 'browser_safari', 'browser_edge', 'googlebot', 'curl', 'custom'), 'logging_level' => array('none', 'errors', 'all'), ); foreach ($enums as $key => $allowed_values) { if (!isset($input[$key])) { continue; } $validated[$key] = in_array($input[$key], $allowed_values, true) ? $input[$key] : $this->defaults[$key]; } } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_text_settings($input, array &$validated) { if (isset($input['custom_user_agent'])) { $validated['custom_user_agent'] = substr(sanitize_text_field($input['custom_user_agent']), 0, 500); } } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_boolean_settings($input, array &$validated) { $boolean_settings = array( 'auto_update_content', 'preserve_original_urls', 'group_duplicate_urls', 'scan_private_posts', 'enable_progressive_scan', 'rate_limiting_enabled', ); foreach ($boolean_settings as $setting) { // These correspond to checkboxes: an unchecked checkbox is // simply absent from $input, so treat "not present" as false. $validated[$setting] = isset($input[$setting]) ? (bool) $input[$setting] : false; } } /** * @param array $input * @param array $validated Passed by reference. */ private function validate_list_settings($input, array &$validated) { if (isset($input['scan_post_types']) && is_array($input['scan_post_types'])) { $validated['scan_post_types'] = array_values(array_filter(array_map('sanitize_key', $input['scan_post_types']))); } elseif (array_key_exists('scan_post_types', $input)) { // Present but not an array (e.g. no checkboxes checked): clear it. $validated['scan_post_types'] = array(); } } /** * Get large site threshold * * @return int */ public function get_large_site_threshold() { return $this->get('large_site_threshold', $this->defaults['large_site_threshold']); } /** * Check if auto progressive scan is enabled for large sites * * @return bool */ public function get_auto_progressive_for_large_sites() { return $this->get('auto_progressive_for_large_sites', $this->defaults['auto_progressive_for_large_sites']); } /** * Get memory usage threshold * * @return float */ public function get_memory_threshold() { return $this->get('memory_threshold', $this->defaults['memory_threshold']); } /** * Get maximum scan time * * @return int */ public function get_max_scan_time() { return $this->get('max_scan_time', $this->defaults['max_scan_time']); } /** * Check if performance metrics logging is enabled * * @return bool */ public function should_log_performance_metrics() { return $this->get('log_performance_metrics', $this->defaults['log_performance_metrics']); } }