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 / admin.php

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

481 lines 13.9 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 * Check if Imagify is activated on the network.
6 *
7 * @since 1.0
8 *
9 * return bool True if Imagify is activated on the network.
10 */
11 function imagify_is_active_for_network() {
12 static $is;
13
14 if ( isset( $is ) ) {
15 return $is;
16 }
17
18 if ( ! is_multisite() ) {
19 $is = false;
20 return $is;
21 }
22
23 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
24 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
25 }
26
27 $is = is_plugin_active_for_network( plugin_basename( IMAGIFY_FILE ) );
28
29 return $is;
30 }
31
32 /**
33 * Tell if the current screen is what we're looking for.
34 *
35 * @since 1.6.10
36 * @author Grégory Viguier
37 *
38 * @param string $identifier The screen "name".
39 * @return bool
40 */
41 function imagify_is_screen( $identifier ) {
42 global $post_id;
43
44 if ( ! $identifier ) {
45 return false;
46 }
47
48 $current_screen = get_current_screen();
49
50 if ( ! $current_screen || ! $current_screen->in_admin() ) {
51 return false;
52 }
53
54 switch ( $identifier ) {
55 case 'imagify-settings':
56 // Imagify Settings or Imagify Network Settings.
57 $slug = Imagify_Views::get_instance()->get_settings_page_slug();
58 return 'settings_page_' . $slug === $current_screen->id || $slug . '_page_' . $slug . '-network' === $current_screen->id || 'toplevel_page_' . $slug . '-network' === $current_screen->id;
59
60 case 'imagify-network-settings':
61 // Imagify Network Settings.
62 $slug = Imagify_Views::get_instance()->get_settings_page_slug();
63 return $slug . '_page_' . $slug . '-network' === $current_screen->id || 'toplevel_page_' . $slug . '-network' === $current_screen->id;
64
65 case 'library':
66 // Media Library.
67 return 'upload' === $current_screen->id;
68
69 case 'upload':
70 // Upload New Media.
71 return 'media' === $current_screen->id;
72
73 case 'post':
74 // Edit Post, Page, Attachment, etc.
75 return 'post' === $current_screen->base;
76
77 case 'attachment':
78 case 'post-attachment':
79 // Edit Attachment.
80 return 'post' === $current_screen->base && 'attachment' === $current_screen->id && $post_id && imagify_is_attachment_mime_type_supported( $post_id );
81
82 case 'bulk':
83 case 'bulk-optimization':
84 // Bulk Optimization (any).
85 $slug = Imagify_Views::get_instance()->get_bulk_page_slug();
86 return 'toplevel_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
87
88 case 'files-bulk-optimization':
89 // Bulk Optimization (custom folders).
90 $slug = Imagify_Views::get_instance()->get_bulk_page_slug();
91 return 'toplevel_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
92
93 case 'files':
94 case 'files-list':
95 // "Custom folders" files list.
96 $slug = Imagify_Views::get_instance()->get_files_page_slug();
97 return 'imagify_page_' . $slug . '-network' === $current_screen->id || 'media_page_' . $slug === $current_screen->id;
98
99 case 'media-modal':
100 // Media modal.
101 return did_action( 'wp_enqueue_media' ) || doing_filter( 'wp_enqueue_media' );
102
103 default:
104 return $identifier === $current_screen->id;
105 }
106 }
107
108 /**
109 * Get the URL related to specific admin page or action.
110 *
111 * @since 1.0
112 *
113 * @param string $action An action.
114 * @param array|string $arg An array of arguments. It can contain an attachment ID and/or a context.
115 * @return string The URL of the specific admin page or action.
116 */
117 function get_imagify_admin_url( $action = 'settings', $arg = array() ) {
118 if ( is_array( $arg ) ) {
119 $id = isset( $arg['attachment_id'] ) ? $arg['attachment_id'] : 0;
120 $context = isset( $arg['context'] ) ? $arg['context'] : 'wp';
121 $level = isset( $arg['optimization_level'] ) ? $arg['optimization_level'] : '';
122 }
123
124 switch ( $action ) {
125 case 'manual-override-upload':
126 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_override_upload&attachment_id=' . $id . '&optimization_level=' . $level . '&context=' . $context ), 'imagify-manual-override-upload-' . $id . '-' . $context );
127
128 case 'optimize-missing-sizes':
129 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_optimize_missing_sizes&attachment_id=' . $id . '&context=' . $context ), 'imagify-optimize-missing-sizes-' . $id . '-' . $context );
130
131 case 'manual-upload':
132 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_upload&attachment_id=' . $id . '&context=' . $context ), 'imagify-manual-upload-' . $id . '-' . $context );
133
134 case 'restore-upload':
135 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_restore_upload&attachment_id=' . $id . '&context=' . $context ), 'imagify-restore-upload-' . $id . '-' . $context );
136
137 case 'dismiss-notice':
138 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice&notice=' . $arg ), Imagify_Notices::DISMISS_NONCE_ACTION );
139
140 case 'optimize-file':
141 case 'restore-file':
142 case 'refresh-file-modified':
143 $action = 'imagify_' . str_replace( '-', '_', $action );
144 return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id ), $action );
145
146 case 'reoptimize-file':
147 $action = 'imagify_' . str_replace( '-', '_', $action );
148 return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id . '&level=' . $level ), $action );
149
150 case 'get-files-tree':
151 return wp_nonce_url( admin_url( 'admin-ajax.php?action=imagify_get_files_tree' ), 'get-files-tree' );
152
153 case 'bulk-optimization':
154 return admin_url( 'upload.php?page=' . Imagify_Views::get_instance()->get_bulk_page_slug() );
155
156 case 'files-bulk-optimization':
157 $page = '?page=' . Imagify_Views::get_instance()->get_bulk_page_slug();
158 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
159
160 case 'files-list':
161 $page = '?page=' . Imagify_Views::get_instance()->get_files_page_slug();
162 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
163
164 case 'folder-errors':
165 switch ( $arg ) {
166 case 'library':
167 return add_query_arg( array(
168 'mode' => 'list',
169 'imagify-status' => 'errors',
170 ), admin_url( 'upload.php' ) );
171
172 case 'custom-folders':
173 return add_query_arg( array(
174 'status-filter' => 'errors',
175 ), get_imagify_admin_url( 'files-list' ) );
176 }
177 return '';
178
179 default:
180 $page = '?page=' . Imagify_Views::get_instance()->get_settings_page_slug();
181 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'options-general.php' . $page );
182 }
183 }
184
185 /**
186 * Get maximal width and height from all thumbnails.
187 *
188 * @since 1.1
189 *
190 * @return array An array containing the max width and height.
191 */
192 function get_imagify_max_intermediate_image_size() {
193 $width = 0;
194 $height = 0;
195 $limit = 9999;
196
197 foreach ( get_imagify_thumbnail_sizes() as $_size ) {
198 if ( $_size['width'] > $width && $_size['width'] < $limit ) {
199 $width = $_size['width'];
200 }
201
202 if ( $_size['height'] > $height && $_size['height'] < $limit ) {
203 $height = $_size['height'];
204 }
205 }
206
207 return array(
208 'width' => $width,
209 'height' => $height,
210 );
211 }
212
213 /**
214 * Get the default Bulk Optimization buffer size.
215 *
216 * @since 1.5.10
217 * @since 1.7 Added $sizes parameter.
218 * @author Jonathan Buttigieg
219 *
220 * @param int $sizes Number of image sizes per item (attachment).
221 * @return int The buffer size.
222 */
223 function get_imagify_bulk_buffer_size( $sizes = false ) {
224 if ( ! $sizes ) {
225 $sizes = count( get_imagify_thumbnail_sizes() );
226 }
227
228 switch ( true ) {
229 case ( $sizes >= 10 ):
230 return 1;
231
232 case ( $sizes >= 8 ):
233 return 2;
234
235 case ( $sizes >= 6 ):
236 return 3;
237
238 default:
239 return 4;
240 }
241 }
242
243 /**
244 * Simple helper to get the WP Rocket's site URL.
245 * The URL is localized and contains some utm_*** parameters.
246 *
247 * @since 1.6.8
248 * @since 1.6.9 Added $path and $query parameters.
249 * @author Grégory Viguier
250 *
251 * @param string $path A path to add to the URL (URI). Not in use yet.
252 * @param array $query An array of query arguments (utm_*).
253 * @return string The URL.
254 */
255 function imagify_get_wp_rocket_url( $path = false, $query = array() ) {
256 $wprocket_url = 'https://wp-rocket.me/';
257
258 // Current lang.
259 $lang = imagify_get_current_lang_in( array( 'de', 'es', 'fr', 'it' ) );
260
261 if ( 'en' !== $lang ) {
262 $wprocket_url .= $lang . '/';
263 }
264
265 // URI.
266 $paths = array(
267 'pricing' => array(
268 'de' => 'preise',
269 'en' => 'pricing',
270 'es' => 'precios',
271 'fr' => 'offres',
272 'it' => 'offerte',
273 ),
274 );
275
276 if ( $path ) {
277 $path = trim( $path, '/' );
278
279 if ( isset( $paths[ $path ] ) ) {
280 $wprocket_url .= $paths[ $path ][ $lang ] . '/';
281 } else {
282 $wprocket_url .= $path . '/';
283 }
284 }
285
286 // Query args.
287 $query = array_merge( array(
288 'utm_source' => 'imagify-coupon',
289 'utm_medium' => 'plugin',
290 'utm_campaign' => 'imagify',
291 ), $query );
292
293 return add_query_arg( $query, $wprocket_url );
294 }
295
296 /**
297 * Check for nonce.
298 *
299 * @since 1.6.10
300 * @author Grégory Viguier
301 *
302 * @param string $action Action nonce.
303 * @param string|bool $query_arg Optional. Key to check for the nonce in `$_REQUEST`. If false, `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' (in that order). Default false.
304 */
305 function imagify_check_nonce( $action, $query_arg = false ) {
306 if ( ! check_ajax_referer( $action, $query_arg, false ) ) {
307 imagify_die();
308 }
309 }
310
311 /**
312 * Check for user capacity.
313 *
314 * @since 1.6.10
315 * @since 1.6.11 Uses a capacity describer instead of a capacity itself.
316 * @see imagify_get_capacity()
317 * @author Grégory Viguier
318 *
319 * @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity.
320 * @param int $post_id A post ID.
321 */
322 function imagify_check_user_capacity( $describer = 'manage', $post_id = null ) {
323 if ( ! imagify_current_user_can( $describer, $post_id ) ) {
324 imagify_die();
325 }
326 }
327
328 /**
329 * Die Today.
330 *
331 * @since 1.6.10
332 * @author Grégory Viguier
333 *
334 * @param string $message A message to display.
335 */
336 function imagify_die( $message = null ) {
337 if ( ! isset( $message ) ) {
338 /* translators: This sentense already exists in WordPress. */
339 $message = __( 'Sorry, you are not allowed to do that.', 'imagify' );
340 } elseif ( is_wp_error( $message ) ) {
341 $message = imagify_translate_api_message( $message->get_error_message() );
342 }
343
344 if ( is_array( $message ) ) {
345 if ( ! empty( $message['error'] ) ) {
346 $message['error'] = imagify_translate_api_message( $message['error'] );
347 } elseif ( ! empty( $message['detail'] ) ) {
348 $message['detail'] = imagify_translate_api_message( $message['detail'] );
349 }
350 }
351
352 if ( wp_doing_ajax() ) {
353 wp_send_json_error( $message );
354 }
355
356 if ( is_array( $message ) ) {
357 if ( ! empty( $message['error'] ) ) {
358 $message = $message['error'];
359 } elseif ( ! empty( $message['detail'] ) ) {
360 $message = $message['detail'];
361 } else {
362 $message = reset( $message );
363 }
364 }
365
366 if ( wp_get_referer() ) {
367 $message .= '</p><p>';
368 $message .= sprintf( '<a href="%s">%s</a>',
369 esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
370 /* translators: This sentense already exists in WordPress. */
371 __( 'Go back', 'imagify' )
372 );
373 }
374
375 /* translators: %s is the plugin name. */
376 wp_die( $message, sprintf( __( '%s Failure Notice', 'imagify' ), 'Imagify' ), 403 );
377 }
378
379 /**
380 * Redirect if not an ajax request.
381 *
382 * @since 1.6.10
383 * @author Grégory Viguier
384 *
385 * @param string $message A message to display in an admin notice once redirected.
386 * @param array|string $args_or_url An array of query args to add to the redirection URL. If a string, the complete URL.
387 */
388 function imagify_maybe_redirect( $message = false, $args_or_url = array() ) {
389 if ( wp_doing_ajax() ) {
390 return;
391 }
392
393 if ( $args_or_url && is_array( $args_or_url ) ) {
394 $redirect = add_query_arg( $args_or_url, wp_get_referer() );
395 } elseif ( $args_or_url && is_string( $args_or_url ) ) {
396 $redirect = $args_or_url;
397 } else {
398 $redirect = wp_get_referer();
399 }
400
401 /**
402 * Filter the URL to redirect to.
403 *
404 * @since 1.6.10
405 * @author Grégory Viguier
406 *
407 * @param string $redirect The URL to redirect to.
408 */
409 $redirect = apply_filters( 'imagify_redirect_to', $redirect );
410
411 if ( $message ) {
412 if ( is_multisite() && strpos( $redirect, network_admin_url( '/' ) ) === 0 ) {
413 Imagify_Notices::get_instance()->add_network_temporary_notice( $message );
414 } else {
415 Imagify_Notices::get_instance()->add_site_temporary_notice( $message );
416 }
417 }
418
419 wp_safe_redirect( esc_url_raw( $redirect ) );
420 die();
421 }
422
423 /**
424 * Get cached Imagify user data.
425 * This is usefull to prevent triggering an HTTP request to our server on every page load, but it can be used only where the data doesn't need to be in real time.
426 *
427 * @since 1.7
428 * @author Grégory Viguier
429 *
430 * @return object|bool An object on success. False otherwise.
431 */
432 function imagify_get_cached_user() {
433 if ( ! imagify_valid_key() ) {
434 return false;
435 }
436
437 if ( imagify_is_active_for_network() ) {
438 $user = get_site_transient( 'imagify_user' );
439 } else {
440 $user = get_transient( 'imagify_user' );
441 }
442
443 return is_object( $user ) ? $user : false;
444 }
445
446 /**
447 * Cache Imagify user data for 5 minutes.
448 * Runs every methods to store the results. Also stores formatted data like the quota and the next update date.
449 *
450 * @since 1.7
451 * @author Grégory Viguier
452 *
453 * @return object|bool An object on success. False otherwise.
454 */
455 function imagify_cache_user() {
456 if ( ! imagify_valid_key() ) {
457 return false;
458 }
459
460 $user = new Imagify_User();
461 $data = (object) get_object_vars( $user );
462 $methods = get_class_methods( $user );
463
464 foreach ( $methods as $method ) {
465 if ( '__construct' !== $method ) {
466 $data->$method = $user->$method();
467 }
468 }
469
470 $data->quota_formatted = imagify_size_format( $user->quota * 1048576 );
471 $data->next_date_update_formatted = date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) );
472
473 if ( imagify_is_active_for_network() ) {
474 set_site_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
475 } else {
476 set_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
477 }
478
479 return $data;
480 }
481