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

459 lines 11.7 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 * Default settings schema.
16 *
17 * @var array Settings schema.
18 */
19 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
51 );
52 /**
53 * Option key.
54 *
55 * @var string Option name.
56 */
57 private $namespace;
58 /**
59 * Holds all options from db.
60 *
61 * @var array All options.
62 */
63 private $options;
64
65 /**
66 * Optml_Settings constructor.
67 */
68 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' ) );
75 $this->options = wp_parse_args( get_option( $this->namespace, $this->default_schema ), $this->default_schema );
76 restore_current_blog();
77 }
78 }
79
80 /**
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 * Check if the user is connected to Optimole.
260 *
261 * @return bool Connection status.
262 */
263 public function is_connected() {
264 $service_data = $this->get( 'service_data' );
265 if ( ! isset( $service_data['cdn_key'] ) ) {
266 return false;
267 }
268 if ( empty( $service_data ['cdn_key'] ) || empty( $service_data['cdn_secret'] ) ) {
269 return false;
270 }
271
272 return true;
273 }
274
275 /**
276 * Get setting value by key.
277 *
278 * @param string $key Key to search against.
279 *
280 * @return mixed|null Setting value.
281 */
282 public function get( $key ) {
283 if ( ! $this->is_allowed( $key ) ) {
284 return null;
285 }
286
287 return isset( $this->options[ $key ] ) ? $this->options[ $key ] : '';
288 }
289
290 /**
291 * Return site settings.
292 *
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.
325 *
326 * @return string Quality factor.
327 */
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;
343 }
344
345 /**
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 * Check if replacer is enabled.
383 *
384 * @return bool Replacer enabled
385 */
386 public function is_enabled() {
387 $status = $this->get( 'image_replacer' );
388 $service_data = $this->get( 'service_data' );
389 if ( empty( $service_data ) ) {
390 return false;
391 }
392 if ( isset( $service_data['status'] ) && $service_data['status'] === 'inactive' ) {
393 return false;
394 }
395 return $this->to_boolean( $status );
396 }
397
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 );
407 }
408
409 /**
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 * Return cdn url.
422 *
423 * @return string CDN url.
424 */
425 public function get_cdn_url() {
426 $service_data = $this->get( 'service_data' );
427 if ( ! isset( $service_data['cdn_key'] ) ) {
428 return '';
429 }
430 if ( defined( 'OPTML_CUSTOM_DOMAIN' ) && constant( 'OPTML_CUSTOM_DOMAIN' ) ) {
431 return parse_url( strtolower( OPTML_CUSTOM_DOMAIN ), PHP_URL_HOST );
432 }
433
434 return sprintf(
435 '%s.%s',
436 strtolower( $service_data['cdn_key'] ),
437 'i.optimole.com'
438 );
439 }
440
441 /**
442 * Reset options to defaults.
443 *
444 * @return bool Reset action status.
445 */
446 public function reset() {
447 $reset_schema = $this->default_schema;
448 $reset_schema['filters'] = $this->options['filters'];
449
450 $update = update_option( $this->namespace, $reset_schema );
451 if ( $update ) {
452 $this->options = $reset_schema;
453 }
454
455 return $update;
456 }
457
458 }
459