PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.12.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.12.0
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 3.12.1 All 134 releases
optimole-wp / inc / settings.php

settings.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.12.0, at inc/settings.php

804 lines 21.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Optml_Settings.
5 */
6 class Optml_Settings {
7 use Optml_Normalizer;
8
9 const FILTER_EXT = 'extension';
10 const FILTER_URL = 'page_url';
11 const FILTER_URL_MATCH = 'page_url_match';
12 const FILTER_FILENAME = 'filename';
13 const FILTER_CLASS = 'class';
14 const FILTER_TYPE_LAZYLOAD = 'lazyload';
15 const FILTER_TYPE_OPTIMIZE = 'optimize';
16 const OPTML_USER_EMAIL = 'optml_user_email';
17 /**
18 * Holds an array of possible settings to alter via wp cli or wp-config constants.
19 *
20 * @var array Whitelisted settings.
21 */
22 public static $whitelisted_settings = [
23 'image_replacer' => 'bool',
24 'quality' => 'int',
25 'lazyload' => 'bool',
26 'lazyload_placeholder' => 'bool',
27 'network_optimization' => 'bool',
28 'autoquality' => 'bool',
29 'img_to_video' => 'bool',
30 'resize_smart' => 'bool',
31 'retina_images' => 'bool',
32 'native_lazyload' => 'bool',
33 'video_lazyload' => 'bool',
34 'bg_replacer' => 'bool',
35 'scale' => 'bool',
36 'cdn' => 'bool',
37 ];
38 /**
39 * Holds the status of the auto connect hook.
40 *
41 * @var boolean Whether or not the auto connect action is hooked.
42 */
43 private static $auto_connect_hooked = false;
44
45 /**
46 * Default settings schema.
47 *
48 * @var array Settings schema.
49 */
50 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' => '',
101 ];
102 /**
103 * Option key.
104 *
105 * @var string Option name.
106 */
107 private $namespace;
108 /**
109 * Holds all options from db.
110 *
111 * @var array All options.
112 */
113 private $options;
114
115 /**
116 * Optml_Settings constructor.
117 */
118 public function __construct() {
119 $this->default_schema['cloud_sites'] = $this->get_cloud_sites_whitelist_default();
120
121 $this->namespace = OPTML_NAMESPACE . '_settings';
122 $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 );
124
125 if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) {
126 switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) );
127 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
128 restore_current_blog();
129 }
130
131 if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) {
132
133 if ( defined( 'OPTIML_API_KEY' )
134 && constant( 'OPTIML_API_KEY' ) !== ''
135 ) {
136 if ( ! $this->is_connected() && ! self::$auto_connect_hooked ) {
137 self::$auto_connect_hooked = true;
138 add_action(
139 'plugins_loaded',
140 [ $this, 'auto_connect' ]
141 );
142 }
143 }
144
145 foreach ( self::$whitelisted_settings as $key => $type ) {
146 $env_key = 'OPTIML_' . strtoupper( $key );
147 if ( defined( $env_key ) && constant( $env_key ) ) {
148 $value = constant( $env_key );
149 if ( $type === 'bool' && ( (string) $value === '' || ! in_array(
150 $value,
151 [
152 'on',
153 'off',
154 ],
155 true
156 ) ) ) {
157 continue;
158 }
159
160 if ( $type === 'int' && ( (string) $value === '' || (int) $value > 100 || (int) $value < 0 ) ) {
161 continue;
162 }
163 $sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value;
164 $this->options[ $key ] = $sanitized_value;
165 }
166 }
167 }
168
169 add_action( 'init', [ $this, 'register_settings' ] );
170 }
171
172 /**
173 * Check if the user is connected to Optimole.
174 *
175 * @return bool Connection status.
176 */
177 public function is_connected() {
178 $service_data = $this->get( 'service_data' );
179 if ( ! isset( $service_data['cdn_key'] ) ) {
180 return false;
181 }
182 if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) {
183 return false;
184 }
185
186 return true;
187 }
188
189 /**
190 * Get setting value by key.
191 *
192 * @param string $key Key to search against.
193 *
194 * @return mixed|null Setting value.
195 */
196 public function get( $key ) {
197 if ( ! $this->is_allowed( $key ) ) {
198 return null;
199 }
200
201 return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
202 }
203
204 /**
205 * Check if key is allowed.
206 *
207 * @param string $key Is key allowed or not.
208 *
209 * @return bool Is key allowed or not.
210 */
211 private function is_allowed( $key ) {
212 return isset( $this->default_schema[ $key ] );
213 }
214
215 /**
216 * Auto connect action.
217 */
218 public function auto_connect() {
219 $request = new WP_REST_Request( 'POST' );
220 $request->set_param( 'api_key', constant( 'OPTIML_API_KEY' ) );
221 Optml_Main::instance()->rest->connect( $request );
222
223 remove_action( 'plugins_loaded', [ $this, 'auto_connect' ] );
224 self::$auto_connect_hooked = false;
225 }
226
227 /**
228 * Process settings.
229 *
230 * @param array $new_settings List of settings.
231 *
232 * @return array
233 */
234 public function parse_settings( $new_settings ) {
235 $sanitized = [];
236 foreach ( $new_settings as $key => $value ) {
237 switch ( $key ) {
238 case 'admin_bar_item':
239 case 'lazyload':
240 case 'scale':
241 case 'image_replacer':
242 case 'cdn':
243 case 'network_optimization':
244 case 'lazyload_placeholder':
245 case 'retina_images':
246 case 'limit_dimensions':
247 case 'resize_smart':
248 case 'bg_replacer':
249 case 'video_lazyload':
250 case 'report_script':
251 case 'avif':
252 case 'offload_media':
253 case 'cloud_images':
254 case 'autoquality':
255 case 'img_to_video':
256 case 'css_minify':
257 case 'js_minify':
258 case 'native_lazyload':
259 case 'strip_metadata':
260 case 'no_script':
261 case 'banner_frontend':
262 case 'offloading_status':
263 case 'rollback_status':
264 case 'best_format':
265 case 'offload_limit_reached':
266 $sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' );
267 break;
268 case 'offload_limit':
269 $sanitized_value = absint( $value );
270 break;
271 case 'limit_height':
272 case 'limit_width':
273 $sanitized_value = $this->to_bound_integer( $value, 100, 5000 );
274 break;
275 case 'quality':
276 $sanitized_value = $this->to_bound_integer( $value, 1, 100 );
277 break;
278 case 'wm_id':
279 $sanitized_value = intval( $value );
280 break;
281 case 'show_offload_finish_notice':
282 $sanitized_value = $this->to_map_values( $value, [ 'offload', 'rollback' ], '' );
283 break;
284 case 'cache_buster_assets':
285 case 'cache_buster_images':
286 case 'cache_buster':
287 $sanitized_value = is_string( $value ) ? sanitize_text_field( $value ) : '';
288 break;
289 case 'cloud_sites':
290 $current_sites = $this->get( 'cloud_sites' );
291 $sanitized_value = array_replace_recursive( $current_sites, $value );
292 if ( isset( $value['all'] ) && $value['all'] === 'true' ) {
293 $sanitized_value = [ 'all' => 'true' ];
294 }
295 break;
296 case 'defined_image_sizes':
297 $current_sizes = $this->get( 'defined_image_sizes' );
298 foreach ( $value as $size_name => $size_value ) {
299 if ( $size_value === 'remove' ) {
300 unset( $current_sizes[ $size_name ] );
301 unset( $value[ $size_name ] );
302 }
303 }
304 $sanitized_value = array_replace_recursive( $current_sizes, $value );
305 break;
306 case 'filters':
307 $current_filters = $this->get_filters();
308 $sanitized_value = array_replace_recursive( $current_filters, $value );
309 // Remove falsy vars.
310 foreach ( $sanitized_value as $filter_type => $filter_values ) {
311 foreach ( $filter_values as $filter_rule_type => $filter_rules_value ) {
312 $sanitized_value[ $filter_type ][ $filter_rule_type ] = array_filter(
313 $filter_rules_value,
314 function ( $value ) {
315 return ( $value !== 'false' && $value !== false );
316 }
317 );
318 }
319 }
320 break;
321 case 'watchers':
322 case 'placeholder_color':
323 $sanitized_value = sanitize_text_field( $value );
324 break;
325 case 'skip_lazyload_images':
326 $sanitized_value = $this->to_bound_integer( $value, 0, 100 );
327 break;
328 case 'wm_opacity':
329 case 'wm_scale':
330 case 'wm_x':
331 case 'wm_y':
332 $sanitized_value = floatval( $value );
333 break;
334 case 'wm_position':
335 $sanitized_value = $this->to_map_values(
336 $value,
337 [
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,
347 ],
348 Optml_Resize::GRAVITY_SOUTH_EAST
349 );
350 break;
351 default:
352 $sanitized_value = '';
353 break;
354
355 }
356
357 $sanitized[ $key ] = $sanitized_value;
358 $this->update( $key, $sanitized_value );
359 }
360
361 return $sanitized;
362 }
363
364 /**
365 * Return filter definitions.
366 *
367 * @return mixed|null Filter values.
368 */
369 public function get_filters() {
370
371 $filters = $this->get( 'filters' );
372 if ( ! isset( $filters[ self::FILTER_TYPE_LAZYLOAD ] ) ) {
373 $filters[ self::FILTER_TYPE_LAZYLOAD ] = [];
374 }
375 if ( ! isset( $filters[ self::FILTER_TYPE_OPTIMIZE ] ) ) {
376 $filters[ self::FILTER_TYPE_OPTIMIZE ] = [];
377 }
378 foreach ( $filters as $filter_key => $filter_rules ) {
379 if ( ! isset( $filter_rules[ self::FILTER_EXT ] ) ) {
380 $filters[ $filter_key ][ self::FILTER_EXT ] = [];
381 }
382 if ( ! isset( $filter_rules[ self::FILTER_FILENAME ] ) ) {
383 $filters[ $filter_key ][ self::FILTER_FILENAME ] = [];
384 }
385 if ( ! isset( $filter_rules[ self::FILTER_URL ] ) ) {
386 $filters[ $filter_key ][ self::FILTER_URL ] = [];
387 }
388 if ( ! isset( $filter_rules[ self::FILTER_URL_MATCH ] ) ) {
389 $filters[ $filter_key ][ self::FILTER_URL_MATCH ] = [];
390 }
391 if ( ! isset( $filter_rules[ self::FILTER_CLASS ] ) ) {
392 $filters[ $filter_key ][ self::FILTER_CLASS ] = [];
393 }
394 }
395
396 return $filters;
397 }
398
399 /**
400 * Update frontend banner setting from remote.
401 *
402 * @param bool $value Value.
403 *
404 * @return bool
405 */
406 public function update_frontend_banner_from_remote( $value ) {
407 if ( ! $this->is_main_mu_site() ) {
408 return false;
409 }
410
411 $opts = $this->options;
412 $opts['banner_frontend'] = $value ? 'enabled' : 'disabled';
413
414 $update = update_option( $this->namespace, $opts, false );
415
416 if ( $update ) {
417 $this->options = $opts;
418 }
419
420 return $update;
421 }
422
423 /**
424 * Update settings.
425 *
426 * @param string $key Settings key.
427 * @param mixed $value Settings value.
428 *
429 * @return bool Update result.
430 */
431 public function update( $key, $value ) {
432 if ( ! $this->is_allowed( $key ) ) {
433 return false;
434 }
435
436 if ( ! $this->is_main_mu_site() ) {
437 return false;
438 }
439 $opt = $this->options;
440
441 if ( $key === 'banner_frontend' ) {
442 $api = new Optml_Api();
443 $service_data = $this->get( 'service_data' );
444 $application = isset( $service_data['cdn_key'] ) ? $service_data['cdn_key'] : '';
445 $response = $api->update_extra_visits( $opt['api_key'], $value, $application );
446 }
447
448 $opt[ $key ] = $value;
449 $update = update_option( $this->namespace, $opt, false );
450 if ( $update ) {
451 $this->options = $opt;
452 }
453 if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
454 do_action( 'optml_settings_updated' );
455 }
456 return $update;
457 }
458
459 /**
460 * Check that we're on the main OPTML blog.
461 *
462 * @return bool
463 */
464 private function is_main_mu_site() {
465 // If we try to update from a website which is not the main OPTML blog, bail.
466 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()
468 ) {
469 return false;
470 }
471
472 return true;
473 }
474
475 /**
476 * Return site settings.
477 *
478 * @return array Site settings.
479 */
480 public function get_site_settings() {
481 $service_data = $this->get( 'service_data' );
482 $whitelist = [];
483 if ( isset( $service_data['whitelist'] ) ) {
484 $whitelist = $service_data['whitelist'];
485 }
486
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' ),
527 'show_offload_finish_notice' => $this->get( 'show_offload_finish_notice' ),
528 ];
529 }
530
531 /**
532 * Return quality factor.
533 *
534 * @return string Quality factor.
535 */
536 public function get_quality() {
537 $quality = $this->get( 'quality' );
538 if ( $this->get( 'autoquality' ) === 'enabled' ) {
539 return 'mauto';
540 }
541 // Legacy compat.
542 if ( $quality === 'low' ) {
543 return 'high_c';
544 }
545 if ( $quality === 'medium' ) {
546 return 'medium_c';
547 }
548
549 if ( $quality === 'high' ) {
550 return 'low_c';
551 }
552
553 return $quality;
554 }
555
556 /**
557 * Return filter definitions.
558 *
559 * @return mixed|null Filter values.
560 */
561 public function get_watchers() {
562
563 return $this->get( 'watchers' );
564
565 }
566
567 /**
568 * Return an watermark array.
569 *
570 * @return array
571 */
572 public function get_watermark() {
573 return [
574 'id' => $this->get( 'wm_id' ),
575 'opacity' => $this->get( 'wm_opacity' ),
576 'position' => $this->get( 'wm_position' ),
577 'x_offset' => $this->get( 'wm_x' ),
578 'y_offset' => $this->get( 'wm_y' ),
579 'scale' => $this->get( 'wm_scale' ),
580 ];
581 }
582
583 /**
584 * Check if smart cropping is enabled.
585 *
586 * @return bool Is smart cropping enabled.
587 */
588 public function is_smart_cropping() {
589 return $this->get( 'resize_smart' ) === 'enabled';
590 }
591
592 /**
593 * Check if best format is enabled.
594 *
595 * @return bool
596 */
597 public function is_best_format() {
598 return $this->get( 'best_format' ) === 'enabled';
599 }
600
601 /**
602 * Check if offload limit was reached.
603 *
604 * @return bool
605 */
606 public function is_offload_limit_reached() {
607 return $this->get( 'offload_limit_reached' ) === 'enabled';
608 }
609
610 /**
611 * Get numeric quality used by the service.
612 *
613 * @return int Numeric quality.
614 */
615 public function get_numeric_quality() {
616 $value = $this->get_quality();
617
618 return (int) $this->to_accepted_quality( $value );
619 }
620
621 /**
622 * Check if replacer is enabled.
623 *
624 * @return bool Replacer enabled
625 */
626 public function is_enabled() {
627 $status = $this->get( 'image_replacer' );
628 $service_data = $this->get( 'service_data' );
629 if ( empty( $service_data ) ) {
630 return false;
631 }
632 if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) {
633 return false;
634 }
635
636 return $this->to_boolean( $status );
637 }
638
639 /**
640 * Check if lazyload is enabled.
641 *
642 * @return bool Lazyload enabled
643 */
644 public function use_lazyload() {
645 $status = $this->get( 'lazyload' );
646
647 return $this->to_boolean( $status );
648 }
649
650 /**
651 * Check if replacer is enabled.
652 *
653 * @return bool Replacer enabled
654 */
655 public function use_cdn() {
656 $status = $this->get( 'cdn' );
657
658 return $this->to_boolean( $status );
659 }
660
661 /**
662 * Return cdn url.
663 *
664 * @return string CDN url.
665 */
666 public function get_cdn_url() {
667 $service_data = $this->get( 'service_data' );
668 if ( ! isset( $service_data['cdn_key'] ) ) {
669 return '';
670 }
671
672 if ( isset( $service_data['is_cname_assigned'] ) && $service_data['is_cname_assigned'] === 'yes' && ! empty( $service_data['domain'] ) ) {
673 return strtolower( $service_data['domain'] );
674 }
675
676 if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
677 return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
678 }
679
680 return sprintf(
681 '%s.%s',
682 strtolower( $service_data['cdn_key'] ),
683 Optml_Config::$base_domain
684 );
685 }
686
687 /**
688 * Reset options to defaults.
689 *
690 * @return bool Reset action status.
691 */
692 public function reset() {
693 $reset_schema = $this->default_schema;
694 $reset_schema['filters'] = $this->options['filters'];
695
696 $update = update_option( $this->namespace, $reset_schema );
697 if ( $update ) {
698 $this->options = $reset_schema;
699 }
700
701 return $update;
702 }
703 /**
704 * Get raw settings value.
705 *
706 * @return array
707 */
708 public function get_raw_settings() {
709 return get_option( $this->namespace, false );
710 }
711
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
730 /**
731 * Clear cache.
732 *
733 * @param string $type Cache type.
734 *
735 * @return string|WP_Error
736 */
737 public function clear_cache( $type = '' ) {
738 $token = $this->get( 'cache_buster' );
739 $token_images = $this->get( 'cache_buster_images' );
740
741 if ( ! empty( $token_images ) ) {
742 $token = $token_images;
743 }
744
745 if ( ! empty( $type ) && $type === 'assets' ) {
746 $token = $this->get( 'cache_buster_assets' );
747 }
748
749 $request = new Optml_Api();
750 $data = $request->get_cache_token( $token, $type );
751
752 if ( $data === false || is_wp_error( $data ) || empty( $data ) || ! isset( $data['token'] ) ) {
753 $extra = '';
754
755 if ( is_wp_error( $data ) ) {
756 /**
757 * Error from api.
758 *
759 * @var WP_Error $data Error object.
760 */
761 $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
762 }
763
764 return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra );
765 }
766
767 if ( ! empty( $type ) && $type === 'assets' ) {
768 set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
769 $this->update( 'cache_buster_assets', $data['token'] );
770 } else {
771 set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
772 $this->update( 'cache_buster_images', $data['token'] );
773 }
774
775 return $data['token'];
776 }
777
778 /**
779 * Utility to check if offload is enabled.
780 *
781 * @return bool
782 */
783 public function is_offload_enabled() {
784 return $this->get( 'offload_media' ) === 'enabled' || $this->get( 'rollback_status' ) !== 'disabled';
785 }
786
787 /**
788 * Get cloud sites whitelist for current domain only.
789 *
790 * @return array
791 */
792 public function get_cloud_sites_whitelist_default() {
793 $site_url = get_site_url();
794
795 $site_url = preg_replace( '/^https?:\/\//', '', $site_url );
796 $site_url = trim( $site_url, '/' );
797
798 return [
799 'all' => 'false',
800 $site_url => 'true',
801 ];
802 }
803 }
804