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

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

457 lines 13.6 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 '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 );
189 }
190 }
191
192 /**
193 * Get maximal width and height from all thumbnails.
194 *
195 * @since 1.1
196 *
197 * @return array An array containing the max width and height.
198 */
199 function get_imagify_max_intermediate_image_size() {
200 $width = 0;
201 $height = 0;
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 }
212 }
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
270 );
271
272 return add_query_arg( $query, $wprocket_url );
273 }
274
275 /**
276 * Check for nonce.
277 *
278 * @since 1.9.11 Return true when nonce is good.
279 * @since 1.6.10
280 *
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.
285 */
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 }
291
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() );
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
344 }
345
346 /**
347 * Redirect if not an ajax request.
348 *
349 * @since 1.6.10
350 *
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.
353 */
354 function imagify_maybe_redirect( $message = false, $args_or_url = [] ) {
355 if ( wp_doing_ajax() ) {
356 return;
357 }
358
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 }
457