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

302 lines 7.9 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 to backup in this context.
68 *
69 * @var bool
70 * @since 1.9
71 * @access protected
72 * @author Grégory Viguier
73 */
74 protected $can_backup;
75
76 /**
77 * Tell if the optimization process is allowed to keep exif in this context.
78 *
79 * @var bool
80 * @since 1.9
81 * @access protected
82 * @author Grégory Viguier
83 */
84 protected $can_keep_exif;
85
86 /**
87 * Get the context "short name".
88 *
89 * @since 1.9
90 * @access public
91 * @author Grégory Viguier
92 *
93 * @return string
94 */
95 public function get_name() {
96 return $this->context;
97 }
98
99 /**
100 * Tell if the context is network-wide.
101 *
102 * @since 1.9
103 * @access public
104 * @author Grégory Viguier
105 *
106 * @return bool
107 */
108 public function is_network_wide() {
109 return $this->is_network_wide;
110 }
111
112 /**
113 * Get the type of files this context allows.
114 *
115 * @since 1.9
116 * @access protected
117 * @see imagify_get_mime_types()
118 * @author Grégory Viguier
119 *
120 * @return string Possible values are:
121 * - 'all' to allow all types.
122 * - 'image' to allow only images.
123 * - 'not-image' to allow only pdf files.
124 */
125 public function get_allowed_mime_types() {
126 return $this->allowed_mime_types;
127 }
128
129 /**
130 * Get the thumbnail sizes for this context, except the full size.
131 *
132 * @since 1.9
133 * @access public
134 * @author Grégory Viguier
135 *
136 * @return array {
137 * Data for the currently registered thumbnail sizes.
138 * Size names are used as array keys.
139 *
140 * @type int $width The image width.
141 * @type int $height The image height.
142 * @type bool $crop True to crop, false to resize.
143 * @type string $name The size name.
144 * }
145 */
146 public function get_thumbnail_sizes() {
147 return $this->thumbnail_sizes;
148 }
149
150 /**
151 * Tell if the optimization process is allowed resize in this context.
152 *
153 * @since 1.9
154 * @access public
155 * @author Grégory Viguier
156 *
157 * @return bool
158 */
159 public function can_resize() {
160 return $this->get_resizing_threshold() > 0;
161 }
162
163 /**
164 * Tell if the optimization process is allowed to backup in this context.
165 *
166 * @since 1.9
167 * @access public
168 * @author Grégory Viguier
169 *
170 * @return bool
171 */
172 public function can_backup() {
173 return $this->can_backup;
174 }
175
176 /**
177 * Tell if the optimization process is allowed to keep exif in this context.
178 *
179 * @since 1.9
180 * @access public
181 * @author Grégory Viguier
182 *
183 * @return bool
184 */
185 public function can_keep_exif() {
186 return $this->can_keep_exif;
187 }
188
189 /**
190 * Tell if the current user is allowed to operate Imagify in this context.
191 *
192 * @since 1.9
193 * @access public
194 * @author Grégory Viguier
195 *
196 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
197 * @param int $media_id A media ID.
198 * @return bool
199 */
200 public function current_user_can( $describer, $media_id = null ) {
201 return $this->user_can( null, $describer, $media_id );
202 }
203
204 /**
205 * Tell if a user is allowed to operate Imagify in this context.
206 *
207 * @since 1.9
208 * @access public
209 * @author Grégory Viguier
210 *
211 * @param int|\WP_User $user_id A user ID or \WP_User object. Fallback to the current user ID.
212 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
213 * @param int $media_id A media ID.
214 * @return bool
215 */
216 public function user_can( $user_id, $describer, $media_id = null ) {
217 $current_user_id = get_current_user_id();
218
219 if ( ! $user_id ) {
220 $user = $current_user_id;
221 $user_id = $current_user_id;
222 } elseif ( $user_id instanceof \WP_User ) {
223 $user = $user_id;
224 $user_id = (int) $user->ID;
225 } elseif ( is_numeric( $user_id ) ) {
226 $user = (int) $user_id;
227 $user_id = $user;
228 } else {
229 $user_id = 0;
230 }
231
232 if ( ! $user_id ) {
233 return false;
234 }
235
236 $media_id = $media_id ? (int) $media_id : null;
237 $capacity = $this->get_capacity( $describer );
238
239 if ( $user_id === $current_user_id ) {
240 $user_can = current_user_can( $capacity, $media_id );
241
242 /**
243 * Tell if the current user is allowed to operate Imagify in this context.
244 *
245 * @since 1.6.11
246 * @since 1.9 Added the context name as parameter.
247 *
248 * @param bool $user_can Tell if the current user is allowed to operate Imagify in this context.
249 * @param string $capacity The user capacity.
250 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
251 * @param int $media_id A media ID.
252 * @param string $context The context name.
253 */
254 $user_can = (bool) apply_filters( 'imagify_current_user_can', $user_can, $capacity, $describer, $media_id, $this->get_name() );
255 } else {
256 $user_can = user_can( $user, $capacity, $media_id );
257 }
258
259 /**
260 * Tell if the given user is allowed to operate Imagify in this context.
261 *
262 * @since 1.9
263 * @author Grégory Viguier
264 *
265 * @param bool $user_can Tell if the given user is allowed to operate Imagify in this context.
266 * @param int $user_id The user ID.
267 * @param string $capacity The user capacity.
268 * @param string $describer Capacity describer. See $this->get_capacity() for possible values. Can also be a "real" user capacity.
269 * @param int $media_id A media ID.
270 * @param string $context The context name.
271 */
272 return (bool) apply_filters( 'imagify_user_can', $user_can, $user_id, $capacity, $describer, $media_id, $this->get_name() );
273 }
274
275 /**
276 * Filter a user capacity used to operate Imagify in this context.
277 *
278 * @since 1.9
279 * @access protected
280 * @author Grégory Viguier
281 *
282 * @param string $capacity The user capacity.
283 * @param string $describer Capacity describer. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
284 * @return string
285 */
286 protected function filter_capacity( $capacity, $describer ) {
287 /**
288 * Filter a user capacity used to operate Imagify in this context.
289 *
290 * @since 1.0
291 * @since 1.6.5 Added $force_mono parameter.
292 * @since 1.6.11 Replaced $force_mono by $describer.
293 * @since 1.9 Added the context name as parameter.
294 *
295 * @param string $capacity The user capacity.
296 * @param string $describer Capacity describer. Possible values are like 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize'.
297 * @param string $context The context name.
298 */
299 return (string) apply_filters( 'imagify_capacity', $capacity, $describer, $this->get_name() );
300 }
301 }
302