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

504 lines 13.1 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 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';
14 /**
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 * Default settings schema.
36 *
37 * @var array Settings schema.
38 */
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 ];
72 /**
73 * Option key.
74 *
75 * @var string Option name.
76 */
77 private $namespace;
78 /**
79 * Holds all options from db.
80 *
81 * @var array All options.
82 */
83 private $options;
84
85 /**
86 * Optml_Settings constructor.
87 */
88 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' ) );
95 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
96 restore_current_blog();
97 }
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 }
124
125 /**
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 * Check if the user is connected to Optimole.
305 *
306 * @return bool Connection status.
307 */
308 public function is_connected() {
309 $service_data = $this->get( 'service_data' );
310 if ( ! isset( $service_data['cdn_key'] ) ) {
311 return false;
312 }
313 if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) {
314 return false;
315 }
316
317 return true;
318 }
319
320 /**
321 * Get setting value by key.
322 *
323 * @param string $key Key to search against.
324 *
325 * @return mixed|null Setting value.
326 */
327 public function get( $key ) {
328 if ( ! $this->is_allowed( $key ) ) {
329 return null;
330 }
331
332 return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
333 }
334
335 /**
336 * Return site settings.
337 *
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.
370 *
371 * @return string Quality factor.
372 */
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;
388 }
389
390 /**
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 * Check if replacer is enabled.
428 *
429 * @return bool Replacer enabled
430 */
431 public function is_enabled() {
432 $status = $this->get( 'image_replacer' );
433 $service_data = $this->get( 'service_data' );
434 if ( empty( $service_data ) ) {
435 return false;
436 }
437 if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) {
438 return false;
439 }
440 return $this->to_boolean( $status );
441 }
442
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 );
452 }
453
454 /**
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 * Return cdn url.
467 *
468 * @return string CDN url.
469 */
470 public function get_cdn_url() {
471 $service_data = $this->get( 'service_data' );
472 if ( ! isset( $service_data['cdn_key'] ) ) {
473 return '';
474 }
475 if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
476 return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
477 }
478
479 return sprintf(
480 '%s.%s',
481 strtolower( $service_data['cdn_key'] ),
482 'i.optimole.com'
483 );
484 }
485
486 /**
487 * Reset options to defaults.
488 *
489 * @return bool Reset action status.
490 */
491 public function reset() {
492 $reset_schema = $this->default_schema;
493 $reset_schema['filters'] = $this->options['filters'];
494
495 $update = update_option( $this->namespace, $reset_schema );
496 if ( $update ) {
497 $this->options = $reset_schema;
498 }
499
500 return $update;
501 }
502
503 }
504