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

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