PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.1.0
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.1.0
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 / traits / normalizer.php

normalizer.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.1.0, at inc/traits/normalizer.php

231 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Normalization traits.
5 *
6 * @package \Optml\Inc\Traits
7 * @author Optimole <friends@optimole.com>
8 */
9 trait Optml_Normalizer {
10
11 /**
12 * Normalize value to boolean.
13 *
14 * @param mixed $value Value to process.
15 *
16 * @return bool
17 */
18 public function to_boolean( $value ) {
19 if ( in_array( $value, [ 'yes', 'enabled', 'true', '1' ], true ) ) {
20 return true;
21 }
22
23 if ( in_array( $value, [ 'no', 'disabled', 'false', '0' ], true ) ) {
24 return false;
25 }
26
27 return boolval( $value );
28 }
29
30 /**
31 * Strip slashes on unicode encoded strings.
32 *
33 * @param string $string Input string.
34 *
35 * @return string Decoded string.
36 */
37 public function strip_slashes( $string ) {
38 return html_entity_decode( stripslashes( preg_replace( '/\\\u([\da-fA-F]{4})/', '&#x\1;', $string ) ) );
39 }
40 /**
41 * Normalize value to an integer within bounds.
42 *
43 * @param mixed $value Value to process.
44 * @param integer $min Lower bound.
45 * @param integer $max Upper bound.
46 *
47 * @return integer
48 */
49 public function to_bound_integer( $value, $min, $max ) {
50 $integer = absint( $value );
51 if ( $integer < $min ) {
52 $integer = $min;
53 }
54 if ( $integer > $max ) {
55 $integer = $max;
56 }
57
58 return $integer;
59 }
60
61 /**
62 * Normalize value to positive integer.
63 *
64 * @param mixed $value Value to process.
65 *
66 * @return integer
67 */
68 public function to_positive_integer( $value ) {
69 $integer = (int) $value;
70
71 return ( $integer > 0 ) ? $integer : 0;
72 }
73
74 /**
75 * Normalize value to map.
76 *
77 * @param mixed $value Value to process.
78 * @param array $map Associative list from witch to return.
79 * @param mixed $default Default.
80 *
81 * @return mixed
82 */
83 public function to_map_values( $value, $map, $default ) {
84 if ( in_array( $value, $map, true ) ) {
85 return $value;
86 }
87
88 return $default;
89 }
90
91 /**
92 * Normalize value to an accepted quality.
93 *
94 * @param mixed $value Value to process.
95 *
96 * @return mixed
97 */
98 public function to_accepted_quality( $value ) {
99 if ( is_numeric( $value ) ) {
100 return intval( $value );
101 }
102 $value = trim( $value );
103
104 $accepted_qualities = [
105 'eco' => 'eco',
106 'auto' => 'auto',
107 'high_c' => 55,
108 'medium_c' => 75,
109 'low_c' => 90,
110 ];
111
112 if ( array_key_exists( $value, $accepted_qualities ) ) {
113 return $accepted_qualities[ $value ];
114 }
115
116 // Legacy values.
117 return 60;
118 }
119
120 /**
121 * Normalize value to an accepted minify.
122 *
123 * @param mixed $value Value to process.
124 *
125 * @return mixed
126 */
127 public function to_accepted_minify( $value ) {
128 if ( is_numeric( $value ) ) {
129 return $this->to_bound_integer( $value, 0, 1 );
130 }
131
132 return 'auto';
133 }
134
135 /**
136 * Normalize arguments for crop.
137 *
138 * @param array $crop_args Crop arguments.
139 *
140 * @return array
141 */
142 public function to_optml_crop( $crop_args = [] ) {
143
144 $enlarge = false;
145 if ( isset( $crop_args['enlarge'] ) ) {
146 $crop_args = $crop_args['crop'];
147 $enlarge = $crop_args['enlarge'];
148 }
149 if ( $crop_args === true ) {
150 return [
151 'type' => Optml_Resize::RESIZE_FILL,
152 'enlarge' => $enlarge,
153 'gravity' => Optml_Resize::GRAVITY_CENTER,
154 ];
155 }
156 if ( $crop_args === false || ! is_array( $crop_args ) || count( $crop_args ) !== 2 ) {
157 return [];
158 }
159
160 $allowed_x = [
161 'left' => true,
162 'center' => true,
163 'right' => true,
164 ];
165 $allowed_y = [
166 'top' => true,
167 'center' => true,
168 'bottom' => true,
169 ];
170 $allowed_gravities = [
171 'left' => Optml_Resize::GRAVITY_WEST,
172 'right' => Optml_Resize::GRAVITY_EAST,
173 'top' => Optml_Resize::GRAVITY_NORTH,
174 'bottom' => Optml_Resize::GRAVITY_SOUTH,
175 'lefttop' => Optml_Resize::GRAVITY_NORTH_WEST,
176 'leftbottom' => Optml_Resize::GRAVITY_SOUTH_WEST,
177 'righttop' => Optml_Resize::GRAVITY_NORTH_EAST,
178 'rightbottom' => Optml_Resize::GRAVITY_SOUTH_EAST,
179 'centertop' => [ 0.5, 0 ],
180 'centerbottom' => [ 0.5, 1 ],
181 'leftcenter' => [ 0, 0.5 ],
182 'rightcenter' => [ 1, 0.5 ],
183 ];
184
185 $gravity = Optml_Resize::GRAVITY_CENTER;
186 $key_search = ( $crop_args[0] === true ? '' :
187 ( isset( $allowed_x[ $crop_args[0] ] ) ? $crop_args[0] : '' ) ) .
188 ( $crop_args[1] === true ? '' :
189 ( isset( $allowed_y[ $crop_args[1] ] ) ? $crop_args[1] : '' ) );
190
191 if ( array_key_exists( $key_search, $allowed_gravities ) ) {
192 $gravity = $allowed_gravities[ $key_search ];
193 }
194
195 return [
196 'type' => Optml_Resize::RESIZE_FILL,
197 'enlarge' => $enlarge,
198 'gravity' => $gravity,
199 ];
200 }
201
202 /**
203 * Normalize arguments for watermark.
204 *
205 * @param array $watermark_args Watermark arguments.
206 *
207 * @return array
208 */
209 public function to_optml_watermark( $watermark_args = [] ) {
210 $allowed_gravities = [
211 'left' => Optml_Resize::GRAVITY_WEST,
212 'right' => Optml_Resize::GRAVITY_EAST,
213 'top' => Optml_Resize::GRAVITY_NORTH,
214 'bottom' => Optml_Resize::GRAVITY_SOUTH,
215 'left_top' => Optml_Resize::GRAVITY_NORTH_WEST,
216 'left_bottom' => Optml_Resize::GRAVITY_SOUTH_WEST,
217 'right_top' => Optml_Resize::GRAVITY_NORTH_EAST,
218 'right_bottom' => Optml_Resize::GRAVITY_SOUTH_EAST,
219 ];
220 $gravity = Optml_Resize::GRAVITY_CENTER;
221 if ( isset( $watermark_args['position'] ) && array_key_exists( $watermark_args['position'], $allowed_gravities ) ) {
222 $gravity = $allowed_gravities[ $watermark_args['position'] ];
223 }
224
225 return [
226 'opacity' => 1,
227 'position' => $gravity,
228 ];
229 }
230 }
231