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 / functions / common.php

common.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/functions/common.php

422 lines 12.2 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 * Get user capacity to operate Imagify.
6 *
7 * @since 1.6.5
8 * @since 1.6.11 Uses a string as describer for the first argument.
9 * @author Grégory Viguier
10 *
11 * @param string $describer Capacity describer. Possible values are 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize', and 'optimize-file'.
12 * @return string
13 */
14 function imagify_get_capacity( $describer = 'manage' ) {
15 static $edit_attachment_cap;
16
17 // Back compat.
18 if ( ! is_string( $describer ) ) {
19 if ( $describer || ! is_multisite() ) {
20 $describer = 'bulk-optimize';
21 } else {
22 $describer = 'manage';
23 }
24 }
25
26 switch ( $describer ) {
27 case 'manage':
28 $capacity = imagify_is_active_for_network() ? 'manage_network_options' : 'manage_options';
29 break;
30 case 'optimize-file':
31 $capacity = is_multisite() ? 'manage_network_options' : 'manage_options';
32 break;
33 case 'bulk-optimize':
34 $capacity = 'manage_options';
35 break;
36 case 'optimize':
37 // This is a generic capacity: don't use it unless you have no other choices!
38 if ( ! isset( $edit_attachment_cap ) ) {
39 $edit_attachment_cap = get_post_type_object( 'attachment' );
40 $edit_attachment_cap = $edit_attachment_cap ? $edit_attachment_cap->cap->edit_posts : 'edit_posts';
41 }
42
43 $capacity = $edit_attachment_cap;
44 break;
45 case 'manual-optimize':
46 // Must be used with an Attachment ID.
47 $capacity = 'edit_post';
48 break;
49 case 'auto-optimize':
50 $capacity = 'upload_files';
51 break;
52 default:
53 $capacity = $describer;
54 }
55
56 /**
57 * Filter the user capacity used to operate Imagify.
58 *
59 * @since 1.0
60 * @since 1.6.5 Added $force_mono parameter.
61 * @since 1.6.11 Replaced $force_mono by $describer.
62 *
63 * @param string $capacity The user capacity.
64 * @param string $describer Capacity describer. Possible values are 'manage', 'bulk-optimize', 'manual-optimize', 'auto-optimize', and 'optimize-file'.
65 */
66 return apply_filters( 'imagify_capacity', $capacity, $describer );
67 }
68
69 /**
70 * Tell if the current user has the required ability to operate Imagify.
71 *
72 * @since 1.6.11
73 * @see imagify_get_capacity()
74 * @author Grégory Viguier
75 *
76 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
77 * @param int $post_id A post ID (a gallery ID for NGG).
78 * @return bool
79 */
80 function imagify_current_user_can( $describer = 'manage', $post_id = null ) {
81 static $can_upload;
82
83 $post_id = $post_id ? $post_id : null;
84 $capacity = imagify_get_capacity( $describer );
85 $user_can = false;
86
87 if ( 'manage' !== $describer && 'bulk-optimize' !== $describer && 'optimize-file' !== $describer ) {
88 // Describers that are not 'manage', 'bulk-optimize', and 'optimize-file' need an additional test for 'upload_files'.
89 if ( ! isset( $can_upload ) ) {
90 $can_upload = current_user_can( 'upload_files' );
91 }
92
93 if ( $can_upload ) {
94 if ( 'upload_files' === $capacity ) {
95 // We already know it's true.
96 $user_can = true;
97 } else {
98 $user_can = current_user_can( $capacity, $post_id );
99 }
100 }
101 } else {
102 $user_can = current_user_can( $capacity );
103 }
104
105 /**
106 * Filter the current user ability to operate Imagify.
107 *
108 * @since 1.6.11
109 *
110 * @param bool $user_can Tell if the current user has the required ability to operate Imagify.
111 * @param string $capacity The user capacity.
112 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
113 * @param int $post_id A post ID (a gallery ID for NGG).
114 */
115 return apply_filters( 'imagify_current_user_can', $user_can, $capacity, $describer, $post_id );
116 }
117
118 /**
119 * Tell if the current user can optimize custom folders.
120 *
121 * @since 1.7
122 * @author Grégory Viguier
123 *
124 * @return bool
125 */
126 function imagify_can_optimize_custom_folders() {
127 static $can;
128
129 if ( isset( $can ) ) {
130 return $can;
131 }
132
133 // Check if the DB tables are ready.
134 if ( ! Imagify_Folders_DB::get_instance()->can_operate() || ! Imagify_Files_DB::get_instance()->can_operate() ) {
135 $can = false;
136 return $can;
137 }
138
139 // Check for user capacity.
140 if ( ! imagify_current_user_can( 'optimize-file' ) ) {
141 $can = false;
142 return $can;
143 }
144
145 $can = true;
146 return $can;
147 }
148
149 /**
150 * Sanitize an optimization context.
151 *
152 * @since 1.6.11
153 * @author Grégory Viguier
154 *
155 * @param string $context The context.
156 * @return string
157 */
158 function imagify_sanitize_context( $context ) {
159 $context = preg_replace( '/[^a-zA-Z0-9_\-]/', '', $context );
160 return $context ? $context : 'wp';
161 }
162
163 /**
164 * Classes autoloader.
165 *
166 * @since 1.6.12
167 * @author Grégory Viguier
168 *
169 * @param string $class Name of the class to include.
170 */
171 function imagify_autoload( $class ) {
172 // Generic classes.
173 $classes = array(
174 'Imagify_Abstract_Attachment' => 1,
175 'Imagify_Abstract_Cron' => 1,
176 'Imagify_Abstract_DB' => 1,
177 'Imagify_Abstract_Options' => 1,
178 'Imagify_Admin_Ajax_Post' => 1,
179 'Imagify_Assets' => 1,
180 'Imagify_Attachment' => 1,
181 'Imagify_Cron_Library_Size' => 1,
182 'Imagify_Cron_Rating' => 1,
183 'Imagify_Cron_Sync_Files' => 1,
184 'Imagify_Custom_Folders' => 1,
185 'Imagify_Data' => 1,
186 'Imagify_DB' => 1,
187 'Imagify_File_Attachment' => 1,
188 'Imagify_Files_DB' => 1,
189 'Imagify_Files_Iterator' => 1,
190 'Imagify_Files_List_Table' => 1,
191 'Imagify_Files_Recursive_Iterator' => 1,
192 'Imagify_Files_Scan' => 1,
193 'Imagify_Files_Stats' => 1,
194 'Imagify_Folders_DB' => 1,
195 'Imagify_Notices' => 1,
196 'Imagify_Options' => 1,
197 'Imagify_Settings' => 1,
198 'Imagify_User' => 1,
199 'Imagify_Views' => 1,
200 'Imagify' => 1,
201 );
202
203 if ( isset( $classes[ $class ] ) ) {
204 $class = str_replace( '_', '-', strtolower( $class ) );
205 include IMAGIFY_CLASSES_PATH . 'class-' . $class . '.php';
206 return;
207 }
208
209 // Third party classes.
210 $classes = array(
211 'Imagify_AS3CF_Attachment' => 'amazon-s3-and-cloudfront',
212 'Imagify_AS3CF' => 'amazon-s3-and-cloudfront',
213 'Imagify_Enable_Media_Replace' => 'enable-media-replace',
214 'Imagify_Formidable_Pro' => 'formidable-pro',
215 'Imagify_NGG_Attachment' => 'nextgen-gallery',
216 'Imagify_NGG_DB' => 'nextgen-gallery',
217 'Imagify_NGG_Storage' => 'nextgen-gallery',
218 'Imagify_NGG' => 'nextgen-gallery',
219 );
220
221 if ( isset( $classes[ $class ] ) ) {
222 $folder = $classes[ $class ];
223 $class = str_replace( '_', '-', strtolower( $class ) );
224 include IMAGIFY_3RD_PARTY_PATH . $folder . '/inc/classes/class-' . $class . '.php';
225 }
226 }
227
228 /**
229 * Simple helper to get some external URLs, like to the documentation.
230 *
231 * @since 1.6.12
232 * @author Grégory Viguier
233 *
234 * @param string $target What we want.
235 * @param array $query_args An array of query arguments.
236 * @return string The URL.
237 */
238 function imagify_get_external_url( $target, $query_args = array() ) {
239 $site_url = 'https://imagify.io/';
240 $app_url = 'https://app.imagify.io/#/';
241
242 switch ( $target ) {
243 case 'plugin':
244 /* translators: Plugin URI of the plugin/theme */
245 $url = __( 'https://wordpress.org/plugins/imagify/', 'imagify' );
246 break;
247
248 case 'rate':
249 $url = 'https://wordpress.org/support/view/plugin-reviews/imagify?rate=5#postform';
250 break;
251
252 case 'share-twitter':
253 $url = rawurlencode( imagify_get_external_url( 'plugin' ) );
254 $url = 'https://twitter.com/intent/tweet?source=webclient&original_referer=' . $url . '&url=' . $url . '&related=imagify&hastags=performance,web,wordpress';
255 break;
256
257 case 'share-facebook':
258 $url = rawurlencode( imagify_get_external_url( 'plugin' ) );
259 $url = 'https://www.facebook.com/sharer/sharer.php?u=' . $url;
260 break;
261
262 case 'exif':
263 /* translators: URL to a Wikipedia page explaining what EXIF means. */
264 $url = __( 'https://en.wikipedia.org/wiki/Exchangeable_image_file_format', 'imagify' );
265 break;
266
267 case 'contact':
268 $lang = imagify_get_current_lang_in( 'fr' );
269 $paths = array(
270 'en' => 'contact',
271 'fr' => 'fr/contact',
272 );
273
274 $url = $site_url . $paths[ $lang ] . '/';
275 break;
276
277 case 'documentation':
278 $url = $site_url . 'documentation/';
279 break;
280
281 case 'register':
282 $partner = imagify_get_partner();
283
284 if ( $partner ) {
285 $query_args['partner'] = $partner;
286 }
287
288 $url = $app_url . 'register';
289 break;
290
291 case 'subscription':
292 $url = $app_url . 'subscription';
293 break;
294
295 case 'get-api-key':
296 $url = $app_url . 'api';
297 break;
298
299 case 'payment':
300 // Don't remove the trailing slash.
301 $url = $app_url . 'plugin/';
302 break;
303
304 default:
305 return '';
306 }
307
308 if ( $query_args ) {
309 $url = add_query_arg( $query_args, $url );
310 }
311
312 return $url;
313 }
314
315 /**
316 * Get the current lang ('fr', 'en', 'de'...), limited to a given list.
317 *
318 * @since 1.6.14
319 * @author Grégory Viguier
320 *
321 * @param array $langs An array of langs, like array( 'de', 'es', 'fr', 'it' ).
322 * @return string The current lang. Default is 'en'.
323 */
324 function imagify_get_current_lang_in( $langs ) {
325 static $locale;
326
327 if ( ! isset( $locale ) ) {
328 $locale = imagify_get_locale();
329 $locale = explode( '_', strtolower( $locale . '_' ) ); // Trailing underscore is to make sure $locale[1] is set.
330 }
331
332 foreach ( (array) $langs as $lang ) {
333 if ( $lang === $locale[0] || $lang === $locale[1] ) {
334 return $lang;
335 }
336 }
337
338 return 'en';
339 }
340
341 /**
342 * Get the current locale.
343 *
344 * @since 1.6.14
345 * @author Grégory Viguier
346 *
347 * @return string The current locale.
348 */
349 function imagify_get_locale() {
350 $locale = function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
351 /**
352 * Filter the locale used by Imagify.
353 *
354 * @since 1.6.14
355 * @author Grégory Viguier
356 *
357 * @param string $locale The current locale.
358 */
359 return apply_filters( 'imagify_locale', $locale );
360 }
361
362 /**
363 * Get the label corresponding to the given optimization label.
364 *
365 * @since 1.7
366 * @author Grégory Viguier
367 *
368 * @param int|bool $level Optimization level (between 0 and 2). False if no level.
369 * @param string $format Format to display the label. Use %ICON% for the icon and %s for the label.
370 * @return string The label.
371 */
372 function imagify_get_optimization_level_label( $level, $format = '%s' ) {
373 if ( ! is_numeric( $level ) ) {
374 return '';
375 }
376
377 if ( strpos( $format, '%ICON%' ) !== false ) {
378 $icon = '<svg width="12" height="12" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg"><g fill="#40B1D0" fill-rule="evenodd">';
379
380 switch ( $level ) {
381 case 2:
382 $icon .= '<polygon points="11.6054688 11.6054688 8.7890625 11.6054688 8.7890625 0.39453125 11.6054688 0.39453125"/><polygon points="7.39453125 11.6054688 4.60546875 11.6054688 4.60546875 3.89453125 7.39453125 3.89453125"/><polygon points="3.2109375 11.6054688 0.39453125 11.6054688 0.39453125 6 3.2109375 6"/>';
383 break;
384 case 1:
385 $icon .= '<polygon fill="#CCD1D6" points="11.6054688 11.6054688 8.7890625 11.6054688 8.7890625 0.39453125 11.6054688 0.39453125"/><polygon points="7.39453125 11.6054688 4.60546875 11.6054688 4.60546875 3.89453125 7.39453125 3.89453125"/><polygon points="3.2109375 11.6054688 0.39453125 11.6054688 0.39453125 6 3.2109375 6"/>';
386 break;
387 case 0:
388 $icon .= '<polygon fill="#CCD1D6" points="11.6054688 11.6054688 8.7890625 11.6054688 8.7890625 0.39453125 11.6054688 0.39453125"/><polygon fill="#CCD1D6" points="7.39453125 11.6054688 4.60546875 11.6054688 4.60546875 3.89453125 7.39453125 3.89453125"/><polygon points="3.2109375 11.6054688 0.39453125 11.6054688 0.39453125 6 3.2109375 6"/>';
389 }
390
391 $icon .= '</g></svg>';
392
393 $format = str_replace( '%ICON%', $icon, $format );
394 }
395
396 switch ( $level ) {
397 case 2:
398 return sprintf( $format, __( 'Ultra', 'imagify' ) );
399 case 1:
400 return sprintf( $format, __( 'Aggressive', 'imagify' ) );
401 case 0:
402 return sprintf( $format, __( 'Normal', 'imagify' ) );
403 }
404
405 return '';
406 }
407
408 /**
409 * `array_merge()` + `array_intersect_key()`.
410 *
411 * @since 1.7
412 * @author Grégory Viguier
413 *
414 * @param array $values The array we're interested in.
415 * @param array $default The array we use as boundaries.
416 * @return array
417 */
418 function imagify_merge_intersect( $values, $default ) {
419 $values = array_merge( $default, (array) $values );
420 return array_intersect_key( $values, $default );
421 }
422