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 -409 3.0.01.0.4 View file →
@@ -3,76 +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 -
74 - ];
13 + private $default_schema = array(
14 + 'api_key' => '',
15 + 'service_data' => '',
16 + 'admin_bar_item' => 'enabled',
17 + 'quality' => 'auto',
18 + 'image_replacer' => 'enabled',
19 + );
75 20 /**
76 21 * Option key.
77 22 *
78 23 * @var string Option name.
@@ -88,232 +33,18 @@
88 33 /**
89 34 * Optml_Settings constructor.
90 35 */
91 36 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' ) );
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 );
98 41 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
99 42 restore_current_blog();
100 43 }
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 44 }
127 45
128 46 /**
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 47 * Check if the user is connected to Optimole.
317 48 *
318 49 * @return bool Connection status.
319 50 */
@@ -340,150 +71,40 @@
340 71 if ( ! $this->is_allowed( $key ) ) {
341 72 return null;
342 73 }
343 74
344 - return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
75 + return isset ( $this->options[ $key ] ) ? $this->options[ $key ] : '';
345 76 }
346 77
347 78 /**
348 - * Return site settings.
79 + * Check if key is allowed.
349 80 *
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.
81 + * @param string $key Is key allowed or not.
390 82 *
391 - * @return string Quality factor.
83 + * @return bool Is key allowed or not.
392 84 */
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;
85 + private function is_allowed( $key ) {
86 + return isset( $this->default_schema[ $key ] );
408 87 }
409 88
410 89 /**
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 90 * Check if replacer is enabled.
448 91 *
449 92 * @return bool Replacer enabled
450 93 */
451 94 public function is_enabled() {
452 - $status = $this->get( 'image_replacer' );
453 - $service_data = $this->get( 'service_data' );
454 - if ( empty( $service_data ) ) {
95 + $status = $this->get( 'image_replacer' );
96 + if ( $status === 'disabled' ) {
455 97 return false;
456 98 }
457 - if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) {
99 + if ( empty( $status ) ) {
458 100 return false;
459 101 }
460 - return $this->to_boolean( $status );
461 - }
462 102
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 );
103 + return true;
472 104 }
473 105
474 106 /**
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 107 * Return cdn url.
487 108 *
488 109 * @return string CDN url.
489 110 */
@@ -491,16 +112,13 @@
491 112 $service_data = $this->get( 'service_data' );
492 113 if ( ! isset( $service_data['cdn_key'] ) ) {
493 114 return '';
494 115 }
495 - if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
496 - return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
497 - }
498 116
499 117 return sprintf(
500 118 '%s.%s',
501 119 strtolower( $service_data['cdn_key'] ),
502 - Optml_Config::$base_domain
120 + 'i.optimole.com'
503 121 );
504 122 }
505 123
506 124 /**
@@ -508,14 +126,40 @@
508 126 *
509 127 * @return bool Reset action status.
510 128 */
511 129 public function reset() {
512 - $reset_schema = $this->default_schema;
513 - $reset_schema['filters'] = $this->options['filters'];
514 130
515 - $update = update_option( $this->namespace, $reset_schema );
131 + $update = update_option( $this->namespace, $this->default_schema );
516 132 if ( $update ) {
517 - $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;
518 162 }
519 163
520 164 return $update;
521 165 }