PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.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 / classes / Context / AbstractContext.php

AbstractContext.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.9.7, at classes/Context/AbstractContext.php

312 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Context;
3
4 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
5
6 /**
7 * Abstract used for contexts.
8 *
9 * @since 1.9
10 * @author Grégory Viguier
11 */
12 abstract class AbstractContext implements ContextInterface {
13
14 /**
15 * Context "short name".
16 *
17 * @var string
18 * @since 1.9
19 * @access protected
20 * @author Grégory Viguier
21 */
22 protected $context;
23
24 /**
25 * Tell if the media/context is network-wide.
26 *
27 * @var bool
28 * @since 1.9
29 * @access protected
30 * @author Grégory Viguier
31 */
32 protected $is_network_wide = false;
33
34 /**
35 * Type of files this context allows.
36 *
37 * @var string Possible values are:
38 * - 'all' to allow all types.
39 * - 'image' to allow only images.
40 * - 'not-image' to allow only pdf files.
41 * @since 1.9
42 * @access protected
43 * @see imagify_get_mime_types()
44 * @author Grégory Viguier
45 */
46 protected $allowed_mime_types = 'all';
47
48 /**
49 * The thumbnail sizes for this context, except the full size.
50 *
51 * @var array {
52 * Data for the currently registered thumbnail sizes.
53 * Size names are used as array keys.
54 *
55 * @type int $width The image width.
56 * @type int $height The image height.
57 * @type bool $crop True to crop, false to resize.
58 * @type string $name The size name.
59 * }
60 * @since 1.9
61 * @access protected
62 * @author Grégory Viguier
63 */
64 protected $thumbnail_sizes;
65
66 /**
67 * Tell if the optimization process is allowed resize in this context.
68 *
69 * @var bool
70 * @since 1.9
71 * @access protected
72 * @author Grégory Viguier
73 */
74 protected $can_resize;
75
76 /**
77 * Tell if the optimization process is allowed to backup in this context.
78 *
79 * @var bool
80 * @since 1.9
81 * @access protected
82 * @author Grégory Viguier
83 */
84 protected $can_backup;
85
86 /**
87 * Tell if the optimization process is allowed to keep exif in this context.
88 *
89 * @var bool
90 * @since 1.9
91 * @access protected
92 * @author Grégory Viguier
93 */
94 protected $can_keep_exif;
95
96 /**
97 * Get the context "short name".
98 *
99 * @since 1.9
100 * @access public
101 * @author Grégory Viguier
102 *
103 * @return string
104 */
105 public function get_name() {
106 return $this->context;
107 }
108
109 /**
110 * Tell if the context is network-wide.
111 *
112 * @since 1.9
113 * @access public
114 * @author Grégory Viguier
115 *
116 * @return bool
117 */
118 public function is_network_wide() {
119 return $this->is_network_wide;
120 }
121
122 /**
123 * Get the type of files this context allows.
124 *
125 * @since 1.9
126 * @access protected
127 * @see imagify_get_mime_types()
128 * @author Grégory Viguier
129 *
130 * @return string Possible values are:
131 * - 'all' to allow all types.
132 * - 'image' to allow only images.
133 * - 'not-image' to allow only pdf files.
134 */
135 public function get_allowed_mime_types() {
136 return $this->allowed_mime_types;
137 }
138
139 /**
140 * Get the thumbnail sizes for this context, except the full size.
141 *
142 * @since 1.9
143 * @access public
144 * @author Grégory Viguier
145 *
146 * @return array {
147 * Data for the currently registered thumbnail sizes.
148 * Size names are used as array keys.
149 *
150 * @type int $width The image width.
151 * @type int $height The image height.
152 * @type bool $crop True to crop, false to resize.
153 * @type string $name The size name.
154 * }
155 */
156 public function get_thumbnail_sizes() {
157 return $this->thumbnail_sizes;
158 }
159
160 /**
161 * Tell if the optimization process is allowed resize in this context.
162 *
163 * @since 1.9
164 * @access public
165 * @author Grégory Viguier
166 *
167 * @return bool
168 */
169 public function can_resize() {
170 return $this->can_resize;
171 }
172
173 /**
174 * Tell if the optimization process is allowed to backup in this context.
175 *
176 * @since 1.9
177 * @access public
178 * @author Grégory Viguier
179 *
180 * @return bool
181 */
182 public function can_backup() {
183 return $this->can_backup;
184 }
185
186 /**
187 * Tell if the optimization process is allowed to keep exif in this context.
188 *
189 * @since 1.9
190 * @access public
191 * @author Grégory Viguier
192 *
193 * @return bool
194 */
195 public function can_keep_exif() {
196 return $this->can_keep_exif;
197 }
198
199 /**
200 * Tell if the current user is allowed to operate Imagify in this context.
201 *
202 * @since 1.9
203 * @access public
204 * @author Grégory Viguier
205 *
206 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
207 * @param int $media_id A media ID.
208 * @return bool
209 */
210 public function current_user_can( $describer, $media_id = null ) {
211 return $this->user_can( null, $describer, $media_id );
212 }
213
214 /**
215 * Tell if a user is allowed to operate Imagify in this context.
216 *
217 * @since 1.9
218 * @access public
219 * @author Grégory Viguier
220 *
221 * @param int|\WP_User $user_id A user ID or \WP_User object. Fallback to the current user ID.
222 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
223 * @param int $media_id A media ID.
224 * @return bool
225 */
226 public function user_can( $user_id, $describer, $media_id = null ) {
227 $current_user_id = get_current_user_id();
228
229 if ( ! $user_id ) {
230 $user = $current_user_id;
231 $user_id = $current_user_id;
232 } elseif ( $user_id instanceof \WP_User ) {
233 $user = $user_id;
234 $user_id = (int) $user->ID;
235 } elseif ( is_numeric( $user_id ) ) {
236 $user = (int) $user_id;
237 $user_id = $user;
238 } else {
239 $user_id = 0;
240 }
241
242 if ( ! $user_id ) {
243 return false;
244 }
245
246 $media_id = $media_id ? (int) $media_id : null;
247 $capacity = $this->get_capacity( $describer );
248
249 if ( $user_id === $current_user_id ) {
250 $user_can = current_user_can( $capacity, $media_id );
251
252 /**
253 * Tell if the current user is allowed to operate Imagify in this context.
254 *
255 * @since 1.6.11
256 * @since 1.9 Added the context name as parameter.
257 *
258 * @param bool $user_can Tell if the current user is allowed to operate Imagify in this context.
259 * @param string $capacity The user capacity.
260 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
261 * @param int $media_id A media ID.
262 * @param string $context The context name.
263 */
264 $user_can = (bool) apply_filters( 'imagify_current_user_can', $user_can, $capacity, $describer, $media_id, $this->get_name() );
265 } else {
266 $user_can = user_can( $user, $capacity, $media_id );
267 }
268
269 /**
270 * Tell if the given user is allowed to operate Imagify in this context.
271 *
272 * @since 1.9
273 * @author Grégory Viguier
274 *
275 * @param bool $user_can Tell if the given user is allowed to operate Imagify in this context.
276 * @param int $user_id The user ID.
277 * @param string $capacity The user capacity.
278 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
279 * @param int $media_id A media ID.
280 * @param string $context The context name.
281 */
282 return (bool) apply_filters( 'imagify_user_can', $user_can, $user_id, $capacity, $describer, $media_id, $this->get_name() );
283 }
284
285 /**
286 * Filter a user capacity used to operate Imagify in this context.
287 *
288 * @since 1.9
289 * @access protected
290 * @author Grégory Viguier
291 *
292 * @param string $capacity The user capacity.
293 * @param string $describer Capacity describer. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
294 * @return string
295 */
296 protected function filter_capacity( $capacity, $describer ) {
297 /**
298 * Filter a user capacity used to operate Imagify in this context.
299 *
300 * @since 1.0
301 * @since 1.6.5 Added $force_mono parameter.
302 * @since 1.6.11 Replaced $force_mono by $describer.
303 * @since 1.9 Added the context name as parameter.
304 *
305 * @param string $capacity The user capacity.
306 * @param string $describer Capacity describer. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
307 * @param string $context The context name.
308 */
309 return (string) apply_filters( 'imagify_capacity', $capacity, $describer, $this->get_name() );
310 }
311 }
312