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

422 lines 13.0 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 'optimize':
108 case 'manual-upload': // Deprecated.
109 case 'manual-optimize':
110 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_optimize&attachment_id=' . $id . '&context=' . $context ), 'imagify-optimize-' . $id . '-' . $context );
111
112 case 'restore':
113 case 'restore-upload': // Deprecated.
114 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_restore&attachment_id=' . $id . '&context=' . $context ), 'imagify-restore-' . $id . '-' . $context );
115
116 case 'optimize-file':
117 case 'restore-file':
118 case 'refresh-file-modified':
119 $action = 'imagify_' . str_replace( '-', '_', $action );
120 return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id ), $action );
121
122 case 'reoptimize-file':
123 $action = 'imagify_' . str_replace( '-', '_', $action );
124 return wp_nonce_url( admin_url( 'admin-post.php?action=' . $action . '&id=' . $id . '&level=' . $level ), $action );
125
126 case 'get-files-tree':
127 return wp_nonce_url( admin_url( 'admin-ajax.php?action=imagify_get_files_tree' ), 'get-files-tree' );
128
129 case 'bulk-optimization':
130 return admin_url( 'upload.php?page=' . Imagify_Views::get_instance()->get_bulk_page_slug() );
131
132 case 'files-bulk-optimization':
133 $page = '?page=' . Imagify_Views::get_instance()->get_bulk_page_slug();
134 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
135
136 case 'files-list':
137 $page = '?page=' . Imagify_Views::get_instance()->get_files_page_slug();
138 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'upload.php' . $page );
139
140 case 'folder-errors':
141 switch ( $arg ) {
142 case 'wp':
143 return add_query_arg( array(
144 'mode' => 'list',
145 'imagify-status' => 'errors',
146 ), admin_url( 'upload.php' ) );
147
148 case 'custom-folders':
149 return add_query_arg( array(
150 'status-filter' => 'errors',
151 ), get_imagify_admin_url( 'files-list' ) );
152 }
153 /**
154 * Provide a URL to a page displaying optimization errors for the given context.
155 *
156 * @since 1.9
157 * @author Grégory Viguier
158 *
159 * @param string $url The URL.
160 * @param string $arg The context.
161 */
162 return apply_filters( 'imagify_optimization_errors_url', '', $arg );
163
164 case 'dismiss-notice':
165 return wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice&notice=' . $arg ), Imagify_Notices::DISMISS_NONCE_ACTION );
166
167 default:
168 $page = '?page=' . Imagify_Views::get_instance()->get_settings_page_slug();
169 return imagify_is_active_for_network() ? network_admin_url( 'admin.php' . $page ) : admin_url( 'options-general.php' . $page );
170 }
171 }
172
173 /**
174 * Get maximal width and height from all thumbnails.
175 *
176 * @since 1.1
177 *
178 * @return array An array containing the max width and height.
179 */
180 function get_imagify_max_intermediate_image_size() {
181 $width = 0;
182 $height = 0;
183 $limit = 9999;
184
185 foreach ( get_imagify_thumbnail_sizes() as $_size ) {
186 if ( $_size['width'] > $width && $_size['width'] < $limit ) {
187 $width = $_size['width'];
188 }
189
190 if ( $_size['height'] > $height && $_size['height'] < $limit ) {
191 $height = $_size['height'];
192 }
193 }
194
195 return array(
196 'width' => $width,
197 'height' => $height,
198 );
199 }
200
201 /**
202 * Simple helper to get the WP Rocket's site URL.
203 * The URL is localized and contains some utm_*** parameters.
204 *
205 * @since 1.6.8
206 * @since 1.6.9 Added $path and $query parameters.
207 * @author Grégory Viguier
208 *
209 * @param string $path A path to add to the URL (URI). Not in use yet.
210 * @param array $query An array of query arguments (utm_*).
211 * @return string The URL.
212 */
213 function imagify_get_wp_rocket_url( $path = false, $query = array() ) {
214 $wprocket_url = 'https://wp-rocket.me/';
215
216 // Current lang.
217 $lang = imagify_get_current_lang_in( array( 'de', 'es', 'fr', 'it' ) );
218
219 if ( 'en' !== $lang ) {
220 $wprocket_url .= $lang . '/';
221 }
222
223 // URI.
224 $paths = array(
225 'pricing' => array(
226 'de' => 'preise',
227 'en' => 'pricing',
228 'es' => 'precios',
229 'fr' => 'offres',
230 'it' => 'offerte',
231 ),
232 );
233
234 if ( $path ) {
235 $path = trim( $path, '/' );
236
237 if ( isset( $paths[ $path ] ) ) {
238 $wprocket_url .= $paths[ $path ][ $lang ] . '/';
239 } else {
240 $wprocket_url .= $path . '/';
241 }
242 }
243
244 // Query args.
245 $query = array_merge( array(
246 'utm_source' => 'imagify-coupon',
247 'utm_medium' => 'plugin',
248 'utm_campaign' => 'imagify',
249 ), $query );
250
251 return add_query_arg( $query, $wprocket_url );
252 }
253
254 /**
255 * Check for nonce.
256 *
257 * @since 1.6.10
258 * @author Grégory Viguier
259 *
260 * @param string $action Action nonce.
261 * @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.
262 */
263 function imagify_check_nonce( $action, $query_arg = false ) {
264 if ( ! check_ajax_referer( $action, $query_arg, false ) ) {
265 imagify_die();
266 }
267 }
268
269 /**
270 * Die Today.
271 *
272 * @since 1.6.10
273 * @author Grégory Viguier
274 *
275 * @param string $message A message to display.
276 */
277 function imagify_die( $message = null ) {
278 if ( ! isset( $message ) ) {
279 /* translators: This sentense already exists in WordPress. */
280 $message = __( 'Sorry, you are not allowed to do that.', 'imagify' );
281 } elseif ( is_wp_error( $message ) ) {
282 $message = imagify_translate_api_message( $message->get_error_message() );
283 }
284
285 if ( is_array( $message ) ) {
286 if ( ! empty( $message['error'] ) ) {
287 $message['error'] = imagify_translate_api_message( $message['error'] );
288 } elseif ( ! empty( $message['detail'] ) ) {
289 $message['detail'] = imagify_translate_api_message( $message['detail'] );
290 }
291 }
292
293 if ( wp_doing_ajax() ) {
294 wp_send_json_error( $message );
295 }
296
297 if ( is_array( $message ) ) {
298 if ( ! empty( $message['error'] ) ) {
299 $message = $message['error'];
300 } elseif ( ! empty( $message['detail'] ) ) {
301 $message = $message['detail'];
302 } else {
303 $message = reset( $message );
304 }
305 }
306
307 if ( wp_get_referer() ) {
308 $message .= '</p><p>';
309 $message .= sprintf( '<a href="%s">%s</a>',
310 esc_url( remove_query_arg( 'updated', wp_get_referer() ) ),
311 /* translators: This sentense already exists in WordPress. */
312 __( 'Go back', 'imagify' )
313 );
314 }
315
316 /* translators: %s is the plugin name. */
317 wp_die( $message, sprintf( __( '%s Failure Notice', 'imagify' ), 'Imagify' ), 403 );
318 }
319
320 /**
321 * Redirect if not an ajax request.
322 *
323 * @since 1.6.10
324 * @author Grégory Viguier
325 *
326 * @param string $message A message to display in an admin notice once redirected.
327 * @param array|string $args_or_url An array of query args to add to the redirection URL. If a string, the complete URL.
328 */
329 function imagify_maybe_redirect( $message = false, $args_or_url = array() ) {
330 if ( wp_doing_ajax() ) {
331 return;
332 }
333
334 if ( $args_or_url && is_array( $args_or_url ) ) {
335 $redirect = add_query_arg( $args_or_url, wp_get_referer() );
336 } elseif ( $args_or_url && is_string( $args_or_url ) ) {
337 $redirect = $args_or_url;
338 } else {
339 $redirect = wp_get_referer();
340 }
341
342 /**
343 * Filter the URL to redirect to.
344 *
345 * @since 1.6.10
346 * @author Grégory Viguier
347 *
348 * @param string $redirect The URL to redirect to.
349 */
350 $redirect = apply_filters( 'imagify_redirect_to', $redirect );
351
352 if ( $message ) {
353 if ( is_multisite() && strpos( $redirect, network_admin_url( '/' ) ) === 0 ) {
354 Imagify_Notices::get_instance()->add_network_temporary_notice( $message );
355 } else {
356 Imagify_Notices::get_instance()->add_site_temporary_notice( $message );
357 }
358 }
359
360 wp_safe_redirect( esc_url_raw( $redirect ) );
361 die();
362 }
363
364 /**
365 * Get cached Imagify user data.
366 * 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.
367 *
368 * @since 1.7
369 * @author Grégory Viguier
370 *
371 * @return object|bool An object on success. False otherwise.
372 */
373 function imagify_get_cached_user() {
374 if ( ! Imagify_Requirements::is_api_key_valid() ) {
375 return false;
376 }
377
378 if ( imagify_is_active_for_network() ) {
379 $user = get_site_transient( 'imagify_user' );
380 } else {
381 $user = get_transient( 'imagify_user' );
382 }
383
384 return is_object( $user ) ? $user : false;
385 }
386
387 /**
388 * Cache Imagify user data for 5 minutes.
389 * Runs every methods to store the results. Also stores formatted data like the quota and the next update date.
390 *
391 * @since 1.7
392 * @author Grégory Viguier
393 *
394 * @return object|bool An object on success. False otherwise.
395 */
396 function imagify_cache_user() {
397 if ( ! Imagify_Requirements::is_api_key_valid() ) {
398 return false;
399 }
400
401 $user = new Imagify_User();
402 $data = (object) get_object_vars( $user );
403 $methods = get_class_methods( $user );
404
405 foreach ( $methods as $method ) {
406 if ( '__construct' !== $method ) {
407 $data->$method = $user->$method();
408 }
409 }
410
411 $data->quota_formatted = imagify_size_format( $user->quota * pow( 1024, 2 ) );
412 $data->next_date_update_formatted = date_i18n( get_option( 'date_format' ), strtotime( $user->next_date_update ) );
413
414 if ( imagify_is_active_for_network() ) {
415 set_site_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
416 } else {
417 set_transient( 'imagify_user', $data, 5 * MINUTE_IN_SECONDS );
418 }
419
420 return $data;
421 }
422