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