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-ui.php

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

519 lines 16.1 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 * Get the optimization data list for a specific media.
6 *
7 * @since 1.0
8 * @since 1.9 Function signature changed.
9 * @author Jonathan Buttigieg
10 *
11 * @param ProcessInterface $process The optimization process object.
12 * @return string The output to print.
13 */
14 function get_imagify_attachment_optimization_text( $process ) {
15 if ( ! $process->is_valid() ) {
16 return '';
17 }
18
19 $is_media_page = Imagify_Views::get_instance()->is_media_page();
20 $is_library_page = Imagify_Views::get_instance()->is_wp_library_page();
21 $output = $is_media_page ? '' : '<ul class="imagify-datas-list">';
22 $output_before = $is_media_page ? '' : '<li class="imagify-data-item">';
23 $output_after = $is_media_page ? '<br/>' : '</li>';
24 $reoptimize_link = get_imagify_attachment_reoptimize_link( $process );
25 $reoptimize_link .= get_imagify_attachment_optimize_missing_thumbnails_link( $process );
26 $reoptimize_link .= get_imagify_attachment_generate_webp_versions_link( $process );
27 $reoptimize_link .= get_imagify_attachment_delete_webp_versions_link( $process );
28 $reoptimize_output = $reoptimize_link ? $reoptimize_link : '';
29 $reoptimize_output_before = '<div class="imagify-datas-actions-links">';
30 $reoptimize_output_after = '</div><!-- .imagify-datas-actions-links -->';
31 $error = get_imagify_attachment_error_text( $process );
32 $media = $process->get_media();
33
34 if ( $error ) {
35 if ( ! $is_media_page && $reoptimize_link && $media->has_backup() ) {
36 $reoptimize_output .= '<span class="attachment-has-backup hidden"></span>';
37 }
38
39 $reoptimize_output = $reoptimize_output_before . $reoptimize_output . $reoptimize_output_after;
40
41 return $is_media_page ? $output_before . $error . $reoptimize_output . $output_after : $error . $reoptimize_output;
42 }
43
44 $data = $process->get_data();
45 $attachment_id = $media->get_id();
46 $optimization_level = imagify_get_optimization_level_label( $data->get_optimization_level() );
47
48 if ( ! $is_media_page ) {
49 $output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . $data->get_optimized_size() . '</strong>' . $output_after;
50 }
51
52 $chart = '';
53
54 if ( ! $is_media_page ) {
55 if ( ! $is_library_page ) {
56 // No need to print this on the library page, the event whould be triggered before the handler is attached (the JS file is loaded in the footer).
57 $chart = '<script type="text/javascript">jQuery( window ).trigger( "canvasprinted.imagify", [ ".imagify-consumption-chart-' . $attachment_id . '" ] ); </script>';
58 }
59
60 $chart = '<span class="imagify-chart">
61 <span class="imagify-chart-container">
62 <canvas class="imagify-consumption-chart imagify-consumption-chart-' . $attachment_id . '" width="15" height="15"></canvas>
63 ' . $chart . '
64 </span>
65 </span>';
66 }
67
68 $output .= $output_before;
69 $output .= '<span class="data">' . __( 'Original Saving:', 'imagify' ) . '</span> ';
70 $output .= '<strong>' . $chart . '<span class="imagify-chart-value">' . $data->get_saving_percent() . '</span>%</strong>';
71 $output .= $output_after;
72
73 // More details section.
74 if ( ! $is_media_page ) {
75 // New list.
76 $output .= '</ul>';
77 $output .= '<p class="imagify-datas-more-action">';
78 $output .= '<a href="#imagify-view-details-' . $attachment_id . '" data-close="' . __( 'Close details', 'imagify' ) . '" data-open="' . __( 'View details', 'imagify' ) . '">';
79 $output .= '<span class="the-text">' . __( 'View details', 'imagify' ) . '</span>';
80 $output .= '<span class="dashicons dashicons-arrow-down-alt2"></span>';
81 $output .= '</a>';
82 $output .= '</p>';
83 $output .= '<ul id="imagify-view-details-' . $attachment_id . '" class="imagify-datas-list imagify-datas-details">';
84
85 // Not in metabox.
86 $output .= $output_before . '<span class="data">' . __( 'Original Filesize:', 'imagify' ) . '</span> <strong class="original">' . $data->get_original_size() . '</strong>' . $output_after;
87 }
88
89 $output .= $output_before . '<span class="data">' . __( 'Level:', 'imagify' ) . '</span> <strong>' . $optimization_level . '</strong>' . $output_after;
90
91 if ( $media->is_image() ) {
92 $has_webp = $process->has_webp() ? __( 'Yes', 'imagify' ) : __( 'No', 'imagify' );
93 $output .= $output_before . '<span class="data">' . __( 'Webp generated:', 'imagify' ) . '</span> <strong class="big">' . esc_html( $has_webp ) . '</strong>' . $output_after;
94
95 $total_optimized_thumbnails = $data->get_optimized_sizes_count();
96
97 if ( $total_optimized_thumbnails ) {
98 $output .= $output_before . '<span class="data">' . __( 'Thumbnails Optimized:', 'imagify' ) . '</span> <strong>' . $total_optimized_thumbnails . '</strong>' . $output_after;
99 $output .= $output_before . '<span class="data">' . __( 'Overall Saving:', 'imagify' ) . '</span> <strong>' . $data->get_overall_saving_percent() . '%</strong>' . $output_after;
100 }
101 }
102
103 // End of list.
104 $output .= $is_media_page ? '' : '</ul>';
105
106 // Actions section.
107 $output .= $is_media_page ? $output_before : '';
108 $output .= $reoptimize_output_before;
109 $output .= $reoptimize_output;
110
111 if ( $media->has_backup() ) {
112 $url = get_imagify_admin_url( 'restore', [
113 'attachment_id' => $attachment_id,
114 'context' => $media->get_context(),
115 ] );
116
117 $output .= Imagify_Views::get_instance()->get_template( 'button/restore', [
118 'url' => $url,
119 'atts' => [
120 'class' => $is_media_page ? '' : null,
121 ],
122 ] );
123
124 if ( ! $is_library_page ) {
125 $output .= '<input id="imagify-original-src" type="hidden" value="' . esc_url( $media->get_backup_url() ) . '">';
126 $output .= '<input id="imagify-original-size" type="hidden" value="' . $data->get_original_size() . '">';
127 $output .= '<input id="imagify-full-src" type="hidden" value="' . esc_url( $media->get_fullsize_url() ) . '">';
128
129 if ( $media->is_image() ) {
130 $dimensions = $media->get_dimensions();
131
132 $output .= '<input id="imagify-full-width" type="hidden" value="' . $dimensions['width'] . '">';
133 $output .= '<input id="imagify-full-height" type="hidden" value="' . $dimensions['height'] . '">';
134 }
135 }
136 }
137
138 $output .= $reoptimize_output_after;
139
140 return $output;
141 }
142
143 /**
144 * Get the error message for a specific attachment.
145 *
146 * @since 1.0
147 * @since 1.9 Function signature changed.
148 * @author Jonathan Buttigieg
149 *
150 * @param ProcessInterface $process The optimization process object.
151 * @return string The output to print.
152 */
153 function get_imagify_attachment_error_text( $process ) {
154 if ( ! $process->is_valid() ) {
155 return '';
156 }
157
158 $data = $process->get_data()->get_optimization_data();
159
160 if ( ! isset( $data['sizes']['full']['success'] ) || $data['sizes']['full']['success'] ) {
161 return '';
162 }
163
164 $class = 'button';
165 $media = $process->get_media();
166 $url = get_imagify_admin_url( 'optimize', [
167 'attachment_id' => $media->get_id(),
168 'context' => $media->get_context(),
169 ] );
170
171 if ( ! Imagify_Views::get_instance()->is_media_page() ) {
172 $class .= ' button-imagify-optimize';
173 }
174
175 return Imagify_Views::get_instance()->get_template( 'button/retry-optimize', [
176 'url' => $url,
177 'error' => $data['sizes']['full']['error'],
178 'atts' => [
179 'class' => $class,
180 ],
181 ] );
182 }
183
184 /**
185 * Get the re-optimize link for a specific attachment.
186 *
187 * @since 1.0
188 * @since 1.9 Function signature changed.
189 * @author Jonathan Buttigieg
190 *
191 * @param ProcessInterface $process The optimization process object.
192 * @return string The output to print.
193 */
194 function get_imagify_attachment_reoptimize_link( $process ) {
195 if ( ! $process->is_valid() ) {
196 return '';
197 }
198
199 $data = $process->get_data();
200
201 if ( ! $data->get_optimization_status() ) {
202 // Not optimized yet.
203 return '';
204 }
205
206 // Stop the process if the API key isn't valid.
207 if ( ! Imagify_Requirements::is_api_key_valid() ) {
208 return '';
209 }
210
211 $is_already_optimized = $data->is_already_optimized();
212 $media = $process->get_media();
213 $can_reoptimize = $is_already_optimized || $media->has_backup();
214
215 // Don't display anything if there is no backup or the image has been optimized.
216 if ( ! $can_reoptimize ) {
217 return '';
218 }
219
220 $output = '';
221 $views = Imagify_Views::get_instance();
222 $media_level = $data->get_optimization_level();
223 $data = [];
224 $url_args = [
225 'attachment_id' => $media->get_id(),
226 'context' => $media->get_context(),
227 ];
228
229 if ( Imagify_Views::get_instance()->is_media_page() ) {
230 $data['atts'] = [
231 'class' => '',
232 ];
233 }
234
235 foreach ( [ 2, 1, 0 ] as $level ) {
236 /**
237 * Display a link if:
238 * - the level is lower than the one used to optimize the media,
239 * - or, the level is higher and the media is not already optimized.
240 */
241 if ( $media_level < $level || ( $media_level > $level && ! $is_already_optimized ) ) {
242 $url_args['optimization_level'] = $level;
243 $data['optimization_level'] = $level;
244 $data['url'] = get_imagify_admin_url( 'manual-reoptimize', $url_args );
245
246 $output .= $views->get_template( 'button/re-optimize', $data );
247 $output .= '<br/>';
248 }
249 }
250
251 return $output;
252 }
253
254 /**
255 * Get the link to optimize missing thumbnail sizes for a specific attachment.
256 *
257 * @since 1.6.10
258 * @since 1.9 Function signature changed.
259 * @author Grégory Viguier
260 *
261 * @param ProcessInterface $process The optimization process object.
262 * @return string The output to print.
263 */
264 function get_imagify_attachment_optimize_missing_thumbnails_link( $process ) {
265 if ( ! $process->is_valid() ) {
266 return '';
267 }
268
269 $media = $process->get_media();
270
271 if ( ! $media->is_image() || ! Imagify_Requirements::is_api_key_valid() || ! $media->has_backup() ) {
272 return '';
273 }
274
275 $context = $media->get_context();
276
277 /**
278 * Allow to not display the "Optimize missing thumbnails" link.
279 *
280 * @since 1.6.10
281 * @since 1.9 The $attachment object is replaced by a $process object.
282 * @author Grégory Viguier
283 *
284 * @param bool $display True to display the link. False to not display it.
285 * @param ProcessInterface $process The optimization process object.
286 * @param string $context The context.
287 */
288 $display = apply_filters( 'imagify_display_missing_thumbnails_link', true, $process, $context );
289
290 // Stop the process if the filter is false.
291 if ( ! $display ) {
292 return '';
293 }
294
295 $missing_sizes = $process->get_missing_sizes();
296
297 if ( ! $missing_sizes || is_wp_error( $missing_sizes ) ) {
298 return '';
299 }
300
301 $url = get_imagify_admin_url( 'optimize-missing-sizes', [
302 'attachment_id' => $media->get_id(),
303 'context' => $context,
304 ] );
305
306 return Imagify_Views::get_instance()->get_template( 'button/optimize-missing-sizes', [
307 'url' => $url,
308 'count' => count( $missing_sizes ),
309 ] );
310 }
311
312 /**
313 * Get the link to generate webp versions if they are missing.
314 *
315 * @since 1.9
316 * @author Grégory Viguier
317 *
318 * @param ProcessInterface $process The optimization process object.
319 * @return string The output to print.
320 */
321 function get_imagify_attachment_generate_webp_versions_link( $process ) {
322 if ( ! $process->is_valid() ) {
323 return '';
324 }
325
326 if ( ! get_imagify_option( 'convert_to_webp' ) ) {
327 return '';
328 }
329
330 $media = $process->get_media();
331
332 if ( ! $media->is_image() || ! Imagify_Requirements::is_api_key_valid() || ! $media->has_backup() ) {
333 return '';
334 }
335
336 $data = $process->get_data();
337
338 if ( ! $data->is_optimized() && ! $data->is_already_optimized() ) {
339 return '';
340 }
341
342 if ( $process->has_webp() ) {
343 return '';
344 }
345
346 $context = $media->get_context();
347
348 /**
349 * Allow to not display the "Generate webp versions" link.
350 *
351 * @since 1.9
352 * @author Grégory Viguier
353 *
354 * @param bool $display True to display the link. False to not display it.
355 * @param ProcessInterface $process The optimization process object.
356 * @param string $context The context.
357 */
358 $display = apply_filters( 'imagify_display_generate_webp_versions_link', true, $process, $context );
359
360 // Stop the process if the filter is false.
361 if ( ! $display ) {
362 return '';
363 }
364
365 $url = get_imagify_admin_url( 'generate-webp-versions', [
366 'attachment_id' => $media->get_id(),
367 'context' => $context,
368 ] );
369
370 $output = Imagify_Views::get_instance()->get_template( 'button/generate-webp', [
371 'url' => $url,
372 ] );
373
374 return $output . '<br/>';
375 }
376
377 /**
378 * Get the link to delete webp versions when the status is "already_optimized".
379 *
380 * @since 1.9.6
381 * @author Grégory Viguier
382 *
383 * @param ProcessInterface $process The optimization process object.
384 * @return string The output to print.
385 */
386 function get_imagify_attachment_delete_webp_versions_link( $process ) {
387 if ( ! $process->is_valid() ) {
388 return '';
389 }
390
391 $media = $process->get_media();
392 $context = $media->get_context();
393 $media_id = $media->get_id();
394
395 if ( ! imagify_get_context( $context )->current_user_can( 'manual-restore', $media_id ) ) {
396 imagify_die();
397 }
398
399 $data = $process->get_data();
400
401 if ( ! $data->is_already_optimized() || ! $process->has_webp() ) {
402 return '';
403 }
404
405 $class = '';
406 $url = get_imagify_admin_url( 'delete-webp-versions', [
407 'attachment_id' => $media_id,
408 'context' => $context,
409 ] );
410
411 if ( ! Imagify_Views::get_instance()->is_media_page() ) {
412 $class .= 'button-imagify-delete-webp';
413 }
414
415 return Imagify_Views::get_instance()->get_template( 'button/delete-webp', [
416 'url' => $url,
417 'atts' => [
418 'class' => $class,
419 ],
420 ] );
421 }
422
423 /**
424 * Get all data to diplay for a specific media.
425 *
426 * @since 1.2
427 * @since 1.9 Function signature changed.
428 * @author Jonathan Buttigieg
429 *
430 * @param ProcessInterface $process The optimization process object.
431 * @param bool $with_container Set to false to not return the HTML container.
432 * @return string The output to print.
433 */
434 function get_imagify_media_column_content( $process, $with_container = true ) {
435 if ( ! $process->is_valid() ) {
436 return __( 'This media is not valid.', 'imagify' );
437 }
438
439 if ( ! $process->current_user_can( 'manual-optimize' ) ) {
440 return __( 'You are not allowed to optimize this file.', 'imagify' );
441 }
442
443 $media = $process->get_media();
444
445 // Check if the media is supported.
446 if ( ! $media->is_supported() ) {
447 return __( 'This media is not supported.', 'imagify' );
448 }
449
450 // Check if the media has the required WP data.
451 if ( ! $media->has_required_media_data() ) {
452 return __( 'This media lacks the required metadata and cannot be optimized.', 'imagify' );
453 }
454
455 $data = $process->get_data();
456
457 // Check if the API key is valid.
458 if ( ! Imagify_Requirements::is_api_key_valid() && ! $data->is_optimized() ) {
459 $output = __( 'Invalid API key', 'imagify' );
460 $output .= '<br/>';
461 $output .= '<a href="' . esc_url( get_imagify_admin_url() ) . '">' . __( 'Check your Settings', 'imagify' ) . '</a>';
462 return $output;
463 }
464
465 $media_id = $media->get_id();
466 $context = $media->get_context();
467 $views = Imagify_Views::get_instance();
468 $is_locked = $process->is_locked();
469
470 if ( $is_locked ) {
471 switch ( $is_locked ) {
472 case 'optimizing':
473 $lock_label = __( 'Optimizing...', 'imagify' );
474 break;
475 case 'restoring':
476 $lock_label = __( 'Restoring...', 'imagify' );
477 break;
478 default:
479 $lock_label = __( 'Processing...', 'imagify' );
480 }
481
482 if ( ! $with_container ) {
483 return $views->get_template( 'button/processing', [ 'label' => $lock_label ] );
484 }
485
486 return $views->get_template( 'container/data-actions', [
487 'media_id' => $media_id,
488 'context' => $context,
489 'content' => $views->get_template( 'button/processing', [ 'label' => $lock_label ] ),
490 ] );
491 }
492
493 // Check if the image was optimized.
494 if ( ! $data->get_optimization_status() ) {
495 $output = Imagify_Views::get_instance()->get_template( 'button/optimize', [
496 'url' => get_imagify_admin_url( 'manual-optimize', [
497 'attachment_id' => $media_id,
498 'context' => $context,
499 ] ),
500 ] );
501
502 if ( $media->has_backup() ) {
503 $output .= '<span class="attachment-has-backup hidden"></span>';
504 }
505 } else {
506 $output = get_imagify_attachment_optimization_text( $process );
507 }
508
509 if ( ! $with_container ) {
510 return $output;
511 }
512
513 return $views->get_template( 'container/data-actions', [
514 'media_id' => $media_id,
515 'context' => $context,
516 'content' => $output,
517 ] );
518 }
519