PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
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.7, at inc/classes/class-imagify-options.php

196 lines 4.3 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.0';
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 'exif' => 0,
45 'disallowed-sizes' => array(),
46 'admin_bar_menu' => 0,
47 );
48
49 /**
50 * The Imagify main option values used when they are set the first time or reset.
51 * Values identical to default values are not listed.
52 *
53 * @var array
54 * @since 1.7
55 * @access protected
56 */
57 protected $reset_values = array(
58 'optimization_level' => 1,
59 'auto_optimize' => 1,
60 'backup' => 1,
61 'admin_bar_menu' => 1,
62 );
63
64 /**
65 * The single instance of the class.
66 *
67 * @var object
68 * @since 1.7
69 * @access protected
70 */
71 protected static $_instance;
72
73 /**
74 * The constructor.
75 * Side note: $this->hook_identifier value is "option".
76 *
77 * @since 1.7
78 * @author Grégory Viguier
79 * @access protected
80 */
81 protected function __construct() {
82 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
83 $this->default_values['api_key'] = (string) IMAGIFY_API_KEY;
84 }
85
86 $this->network_option = imagify_is_active_for_network();
87
88 parent::__construct();
89 }
90
91 /**
92 * Get the main Instance.
93 *
94 * @since 1.7
95 * @author Grégory Viguier
96 * @access public
97 *
98 * @return object Main instance.
99 */
100 public static function get_instance() {
101 if ( ! isset( self::$_instance ) ) {
102 self::$_instance = new self();
103 }
104
105 return self::$_instance;
106 }
107
108
109 /** ----------------------------------------------------------------------------------------- */
110 /** SANITIZATION, VALIDATION ================================================================ */
111 /** ----------------------------------------------------------------------------------------- */
112
113 /**
114 * Sanitize and validate an option value. Basic casts have been made.
115 *
116 * @since 1.7
117 * @author Grégory Viguier
118 * @access public
119 *
120 * @param string $key The option key.
121 * @param mixed $value The value.
122 * @param mixed $default The default value.
123 * @return mixed
124 */
125 public function sanitize_and_validate_value( $key, $value, $default ) {
126 static $max_sizes;
127
128 switch ( $key ) {
129 case 'api_key':
130 if ( defined( 'IMAGIFY_API_KEY' ) && IMAGIFY_API_KEY ) {
131 return (string) IMAGIFY_API_KEY;
132 }
133 return $value ? sanitize_key( $value ) : '';
134
135 case 'optimization_level':
136 if ( $value < 0 || $value > 2 ) {
137 // For an invalid value, return the "reset" value.
138 $reset_values = $this->get_reset_values();
139 return $reset_values[ $key ];
140 }
141 return $value;
142
143 case 'auto_optimize':
144 case 'backup':
145 case 'resize_larger':
146 case 'exif':
147 case 'admin_bar_menu':
148 return 1;
149
150 case 'resize_larger_w':
151 if ( $value <= 0 ) {
152 // Invalid.
153 return 0;
154 }
155 if ( ! isset( $max_sizes ) ) {
156 $max_sizes = get_imagify_max_intermediate_image_size();
157 }
158 if ( $value < $max_sizes['width'] ) {
159 // Invalid.
160 return $max_sizes['width'];
161 }
162 return $value;
163
164 case 'disallowed-sizes':
165 if ( ! $value ) {
166 return $default;
167 }
168
169 $value = array_keys( $value );
170 $value = array_map( 'sanitize_text_field', $value );
171 return array_fill_keys( $value, 1 );
172 }
173
174 return false;
175 }
176
177 /**
178 * Validate Imagify's options before storing them. Basic sanitization and validation have been made, row by row.
179 *
180 * @since 1.7
181 * @author Grégory Viguier
182 * @access public
183 *
184 * @param string $values The option value.
185 * @return array
186 */
187 public function validate_values_on_update( $values ) {
188 // The max width for the "Resize larger images" option can't be 0.
189 if ( empty( $values['resize_larger_w'] ) ) {
190 unset( $values['resize_larger'], $values['resize_larger_w'] );
191 }
192
193 return $values;
194 }
195 }
196