| 1 |
<?php |
| 2 |
/** |
| 3 |
* Metasync_Minification_Settings |
| 4 |
* Manages settings for the Code Minification & Delivery module. |
| 5 |
* |
| 6 |
* @package Search Atlas SEO |
| 7 |
* @copyright Copyright (C) 2021-2025, Search Atlas Group - support@searchatlas.com |
| 8 |
* @since 2.5.23 |
| 9 |
*/ |
| 10 |
|
| 11 |
if (!defined('ABSPATH')) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
class Metasync_Minification_Settings { |
| 16 |
|
| 17 |
const OPTION_KEY = 'metasync_code_minification'; |
| 18 |
|
| 19 |
private static $defaults = [ |
| 20 |
// CSS Minification |
| 21 |
'enable_css_minify' => false, |
| 22 |
'css_exclude_handles' => '', |
| 23 |
|
| 24 |
// JS Minification |
| 25 |
'enable_js_minify' => false, |
| 26 |
'js_exclude_handles' => '', |
| 27 |
|
| 28 |
// JS Defer |
| 29 |
'enable_js_defer' => false, |
| 30 |
'js_defer_exclude_handles' => 'jquery,jquery-core,jquery-migrate', |
| 31 |
|
| 32 |
// JS Delay |
| 33 |
'enable_js_delay' => false, |
| 34 |
'js_delay_handles' => '', |
| 35 |
'js_delay_patterns' => 'facebook.net,google-analytics.com,googletagmanager.com,hotjar.com,connect.facebook.net', |
| 36 |
|
| 37 |
// Cache |
| 38 |
'cache_ttl_days' => 30, |
| 39 |
]; |
| 40 |
|
| 41 |
/** |
| 42 |
* Get merged settings with defaults. |
| 43 |
*/ |
| 44 |
public static function get_settings(): array { |
| 45 |
$saved = get_option(self::OPTION_KEY, []); |
| 46 |
return wp_parse_args($saved, self::$defaults); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get default settings. |
| 51 |
*/ |
| 52 |
public static function get_defaults(): array { |
| 53 |
return self::$defaults; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Save settings with sanitization. |
| 58 |
*/ |
| 59 |
public static function save_settings(array $input): bool { |
| 60 |
$sanitized = self::sanitize($input); |
| 61 |
return update_option(self::OPTION_KEY, $sanitized); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Sanitize and validate settings input. |
| 66 |
*/ |
| 67 |
public static function sanitize(array $input): array { |
| 68 |
return [ |
| 69 |
// CSS Minification |
| 70 |
'enable_css_minify' => !empty($input['enable_css_minify']), |
| 71 |
'css_exclude_handles' => sanitize_text_field($input['css_exclude_handles'] ?? ''), |
| 72 |
|
| 73 |
// JS Minification |
| 74 |
'enable_js_minify' => !empty($input['enable_js_minify']), |
| 75 |
'js_exclude_handles' => sanitize_text_field($input['js_exclude_handles'] ?? ''), |
| 76 |
|
| 77 |
// JS Defer |
| 78 |
'enable_js_defer' => !empty($input['enable_js_defer']), |
| 79 |
'js_defer_exclude_handles' => sanitize_text_field($input['js_defer_exclude_handles'] ?? 'jquery,jquery-core,jquery-migrate'), |
| 80 |
|
| 81 |
// JS Delay |
| 82 |
'enable_js_delay' => !empty($input['enable_js_delay']), |
| 83 |
'js_delay_handles' => sanitize_text_field($input['js_delay_handles'] ?? ''), |
| 84 |
'js_delay_patterns' => sanitize_text_field($input['js_delay_patterns'] ?? ''), |
| 85 |
|
| 86 |
// Cache |
| 87 |
'cache_ttl_days' => max(1, min(365, (int) ($input['cache_ttl_days'] ?? 30))), |
| 88 |
]; |
| 89 |
} |
| 90 |
} |
| 91 |
|