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 +399 -107 1.5 → trunk View file →
@@ -1,164 +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 = '';
27 - $id = ( isset( $arg['attachment_id'] ) ) ? $arg['attachment_id'] : 0;
28 - $context = ( isset( $arg['context'] ) ) ? $arg['context'] : 'wp';
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 + }
29 97
30 - switch( $action ) {
31 - case 'manual-override-upload':
32 - $level = ( isset( $arg['optimization_level'] ) ) ? $arg['optimization_level'] : 0;
33 - $url = 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' );
34 - 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 );
35 102
36 - case 'manual-upload':
37 - $url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_upload&attachment_id=' . $id . '&context=' . $context ), 'imagify-manual-upload' );
38 - 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 );
39 105
40 - case 'restore-upload' :
41 - $url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_restore_upload&attachment_id=' . $id . '&context=' . $context ), 'imagify-restore-upload' );
42 - 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 );
43 108
44 - case 'dismiss-notice':
45 - $url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice&notice=' . $arg ), 'imagify-dismiss-notice' );
46 - 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 );
47 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 +
48 134 case 'bulk-optimization':
49 - $url = admin_url( 'upload.php?page=' . IMAGIFY_SLUG . '-bulk-optimization' );
50 - break;
135 + return admin_url( 'upload.php?page=' . Imagify_Views::get_instance()->get_bulk_page_slug() );
51 136
52 - case 'options-general':
53 - default :
54 - $page = imagify_is_active_for_network() ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' );
55 - $url = $page . '?page=' . IMAGIFY_SLUG;
56 - 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 );
57 189 }
58 -
59 - return $url;
60 190 }
61 191
62 -/*
192 +/**
63 193 * Get maximal width and height from all thumbnails.
64 194 *
65 195 * @since 1.1
66 196 *
67 - * @return array An array containing the max with and height.
197 + * @return array An array containing the max width and height.
68 198 */
69 199 function get_imagify_max_intermediate_image_size() {
70 - global $_wp_additional_image_sizes;
71 -
72 200 $width = 0;
73 201 $height = 0;
74 - $limit = 9999;
75 - $get_intermediate_image_sizes = get_intermediate_image_sizes();
76 -
77 - // Create the full array with sizes and crop info
78 - foreach( $get_intermediate_image_sizes as $_size ) {
79 - if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
80 - $_size_width = get_option( $_size . '_size_w' );
81 - $_size_height = get_option( $_size . '_size_h' );
82 - } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
83 - $_size_width = $_wp_additional_image_sizes[ $_size ]['width'];
84 - $_size_height = $_wp_additional_image_sizes[ $_size ]['height'];
85 - }
86 -
87 - if ( ! isset( $_size_width, $_size_height ) ) {
88 - continue;
89 - }
90 -
91 - if ( $_size_width < $limit ) {
92 - $width = max( $width, $_size_width );
93 - }
94 -
95 - if ( $_size_height < $limit ) {
96 - $height = max( $height, $_size_height );
97 - }
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 + }
98 212 }
99 - return array(
100 - 'width' => $width,
101 - '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
102 270 );
271 +
272 + return add_query_arg( $query, $wprocket_url );
103 273 }
104 274
105 275 /**
106 - * Renew a dismissed Imagify notice.
276 + * Check for nonce.
107 277 *
108 - * @since 1.0
278 + * @since 1.9.11 Return true when nonce is good.
279 + * @since 1.6.10
109 280 *
110 - * @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.
111 285 */
112 -function imagify_renew_notice( $notice, $user_id = 0 ) {
113 - global $current_user;
114 - $user_id = ( 0 === $user_id ) ? $current_user->ID : $user_id;
115 - $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 + }
116 291
117 - if( $notices && false !== array_search( $notice, $notices ) ) {
118 - unset( $notices[array_search( $notice, $notices )] );
119 - 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() );
120 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
121 344 }
122 345
123 346 /**
124 - * Dismissed an Imagify notice.
347 + * Redirect if not an ajax request.
125 348 *
126 - * @since 1.0
349 + * @since 1.6.10
127 350 *
128 - * @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.
129 353 */
130 -function imagify_dismiss_notice( $notice, $user_id = 0 ) {
131 - global $current_user;
132 - $user_id = ( 0 === $user_id ) ? $current_user->ID : $user_id;
133 - $notices = get_user_meta( $user_id, '_imagify_ignore_notices', true );
134 - $notices[] = $notice;
135 - $notices = array_filter( $notices );
136 - $notices = array_unique( $notices );
354 +function imagify_maybe_redirect( $message = false, $args_or_url = [] ) {
355 + if ( wp_doing_ajax() ) {
356 + return;
357 + }
137 358
138 - update_user_meta( $user_id, '_imagify_ignore_notices', $notices );
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();
139 386 }
140 387
141 388 /**
142 - * Combine two arrays with some specific keys.
143 - * We use this function to combine the result of 2 SQL queries.
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.
144 391 *
145 - * @since 1.4.5
392 + * @since 1.7
146 393 *
147 - * @return array $result The combined array
394 + * @return object|bool An object on success. False otherwise.
148 395 */
149 -function imagify_query_results_combine( $keys, $values ) {
150 - if ( ! $values ) {
151 - return array();
396 +function imagify_get_cached_user() {
397 + if ( ! Imagify_Requirements::is_api_key_valid() ) {
398 + return false;
152 399 }
153 -
154 - $result = array();
155 - $keys = array_flip( $keys );
156 -
157 - foreach ( $values as $v ) {
158 - if ( isset( $keys[ $v['id'] ] ) ) {
159 - $result[ $v['id'] ] = $v['value'];
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();
160 430 }
161 431 }
162 -
163 - return $result;
164 -}
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 +}