PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.9
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.9
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 2.2.9, at inc/functions/admin.php

448 lines 13.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 use Imagify\Notices\Notices;
3 use Imagify\User\User;
4
5 defined( 'ABSPATH' ) || exit;
6
7 /**
8 * Tell if the current screen is what we're looking for.
9 *
10 * @since 1.6.10
11 *
12 * @param string $identifier The screen "name".
13 * @return bool
14 */
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 }
80 }
81
82 /**
83 * Get the URL related to specific admin page or action.
84 *
85 * @since 1.0
86 *
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.
90 */
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 }
97
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 );
102
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 );
105
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 );
108
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 );
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
134 case 'bulk-optimization':
135 return admin_url( 'upload.php?page=' . Imagify_Views::get_instance()->get_bulk_page_slug() );
136
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 'dismiss-notice':
175 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice&notice=' . $arg ), Notices::DISMISS_NONCE_ACTION );
176
177 default:
178 $page = '?page=' . Imagify_Views::get_instance()->get_settings_page_slug();
179 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'options-general.php' . $page );
180 }
181 }
182
183 /**
184 * Get maximal width and height from all thumbnails.
185 *
186 * @since 1.1
187 *
188 * @return array An array containing the max width and height.
189 */
190 function get_imagify_max_intermediate_image_size() {
191 $width = 0;
192 $height = 0;
193 $limit = 9999;
194
195 foreach ( get_imagify_thumbnail_sizes() as $_size ) {
196 if ( $_size['width'] > $width && $_size['width'] < $limit ) {
197 $width = $_size['width'];
198 }
199
200 if ( $_size['height'] > $height && $_size['height'] < $limit ) {
201 $height = $_size['height'];
202 }
203 }
204
205 return [
206 'width' => $width,
207 'height' => $height,
208 ];
209 }
210
211 /**
212 * Simple helper to get the WP Rocket's site URL.
213 * The URL is localized and contains some utm_*** parameters.
214 *
215 * @since 1.6.8
216 * @since 1.6.9 Added $path and $query parameters.
217 *
218 * @param string $path A path to add to the URL (URI). Not in use yet.
219 * @param array $query An array of query arguments (utm_*).
220 * @return string The URL.
221 */
222 function imagify_get_wp_rocket_url( $path = false, $query = [] ) {
223 $wprocket_url = 'https://wp-rocket.me/wp-rocket-for-imagify-users/';
224
225 // Current lang.
226 $lang = imagify_get_current_lang_in( [ 'de', 'es', 'fr', 'it' ] );
227
228 if ( 'en' !== $lang ) {
229 $wprocket_url .= $lang . '/';
230 }
231
232 // URI.
233 $paths = [
234 'pricing' => [
235 'de' => 'preise',
236 'en' => 'pricing',
237 'es' => 'precios',
238 'fr' => 'offres',
239 'it' => 'offerte',
240 ],
241 ];
242
243 if ( $path ) {
244 $path = trim( $path, '/' );
245
246 if ( isset( $paths[ $path ] ) ) {
247 $wprocket_url .= $paths[ $path ][ $lang ] . '/';
248 } else {
249 $wprocket_url .= $path . '/';
250 }
251 }
252
253 // Query args.
254 $query = array_merge(
255 [
256 'utm_source' => 'imagify',
257 'utm_medium' => 'partners',
258 'utm_campaign' => 'imagify-benefits',
259 ],
260 $query
261 );
262
263 return add_query_arg( $query, $wprocket_url );
264 }
265
266 /**
267 * Check for nonce.
268 *
269 * @since 1.9.11 Return true when nonce is good.
270 * @since 1.6.10
271 *
272 * @param string $action Action nonce.
273 * @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.
274 *
275 * @return bool True if the nonce is good; otherwise terminates.
276 */
277 function imagify_check_nonce( $action, $query_arg = false ) {
278 if ( ! check_ajax_referer( $action, $query_arg, false ) ) {
279 imagify_die();
280 return false;
281 }
282
283 return true;
284 }
285
286 /**
287 * Die Today.
288 *
289 * @since 1.6.10
290 *
291 * @param string $message A message to display.
292 */
293 function imagify_die( $message = null ) {
294 if ( ! isset( $message ) ) {
295 /* translators: This sentense already exists in WordPress. */
296 $message = __( 'Sorry, you are not allowed to do that.', 'imagify' );
297 } elseif ( is_wp_error( $message ) ) {
298 $message = imagify_translate_api_message( $message->get_error_message() );
299 }
300
301 if ( is_array( $message ) ) {
302 if ( ! empty( $message['error'] ) ) {
303 $message['error'] = imagify_translate_api_message( $message['error'] );
304 } elseif ( ! empty( $message['detail'] ) ) {
305 $message['detail'] = imagify_translate_api_message( $message['detail'] );
306 }
307 }
308
309 if ( wp_doing_ajax() ) {
310 wp_send_json_error( $message );
311 }
312
313 if ( is_array( $message ) ) {
314 if ( ! empty( $message['error'] ) ) {
315 $message = $message['error'];
316 } elseif ( ! empty( $message['detail'] ) ) {
317 $message = $message['detail'];
318 } else {
319 $message = reset( $message );
320 }
321 }
322
323 if ( wp_get_referer() ) {
324 $message .= '</p><p>';
325 $message .= sprintf(
326 '<a href="%s">%s</a>',
327 esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
328 /* translators: This sentense already exists in WordPress. */
329 __( 'Go back', 'imagify' )
330 );
331 }
332
333 /* translators: %s is the plugin name. */
334 wp_die( $message, sprintf( esc_html__( '%s Failure Notice', 'imagify' ), 'Imagify' ), 403 ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
335 }
336
337 /**
338 * Redirect if not an ajax request.
339 *
340 * @since 1.6.10
341 *
342 * @param string $message A message to display in an admin notice once redirected.
343 * @param array|string $args_or_url An array of query args to add to the redirection URL. If a string, the complete URL.
344 */
345 function imagify_maybe_redirect( $message = false, $args_or_url = [] ) {
346 if ( wp_doing_ajax() ) {
347 return;
348 }
349
350 if ( $args_or_url && is_array( $args_or_url ) ) {
351 $redirect = add_query_arg( $args_or_url, wp_get_referer() );
352 } elseif ( $args_or_url && is_string( $args_or_url ) ) {
353 $redirect = $args_or_url;
354 } else {
355 $redirect = wp_get_referer();
356 }
357
358 /**
359 * Filter the URL to redirect to.
360 *
361 * @since 1.6.10
362 *
363 * @param string $redirect The URL to redirect to.
364 */
365 $redirect = apply_filters( 'imagify_redirect_to', $redirect );
366
367 if ( $message ) {
368 if ( is_multisite() && strpos( $redirect, network_admin_url( '/' ) ) === 0 ) {
369 Notices::get_instance()->add_network_temporary_notice( $message );
370 } else {
371 Notices::get_instance()->add_site_temporary_notice( $message );
372 }
373 }
374
375 wp_safe_redirect( esc_url_raw( $redirect ) );
376 die();
377 }
378
379 /**
380 * Get cached Imagify user data.
381 * 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.
382 *
383 * @since 1.7
384 *
385 * @return object|bool An object on success. False otherwise.
386 */
387 function imagify_get_cached_user() {
388 if ( ! Imagify_Requirements::is_api_key_valid() ) {
389 return false;
390 }
391
392 if ( imagify_is_active_for_network() ) {
393 $user = get_site_transient( 'imagify_user' );
394 } else {
395 $user = get_transient( 'imagify_user' );
396 }
397
398 return is_object( $user ) ? $user : false;
399 }
400
401 /**
402 * Cache Imagify user data for 5 minutes.
403 * Runs every methods to store the results. Also stores formatted data like the quota and the next update date.
404 *
405 * @since 1.7
406 *
407 * @return object|bool An object on success. False otherwise.
408 */
409 function imagify_cache_user() {
410 if ( ! Imagify_Requirements::is_api_key_valid() ) {
411 return false;
412 }
413
414 $user = new User();
415 $data = (object) get_object_vars( $user );
416 $methods = get_class_methods( $user );
417
418 foreach ( $methods as $method ) {
419 if ( '__construct' !== $method ) {
420 $data->$method = $user->$method();
421 }
422 }
423
424 $data->quota_formatted = imagify_size_format( $user->get_quota() * pow( 1024, 2 ) );
425 $data->next_date_update_formatted = date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) );
426
427 if ( imagify_is_active_for_network() ) {
428 set_site_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
429 } else {
430 set_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
431 }
432
433 return $data;
434 }
435
436 /**
437 * Delete cached Imagify user data.
438 *
439 * @since 1.9.5
440 */
441 function imagify_delete_cached_user() {
442 if ( imagify_is_active_for_network() ) {
443 delete_site_transient( 'imagify_user' );
444 } else {
445 delete_transient( 'imagify_user' );
446 }
447 }
448