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