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