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