| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class Optml_Settings. |
| 5 |
*/ |
| 6 |
class Optml_Settings { |
| 7 |
use Optml_Normalizer; |
| 8 |
const FILTER_EXT = 'extension'; |
| 9 |
const FILTER_URL = 'page_url'; |
| 10 |
const FILTER_FILENAME = 'filename'; |
| 11 |
const FILTER_CLASS = 'class'; |
| 12 |
const FILTER_TYPE_LAZYLOAD = 'lazyload'; |
| 13 |
const FILTER_TYPE_OPTIMIZE = 'optimize'; |
| 14 |
/** |
| 15 |
* Holds an array of possible settings to alter via wp cli or wp-config constants. |
| 16 |
* |
| 17 |
* @var array Whitelisted settings. |
| 18 |
*/ |
| 19 |
public static $whitelisted_settings = [ |
| 20 |
'image_replacer' => 'bool', |
| 21 |
'quality' => 'int', |
| 22 |
'lazyload' => 'bool', |
| 23 |
'lazyload_placeholder' => 'bool', |
| 24 |
'network_optimization' => 'bool', |
| 25 |
'img_to_video' => 'bool', |
| 26 |
'resize_smart' => 'bool', |
| 27 |
'retina_images' => 'bool', |
| 28 |
'native_lazyload' => 'bool', |
| 29 |
'video_lazyload' => 'bool', |
| 30 |
'bg_replacer' => 'bool', |
| 31 |
'scale' => 'bool', |
| 32 |
'cdn' => 'bool', |
| 33 |
]; |
| 34 |
/** |
| 35 |
* Default settings schema. |
| 36 |
* |
| 37 |
* @var array Settings schema. |
| 38 |
*/ |
| 39 |
private $default_schema = [ |
| 40 |
'api_key' => '', |
| 41 |
'service_data' => '', |
| 42 |
'cache_buster' => '', |
| 43 |
'cdn' => 'disabled', |
| 44 |
'max_height' => 1500, |
| 45 |
'max_width' => 2000, |
| 46 |
'admin_bar_item' => 'enabled', |
| 47 |
'lazyload' => 'disabled', |
| 48 |
'scale' => 'disabled', |
| 49 |
'network_optimization' => 'disabled', |
| 50 |
'lazyload_placeholder' => 'disabled', |
| 51 |
'bg_replacer' => 'enabled', |
| 52 |
'video_lazyload' => 'disabled', |
| 53 |
'retina_images' => 'disabled', |
| 54 |
'resize_smart' => 'disabled', |
| 55 |
'filters' => [], |
| 56 |
'cloud_sites' => [ 'all' => 'true' ], |
| 57 |
'watchers' => '', |
| 58 |
'quality' => 'auto', |
| 59 |
'wm_id' => - 1, |
| 60 |
'wm_opacity' => 1, |
| 61 |
'wm_position' => Optml_Resize::GRAVITY_SOUTH_EAST, |
| 62 |
'wm_x' => 0, |
| 63 |
'wm_y' => 0, |
| 64 |
'wm_scale' => 0, |
| 65 |
'image_replacer' => 'enabled', |
| 66 |
'img_to_video' => 'disabled', |
| 67 |
'css_minify' => 'enabled', |
| 68 |
'js_minify' => 'disabled', |
| 69 |
'report_script' => 'disabled', |
| 70 |
'native_lazyload' => 'disabled', |
| 71 |
'offload_media' => 'disabled', |
| 72 |
'cloud_images' => 'disabled', |
| 73 |
|
| 74 |
]; |
| 75 |
/** |
| 76 |
* Option key. |
| 77 |
* |
| 78 |
* @var string Option name. |
| 79 |
*/ |
| 80 |
private $namespace; |
| 81 |
/** |
| 82 |
* Holds all options from db. |
| 83 |
* |
| 84 |
* @var array All options. |
| 85 |
*/ |
| 86 |
private $options; |
| 87 |
|
| 88 |
/** |
| 89 |
* Optml_Settings constructor. |
| 90 |
*/ |
| 91 |
public function __construct() { |
| 92 |
$this->namespace = OPTML_NAMESPACE . '_settings'; |
| 93 |
$this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema ); |
| 94 |
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); |
| 95 |
|
| 96 |
if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) { |
| 97 |
switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) ); |
| 98 |
$this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema ); |
| 99 |
restore_current_blog(); |
| 100 |
} |
| 101 |
|
| 102 |
if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) { |
| 103 |
foreach ( self::$whitelisted_settings as $key => $type ) { |
| 104 |
$env_key = 'OPTIML_' . strtoupper( $key ); |
| 105 |
if ( defined( $env_key ) && constant( $env_key ) ) { |
| 106 |
$value = constant( $env_key ); |
| 107 |
if ( $type === 'bool' && ( $value === '' || ! in_array( |
| 108 |
$value, |
| 109 |
[ |
| 110 |
'on', |
| 111 |
'off', |
| 112 |
], |
| 113 |
true |
| 114 |
) ) ) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
|
| 118 |
if ( $type === 'int' && ( $value === '' || (int) $value > 100 || (int) $value < 0 ) ) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
$sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value; |
| 122 |
$this->options[ $key ] = $sanitized_value; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Return filter definitions. |
| 130 |
* |
| 131 |
* @return mixed|null Filter values. |
| 132 |
*/ |
| 133 |
public function get_watchers() { |
| 134 |
|
| 135 |
return $this->get( 'watchers' ); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Return filter definitions. |
| 141 |
* |
| 142 |
* @return mixed|null Filter values. |
| 143 |
*/ |
| 144 |
public function get_filters() { |
| 145 |
|
| 146 |
$filters = $this->get( 'filters' ); |
| 147 |
if ( ! isset( $filters[ self::FILTER_TYPE_LAZYLOAD ] ) ) { |
| 148 |
$filters[ self::FILTER_TYPE_LAZYLOAD ] = []; |
| 149 |
} |
| 150 |
if ( ! isset( $filters[ self::FILTER_TYPE_OPTIMIZE ] ) ) { |
| 151 |
$filters[ self::FILTER_TYPE_OPTIMIZE ] = []; |
| 152 |
} |
| 153 |
foreach ( $filters as $filter_key => $filter_rules ) { |
| 154 |
if ( ! isset( $filter_rules[ self::FILTER_EXT ] ) ) { |
| 155 |
$filters[ $filter_key ][ self::FILTER_EXT ] = []; |
| 156 |
} |
| 157 |
if ( ! isset( $filter_rules[ self::FILTER_FILENAME ] ) ) { |
| 158 |
$filters[ $filter_key ][ self::FILTER_FILENAME ] = []; |
| 159 |
} |
| 160 |
if ( ! isset( $filter_rules[ self::FILTER_URL ] ) ) { |
| 161 |
$filters[ $filter_key ][ self::FILTER_URL ] = []; |
| 162 |
} |
| 163 |
if ( ! isset( $filter_rules[ self::FILTER_CLASS ] ) ) { |
| 164 |
$filters[ $filter_key ][ self::FILTER_CLASS ] = []; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return $filters; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Process settings. |
| 173 |
* |
| 174 |
* @param array $new_settings List of settings. |
| 175 |
* |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
public function parse_settings( $new_settings ) { |
| 179 |
$sanitized = []; |
| 180 |
foreach ( $new_settings as $key => $value ) { |
| 181 |
switch ( $key ) { |
| 182 |
case 'admin_bar_item': |
| 183 |
case 'lazyload': |
| 184 |
case 'scale': |
| 185 |
case 'image_replacer': |
| 186 |
case 'cdn': |
| 187 |
case 'network_optimization': |
| 188 |
case 'lazyload_placeholder': |
| 189 |
case 'retina_images': |
| 190 |
case 'resize_smart': |
| 191 |
case 'bg_replacer': |
| 192 |
case 'video_lazyload': |
| 193 |
case 'report_script': |
| 194 |
case 'offload_media': |
| 195 |
case 'cloud_images': |
| 196 |
case 'img_to_video': |
| 197 |
case 'css_minify': |
| 198 |
case 'js_minify': |
| 199 |
case 'native_lazyload': |
| 200 |
$sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' ); |
| 201 |
break; |
| 202 |
case 'max_width': |
| 203 |
case 'max_height': |
| 204 |
$sanitized_value = $this->to_bound_integer( $value, 100, 5000 ); |
| 205 |
break; |
| 206 |
case 'quality': |
| 207 |
$sanitized_value = $this->to_bound_integer( $value, 1, 100 ); |
| 208 |
break; |
| 209 |
case 'wm_id': |
| 210 |
$sanitized_value = intval( $value ); |
| 211 |
break; |
| 212 |
case 'cache_buster': |
| 213 |
$sanitized_value = is_string( $value ) ? $value : ''; |
| 214 |
break; |
| 215 |
case 'cloud_sites': |
| 216 |
$current_sites = $this->get( 'cloud_sites' ); |
| 217 |
$sanitized_value = array_replace_recursive( $current_sites, $value ); |
| 218 |
if ( isset( $value['all'] ) && $value['all'] === 'true' ) { |
| 219 |
$sanitized_value = [ 'all' => 'true' ]; |
| 220 |
} |
| 221 |
break; |
| 222 |
case 'filters': |
| 223 |
$current_filters = $this->get_filters(); |
| 224 |
$sanitized_value = array_replace_recursive( $current_filters, $value ); |
| 225 |
// Remove falsy vars. |
| 226 |
foreach ( $sanitized_value as $filter_type => $filter_values ) { |
| 227 |
foreach ( $filter_values as $filter_rule_type => $filter_rules_value ) { |
| 228 |
$sanitized_value[ $filter_type ][ $filter_rule_type ] = array_filter( |
| 229 |
$filter_rules_value, |
| 230 |
function ( $value ) { |
| 231 |
return ( $value !== 'false' && $value !== false ); |
| 232 |
} |
| 233 |
); |
| 234 |
} |
| 235 |
} |
| 236 |
break; |
| 237 |
case 'watchers': |
| 238 |
$sanitized_value = $value; |
| 239 |
break; |
| 240 |
case 'wm_opacity': |
| 241 |
case 'wm_scale': |
| 242 |
case 'wm_x': |
| 243 |
case 'wm_y': |
| 244 |
$sanitized_value = floatval( $value ); |
| 245 |
break; |
| 246 |
case 'wm_position': |
| 247 |
$sanitized_value = $this->to_map_values( |
| 248 |
$value, |
| 249 |
[ |
| 250 |
Optml_Resize::GRAVITY_NORTH, |
| 251 |
Optml_Resize::GRAVITY_NORTH_EAST, |
| 252 |
Optml_Resize::GRAVITY_NORTH_WEST, |
| 253 |
Optml_Resize::GRAVITY_CENTER, |
| 254 |
Optml_Resize::GRAVITY_EAST, |
| 255 |
Optml_Resize::GRAVITY_WEST, |
| 256 |
Optml_Resize::GRAVITY_SOUTH_EAST, |
| 257 |
Optml_Resize::GRAVITY_SOUTH, |
| 258 |
Optml_Resize::GRAVITY_SOUTH_WEST, |
| 259 |
], |
| 260 |
Optml_Resize::GRAVITY_SOUTH_EAST |
| 261 |
); |
| 262 |
break; |
| 263 |
default: |
| 264 |
$sanitized_value = ''; |
| 265 |
break; |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
$sanitized[ $key ] = $sanitized_value; |
| 270 |
$this->update( $key, $sanitized_value ); |
| 271 |
} |
| 272 |
|
| 273 |
return $sanitized; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Update settings. |
| 278 |
* |
| 279 |
* @param string $key Settings key. |
| 280 |
* @param mixed $value Settings value. |
| 281 |
* |
| 282 |
* @return bool Update result. |
| 283 |
*/ |
| 284 |
public function update( $key, $value ) { |
| 285 |
if ( ! $this->is_allowed( $key ) ) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
// If we try to update from a website which is not the main OPTML blog, bail. |
| 289 |
if ( defined( 'OPTIML_ENABLED_MU' ) && constant( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && constant( 'OPTIML_MU_SITE_ID' ) && |
| 290 |
intval( constant( 'OPTIML_MU_SITE_ID' ) ) !== get_current_blog_id() |
| 291 |
) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
$opt = $this->options; |
| 295 |
$opt[ $key ] = $value; |
| 296 |
$update = update_option( $this->namespace, $opt, false ); |
| 297 |
if ( $update ) { |
| 298 |
$this->options = $opt; |
| 299 |
} |
| 300 |
|
| 301 |
return $update; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Check if key is allowed. |
| 306 |
* |
| 307 |
* @param string $key Is key allowed or not. |
| 308 |
* |
| 309 |
* @return bool Is key allowed or not. |
| 310 |
*/ |
| 311 |
private function is_allowed( $key ) { |
| 312 |
return isset( $this->default_schema[ $key ] ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Check if the user is connected to Optimole. |
| 317 |
* |
| 318 |
* @return bool Connection status. |
| 319 |
*/ |
| 320 |
public function is_connected() { |
| 321 |
$service_data = $this->get( 'service_data' ); |
| 322 |
if ( ! isset( $service_data['cdn_key'] ) ) { |
| 323 |
return false; |
| 324 |
} |
| 325 |
if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) { |
| 326 |
return false; |
| 327 |
} |
| 328 |
|
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Get setting value by key. |
| 334 |
* |
| 335 |
* @param string $key Key to search against. |
| 336 |
* |
| 337 |
* @return mixed|null Setting value. |
| 338 |
*/ |
| 339 |
public function get( $key ) { |
| 340 |
if ( ! $this->is_allowed( $key ) ) { |
| 341 |
return null; |
| 342 |
} |
| 343 |
|
| 344 |
return isset( $this->options[ $key ] ) ? $this->options[ $key ] : ''; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Return site settings. |
| 349 |
* |
| 350 |
* @return array Site settings. |
| 351 |
*/ |
| 352 |
public function get_site_settings() { |
| 353 |
$service_data = $this->get( 'service_data' ); |
| 354 |
$whitelist = []; |
| 355 |
if ( isset( $service_data['whitelist'] ) ) { |
| 356 |
$whitelist = $service_data['whitelist']; |
| 357 |
} |
| 358 |
return [ |
| 359 |
'quality' => $this->get_quality(), |
| 360 |
'admin_bar_item' => $this->get( 'admin_bar_item' ), |
| 361 |
'lazyload' => $this->get( 'lazyload' ), |
| 362 |
'network_optimization' => $this->get( 'network_optimization' ), |
| 363 |
'retina_images' => $this->get( 'retina_images' ), |
| 364 |
'lazyload_placeholder' => $this->get( 'lazyload_placeholder' ), |
| 365 |
'bg_replacer' => $this->get( 'bg_replacer' ), |
| 366 |
'video_lazyload' => $this->get( 'video_lazyload' ), |
| 367 |
'resize_smart' => $this->get( 'resize_smart' ), |
| 368 |
'image_replacer' => $this->get( 'image_replacer' ), |
| 369 |
'cdn' => $this->get( 'cdn' ), |
| 370 |
'max_width' => $this->get( 'max_width' ), |
| 371 |
'max_height' => $this->get( 'max_height' ), |
| 372 |
'filters' => $this->get_filters(), |
| 373 |
'cloud_sites' => $this->get( 'cloud_sites' ), |
| 374 |
'watchers' => $this->get_watchers(), |
| 375 |
'watermark' => $this->get_watermark(), |
| 376 |
'img_to_video' => $this->get( 'img_to_video' ), |
| 377 |
'scale' => $this->get( 'scale' ), |
| 378 |
'css_minify' => $this->get( 'css_minify' ), |
| 379 |
'js_minify' => $this->get( 'js_minify' ), |
| 380 |
'native_lazyload' => $this->get( 'native_lazyload' ), |
| 381 |
'report_script' => $this->get( 'report_script' ), |
| 382 |
'offload_media' => $this->get( 'offload_media' ), |
| 383 |
'cloud_images' => $this->get( 'cloud_images' ), |
| 384 |
'whitelist_domains' => $whitelist, |
| 385 |
]; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Return quality factor. |
| 390 |
* |
| 391 |
* @return string Quality factor. |
| 392 |
*/ |
| 393 |
public function get_quality() { |
| 394 |
$quality = $this->get( 'quality' ); |
| 395 |
// Legacy compat. |
| 396 |
if ( $quality === 'low' ) { |
| 397 |
return 'high_c'; |
| 398 |
} |
| 399 |
if ( $quality === 'medium' ) { |
| 400 |
return 'medium_c'; |
| 401 |
} |
| 402 |
|
| 403 |
if ( $quality === 'high' ) { |
| 404 |
return 'low_c'; |
| 405 |
} |
| 406 |
|
| 407 |
return $quality; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Return an watermark array. |
| 412 |
* |
| 413 |
* @return array |
| 414 |
*/ |
| 415 |
public function get_watermark() { |
| 416 |
return [ |
| 417 |
'id' => $this->get( 'wm_id' ), |
| 418 |
'opacity' => $this->get( 'wm_opacity' ), |
| 419 |
'position' => $this->get( 'wm_position' ), |
| 420 |
'x_offset' => $this->get( 'wm_x' ), |
| 421 |
'y_offset' => $this->get( 'wm_y' ), |
| 422 |
'scale' => $this->get( 'wm_scale' ), |
| 423 |
]; |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* Check if smart cropping is enabled. |
| 428 |
* |
| 429 |
* @return bool Is smart cropping enabled. |
| 430 |
*/ |
| 431 |
public function is_smart_cropping() { |
| 432 |
return $this->get( 'resize_smart' ) === 'enabled'; |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Get numeric quality used by the service. |
| 437 |
* |
| 438 |
* @return int Numeric quality. |
| 439 |
*/ |
| 440 |
public function get_numeric_quality() { |
| 441 |
$value = $this->get_quality(); |
| 442 |
|
| 443 |
return (int) $this->to_accepted_quality( $value ); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Check if replacer is enabled. |
| 448 |
* |
| 449 |
* @return bool Replacer enabled |
| 450 |
*/ |
| 451 |
public function is_enabled() { |
| 452 |
$status = $this->get( 'image_replacer' ); |
| 453 |
$service_data = $this->get( 'service_data' ); |
| 454 |
if ( empty( $service_data ) ) { |
| 455 |
return false; |
| 456 |
} |
| 457 |
if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) { |
| 458 |
return false; |
| 459 |
} |
| 460 |
return $this->to_boolean( $status ); |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* Check if lazyload is enabled. |
| 465 |
* |
| 466 |
* @return bool Lazyload enabled |
| 467 |
*/ |
| 468 |
public function use_lazyload() { |
| 469 |
$status = $this->get( 'lazyload' ); |
| 470 |
|
| 471 |
return $this->to_boolean( $status ); |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Check if replacer is enabled. |
| 476 |
* |
| 477 |
* @return bool Replacer enabled |
| 478 |
*/ |
| 479 |
public function use_cdn() { |
| 480 |
$status = $this->get( 'cdn' ); |
| 481 |
|
| 482 |
return $this->to_boolean( $status ); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Return cdn url. |
| 487 |
* |
| 488 |
* @return string CDN url. |
| 489 |
*/ |
| 490 |
public function get_cdn_url() { |
| 491 |
$service_data = $this->get( 'service_data' ); |
| 492 |
if ( ! isset( $service_data['cdn_key'] ) ) { |
| 493 |
return ''; |
| 494 |
} |
| 495 |
if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) { |
| 496 |
return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST ); |
| 497 |
} |
| 498 |
|
| 499 |
return sprintf( |
| 500 |
'%s.%s', |
| 501 |
strtolower( $service_data['cdn_key'] ), |
| 502 |
Optml_Config::$base_domain |
| 503 |
); |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Reset options to defaults. |
| 508 |
* |
| 509 |
* @return bool Reset action status. |
| 510 |
*/ |
| 511 |
public function reset() { |
| 512 |
$reset_schema = $this->default_schema; |
| 513 |
$reset_schema['filters'] = $this->options['filters']; |
| 514 |
|
| 515 |
$update = update_option( $this->namespace, $reset_schema ); |
| 516 |
if ( $update ) { |
| 517 |
$this->options = $reset_schema; |
| 518 |
} |
| 519 |
|
| 520 |
return $update; |
| 521 |
} |
| 522 |
|
| 523 |
} |
| 524 |
|