PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
← All changes | inc/settings.php +264 -154 3.12.0 → trunk View file →
@@ -1,6 +1,8 @@
1 1 <?php
2 2
3 +use Optimole\Sdk\ValueObject\Position;
4 +
3 5 /**
4 6 * Class Optml_Settings.
5 7 */
6 8 class Optml_Settings {
@@ -13,8 +15,9 @@
13 15 const FILTER_CLASS = 'class';
14 16 const FILTER_TYPE_LAZYLOAD = 'lazyload';
15 17 const FILTER_TYPE_OPTIMIZE = 'optimize';
16 18 const OPTML_USER_EMAIL = 'optml_user_email';
19 + const INDIVIDUAL_CACHE_TOKENS_KEY = '_optml_cache_tokens_individual';
17 20 /**
18 21 * Holds an array of possible settings to alter via wp cli or wp-config constants.
19 22 *
20 23 * @var array Whitelisted settings.
@@ -47,58 +50,62 @@
47 50 *
48 51 * @var array Settings schema.
49 52 */
50 53 private $default_schema = [
51 - 'api_key' => '',
52 - 'service_data' => '',
53 - 'cache_buster' => '',
54 - 'cache_buster_assets' => '',
55 - 'cache_buster_images' => '',
56 - 'cdn' => 'disabled',
57 - 'admin_bar_item' => 'enabled',
58 - 'lazyload' => 'disabled',
59 - 'scale' => 'disabled',
60 - 'network_optimization' => 'disabled',
61 - 'lazyload_placeholder' => 'enabled',
62 - 'bg_replacer' => 'enabled',
63 - 'video_lazyload' => 'enabled',
64 - 'retina_images' => 'disabled',
65 - 'limit_dimensions' => 'enabled',
66 - 'limit_height' => 1080,
67 - 'limit_width' => 1920,
68 - 'resize_smart' => 'disabled',
69 - 'no_script' => 'disabled',
70 - 'filters' => [],
71 - 'cloud_sites' => [ 'all' => 'true' ],
72 - 'watchers' => '',
73 - 'quality' => 'auto',
74 - 'wm_id' => - 1,
75 - 'wm_opacity' => 1,
76 - 'wm_position' => Optml_Resize::GRAVITY_SOUTH_EAST,
77 - 'wm_x' => 0,
78 - 'wm_y' => 0,
79 - 'wm_scale' => 0,
80 - 'image_replacer' => 'enabled',
81 - 'img_to_video' => 'disabled',
82 - 'css_minify' => 'enabled',
83 - 'js_minify' => 'disabled',
84 - 'report_script' => 'disabled',
85 - 'avif' => 'enabled',
86 - 'autoquality' => 'enabled',
87 - 'native_lazyload' => 'disabled',
88 - 'offload_media' => 'disabled',
89 - 'cloud_images' => 'enabled',
90 - 'strip_metadata' => 'enabled',
91 - 'skip_lazyload_images' => 3,
92 - 'defined_image_sizes' => [ ],
93 - 'banner_frontend' => 'disabled',
94 - 'offloading_status' => 'disabled',
95 - 'rollback_status' => 'disabled',
96 - 'best_format' => 'enabled',
97 - 'offload_limit_reached' => 'disabled',
98 - 'offload_limit' => 50000,
99 - 'placeholder_color' => '',
100 - 'show_offload_finish_notice' => '',
54 + 'api_key' => '',
55 + 'service_data' => '',
56 + 'cache_buster' => '',
57 + 'cache_buster_assets' => '',
58 + 'cache_buster_images' => '',
59 + 'cdn' => 'disabled',
60 + 'admin_bar_item' => 'enabled',
61 + 'lazyload' => 'disabled',
62 + 'scale' => 'disabled', // Due to legacy reasons the disabled state means that the scale is enabled and the enabled state means that the scale is disabled.
63 + 'network_optimization' => 'enabled',
64 + 'lazyload_placeholder' => 'enabled',
65 + 'bg_replacer' => 'enabled',
66 + 'video_lazyload' => 'enabled',
67 + 'retina_images' => 'disabled',
68 + 'lazyload_type' => 'fixed|viewport',
69 + 'limit_dimensions' => 'enabled',
70 + 'limit_height' => 4000,
71 + 'limit_width' => 1920,
72 + 'resize_smart' => 'disabled',
73 + 'no_script' => 'disabled',
74 + 'filters' => [],
75 + 'compression_mode' => 'custom',
76 + 'cloud_sites' => [ 'all' => 'true' ],
77 + 'watchers' => '',
78 + 'quality' => 80,
79 + 'wm_id' => - 1,
80 + 'wm_opacity' => 1,
81 + 'wm_position' => Position::SOUTH_EAST,
82 + 'wm_x' => 0,
83 + 'wm_y' => 0,
84 + 'wm_scale' => 0,
85 + 'image_replacer' => 'enabled',
86 + 'img_to_video' => 'disabled',
87 + 'css_minify' => 'enabled',
88 + 'js_minify' => 'disabled',
89 + 'avif' => 'enabled', // legacy setting, is no longer used in the UI
90 + 'autoquality' => 'enabled',
91 + 'native_lazyload' => 'disabled',
92 + 'offload_media' => 'disabled',
93 + 'transfer_status' => 'disabled',
94 + 'cloud_images' => 'enabled',
95 + 'strip_metadata' => 'enabled',
96 + 'skip_lazyload_images' => 2,
97 + 'defined_image_sizes' => [],
98 + 'banner_frontend' => 'disabled',
99 + 'offloading_status' => 'disabled',
100 + 'rollback_status' => 'disabled',
101 + 'best_format' => 'enabled',
102 + 'offload_limit_reached' => 'disabled',
103 + 'offload_limit' => 50000,
104 + 'placeholder_color' => '',
105 + 'show_offload_finish_notice' => '',
106 + 'show_badge_icon' => 'disabled',
107 + 'badge_position' => 'left',
101 108 ];
102 109 /**
103 110 * Option key.
104 111 *
@@ -109,11 +116,18 @@
109 116 * Holds all options from db.
110 117 *
111 118 * @var array All options.
112 119 */
113 - private $options;
120 + private static $options;
114 121
115 122 /**
123 + * Cache for site settings to avoid multiple calls to get_option on the same request.
124 + *
125 + * @var array<string, mixed>|null Cached site settings.
126 + */
127 + private static $site_settings_cache = null;
128 +
129 + /**
116 130 * Optml_Settings constructor.
117 131 */
118 132 public function __construct() {
119 133 $this->default_schema['cloud_sites'] = $this->get_cloud_sites_whitelist_default();
@@ -119,13 +133,13 @@
119 133 $this->default_schema['cloud_sites'] = $this->get_cloud_sites_whitelist_default();
120 134
121 135 $this->namespace = OPTML_NAMESPACE . '_settings';
122 136 $this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema );
123 - $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
137 + self::$options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
124 138
125 139 if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) {
126 140 switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) );
127 - $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
141 + self::$options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
128 142 restore_current_blog();
129 143 }
130 144
131 145 if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) {
@@ -130,9 +144,9 @@
130 144
131 145 if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) {
132 146
133 147 if ( defined( 'OPTIML_API_KEY' )
134 - && constant( 'OPTIML_API_KEY' ) !== ''
148 + && constant( 'OPTIML_API_KEY' ) !== ''
135 149 ) {
136 150 if ( ! $this->is_connected() && ! self::$auto_connect_hooked ) {
137 151 self::$auto_connect_hooked = true;
138 152 add_action(
@@ -160,14 +174,12 @@
160 174 if ( $type === 'int' && ( (string) $value === '' || (int) $value > 100 || (int) $value < 0 ) ) {
161 175 continue;
162 176 }
163 177 $sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value;
164 - $this->options[ $key ] = $sanitized_value;
178 + self::$options[ $key ] = $sanitized_value;
165 179 }
166 180 }
167 181 }
168 -
169 - add_action( 'init', [ $this, 'register_settings' ] );
170 182 }
171 183
172 184 /**
173 185 * Check if the user is connected to Optimole.
@@ -197,9 +209,9 @@
197 209 if ( ! $this->is_allowed( $key ) ) {
198 210 return null;
199 211 }
200 212
201 - return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
213 + return isset( self::$options[ $key ] ) ? self::$options[ $key ] : '';
202 214 }
203 215
204 216 /**
205 217 * Check if key is allowed.
@@ -215,8 +227,14 @@
215 227 /**
216 228 * Auto connect action.
217 229 */
218 230 public function auto_connect() {
231 +
232 + /**
233 + * Connect rest request.
234 + *
235 + * @var WP_REST_Request<array{api_key: string, application?: string}>
236 + */
219 237 $request = new WP_REST_Request( 'POST' );
220 238 $request->set_param( 'api_key', constant( 'OPTIML_API_KEY' ) );
221 239 Optml_Main::instance()->rest->connect( $request );
222 240
@@ -231,9 +249,11 @@
231 249 *
232 250 * @return array
233 251 */
234 252 public function parse_settings( $new_settings ) {
235 - $sanitized = [];
253 + $sanitized = [];
254 + $sanitized_value = '';
255 +
236 256 foreach ( $new_settings as $key => $value ) {
237 257 switch ( $key ) {
238 258 case 'admin_bar_item':
239 259 case 'lazyload':
@@ -246,9 +266,8 @@
246 266 case 'limit_dimensions':
247 267 case 'resize_smart':
248 268 case 'bg_replacer':
249 269 case 'video_lazyload':
250 - case 'report_script':
251 270 case 'avif':
252 271 case 'offload_media':
253 272 case 'cloud_images':
254 273 case 'autoquality':
@@ -262,8 +281,9 @@
262 281 case 'offloading_status':
263 282 case 'rollback_status':
264 283 case 'best_format':
265 284 case 'offload_limit_reached':
285 + case 'show_badge_icon':
266 286 $sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' );
267 287 break;
268 288 case 'offload_limit':
269 289 $sanitized_value = absint( $value );
@@ -272,9 +292,11 @@
272 292 case 'limit_width':
273 293 $sanitized_value = $this->to_bound_integer( $value, 100, 5000 );
274 294 break;
275 295 case 'quality':
276 - $sanitized_value = $this->to_bound_integer( $value, 1, 100 );
296 + if ( 'mauto' !== $value ) {
297 + $sanitized_value = $this->to_bound_integer( $value, 50, 100 );
298 + }
277 299 break;
278 300 case 'wm_id':
279 301 $sanitized_value = intval( $value );
280 302 break;
@@ -285,8 +307,14 @@
285 307 case 'cache_buster_images':
286 308 case 'cache_buster':
287 309 $sanitized_value = is_string( $value ) ? sanitize_text_field( $value ) : '';
288 310 break;
311 + case 'lazyload_type':
312 + $sanitized_value = $this->to_map_values( $value, [ 'fixed', 'viewport', 'all', 'fixed|viewport' ], 'fixed' );
313 + break;
314 + case 'compression_mode':
315 + $sanitized_value = $this->to_map_values( $value, [ 'speed_optimized', 'quality_optimized', 'custom' ], 'custom' );
316 + break;
289 317 case 'cloud_sites':
290 318 $current_sites = $this->get( 'cloud_sites' );
291 319 $sanitized_value = array_replace_recursive( $current_sites, $value );
292 320 if ( isset( $value['all'] ) && $value['all'] === 'true' ) {
@@ -293,9 +321,9 @@
293 321 $sanitized_value = [ 'all' => 'true' ];
294 322 }
295 323 break;
296 324 case 'defined_image_sizes':
297 - $current_sizes = $this->get( 'defined_image_sizes' );
325 + $current_sizes = $this->get( 'defined_image_sizes' );
298 326 foreach ( $value as $size_name => $size_value ) {
299 327 if ( $size_value === 'remove' ) {
300 328 unset( $current_sizes[ $size_name ] );
301 329 unset( $value[ $size_name ] );
@@ -319,8 +347,9 @@
319 347 }
320 348 break;
321 349 case 'watchers':
322 350 case 'placeholder_color':
351 + case 'transfer_status':
323 352 $sanitized_value = sanitize_text_field( $value );
324 353 break;
325 354 case 'skip_lazyload_images':
326 355 $sanitized_value = $this->to_bound_integer( $value, 0, 100 );
@@ -334,21 +363,24 @@
334 363 case 'wm_position':
335 364 $sanitized_value = $this->to_map_values(
336 365 $value,
337 366 [
338 - Optml_Resize::GRAVITY_NORTH,
339 - Optml_Resize::GRAVITY_NORTH_EAST,
340 - Optml_Resize::GRAVITY_NORTH_WEST,
341 - Optml_Resize::GRAVITY_CENTER,
342 - Optml_Resize::GRAVITY_EAST,
343 - Optml_Resize::GRAVITY_WEST,
344 - Optml_Resize::GRAVITY_SOUTH_EAST,
345 - Optml_Resize::GRAVITY_SOUTH,
346 - Optml_Resize::GRAVITY_SOUTH_WEST,
367 + Position::NORTH,
368 + Position::NORTH_EAST,
369 + Position::NORTH_WEST,
370 + Position::CENTER,
371 + Position::EAST,
372 + Position::WEST,
373 + Position::SOUTH_EAST,
374 + Position::SOUTH,
375 + Position::SOUTH_WEST,
347 376 ],
348 - Optml_Resize::GRAVITY_SOUTH_EAST
377 + Position::SOUTH_EAST
349 378 );
350 379 break;
380 + case 'badge_position':
381 + $sanitized_value = $this->to_map_values( $value, [ 'left', 'right' ], 'right' );
382 + break;
351 383 default:
352 384 $sanitized_value = '';
353 385 break;
354 386
@@ -407,15 +439,16 @@
407 439 if ( ! $this->is_main_mu_site() ) {
408 440 return false;
409 441 }
410 442
411 - $opts = $this->options;
443 + $opts = self::$options;
412 444 $opts['banner_frontend'] = $value ? 'enabled' : 'disabled';
413 445
414 446 $update = update_option( $this->namespace, $opts, false );
415 447
416 448 if ( $update ) {
417 - $this->options = $opts;
449 + self::$options = $opts;
450 + $this->set_settings_cache( 'banner_frontend', $opts['banner_frontend'] );
418 451 }
419 452
420 453 return $update;
421 454 }
@@ -435,9 +468,9 @@
435 468
436 469 if ( ! $this->is_main_mu_site() ) {
437 470 return false;
438 471 }
439 - $opt = $this->options;
472 + $opt = self::$options;
440 473
441 474 if ( $key === 'banner_frontend' ) {
442 475 $api = new Optml_Api();
443 476 $service_data = $this->get( 'service_data' );
@@ -445,14 +478,39 @@
445 478 $response = $api->update_extra_visits( $opt['api_key'], $value, $application );
446 479 }
447 480
448 481 $opt[ $key ] = $value;
482 +
449 483 $update = update_option( $this->namespace, $opt, false );
484 +
450 485 if ( $update ) {
451 - $this->options = $opt;
486 + self::$options = $opt;
487 + $this->set_settings_cache( $key, $value );
452 488 }
453 489 if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
490 + /**
491 + * Fires when Optimole settings are updated.
492 + *
493 + * This action is triggered after settings have been successfully updated
494 + * and provides an opportunity for other plugins or themes to react to
495 + * Optimole configuration changes.
496 + */
454 497 do_action( 'optml_settings_updated' );
498 + static $cleared = false;
499 + if ( $cleared ) {
500 + return $update;
501 + }
502 + $cleared = true;
503 + /**
504 + * Fires when Optimole cache needs to be cleared after settings update.
505 + *
506 + * This action is triggered after settings are updated to ensure that
507 + * any cached data is refreshed with the new configuration.
508 + *
509 + * @param bool|string $location Whether to clear the cache globally or just for a particular request.
510 + * You can pass a string with the URL to clear the cache for a particular URL only.
511 + */
512 + do_action( 'optml_clear_cache', true );
455 513 }
456 514 return $update;
457 515 }
458 516
@@ -463,9 +521,9 @@
463 521 */
464 522 private function is_main_mu_site() {
465 523 // If we try to update from a website which is not the main OPTML blog, bail.
466 524 if ( defined( 'OPTIML_ENABLED_MU' ) && constant( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && constant( 'OPTIML_MU_SITE_ID' ) &&
467 - intval( constant( 'OPTIML_MU_SITE_ID' ) ) !== get_current_blog_id()
525 + intval( constant( 'OPTIML_MU_SITE_ID' ) ) !== get_current_blog_id()
468 526 ) {
469 527 return false;
470 528 }
471 529
@@ -477,8 +535,12 @@
477 535 *
478 536 * @return array Site settings.
479 537 */
480 538 public function get_site_settings() {
539 + if ( self::$site_settings_cache !== null ) {
540 + return self::$site_settings_cache;
541 + }
542 +
481 543 $service_data = $this->get( 'service_data' );
482 544 $whitelist = [];
483 545 if ( isset( $service_data['whitelist'] ) ) {
484 546 $whitelist = $service_data['whitelist'];
@@ -483,50 +545,54 @@
483 545 if ( isset( $service_data['whitelist'] ) ) {
484 546 $whitelist = $service_data['whitelist'];
485 547 }
486 548
487 - return [
488 - 'quality' => $this->get_quality(),
489 - 'admin_bar_item' => $this->get( 'admin_bar_item' ),
490 - 'lazyload' => $this->get( 'lazyload' ),
491 - 'network_optimization' => $this->get( 'network_optimization' ),
492 - 'retina_images' => $this->get( 'retina_images' ),
493 - 'limit_dimensions' => $this->get( 'limit_dimensions' ),
494 - 'limit_height' => $this->get( 'limit_height' ),
495 - 'limit_width' => $this->get( 'limit_width' ),
496 - 'lazyload_placeholder' => $this->get( 'lazyload_placeholder' ),
497 - 'skip_lazyload_images' => $this->get( 'skip_lazyload_images' ),
498 - 'bg_replacer' => $this->get( 'bg_replacer' ),
499 - 'video_lazyload' => $this->get( 'video_lazyload' ),
500 - 'resize_smart' => $this->get( 'resize_smart' ),
501 - 'no_script' => $this->get( 'no_script' ),
502 - 'image_replacer' => $this->get( 'image_replacer' ),
503 - 'cdn' => $this->get( 'cdn' ),
504 - 'filters' => $this->get_filters(),
505 - 'cloud_sites' => $this->get( 'cloud_sites' ),
506 - 'defined_image_sizes' => $this->get( 'defined_image_sizes' ),
507 - 'watchers' => $this->get_watchers(),
508 - 'watermark' => $this->get_watermark(),
509 - 'img_to_video' => $this->get( 'img_to_video' ),
510 - 'scale' => $this->get( 'scale' ),
511 - 'css_minify' => $this->get( 'css_minify' ),
512 - 'js_minify' => $this->get( 'js_minify' ),
513 - 'native_lazyload' => $this->get( 'native_lazyload' ),
514 - 'report_script' => $this->get( 'report_script' ),
515 - 'avif' => $this->get( 'avif' ),
516 - 'autoquality' => $this->get( 'autoquality' ),
517 - 'offload_media' => $this->get( 'offload_media' ),
518 - 'cloud_images' => $this->get( 'cloud_images' ),
519 - 'strip_metadata' => $this->get( 'strip_metadata' ),
520 - 'whitelist_domains' => $whitelist,
521 - 'banner_frontend' => $this->get( 'banner_frontend' ),
522 - 'offloading_status' => $this->get( 'offloading_status' ),
523 - 'rollback_status' => $this->get( 'rollback_status' ),
524 - 'best_format' => $this->get( 'best_format' ),
525 - 'offload_limit_reached' => $this->get( 'offload_limit_reached' ),
526 - 'placeholder_color' => $this->get( 'placeholder_color' ),
549 + self::$site_settings_cache = [
550 + 'quality' => $this->get_quality(),
551 + 'admin_bar_item' => $this->get( 'admin_bar_item' ),
552 + 'lazyload' => $this->get( 'lazyload' ),
553 + 'network_optimization' => $this->get( 'network_optimization' ),
554 + 'retina_images' => $this->get( 'retina_images' ),
555 + 'limit_dimensions' => $this->get( 'limit_dimensions' ),
556 + 'limit_height' => $this->get( 'limit_height' ),
557 + 'limit_width' => $this->get( 'limit_width' ),
558 + 'lazyload_placeholder' => $this->get( 'lazyload_placeholder' ),
559 + 'skip_lazyload_images' => $this->get( 'skip_lazyload_images' ),
560 + 'bg_replacer' => $this->get( 'bg_replacer' ),
561 + 'video_lazyload' => $this->get( 'video_lazyload' ),
562 + 'resize_smart' => $this->get( 'resize_smart' ),
563 + 'no_script' => $this->get( 'no_script' ),
564 + 'lazyload_type' => $this->get( 'lazyload_type' ),
565 + 'compression_mode' => $this->get( 'compression_mode' ),
566 + 'image_replacer' => $this->get( 'image_replacer' ),
567 + 'cdn' => $this->get( 'cdn' ),
568 + 'filters' => $this->get_filters(),
569 + 'cloud_sites' => $this->get( 'cloud_sites' ),
570 + 'defined_image_sizes' => $this->get( 'defined_image_sizes' ),
571 + 'watchers' => $this->get_watchers(),
572 + 'watermark' => $this->get_watermark(),
573 + 'img_to_video' => $this->get( 'img_to_video' ), // @deprecated
574 + 'scale' => $this->get( 'scale' ),
575 + 'css_minify' => $this->get( 'css_minify' ),
576 + 'js_minify' => $this->get( 'js_minify' ),
577 + 'native_lazyload' => $this->get( 'native_lazyload' ),
578 + 'avif' => $this->get( 'avif' ),
579 + 'autoquality' => $this->get( 'autoquality' ),
580 + 'offload_media' => $this->get( 'offload_media' ),
581 + 'cloud_images' => $this->get( 'cloud_images' ),
582 + 'strip_metadata' => $this->get( 'strip_metadata' ),
583 + 'whitelist_domains' => $whitelist,
584 + 'banner_frontend' => $this->get( 'banner_frontend' ),
585 + 'offloading_status' => $this->get( 'offloading_status' ),
586 + 'rollback_status' => $this->get( 'rollback_status' ),
587 + 'best_format' => $this->get( 'best_format' ),
588 + 'offload_limit_reached' => $this->get( 'offload_limit_reached' ),
589 + 'placeholder_color' => $this->get( 'placeholder_color' ),
527 590 'show_offload_finish_notice' => $this->get( 'show_offload_finish_notice' ),
591 + 'show_badge_icon' => $this->get( 'show_badge_icon' ),
592 + 'badge_position' => $this->get( 'badge_position' ),
528 593 ];
594 + return self::$site_settings_cache;
529 595 }
530 596
531 597 /**
532 598 * Return quality factor.
@@ -560,9 +626,8 @@
560 626 */
561 627 public function get_watchers() {
562 628
563 629 return $this->get( 'watchers' );
564 -
565 630 }
566 631
567 632 /**
568 633 * Return an watermark array.
@@ -596,8 +661,16 @@
596 661 */
597 662 public function is_best_format() {
598 663 return $this->get( 'best_format' ) === 'enabled';
599 664 }
665 + /**
666 + * Check if scale is enabled.
667 + *
668 + * @return bool Scale enabled
669 + */
670 + public function is_scale_enabled() {
671 + return $this->get( 'scale' ) === 'disabled'; // Due to legacy reasons the disabled state means that the scale is enabled and the enabled state means that the scale is disabled.
672 + }
600 673
601 674 /**
602 675 * Check if offload limit was reached.
603 676 *
@@ -668,16 +741,16 @@
668 741 if ( ! isset( $service_data['cdn_key'] ) ) {
669 742 return '';
670 743 }
671 744
745 + if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
746 + return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
747 + }
748 +
672 749 if ( isset( $service_data['is_cname_assigned'] ) && $service_data['is_cname_assigned'] === 'yes' && ! empty( $service_data['domain'] ) ) {
673 750 return strtolower( $service_data['domain'] );
674 751 }
675 752
676 - if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
677 - return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
678 - }
679 -
680 753 return sprintf(
681 754 '%s.%s',
682 755 strtolower( $service_data['cdn_key'] ),
683 756 Optml_Config::$base_domain
@@ -690,17 +763,21 @@
690 763 * @return bool Reset action status.
691 764 */
692 765 public function reset() {
693 766 $reset_schema = $this->default_schema;
694 - $reset_schema['filters'] = $this->options['filters'];
767 + $reset_schema['filters'] = self::$options['filters'];
695 768
696 769 $update = update_option( $this->namespace, $reset_schema );
697 770 if ( $update ) {
698 - $this->options = $reset_schema;
771 + self::$options = $reset_schema;
772 + $this->set_settings_cache( 'filters', $reset_schema['filters'] );
699 773 }
774 + wp_unschedule_hook( Optml_Admin::SYNC_CRON );
775 + wp_unschedule_hook( Optml_Admin::ENRICH_CRON );
700 776
701 777 return $update;
702 778 }
779 +
703 780 /**
704 781 * Get raw settings value.
705 782 *
706 783 * @return array
@@ -705,28 +782,13 @@
705 782 *
706 783 * @return array
707 784 */
708 785 public function get_raw_settings() {
709 - return get_option( $this->namespace, false );
786 + $raw_settings = get_option( $this->namespace, [] );
787 +
788 + return is_array( $raw_settings ) ? $raw_settings : [];
710 789 }
711 790
712 - /**
713 - * Get settings for CSAT.
714 - *
715 - * @return void
716 - */
717 - public function register_settings() {
718 - register_setting(
719 - 'optml_settings',
720 - 'optml_csat',
721 - [
722 - 'type' => 'string',
723 - 'sanitize_callback' => 'sanitize_text_field',
724 - 'show_in_rest' => true,
725 - 'default' => '{}',
726 - ]
727 - );
728 - }
729 791
730 792 /**
731 793 * Clear cache.
732 794 *
@@ -734,21 +796,25 @@
734 796 *
735 797 * @return string|WP_Error
736 798 */
737 799 public function clear_cache( $type = '' ) {
738 - $token = $this->get( 'cache_buster' );
800 + $token = $this->get( 'cache_buster' );
739 801 $token_images = $this->get( 'cache_buster_images' );
740 802
741 - if ( ! empty( $token_images ) ) {
742 - $token = $token_images;
743 - }
744 -
745 - if ( ! empty( $type ) && $type === 'assets' ) {
803 + // here is an individual cache tokens
804 + $individual = get_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY ) ?: [];
805 + if ( ( empty( $type ) || $type === 'images' ) ) {
806 + if ( ! empty( $token_images ) ) {
807 + $token = $token_images;
808 + }
809 + } elseif ( $type === 'assets' ) {
746 810 $token = $this->get( 'cache_buster_assets' );
811 + } else {
812 + $token = $individual[ crc32( $type ) ] ?? $token_images ?: $token;
747 813 }
748 814
749 - $request = new Optml_Api();
750 - $data = $request->get_cache_token( $token, $type );
815 + $request = new Optml_Api();
816 + $data = $request->get_cache_token( $token, $type );
751 817
752 818 if ( $data === false || is_wp_error( $data ) || empty( $data ) || ! isset( $data['token'] ) ) {
753 819 $extra = '';
754 820
@@ -757,20 +823,25 @@
757 823 * Error from api.
758 824 *
759 825 * @var WP_Error $data Error object.
760 826 */
761 - $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
827 + $extra = sprintf( /* translators: Error details */ __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
762 828 }
763 829
764 830 return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra );
765 831 }
766 832
767 - if ( ! empty( $type ) && $type === 'assets' ) {
833 + if ( empty( $type ) || $type === 'images' ) {
834 + set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
835 + $this->update( 'cache_buster_images', $data['token'] );
836 + // we delete individual cache tokens since this is a global cache clear.
837 + delete_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY );
838 + } elseif ( $type === 'assets' ) {
768 839 set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
769 840 $this->update( 'cache_buster_assets', $data['token'] );
770 841 } else {
771 - set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
772 - $this->update( 'cache_buster_images', $data['token'] );
842 + $individual[ crc32( $type ) ] = $data['token'];
843 + set_transient( self::INDIVIDUAL_CACHE_TOKENS_KEY, $individual, 6 * HOUR_IN_SECONDS );
773 844 }
774 845
775 846 return $data['token'];
776 847 }
@@ -775,8 +846,34 @@
775 846 return $data['token'];
776 847 }
777 848
778 849 /**
850 + * Check if lazyload type is viewport.
851 + *
852 + * @return bool
853 + */
854 + public function is_lazyload_type_viewport() {
855 + return false !== strpos( $this->get( 'lazyload_type' ), 'viewport' );
856 + }
857 +
858 + /**
859 + * Check if lazyload type is all.
860 + *
861 + * @return bool
862 + */
863 + public function is_lazyload_type_all() {
864 + return $this->get( 'lazyload_type' ) === 'all';
865 + }
866 +
867 + /**
868 + * Check if lazyload type is fixed.
869 + *
870 + * @return bool
871 + */
872 + public function is_lazyload_type_fixed() {
873 + return false !== strpos( $this->get( 'lazyload_type' ), 'fixed' );
874 + }
875 + /**
779 876 * Utility to check if offload is enabled.
780 877 *
781 878 * @return bool
782 879 */
@@ -798,6 +895,19 @@
798 895 return [
799 896 'all' => 'false',
800 897 $site_url => 'true',
801 898 ];
899 + }
900 +
901 + /**
902 + * Set settings cache value.
903 + *
904 + * @param string $key Cache key.
905 + * @param mixed $value Cache value.
906 + * @return void
907 + */
908 + private function set_settings_cache( $key, $value ) {
909 + if ( self::$site_settings_cache !== null && isset( self::$site_settings_cache[ $key ] ) ) {
910 + self::$site_settings_cache[ $key ] = $value;
911 + }
802 912 }
803 913 }