| 1 |
<?php |
| 2 |
/** |
| 3 |
* Metasync_Media_Settings |
| 4 |
* Manages settings for the Media Optimization module. |
| 5 |
* Settings are stored in the plugin's unified option system. |
| 6 |
* |
| 7 |
* @package Search Atlas SEO |
| 8 |
* @copyright Copyright (C) 2021-2025, Search Atlas Group - support@searchatlas.com |
| 9 |
* @since 2.6.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
if (!defined('ABSPATH')) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
class Metasync_Media_Settings { |
| 17 |
|
| 18 |
const OPTION_KEY = 'metasync_media_optimization'; |
| 19 |
|
| 20 |
private static $defaults = [ |
| 21 |
// Image Conversion |
| 22 |
'enable_conversion' => false, |
| 23 |
'conversion_format' => 'webp', // 'webp' or 'avif' |
| 24 |
'conversion_quality' => 82, |
| 25 |
'conversion_strategy' => 'alongside', // 'replace' or 'alongside' |
| 26 |
'convert_existing_sizes' => true, |
| 27 |
'max_image_dimensions' => 2560, // Max width/height (px) before conversion; 0 disables downscaling |
| 28 |
// Lazy Loading |
| 29 |
'enable_lazy_loading' => false, |
| 30 |
'lazy_load_iframes' => true, |
| 31 |
'lcp_skip_count' => 2, |
| 32 |
// Dimension Injection |
| 33 |
'enable_dimension_injection' => false, |
| 34 |
// Exclusions |
| 35 |
'exclude_classes' => '', // Comma-separated CSS classes to exclude |
| 36 |
'exclude_urls' => '', // Comma-separated URL patterns to exclude |
| 37 |
]; |
| 38 |
|
| 39 |
/** |
| 40 |
* Get merged settings with defaults. |
| 41 |
*/ |
| 42 |
public static function get_settings(): array { |
| 43 |
$saved = get_option(self::OPTION_KEY, []); |
| 44 |
return wp_parse_args($saved, self::$defaults); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get default settings. |
| 49 |
*/ |
| 50 |
public static function get_defaults(): array { |
| 51 |
return self::$defaults; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Save settings with sanitization. |
| 56 |
*/ |
| 57 |
public static function save_settings(array $input): bool { |
| 58 |
$sanitized = self::sanitize($input); |
| 59 |
return update_option(self::OPTION_KEY, $sanitized); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Sanitize and validate settings input. |
| 64 |
*/ |
| 65 |
public static function sanitize(array $input): array { |
| 66 |
return [ |
| 67 |
'enable_conversion' => !empty($input['enable_conversion']), |
| 68 |
'conversion_format' => in_array($input['conversion_format'] ?? '', ['webp', 'avif'], true) ? $input['conversion_format'] : 'webp', |
| 69 |
'conversion_quality' => max(1, min(100, (int) ($input['conversion_quality'] ?? 82))), |
| 70 |
'conversion_strategy' => in_array($input['conversion_strategy'] ?? '', ['replace', 'alongside'], true) ? $input['conversion_strategy'] : 'alongside', |
| 71 |
'convert_existing_sizes' => !empty($input['convert_existing_sizes']), |
| 72 |
'max_image_dimensions' => max(0, min(10000, (int) ($input['max_image_dimensions'] ?? 2560))), |
| 73 |
'enable_lazy_loading' => !empty($input['enable_lazy_loading']), |
| 74 |
'lazy_load_iframes' => !empty($input['lazy_load_iframes']), |
| 75 |
'lcp_skip_count' => max(0, min(10, (int) ($input['lcp_skip_count'] ?? 2))), |
| 76 |
'enable_dimension_injection' => !empty($input['enable_dimension_injection']), |
| 77 |
'exclude_classes' => sanitize_text_field($input['exclude_classes'] ?? ''), |
| 78 |
'exclude_urls' => sanitize_text_field($input['exclude_urls'] ?? ''), |
| 79 |
]; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check if the server supports AVIF conversion. |
| 84 |
*/ |
| 85 |
public static function supports_avif(): bool { |
| 86 |
if (extension_loaded('imagick')) { |
| 87 |
try { |
| 88 |
$formats = \Imagick::queryFormats('AVIF'); |
| 89 |
return !empty($formats); |
| 90 |
} catch (\Exception $e) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
} |
| 94 |
if (function_exists('gd_info')) { |
| 95 |
$info = gd_info(); |
| 96 |
return !empty($info['AVIF Support']); |
| 97 |
} |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if the server supports WebP conversion. |
| 103 |
*/ |
| 104 |
public static function supports_webp(): bool { |
| 105 |
if (extension_loaded('imagick')) { |
| 106 |
try { |
| 107 |
$formats = \Imagick::queryFormats('WEBP'); |
| 108 |
return !empty($formats); |
| 109 |
} catch (\Exception $e) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
} |
| 113 |
if (function_exists('gd_info')) { |
| 114 |
$info = gd_info(); |
| 115 |
return !empty($info['WebP Support']); |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get server capability info for display on admin page. |
| 122 |
*/ |
| 123 |
public static function get_server_capabilities(): array { |
| 124 |
return [ |
| 125 |
'imagick' => extension_loaded('imagick'), |
| 126 |
'gd' => extension_loaded('gd'), |
| 127 |
'webp_support' => self::supports_webp(), |
| 128 |
'avif_support' => self::supports_avif(), |
| 129 |
'has_library' => extension_loaded('imagick') || extension_loaded('gd'), |
| 130 |
]; |
| 131 |
} |
| 132 |
} |
| 133 |
|