PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
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
← All changes | inc/classes/class-imagify-options.php +52 -91 1.10 → trunk View file →
@@ -1,7 +1,8 @@
1 1 <?php
2 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3 2
3 +use Imagify\Traits\InstanceGetterTrait;
4 +
4 5 /**
5 6 * Class that handles the plugin options.
6 7 *
7 8 * @since 1.7
@@ -6,24 +7,16 @@
6 7 *
7 8 * @since 1.7
8 9 */
9 10 class Imagify_Options extends Imagify_Abstract_Options {
11 + use InstanceGetterTrait;
10 12
11 13 /**
12 - * Class version.
14 + * Suffix used in the name of the option.
13 15 *
14 16 * @var string
15 17 * @since 1.7
16 18 */
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 19 protected $identifier = 'settings';
27 20
28 21 /**
29 22 * The default values for the Imagify main options.
@@ -29,62 +22,52 @@
29 22 * The default values for the Imagify main options.
30 23 * These are the "zero state" values.
31 24 * Don't use null as value.
32 25 *
33 - * @var array
34 - * @since 1.7
35 - * @access protected
26 + * @var array
27 + * @since 1.7
36 28 */
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 - );
29 + protected $default_values = [
30 + 'api_key' => '',
31 + 'optimization_level' => 2,
32 + 'lossless' => 0,
33 + 'auto_optimize' => 0,
34 + 'backup' => 0,
35 + 'resize_larger' => 0,
36 + 'resize_larger_w' => 0,
37 + 'display_nextgen' => 0,
38 + 'display_nextgen_method' => 'picture',
39 + 'display_webp' => 0,
40 + 'display_webp_method' => 'picture',
41 + 'cdn_url' => '',
42 + 'disallowed-sizes' => [],
43 + 'admin_bar_menu' => 1,
44 + 'partner_links' => 0,
45 + 'convert_to_avif' => 0,
46 + 'convert_to_webp' => 0,
47 + 'optimization_format' => 'webp',
48 + ];
53 49
54 50 /**
55 51 * The Imagify main option values used when they are set the first time or reset.
56 52 * Values identical to default values are not listed.
57 53 *
58 - * @var array
59 - * @since 1.7
60 - * @access protected
54 + * @var array
55 + * @since 1.7
61 56 */
62 - protected $reset_values = array(
63 - 'optimization_level' => 1,
57 + protected $reset_values = [
58 + 'optimization_level' => 2,
64 59 'auto_optimize' => 1,
65 60 'backup' => 1,
66 - 'convert_to_webp' => 1,
67 61 'admin_bar_menu' => 1,
68 62 'partner_links' => 1,
69 - );
63 + ];
70 64
71 65 /**
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 66 * The constructor.
82 67 * Side note: $this->hook_identifier value is "option".
83 68 *
84 - * @since 1.7
85 - * @author Grégory Viguier
86 - * @access protected
69 + * @since 1.7
87 70 */
88 71 protected function __construct() {
89 72 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
90 73 $this->default_values['api_key'] = (string) IMAGIFY_API_KEY;
@@ -92,9 +75,9 @@
92 75
93 76 if ( function_exists( 'wp_get_original_image_path' ) ) {
94 77 $this->reset_values['resize_larger'] = 1;
95 78
96 - $filter_cb = [ imagify_get_context( 'wp' ), 'get_resizing_threshold' ];
79 + $filter_cb = [ imagify_get_context( 'wp' ), 'filter_big_image_size_threshold' ];
97 80 $filtered = has_filter( 'big_image_size_threshold', $filter_cb );
98 81
99 82 if ( $filtered ) {
100 83 remove_filter( 'big_image_size_threshold', $filter_cb, IMAGIFY_INT_MAX );
@@ -114,42 +97,18 @@
114 97 parent::__construct();
115 98 }
116 99
117 100 /**
118 - * Get the main Instance.
119 - *
120 - * @since 1.7
121 - * @author Grégory Viguier
122 - * @access public
123 - *
124 - * @return object Main instance.
125 - */
126 - public static function get_instance() {
127 - if ( ! isset( self::$_instance ) ) {
128 - self::$_instance = new self();
129 - }
130 -
131 - return self::$_instance;
132 - }
133 -
134 -
135 - /** ----------------------------------------------------------------------------------------- */
136 - /** SANITIZATION, VALIDATION ================================================================ */
137 - /** ----------------------------------------------------------------------------------------- */
138 -
139 - /**
140 101 * Sanitize and validate an option value. Basic casts have been made.
141 102 *
142 - * @since 1.7
143 - * @author Grégory Viguier
144 - * @access public
103 + * @since 1.7
145 104 *
146 105 * @param string $key The option key.
147 106 * @param mixed $value The value.
148 - * @param mixed $default The default value.
107 + * @param mixed $default_value The default value.
149 108 * @return mixed
150 109 */
151 - public function sanitize_and_validate_value( $key, $value, $default ) {
110 + public function sanitize_and_validate_value( $key, $value, $default_value ) {
152 111 static $max_sizes;
153 112
154 113 switch ( $key ) {
155 114 case 'api_key':
@@ -164,23 +123,31 @@
164 123 $reset_values = $this->get_reset_values();
165 124 return $reset_values[ $key ];
166 125 }
167 126 return $value;
168 -
127 + case 'optimization_format':
128 + if ( ! in_array( $value, [ 'off', 'webp', 'avif' ], true ) ) {
129 + // For an invalid value, return the "reset" value.
130 + $reset_values = $this->get_reset_values();
131 + return $reset_values[ $key ];
132 + }
133 + return $value;
169 134 case 'auto_optimize':
170 135 case 'backup':
136 + case 'lossless':
171 137 case 'resize_larger':
172 138 case 'convert_to_webp':
139 + case 'display_nextgen':
173 140 case 'display_webp':
174 - case 'exif':
175 141 case 'admin_bar_menu':
176 142 case 'partner_links':
177 - return 1;
143 + case 'convert_to_avif':
144 + return empty( $value ) ? 0 : 1;
178 145
179 146 case 'resize_larger_w':
180 147 if ( $value <= 0 ) {
181 148 // Invalid.
182 - return $default;
149 + return $default_value;
183 150 }
184 151 if ( ! isset( $max_sizes ) ) {
185 152 $max_sizes = get_imagify_max_intermediate_image_size();
186 153 }
@@ -191,9 +158,9 @@
191 158 return $value;
192 159
193 160 case 'disallowed-sizes':
194 161 if ( ! $value ) {
195 - return $default;
162 + return $default_value;
196 163 }
197 164
198 165 $value = array_keys( $value );
199 166 $value = array_map( 'sanitize_text_field', $value );
@@ -198,8 +165,9 @@
198 165 $value = array_keys( $value );
199 166 $value = array_map( 'sanitize_text_field', $value );
200 167 return array_fill_keys( $value, 1 );
201 168
169 + case 'display_nextgen_method':
202 170 case 'display_webp_method':
203 171 $values = [
204 172 'picture' => 1,
205 173 'rewrite' => 1,
@@ -211,9 +179,9 @@
211 179 $reset_values = $this->get_reset_values();
212 180 return $reset_values[ $key ];
213 181
214 182 case 'cdn_url':
215 - $cdn_source = \Imagify\Webp\Picture\Display::get_instance()->get_cdn_source( $value );
183 + $cdn_source = apply_filters( 'imagify_cdn_source_url', $value );
216 184
217 185 if ( 'option' !== $cdn_source['source'] ) {
218 186 /**
219 187 * If the URL is defined via constant or filter, unset the option.
@@ -230,11 +198,9 @@
230 198
231 199 /**
232 200 * Validate Imagify's options before storing them. Basic sanitization and validation have been made, row by row.
233 201 *
234 - * @since 1.7
235 - * @author Grégory Viguier
236 - * @access public
202 + * @since 1.7
237 203 *
238 204 * @param string $values The option value.
239 205 * @return array
240 206 */
@@ -241,13 +207,8 @@
241 207 public function validate_values_on_update( $values ) {
242 208 // The max width for the "Resize larger images" option can't be 0.
243 209 if ( empty( $values['resize_larger_w'] ) ) {
244 210 unset( $values['resize_larger'], $values['resize_larger_w'] );
245 - }
246 -
247 - // Don't display wepb if conversion is disabled.
248 - if ( empty( $values['convert_to_webp'] ) ) {
249 - unset( $values['convert_to_webp'], $values['display_webp'] );
250 211 }
251 212
252 213 return $values;
253 214 }