options = get_option( AWVB_SETTINGS_KEY, array() ); } /** * Register settings with WordPress Settings API. */ public function register_settings() { register_setting( 'awvb_settings_group', AWVB_SETTINGS_KEY, array( 'type' => 'array', 'description' => 'Ahtesham Web Vitals Booster settings', 'sanitize_callback' => array( $this, 'sanitize_settings' ), 'show_in_rest' => false, 'default' => self::get_default_settings( 'safe' ), ) ); } /** * Retrieve a single setting value. * * @param string $key Setting key. * @param mixed $default Fallback value if setting not found. * @return mixed */ public function get( $key, $default = false ) { if ( null === $this->options ) { $this->options = get_option( AWVB_SETTINGS_KEY, array() ); } if ( isset( $this->options[ $key ] ) ) { return $this->options[ $key ]; } $defaults = self::get_default_settings( 'safe' ); return isset( $defaults[ $key ] ) ? $defaults[ $key ] : $default; } /** * Retrieve all active settings. * * @return array */ public function get_all() { if ( null === $this->options ) { $this->options = get_option( AWVB_SETTINGS_KEY, array() ); } return wp_parse_args( $this->options, self::get_default_settings( 'safe' ) ); } /** * Update settings directly (used by AJAX or presets). * * @param array $new_settings New settings array. * @return bool */ public function update( $new_settings ) { $sanitized = $this->sanitize_settings( $new_settings ); $this->options = $sanitized; return update_option( AWVB_SETTINGS_KEY, $sanitized ); } /** * Default presets: Safe (zero-risk) vs Balanced (high-performance). * * @param string $preset 'safe' or 'balanced'. * @return array */ public static function get_default_settings( $preset = 'safe' ) { $common = array( 'preset' => $preset, // Bloat Removal 'disable_emojis' => true, 'disable_embeds' => true, 'remove_query_strings' => true, 'heartbeat_control' => 'modify_frequency', // 'default', 'disable_frontend', 'modify_frequency' 'heartbeat_frequency' => 60, // 15 - 120 seconds // Images & CLS 'lazy_load_images' => true, 'lazy_load_exclude_count' => 2, // Protect top 2 hero images for LCP 'add_missing_dimensions' => true, // Injects width/height to fix CLS // CSS & Fonts 'optimize_google_fonts' => true, // font-display: swap 'minify_html' => false, // Safe default // Caching 'browser_caching' => true, // Exclusions 'excluded_urls' => "/cart/\n/checkout/\n/my-account/", 'excluded_js' => "jquery\njquery.min.js\nwp-includes", 'delayed_js_scripts' => "gtag\ngtm.js\nanalytics.js\nrecaptcha", // System 'clean_on_uninstall' => false, ); if ( 'balanced' === $preset ) { return array_merge( $common, array( 'preset' => 'balanced', 'defer_js' => true, 'delay_js' => true, 'minify_html' => true, ) ); } // 'safe' preset defaults return array_merge( $common, array( 'preset' => 'safe', 'defer_js' => true, 'delay_js' => false, // Conservative off by default in safe mode 'minify_html' => false, ) ); } /** * Strict sanitization and validation for all plugin settings. * * @param array $input Raw input data from form or AJAX. * @return array Sanitized settings. */ public function sanitize_settings( $input ) { if ( ! is_array( $input ) ) { return self::get_default_settings( 'safe' ); } $output = array(); // Preset $valid_presets = array( 'safe', 'balanced', 'custom' ); $output['preset'] = ( isset( $input['preset'] ) && in_array( $input['preset'], $valid_presets, true ) ) ? $input['preset'] : 'custom'; // Bloat Removal (booleans) $output['disable_emojis'] = ! empty( $input['disable_emojis'] ); $output['disable_embeds'] = ! empty( $input['disable_embeds'] ); $output['remove_query_strings'] = ! empty( $input['remove_query_strings'] ); // Heartbeat Control $valid_heartbeat = array( 'default', 'disable_frontend', 'modify_frequency' ); $output['heartbeat_control'] = ( isset( $input['heartbeat_control'] ) && in_array( $input['heartbeat_control'], $valid_heartbeat, true ) ) ? $input['heartbeat_control'] : 'modify_frequency'; $freq = isset( $input['heartbeat_frequency'] ) ? absint( $input['heartbeat_frequency'] ) : 60; $output['heartbeat_frequency'] = max( 15, min( 120, $freq ) ); // Images & CLS $output['lazy_load_images'] = ! empty( $input['lazy_load_images'] ); $exclude_count = isset( $input['lazy_load_exclude_count'] ) ? absint( $input['lazy_load_exclude_count'] ) : 2; $output['lazy_load_exclude_count'] = max( 0, min( 10, $exclude_count ) ); $output['add_missing_dimensions'] = ! empty( $input['add_missing_dimensions'] ); // JavaScript $output['defer_js'] = ! empty( $input['defer_js'] ); $output['delay_js'] = ! empty( $input['delay_js'] ); // CSS & HTML $output['optimize_google_fonts'] = ! empty( $input['optimize_google_fonts'] ); $output['minify_html'] = ! empty( $input['minify_html'] ); // Browser Caching $output['browser_caching'] = ! empty( $input['browser_caching'] ); // Textarea lists (Clean line breaks and trim) $output['excluded_urls'] = isset( $input['excluded_urls'] ) ? $this->sanitize_textarea_list( $input['excluded_urls'] ) : ''; $output['excluded_js'] = isset( $input['excluded_js'] ) ? $this->sanitize_textarea_list( $input['excluded_js'] ) : ''; $output['delayed_js_scripts'] = isset( $input['delayed_js_scripts'] ) ? $this->sanitize_textarea_list( $input['delayed_js_scripts'] ) : ''; // System $output['clean_on_uninstall'] = ! empty( $input['clean_on_uninstall'] ); return $output; } /** * Sanitize textarea list (urls, script keywords). * * @param string $text Raw textarea input. * @return string Sanitized lines separated by newline. */ private function sanitize_textarea_list( $text ) { $lines = explode( "\n", str_replace( "\r", '', (string) $text ) ); $clean = array(); foreach ( $lines as $line ) { $trimmed = trim( sanitize_text_field( $line ) ); if ( '' !== $trimmed ) { $clean[] = $trimmed; } } return implode( "\n", array_unique( $clean ) ); } }