PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.10.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.10.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.10.0, at inc/settings.php

762 lines 20.6 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 'max_height' => 1500,
58 'max_width' => 2000,
59 'admin_bar_item' => 'enabled',
60 'lazyload' => 'disabled',
61 'scale' => 'disabled',
62 'network_optimization' => 'disabled',
63 'lazyload_placeholder' => 'enabled',
64 'bg_replacer' => 'enabled',
65 'video_lazyload' => 'enabled',
66 'retina_images' => 'disabled',
67 'limit_dimensions' => 'disabled',
68 'limit_height' => 1080,
69 'limit_width' => 1920,
70 'resize_smart' => 'disabled',
71 'no_script' => 'disabled',
72 'filters' => [],
73 'cloud_sites' => [ 'all' => 'true' ],
74 'watchers' => '',
75 'quality' => 'auto',
76 'wm_id' => - 1,
77 'wm_opacity' => 1,
78 'wm_position' => Optml_Resize::GRAVITY_SOUTH_EAST,
79 'wm_x' => 0,
80 'wm_y' => 0,
81 'wm_scale' => 0,
82 'image_replacer' => 'enabled',
83 'img_to_video' => 'disabled',
84 'css_minify' => 'enabled',
85 'js_minify' => 'disabled',
86 'report_script' => 'disabled',
87 'avif' => 'enabled',
88 'autoquality' => 'enabled',
89 'native_lazyload' => 'disabled',
90 'offload_media' => 'disabled',
91 'cloud_images' => 'disabled',
92 'strip_metadata' => 'enabled',
93 'skip_lazyload_images' => 3,
94 'defined_image_sizes' => [ ],
95 'banner_frontend' => 'disabled',
96 'offloading_status' => 'disabled',
97 'rollback_status' => 'disabled',
98 'best_format' => 'enabled',
99 'placeholder_color' => '',
100 ];
101 /**
102 * Option key.
103 *
104 * @var string Option name.
105 */
106 private $namespace;
107 /**
108 * Holds all options from db.
109 *
110 * @var array All options.
111 */
112 private $options;
113
114 /**
115 * Optml_Settings constructor.
116 */
117 public function __construct() {
118
119 $this->namespace = OPTML_NAMESPACE . '_settings';
120 $this->default_schema = apply_filters( 'optml_default_settings', $this->default_schema );
121 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
122
123 if ( defined( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && $this->to_boolean( constant( 'OPTIML_ENABLED_MU' ) ) && constant( 'OPTIML_MU_SITE_ID' ) ) {
124 switch_to_blog( constant( 'OPTIML_MU_SITE_ID' ) );
125 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
126 restore_current_blog();
127 }
128
129 if ( defined( 'OPTIML_USE_ENV' ) && constant( 'OPTIML_USE_ENV' ) && $this->to_boolean( constant( 'OPTIML_USE_ENV' ) ) ) {
130
131 if ( defined( 'OPTIML_API_KEY' )
132 && constant( 'OPTIML_API_KEY' ) !== ''
133 ) {
134 if ( ! $this->is_connected() && ! self::$auto_connect_hooked ) {
135 self::$auto_connect_hooked = true;
136 add_action(
137 'plugins_loaded',
138 [ $this, 'auto_connect' ]
139 );
140 }
141 }
142
143 foreach ( self::$whitelisted_settings as $key => $type ) {
144 $env_key = 'OPTIML_' . strtoupper( $key );
145 if ( defined( $env_key ) && constant( $env_key ) ) {
146 $value = constant( $env_key );
147 if ( $type === 'bool' && ( (string) $value === '' || ! in_array(
148 $value,
149 [
150 'on',
151 'off',
152 ],
153 true
154 ) ) ) {
155 continue;
156 }
157
158 if ( $type === 'int' && ( (string) $value === '' || (int) $value > 100 || (int) $value < 0 ) ) {
159 continue;
160 }
161 $sanitized_value = ( $type === 'bool' ) ? ( $value === 'on' ? 'enabled' : 'disabled' ) : (int) $value;
162 $this->options[ $key ] = $sanitized_value;
163 }
164 }
165 }
166
167 add_action( 'init', [ $this, 'register_settings' ] );
168 }
169
170 /**
171 * Check if the user is connected to Optimole.
172 *
173 * @return bool Connection status.
174 */
175 public function is_connected() {
176 $service_data = $this->get( 'service_data' );
177 if ( ! isset( $service_data['cdn_key'] ) ) {
178 return false;
179 }
180 if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) {
181 return false;
182 }
183
184 return true;
185 }
186
187 /**
188 * Get setting value by key.
189 *
190 * @param string $key Key to search against.
191 *
192 * @return mixed|null Setting value.
193 */
194 public function get( $key ) {
195 if ( ! $this->is_allowed( $key ) ) {
196 return null;
197 }
198
199 return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
200 }
201
202 /**
203 * Check if key is allowed.
204 *
205 * @param string $key Is key allowed or not.
206 *
207 * @return bool Is key allowed or not.
208 */
209 private function is_allowed( $key ) {
210 return isset( $this->default_schema[ $key ] );
211 }
212
213 /**
214 * Auto connect action.
215 */
216 public function auto_connect() {
217 $request = new WP_REST_Request( 'POST' );
218 $request->set_param( 'api_key', constant( 'OPTIML_API_KEY' ) );
219 Optml_Main::instance()->rest->connect( $request );
220
221 remove_action( 'plugins_loaded', [ $this, 'auto_connect' ] );
222 self::$auto_connect_hooked = false;
223 }
224
225 /**
226 * Process settings.
227 *
228 * @param array $new_settings List of settings.
229 *
230 * @return array
231 */
232 public function parse_settings( $new_settings ) {
233 $sanitized = [];
234 foreach ( $new_settings as $key => $value ) {
235 switch ( $key ) {
236 case 'admin_bar_item':
237 case 'lazyload':
238 case 'scale':
239 case 'image_replacer':
240 case 'cdn':
241 case 'network_optimization':
242 case 'lazyload_placeholder':
243 case 'retina_images':
244 case 'limit_dimensions':
245 case 'resize_smart':
246 case 'bg_replacer':
247 case 'video_lazyload':
248 case 'report_script':
249 case 'avif':
250 case 'offload_media':
251 case 'cloud_images':
252 case 'autoquality':
253 case 'img_to_video':
254 case 'css_minify':
255 case 'js_minify':
256 case 'native_lazyload':
257 case 'strip_metadata':
258 case 'no_script':
259 case 'banner_frontend':
260 case 'offloading_status':
261 case 'rollback_status':
262 case 'best_format':
263 $sanitized_value = $this->to_map_values( $value, [ 'enabled', 'disabled' ], 'enabled' );
264 break;
265 case 'max_width':
266 case 'max_height':
267 case 'limit_height':
268 case 'limit_width':
269 $sanitized_value = $this->to_bound_integer( $value, 100, 5000 );
270 break;
271 case 'quality':
272 $sanitized_value = $this->to_bound_integer( $value, 1, 100 );
273 break;
274 case 'wm_id':
275 $sanitized_value = intval( $value );
276 break;
277 case 'cache_buster_assets':
278 case 'cache_buster_images':
279 case 'cache_buster':
280 $sanitized_value = is_string( $value ) ? sanitize_text_field( $value ) : '';
281 break;
282 case 'cloud_sites':
283 $current_sites = $this->get( 'cloud_sites' );
284 $sanitized_value = array_replace_recursive( $current_sites, $value );
285 if ( isset( $value['all'] ) && $value['all'] === 'true' ) {
286 $sanitized_value = [ 'all' => 'true' ];
287 }
288 break;
289 case 'defined_image_sizes':
290 $current_sizes = $this->get( 'defined_image_sizes' );
291 foreach ( $value as $size_name => $size_value ) {
292 if ( $size_value === 'remove' ) {
293 unset( $current_sizes[ $size_name ] );
294 unset( $value[ $size_name ] );
295 }
296 }
297 $sanitized_value = array_replace_recursive( $current_sizes, $value );
298 break;
299 case 'filters':
300 $current_filters = $this->get_filters();
301 $sanitized_value = array_replace_recursive( $current_filters, $value );
302 // Remove falsy vars.
303 foreach ( $sanitized_value as $filter_type => $filter_values ) {
304 foreach ( $filter_values as $filter_rule_type => $filter_rules_value ) {
305 $sanitized_value[ $filter_type ][ $filter_rule_type ] = array_filter(
306 $filter_rules_value,
307 function ( $value ) {
308 return ( $value !== 'false' && $value !== false );
309 }
310 );
311 }
312 }
313 break;
314 case 'watchers':
315 case 'placeholder_color':
316 $sanitized_value = sanitize_text_field( $value );
317 break;
318 case 'skip_lazyload_images':
319 $sanitized_value = $this->to_bound_integer( $value, 0, 100 );
320 break;
321 case 'wm_opacity':
322 case 'wm_scale':
323 case 'wm_x':
324 case 'wm_y':
325 $sanitized_value = floatval( $value );
326 break;
327 case 'wm_position':
328 $sanitized_value = $this->to_map_values(
329 $value,
330 [
331 Optml_Resize::GRAVITY_NORTH,
332 Optml_Resize::GRAVITY_NORTH_EAST,
333 Optml_Resize::GRAVITY_NORTH_WEST,
334 Optml_Resize::GRAVITY_CENTER,
335 Optml_Resize::GRAVITY_EAST,
336 Optml_Resize::GRAVITY_WEST,
337 Optml_Resize::GRAVITY_SOUTH_EAST,
338 Optml_Resize::GRAVITY_SOUTH,
339 Optml_Resize::GRAVITY_SOUTH_WEST,
340 ],
341 Optml_Resize::GRAVITY_SOUTH_EAST
342 );
343 break;
344 default:
345 $sanitized_value = '';
346 break;
347
348 }
349
350 $sanitized[ $key ] = $sanitized_value;
351 $this->update( $key, $sanitized_value );
352 }
353
354 return $sanitized;
355 }
356
357 /**
358 * Return filter definitions.
359 *
360 * @return mixed|null Filter values.
361 */
362 public function get_filters() {
363
364 $filters = $this->get( 'filters' );
365 if ( ! isset( $filters[ self::FILTER_TYPE_LAZYLOAD ] ) ) {
366 $filters[ self::FILTER_TYPE_LAZYLOAD ] = [];
367 }
368 if ( ! isset( $filters[ self::FILTER_TYPE_OPTIMIZE ] ) ) {
369 $filters[ self::FILTER_TYPE_OPTIMIZE ] = [];
370 }
371 foreach ( $filters as $filter_key => $filter_rules ) {
372 if ( ! isset( $filter_rules[ self::FILTER_EXT ] ) ) {
373 $filters[ $filter_key ][ self::FILTER_EXT ] = [];
374 }
375 if ( ! isset( $filter_rules[ self::FILTER_FILENAME ] ) ) {
376 $filters[ $filter_key ][ self::FILTER_FILENAME ] = [];
377 }
378 if ( ! isset( $filter_rules[ self::FILTER_URL ] ) ) {
379 $filters[ $filter_key ][ self::FILTER_URL ] = [];
380 }
381 if ( ! isset( $filter_rules[ self::FILTER_URL_MATCH ] ) ) {
382 $filters[ $filter_key ][ self::FILTER_URL_MATCH ] = [];
383 }
384 if ( ! isset( $filter_rules[ self::FILTER_CLASS ] ) ) {
385 $filters[ $filter_key ][ self::FILTER_CLASS ] = [];
386 }
387 }
388
389 return $filters;
390 }
391
392 /**
393 * Update frontend banner setting from remote.
394 *
395 * @param bool $value Value.
396 *
397 * @return bool
398 */
399 public function update_frontend_banner_from_remote( $value ) {
400 if ( ! $this->is_main_mu_site() ) {
401 return false;
402 }
403
404 $opts = $this->options;
405 $opts['banner_frontend'] = $value ? 'enabled' : 'disabled';
406
407 $update = update_option( $this->namespace, $opts, false );
408
409 if ( $update ) {
410 $this->options = $opts;
411 }
412
413 return $update;
414 }
415
416 /**
417 * Update settings.
418 *
419 * @param string $key Settings key.
420 * @param mixed $value Settings value.
421 *
422 * @return bool Update result.
423 */
424 public function update( $key, $value ) {
425 if ( ! $this->is_allowed( $key ) ) {
426 return false;
427 }
428
429 if ( ! $this->is_main_mu_site() ) {
430 return false;
431 }
432 $opt = $this->options;
433
434 if ( $key === 'banner_frontend' ) {
435 $api = new Optml_Api();
436 $service_data = $this->get( 'service_data' );
437 $application = isset( $service_data['cdn_key'] ) ? $service_data['cdn_key'] : '';
438 $response = $api->update_extra_visits( $opt['api_key'], $value, $application );
439 }
440
441 $opt[ $key ] = $value;
442 $update = update_option( $this->namespace, $opt, false );
443 if ( $update ) {
444 $this->options = $opt;
445 }
446 if ( apply_filters( 'optml_dont_trigger_settings_updated', false ) === false ) {
447 do_action( 'optml_settings_updated' );
448 }
449 return $update;
450 }
451
452 /**
453 * Check that we're on the main OPTML blog.
454 *
455 * @return bool
456 */
457 private function is_main_mu_site() {
458 // If we try to update from a website which is not the main OPTML blog, bail.
459 if ( defined( 'OPTIML_ENABLED_MU' ) && constant( 'OPTIML_ENABLED_MU' ) && defined( 'OPTIML_MU_SITE_ID' ) && constant( 'OPTIML_MU_SITE_ID' ) &&
460 intval( constant( 'OPTIML_MU_SITE_ID' ) ) !== get_current_blog_id()
461 ) {
462 return false;
463 }
464
465 return true;
466 }
467
468 /**
469 * Return site settings.
470 *
471 * @return array Site settings.
472 */
473 public function get_site_settings() {
474 $service_data = $this->get( 'service_data' );
475 $whitelist = [];
476 if ( isset( $service_data['whitelist'] ) ) {
477 $whitelist = $service_data['whitelist'];
478 }
479
480 return [
481 'quality' => $this->get_quality(),
482 'admin_bar_item' => $this->get( 'admin_bar_item' ),
483 'lazyload' => $this->get( 'lazyload' ),
484 'network_optimization' => $this->get( 'network_optimization' ),
485 'retina_images' => $this->get( 'retina_images' ),
486 'limit_dimensions' => $this->get( 'limit_dimensions' ),
487 'limit_height' => $this->get( 'limit_height' ),
488 'limit_width' => $this->get( 'limit_width' ),
489 'lazyload_placeholder' => $this->get( 'lazyload_placeholder' ),
490 'skip_lazyload_images' => $this->get( 'skip_lazyload_images' ),
491 'bg_replacer' => $this->get( 'bg_replacer' ),
492 'video_lazyload' => $this->get( 'video_lazyload' ),
493 'resize_smart' => $this->get( 'resize_smart' ),
494 'no_script' => $this->get( 'no_script' ),
495 'image_replacer' => $this->get( 'image_replacer' ),
496 'cdn' => $this->get( 'cdn' ),
497 'max_width' => $this->get( 'max_width' ),
498 'max_height' => $this->get( 'max_height' ),
499 'filters' => $this->get_filters(),
500 'cloud_sites' => $this->get( 'cloud_sites' ),
501 'defined_image_sizes' => $this->get( 'defined_image_sizes' ),
502 'watchers' => $this->get_watchers(),
503 'watermark' => $this->get_watermark(),
504 'img_to_video' => $this->get( 'img_to_video' ),
505 'scale' => $this->get( 'scale' ),
506 'css_minify' => $this->get( 'css_minify' ),
507 'js_minify' => $this->get( 'js_minify' ),
508 'native_lazyload' => $this->get( 'native_lazyload' ),
509 'report_script' => $this->get( 'report_script' ),
510 'avif' => $this->get( 'avif' ),
511 'autoquality' => $this->get( 'autoquality' ),
512 'offload_media' => $this->get( 'offload_media' ),
513 'cloud_images' => $this->get( 'cloud_images' ),
514 'strip_metadata' => $this->get( 'strip_metadata' ),
515 'whitelist_domains' => $whitelist,
516 'banner_frontend' => $this->get( 'banner_frontend' ),
517 'offloading_status' => $this->get( 'offloading_status' ),
518 'rollback_status' => $this->get( 'rollback_status' ),
519 'best_format' => $this->get( 'best_format' ),
520 'placeholder_color' => $this->get( 'placeholder_color' ),
521 ];
522 }
523
524 /**
525 * Return quality factor.
526 *
527 * @return string Quality factor.
528 */
529 public function get_quality() {
530 $quality = $this->get( 'quality' );
531 if ( $this->get( 'autoquality' ) === 'enabled' ) {
532 return 'mauto';
533 }
534 // Legacy compat.
535 if ( $quality === 'low' ) {
536 return 'high_c';
537 }
538 if ( $quality === 'medium' ) {
539 return 'medium_c';
540 }
541
542 if ( $quality === 'high' ) {
543 return 'low_c';
544 }
545
546 return $quality;
547 }
548
549 /**
550 * Return filter definitions.
551 *
552 * @return mixed|null Filter values.
553 */
554 public function get_watchers() {
555
556 return $this->get( 'watchers' );
557
558 }
559
560 /**
561 * Return an watermark array.
562 *
563 * @return array
564 */
565 public function get_watermark() {
566 return [
567 'id' => $this->get( 'wm_id' ),
568 'opacity' => $this->get( 'wm_opacity' ),
569 'position' => $this->get( 'wm_position' ),
570 'x_offset' => $this->get( 'wm_x' ),
571 'y_offset' => $this->get( 'wm_y' ),
572 'scale' => $this->get( 'wm_scale' ),
573 ];
574 }
575
576 /**
577 * Check if smart cropping is enabled.
578 *
579 * @return bool Is smart cropping enabled.
580 */
581 public function is_smart_cropping() {
582 return $this->get( 'resize_smart' ) === 'enabled';
583 }
584
585 /**
586 * Check if best format is enabled.
587 *
588 * @return bool
589 */
590 public function is_best_format() {
591 return $this->get( 'best_format' ) === 'enabled';
592 }
593
594 /**
595 * Get numeric quality used by the service.
596 *
597 * @return int Numeric quality.
598 */
599 public function get_numeric_quality() {
600 $value = $this->get_quality();
601
602 return (int) $this->to_accepted_quality( $value );
603 }
604
605 /**
606 * Check if replacer is enabled.
607 *
608 * @return bool Replacer enabled
609 */
610 public function is_enabled() {
611 $status = $this->get( 'image_replacer' );
612 $service_data = $this->get( 'service_data' );
613 if ( empty( $service_data ) ) {
614 return false;
615 }
616 if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) {
617 return false;
618 }
619
620 return $this->to_boolean( $status );
621 }
622
623 /**
624 * Check if lazyload is enabled.
625 *
626 * @return bool Lazyload enabled
627 */
628 public function use_lazyload() {
629 $status = $this->get( 'lazyload' );
630
631 return $this->to_boolean( $status );
632 }
633
634 /**
635 * Check if replacer is enabled.
636 *
637 * @return bool Replacer enabled
638 */
639 public function use_cdn() {
640 $status = $this->get( 'cdn' );
641
642 return $this->to_boolean( $status );
643 }
644
645 /**
646 * Return cdn url.
647 *
648 * @return string CDN url.
649 */
650 public function get_cdn_url() {
651 $service_data = $this->get( 'service_data' );
652 if ( ! isset( $service_data['cdn_key'] ) ) {
653 return '';
654 }
655
656 if ( isset( $service_data['is_cname_assigned'] ) && $service_data['is_cname_assigned'] === 'yes' && ! empty( $service_data['domain'] ) ) {
657 return strtolower( $service_data['domain'] );
658 }
659
660 if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
661 return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
662 }
663
664 return sprintf(
665 '%s.%s',
666 strtolower( $service_data['cdn_key'] ),
667 Optml_Config::$base_domain
668 );
669 }
670
671 /**
672 * Reset options to defaults.
673 *
674 * @return bool Reset action status.
675 */
676 public function reset() {
677 $reset_schema = $this->default_schema;
678 $reset_schema['filters'] = $this->options['filters'];
679
680 $update = update_option( $this->namespace, $reset_schema );
681 if ( $update ) {
682 $this->options = $reset_schema;
683 }
684
685 return $update;
686 }
687 /**
688 * Get raw settings value.
689 *
690 * @return array
691 */
692 public function get_raw_settings() {
693 return get_option( $this->namespace, false );
694 }
695
696 /**
697 * Get settings for CSAT.
698 *
699 * @return void
700 */
701 public function register_settings() {
702 register_setting(
703 'optml_settings',
704 'optml_csat',
705 [
706 'type' => 'string',
707 'sanitize_callback' => 'sanitize_text_field',
708 'show_in_rest' => true,
709 'default' => '{}',
710 ]
711 );
712 }
713
714 /**
715 * Clear cache.
716 *
717 * @param string $type Cache type.
718 *
719 * @return string|WP_Error
720 */
721 public function clear_cache( $type = '' ) {
722 $token = $this->get( 'cache_buster' );
723 $token_images = $this->get( 'cache_buster_images' );
724
725 if ( ! empty( $token_images ) ) {
726 $token = $token_images;
727 }
728
729 if ( ! empty( $type ) && $type === 'assets' ) {
730 $token = $this->get( 'cache_buster_assets' );
731 }
732
733 $request = new Optml_Api();
734 $data = $request->get_cache_token( $token, $type );
735
736 if ( $data === false || is_wp_error( $data ) || empty( $data ) || ! isset( $data['token'] ) ) {
737 $extra = '';
738
739 if ( is_wp_error( $data ) ) {
740 /**
741 * Error from api.
742 *
743 * @var WP_Error $data Error object.
744 */
745 $extra = sprintf( __( '. ERROR details: %s', 'optimole-wp' ), $data->get_error_message() );
746 }
747
748 return new WP_Error( 'optimole_cache_buster_error', __( 'Can not get new token from Optimole service', 'optimole-wp' ) . $extra );
749 }
750
751 if ( ! empty( $type ) && $type === 'assets' ) {
752 set_transient( 'optml_cache_lock_assets', 'yes', 5 * MINUTE_IN_SECONDS );
753 $this->update( 'cache_buster_assets', $data['token'] );
754 } else {
755 set_transient( 'optml_cache_lock', 'yes', 5 * MINUTE_IN_SECONDS );
756 $this->update( 'cache_buster_images', $data['token'] );
757 }
758
759 return $data['token'];
760 }
761 }
762