PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.4
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.4
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / classes / class-imagify-options.php

class-imagify-options.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.9.4, at inc/classes/class-imagify-options.php

236 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3
4 /**
5 * Class that handles the plugin options.
6 *
7 * @since 1.7
8 */
9 class Imagify_Options extends Imagify_Abstract_Options {
10
11 /**
12 * Class version.
13 *
14 * @var string
15 * @since 1.7
16 */
17 const VERSION = '1.1';
18
19 /**
20 * Suffix used in the name of the option.
21 *
22 * @var string
23 * @since 1.7
24 * @access protected
25 */
26 protected $identifier = 'settings';
27
28 /**
29 * The default values for the Imagify main options.
30 * These are the "zero state" values.
31 * Don't use null as value.
32 *
33 * @var array
34 * @since 1.7
35 * @access protected
36 */
37 protected $default_values = array(
38 'api_key' => '',
39 'optimization_level' => 0,
40 'auto_optimize' => 0,
41 'backup' => 0,
42 'resize_larger' => 0,
43 'resize_larger_w' => 0,
44 'convert_to_webp' => 0,
45 'display_webp' => 0,
46 'display_webp_method' => 'picture',
47 'cdn_url' => '',
48 'exif' => 0,
49 'disallowed-sizes' => array(),
50 'admin_bar_menu' => 0,
51 'partner_links' => 0,
52 );
53
54 /**
55 * The Imagify main option values used when they are set the first time or reset.
56 * Values identical to default values are not listed.
57 *
58 * @var array
59 * @since 1.7
60 * @access protected
61 */
62 protected $reset_values = array(
63 'optimization_level' => 1,
64 'auto_optimize' => 1,
65 'backup' => 1,
66 'convert_to_webp' => 1,
67 'admin_bar_menu' => 1,
68 'partner_links' => 1,
69 );
70
71 /**
72 * The single instance of the class.
73 *
74 * @var object
75 * @since 1.7
76 * @access protected
77 */
78 protected static $_instance;
79
80 /**
81 * The constructor.
82 * Side note: $this->hook_identifier value is "option".
83 *
84 * @since 1.7
85 * @author Grégory Viguier
86 * @access protected
87 */
88 protected function __construct() {
89 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
90 $this->default_values['api_key'] = (string) IMAGIFY_API_KEY;
91 }
92
93 $this->network_option = imagify_is_active_for_network();
94
95 parent::__construct();
96 }
97
98 /**
99 * Get the main Instance.
100 *
101 * @since 1.7
102 * @author Grégory Viguier
103 * @access public
104 *
105 * @return object Main instance.
106 */
107 public static function get_instance() {
108 if ( ! isset( self::$_instance ) ) {
109 self::$_instance = new self();
110 }
111
112 return self::$_instance;
113 }
114
115
116 /** ----------------------------------------------------------------------------------------- */
117 /** SANITIZATION, VALIDATION ================================================================ */
118 /** ----------------------------------------------------------------------------------------- */
119
120 /**
121 * Sanitize and validate an option value. Basic casts have been made.
122 *
123 * @since 1.7
124 * @author Grégory Viguier
125 * @access public
126 *
127 * @param string $key The option key.
128 * @param mixed $value The value.
129 * @param mixed $default The default value.
130 * @return mixed
131 */
132 public function sanitize_and_validate_value( $key, $value, $default ) {
133 static $max_sizes;
134
135 switch ( $key ) {
136 case 'api_key':
137 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
138 return (string) IMAGIFY_API_KEY;
139 }
140 return $value ? sanitize_key( $value ) : '';
141
142 case 'optimization_level':
143 if ( $value < 0 || $value > 2 ) {
144 // For an invalid value, return the "reset" value.
145 $reset_values = $this->get_reset_values();
146 return $reset_values[ $key ];
147 }
148 return $value;
149
150 case 'auto_optimize':
151 case 'backup':
152 case 'resize_larger':
153 case 'convert_to_webp':
154 case 'display_webp':
155 case 'exif':
156 case 'admin_bar_menu':
157 case 'partner_links':
158 return 1;
159
160 case 'resize_larger_w':
161 if ( $value <= 0 ) {
162 // Invalid.
163 return 0;
164 }
165 if ( ! isset( $max_sizes ) ) {
166 $max_sizes = get_imagify_max_intermediate_image_size();
167 }
168 if ( $value < $max_sizes['width'] ) {
169 // Invalid.
170 return $max_sizes['width'];
171 }
172 return $value;
173
174 case 'disallowed-sizes':
175 if ( ! $value ) {
176 return $default;
177 }
178
179 $value = array_keys( $value );
180 $value = array_map( 'sanitize_text_field', $value );
181 return array_fill_keys( $value, 1 );
182
183 case 'display_webp_method':
184 $values = [
185 'picture' => 1,
186 'rewrite' => 1,
187 ];
188 if ( isset( $values[ $value ] ) ) {
189 return $value;
190 }
191 // For an invalid value, return the "reset" value.
192 $reset_values = $this->get_reset_values();
193 return $reset_values[ $key ];
194
195 case 'cdn_url':
196 $cdn_source = \Imagify\Webp\Picture\Display::get_instance()->get_cdn_source( $value );
197
198 if ( 'option' !== $cdn_source['source'] ) {
199 /**
200 * If the URL is defined via constant or filter, unset the option.
201 * This is useful when the CDN is disabled: there is no need to do anything then.
202 */
203 return '';
204 }
205
206 return $cdn_source['url'];
207 }
208
209 return false;
210 }
211
212 /**
213 * Validate Imagify's options before storing them. Basic sanitization and validation have been made, row by row.
214 *
215 * @since 1.7
216 * @author Grégory Viguier
217 * @access public
218 *
219 * @param string $values The option value.
220 * @return array
221 */
222 public function validate_values_on_update( $values ) {
223 // The max width for the "Resize larger images" option can't be 0.
224 if ( empty( $values['resize_larger_w'] ) ) {
225 unset( $values['resize_larger'], $values['resize_larger_w'] );
226 }
227
228 // Don't display wepb if conversion is disabled.
229 if ( empty( $values['convert_to_webp'] ) ) {
230 unset( $values['convert_to_webp'], $values['display_webp'] );
231 }
232
233 return $values;
234 }
235 }
236