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

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