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

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

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