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