| 1 |
<?php |
| 2 |
|
| 3 |
use Imagify\Traits\InstanceGetterTrait; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class that handles the plugin options. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
*/ |
| 10 |
class Imagify_Options extends Imagify_Abstract_Options { |
| 11 |
use InstanceGetterTrait; |
| 12 |
|
| 13 |
/** |
| 14 |
* Suffix used in the name of the option. |
| 15 |
* |
| 16 |
* @var string |
| 17 |
* @since 1.7 |
| 18 |
*/ |
| 19 |
protected $identifier = 'settings'; |
| 20 |
|
| 21 |
/** |
| 22 |
* The default values for the Imagify main options. |
| 23 |
* These are the "zero state" values. |
| 24 |
* Don't use null as value. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
* @since 1.7 |
| 28 |
*/ |
| 29 |
protected $default_values = [ |
| 30 |
'api_key' => '', |
| 31 |
'optimization_level' => 2, |
| 32 |
'lossless' => 0, |
| 33 |
'auto_optimize' => 0, |
| 34 |
'backup' => 0, |
| 35 |
'resize_larger' => 0, |
| 36 |
'resize_larger_w' => 0, |
| 37 |
'display_nextgen' => 0, |
| 38 |
'display_nextgen_method' => 'picture', |
| 39 |
'display_webp' => 0, |
| 40 |
'display_webp_method' => 'picture', |
| 41 |
'cdn_url' => '', |
| 42 |
'disallowed-sizes' => [], |
| 43 |
'admin_bar_menu' => 0, |
| 44 |
'partner_links' => 0, |
| 45 |
'convert_to_avif' => 0, |
| 46 |
'convert_to_webp' => 0, |
| 47 |
'optimization_format' => 'webp', |
| 48 |
]; |
| 49 |
|
| 50 |
/** |
| 51 |
* The Imagify main option values used when they are set the first time or reset. |
| 52 |
* Values identical to default values are not listed. |
| 53 |
* |
| 54 |
* @var array |
| 55 |
* @since 1.7 |
| 56 |
*/ |
| 57 |
protected $reset_values = [ |
| 58 |
'optimization_level' => 2, |
| 59 |
'auto_optimize' => 1, |
| 60 |
'backup' => 1, |
| 61 |
'admin_bar_menu' => 1, |
| 62 |
'partner_links' => 1, |
| 63 |
]; |
| 64 |
|
| 65 |
/** |
| 66 |
* The constructor. |
| 67 |
* Side note: $this->hook_identifier value is "option". |
| 68 |
* |
| 69 |
* @since 1.7 |
| 70 |
*/ |
| 71 |
protected function __construct() { |
| 72 |
if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) { |
| 73 |
$this->default_values['api_key'] = (string) IMAGIFY_API_KEY; |
| 74 |
} |
| 75 |
|
| 76 |
if ( function_exists( 'wp_get_original_image_path' ) ) { |
| 77 |
$this->reset_values['resize_larger'] = 1; |
| 78 |
|
| 79 |
$filter_cb = [ imagify_get_context( 'wp' ), 'get_resizing_threshold' ]; |
| 80 |
$filtered = has_filter( 'big_image_size_threshold', $filter_cb ); |
| 81 |
|
| 82 |
if ( $filtered ) { |
| 83 |
remove_filter( 'big_image_size_threshold', $filter_cb, IMAGIFY_INT_MAX ); |
| 84 |
} |
| 85 |
|
| 86 |
/** This filter is documented in wp-admin/includes/image.php */ |
| 87 |
$this->reset_values['resize_larger_w'] = (int) apply_filters( 'big_image_size_threshold', 2560, [ 0, 0 ], '', 0 ); |
| 88 |
$this->reset_values['resize_larger_w'] = $this->sanitize_and_validate_value( 'resize_larger_w', $this->reset_values['resize_larger_w'], $this->default_values['resize_larger_w'] ); |
| 89 |
|
| 90 |
if ( $filtered ) { |
| 91 |
add_filter( 'big_image_size_threshold', $filter_cb, IMAGIFY_INT_MAX ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$this->network_option = imagify_is_active_for_network(); |
| 96 |
|
| 97 |
parent::__construct(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Sanitize and validate an option value. Basic casts have been made. |
| 102 |
* |
| 103 |
* @since 1.7 |
| 104 |
* |
| 105 |
* @param string $key The option key. |
| 106 |
* @param mixed $value The value. |
| 107 |
* @param mixed $default The default value. |
| 108 |
* @return mixed |
| 109 |
*/ |
| 110 |
public function sanitize_and_validate_value( $key, $value, $default ) { |
| 111 |
static $max_sizes; |
| 112 |
|
| 113 |
switch ( $key ) { |
| 114 |
case 'api_key': |
| 115 |
if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) { |
| 116 |
return (string) IMAGIFY_API_KEY; |
| 117 |
} |
| 118 |
return $value ? sanitize_key( $value ) : ''; |
| 119 |
|
| 120 |
case 'optimization_level': |
| 121 |
if ( $value < 0 || $value > 2 ) { |
| 122 |
// For an invalid value, return the "reset" value. |
| 123 |
$reset_values = $this->get_reset_values(); |
| 124 |
return $reset_values[ $key ]; |
| 125 |
} |
| 126 |
return $value; |
| 127 |
case 'optimization_format': |
| 128 |
if ( ! in_array( $value, [ 'off', 'webp', 'avif' ], true ) ) { |
| 129 |
// For an invalid value, return the "reset" value. |
| 130 |
$reset_values = $this->get_reset_values(); |
| 131 |
return $reset_values[ $key ]; |
| 132 |
} |
| 133 |
return $value; |
| 134 |
case 'auto_optimize': |
| 135 |
case 'backup': |
| 136 |
case 'lossless': |
| 137 |
case 'resize_larger': |
| 138 |
case 'convert_to_webp': |
| 139 |
case 'display_nextgen': |
| 140 |
case 'display_webp': |
| 141 |
case 'admin_bar_menu': |
| 142 |
case 'partner_links': |
| 143 |
case 'convert_to_avif': |
| 144 |
return empty( $value ) ? 0 : 1; |
| 145 |
|
| 146 |
case 'resize_larger_w': |
| 147 |
if ( $value <= 0 ) { |
| 148 |
// Invalid. |
| 149 |
return $default; |
| 150 |
} |
| 151 |
if ( ! isset( $max_sizes ) ) { |
| 152 |
$max_sizes = get_imagify_max_intermediate_image_size(); |
| 153 |
} |
| 154 |
if ( $value < $max_sizes['width'] ) { |
| 155 |
// Invalid. |
| 156 |
return $max_sizes['width']; |
| 157 |
} |
| 158 |
return $value; |
| 159 |
|
| 160 |
case 'disallowed-sizes': |
| 161 |
if ( ! $value ) { |
| 162 |
return $default; |
| 163 |
} |
| 164 |
|
| 165 |
$value = array_keys( $value ); |
| 166 |
$value = array_map( 'sanitize_text_field', $value ); |
| 167 |
return array_fill_keys( $value, 1 ); |
| 168 |
|
| 169 |
case 'display_nextgen_method': |
| 170 |
case 'display_webp_method': |
| 171 |
$values = [ |
| 172 |
'picture' => 1, |
| 173 |
'rewrite' => 1, |
| 174 |
]; |
| 175 |
if ( isset( $values[ $value ] ) ) { |
| 176 |
return $value; |
| 177 |
} |
| 178 |
// For an invalid value, return the "reset" value. |
| 179 |
$reset_values = $this->get_reset_values(); |
| 180 |
return $reset_values[ $key ]; |
| 181 |
|
| 182 |
case 'cdn_url': |
| 183 |
$cdn_source = apply_filters( 'imagify_cdn_source_url', $value ); |
| 184 |
|
| 185 |
if ( 'option' !== $cdn_source['source'] ) { |
| 186 |
/** |
| 187 |
* If the URL is defined via constant or filter, unset the option. |
| 188 |
* This is useful when the CDN is disabled: there is no need to do anything then. |
| 189 |
*/ |
| 190 |
return ''; |
| 191 |
} |
| 192 |
|
| 193 |
return $cdn_source['url']; |
| 194 |
} |
| 195 |
|
| 196 |
return false; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Validate Imagify's options before storing them. Basic sanitization and validation have been made, row by row. |
| 201 |
* |
| 202 |
* @since 1.7 |
| 203 |
* |
| 204 |
* @param string $values The option value. |
| 205 |
* @return array |
| 206 |
*/ |
| 207 |
public function validate_values_on_update( $values ) { |
| 208 |
// The max width for the "Resize larger images" option can't be 0. |
| 209 |
if ( empty( $values['resize_larger_w'] ) ) { |
| 210 |
unset( $values['resize_larger'], $values['resize_larger_w'] ); |
| 211 |
} |
| 212 |
|
| 213 |
return $values; |
| 214 |
} |
| 215 |
} |
| 216 |
|