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

421 lines 17.2 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 attachment.
6 *
7 * @since 1.0
8 * @author Jonathan Buttigieg
9 *
10 * @param object $attachment The attachment object.
11 * @param string $context A context.
12 * @return string The output to print.
13 */
14 function get_imagify_attachment_optimization_text( $attachment, $context = 'wp' ) {
15 global $pagenow, $typenow;
16
17 $is_media_page = 'post.php' === $pagenow && 'attachment' === $typenow;
18 $is_library_page = 'upload.php' === $pagenow;
19 $output = $is_media_page ? '' : '<ul class="imagify-datas-list">';
20 $output_before = $is_media_page ? '<div class="misc-pub-section misc-pub-imagify imagify-data-item">' : '<li class="imagify-data-item">';
21 $output_after = $is_media_page ? '</div>' : '</li>';
22 $reoptimize_link = get_imagify_attachment_reoptimize_link( $attachment, $context );
23 $reoptimize_link .= get_imagify_attachment_optimize_missing_thumbnails_link( $attachment, $context );
24 $reoptimize_output = $reoptimize_link ? $reoptimize_link : '';
25 $reoptimize_output_before = '<div class="imagify-datas-actions-links">';
26 $reoptimize_output_after = '</div><!-- .imagify-datas-actions-links -->';
27 $error = get_imagify_attachment_error_text( $attachment, $context );
28
29 if ( $error ) {
30 if ( ! $is_media_page && $reoptimize_link && $attachment->has_backup() ) {
31 $reoptimize_output .= '<span class="attachment-has-backup hidden"></span>';
32 }
33
34 $reoptimize_output = $reoptimize_output_before . $reoptimize_output . $reoptimize_output_after;
35
36 return $is_media_page ? $output_before . $error . $reoptimize_output . $output_after : $error . $reoptimize_output;
37 }
38
39 $attachment_id = $attachment->id;
40 $optimization_level = $attachment->get_optimization_level_label();
41
42 if ( ! $is_media_page ) {
43 $output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . $attachment->get_optimized_size() . '</strong>' . $output_after;
44 }
45
46 $chart = '';
47
48 if ( ! $is_media_page ) {
49 if ( ! $is_library_page ) {
50 // 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).
51 $chart = '<script type="text/javascript">jQuery( window ).trigger( "canvasprinted.imagify", [ ".imagify-consumption-chart-' . $attachment_id . '" ] ); </script>';
52 }
53
54 $chart = '<span class="imagify-chart">
55 <span class="imagify-chart-container">
56 <canvas class="imagify-consumption-chart imagify-consumption-chart-' . $attachment_id . '" width="15" height="15"></canvas>
57 ' . $chart . '
58 </span>
59 </span>';
60 }
61
62 $output .= $output_before;
63 $output .= '<span class="data">' . __( 'Original Saving:', 'imagify' ) . '</span> ';
64 $output .= '<strong>' . $chart . '<span class="imagify-chart-value">' . $attachment->get_saving_percent() . '</span>%</strong>';
65 $output .= $output_after;
66
67 // More details section.
68 if ( ! $is_media_page ) {
69 // New list.
70 $output .= '</ul>';
71 $output .= '<p class="imagify-datas-more-action">';
72 $output .= '<a href="#imagify-view-details-' . $attachment_id . '" data-close="' . __( 'Close details', 'imagify' ) . '" data-open="' . __( 'View details', 'imagify' ) . '">';
73 $output .= '<span class="the-text">' . __( 'View details', 'imagify' ) . '</span>';
74 $output .= '<span class="dashicons dashicons-arrow-down-alt2"></span>';
75 $output .= '</a>';
76 $output .= '</p>';
77 $output .= '<ul id="imagify-view-details-' . $attachment_id . '" class="imagify-datas-list imagify-datas-details">';
78
79 // Not in metabox.
80 $output .= $output_before . '<span class="data">' . __( 'Original Filesize:', 'imagify' ) . '</span> <strong class="original">' . $attachment->get_original_size() . '</strong>' . $output_after;
81 }
82
83 $output .= $output_before . '<span class="data">' . __( 'Level:', 'imagify' ) . '</span> <strong>' . $optimization_level . '</strong>' . $output_after;
84
85 $total_optimized_thumbnails = $attachment->get_optimized_sizes_count();
86
87 if ( $total_optimized_thumbnails ) {
88 $output .= $output_before . '<span class="data">' . __( 'Thumbnails Optimized:', 'imagify' ) . '</span> <strong>' . $total_optimized_thumbnails . '</strong>' . $output_after;
89 $output .= $output_before . '<span class="data">' . __( 'Overall Saving:', 'imagify' ) . '</span> <strong>' . $attachment->get_overall_saving_percent() . '%</strong>' . $output_after;
90 }
91
92 // End of list.
93 $output .= $is_media_page ? '' : '</ul>';
94
95 // Actions section.
96 $output .= $is_media_page ? $output_before : '';
97 $output .= $reoptimize_output_before;
98 $output .= $reoptimize_output;
99
100 if ( $attachment->has_backup() ) {
101 $args = array(
102 'attachment_id' => $attachment_id,
103 'context' => $context,
104 );
105 $class = $is_media_page ? '' : ' class="button-imagify-restore attachment-has-backup"';
106 $waiting = $is_media_page ? '' : ' data-waiting-label="' . esc_attr__( 'Restoring...', 'imagify' ) . '"';
107 $output .= '<a id="imagify-restore-' . $attachment_id . '" href="' . esc_url( get_imagify_admin_url( 'restore-upload', $args ) ) . '"' . $class . $waiting . '>';
108 $output .= '<span class="dashicons dashicons-image-rotate"></span>' . __( 'Restore Original', 'imagify' );
109 $output .= '</a>';
110
111 if ( ! $is_library_page ) {
112 $dimensions = $attachment->get_dimensions();
113
114 $output .= '<input id="imagify-original-src" type="hidden" value="' . esc_url( $attachment->get_backup_url() ) . '">';
115 $output .= '<input id="imagify-original-size" type="hidden" value="' . $attachment->get_original_size() . '">';
116 $output .= '<input id="imagify-full-src" type="hidden" value="' . esc_url( $attachment->get_original_url() ) . '">';
117 $output .= '<input id="imagify-full-width" type="hidden" value="' . $dimensions['width'] . '">';
118 $output .= '<input id="imagify-full-height" type="hidden" value="' . $dimensions['height'] . '">';
119 }
120 }
121
122 $output .= $reoptimize_output_after;
123 $output .= $is_media_page ? $output_after : '';
124
125 return $output;
126 }
127
128 /**
129 * Get the error message for a specific attachment.
130 *
131 * @since 1.0
132 * @author Jonathan Buttigieg
133 *
134 * @param object $attachment The attachement object.
135 * @param string $context A context.
136 * @return string The output to print.
137 */
138 function get_imagify_attachment_error_text( $attachment, $context = 'wp' ) {
139 global $pagenow, $typenow;
140
141 $is_media_page = 'post.php' === $pagenow && 'attachment' === $typenow;
142 $attachment_id = $attachment->id;
143 $data = $attachment->get_data();
144 $output = '';
145 $args = array(
146 'attachment_id' => $attachment_id,
147 'context' => $context,
148 );
149
150 if ( isset( $data['sizes']['full']['success'] ) && ! $data['sizes']['full']['success'] ) {
151 $class = $is_media_page ? '' : ' button-imagify-manual-upload';
152 $waiting = $is_media_page ? '' : ' data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"';
153 $output .= '<strong>' . imagify_translate_api_message( $data['sizes']['full']['error'] ) . '</strong><br/>';
154 $output .= '<a id="imagify-upload-' . $attachment_id . '" class="button' . $class . '" href="' . esc_url( get_imagify_admin_url( 'manual-upload', $args ) ) . '"' . $waiting . '>' . __( 'Try again', 'imagify' ) . '</a>';
155 }
156
157 return $output;
158 }
159
160 /**
161 * Get the re-optimize link for a specific attachment.
162 *
163 * @since 1.0
164 * @author Jonathan Buttigieg
165 *
166 * @param object $attachment The attachement object.
167 * @param string $context A context.
168 * @return string The output to print.
169 */
170 function get_imagify_attachment_reoptimize_link( $attachment, $context = 'wp' ) {
171 global $pagenow, $typenow;
172
173 // Stop the process if the API key isn't valid.
174 if ( ! imagify_valid_key() ) {
175 return '';
176 }
177
178 $is_already_optimized = $attachment->is_already_optimized();
179
180 // Don't display anything if there is no backup or the image has been optimized.
181 if ( ! $attachment->has_backup() && ! $is_already_optimized ) {
182 return '';
183 }
184
185 $is_media_page = 'post.php' === $pagenow && 'attachment' === $typenow;
186 $attachment_id = $attachment->id;
187 $level = $attachment->get_optimization_level();
188 $args = array(
189 'attachment_id' => $attachment_id,
190 'context' => $context,
191 );
192 $output = '';
193 $class = $is_media_page ? '' : ' class="button-imagify-manual-override-upload"';
194 $waiting = $is_media_page ? '' : ' data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"';
195
196 // Re-optimize to Ultra.
197 if ( 1 === $level || 0 === $level ) {
198 $args['optimization_level'] = 2;
199 $output .= '<a href="' . esc_url( get_imagify_admin_url( 'manual-override-upload', $args ) ) . '"' . $class . $waiting . '>';
200 /* translators: %s is an optimization level. */
201 $output .= '<span class="dashicons dashicons-admin-generic"></span><span class="imagify-hide-if-small">' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), '</span>' . __( 'Ultra', 'imagify' ) . '<span class="imagify-hide-if-small">' ) . '</span>';
202 $output .= '</a>';
203 }
204
205 // Re-optimize to Aggressive.
206 if ( ( 2 === $level && ! $is_already_optimized ) || 0 === $level ) {
207 $args['optimization_level'] = 1;
208 $output .= '<a href="' . esc_url( get_imagify_admin_url( 'manual-override-upload', $args ) ) . '"' . $class . $waiting . '>';
209 /* translators: %s is an optimization level. */
210 $output .= '<span class="dashicons dashicons-admin-generic"></span><span class="imagify-hide-if-small">' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), '</span>' . __( 'Aggressive', 'imagify' ) . '<span class="imagify-hide-if-small">' ) . '</span>';
211 $output .= '</a>';
212 }
213
214 // Re-optimize to Normal.
215 if ( ( 2 === $level || 1 === $level ) && ! $is_already_optimized ) {
216 $args['optimization_level'] = 0;
217 $output .= '<a href="' . esc_url( get_imagify_admin_url( 'manual-override-upload', $args ) ) . '"' . $class . $waiting . '>';
218 /* translators: %s is an optimization level. */
219 $output .= '<span class="dashicons dashicons-admin-generic"></span><span class="imagify-hide-if-small">' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), '</span>' . __( 'Normal', 'imagify' ) . '<span class="imagify-hide-if-small">' ) . '</span>';
220 $output .= '</a>';
221 }
222
223 return $output;
224 }
225
226 /**
227 * Get the link to optimize missing thumbnail sizes for a specific attachment.
228 *
229 * @since 1.6.10
230 * @author Grégory Viguier
231 *
232 * @param object $attachment The attachement object.
233 * @param string $context A context.
234 * @return string The output to print.
235 */
236 function get_imagify_attachment_optimize_missing_thumbnails_link( $attachment, $context = 'wp' ) {
237 /**
238 * Allow to not display the "Optimize missing thumbnails" link.
239 *
240 * @since 1.6.10
241 * @author Grégory Viguier
242 *
243 * @param bool $display True to display the link. False to not display it.
244 * @param object $attachment The attachement object.
245 * @param string $context The context.
246 */
247 $display = apply_filters( 'imagify_display_missing_thumbnails_link', true, $attachment, $context );
248
249 // Stop the process if the filter is false, or if the API key isn't valid, or if there is no backup file.
250 if ( ! $display || ! imagify_valid_key() || ! $attachment->has_backup() ) {
251 return '';
252 }
253
254 $missing_sizes = $attachment->get_unoptimized_sizes();
255
256 if ( ! $missing_sizes ) {
257 return '';
258 }
259
260 $url = get_imagify_admin_url( 'optimize-missing-sizes', array(
261 'attachment_id' => $attachment->id,
262 'context' => $context,
263 ) );
264
265 $output = '<a href="' . esc_url( $url ) . '" id="imagify-optimize_missing_sizes-' . $attachment->id . '" class="button-imagify-optimize-missing-sizes" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">';
266 /* translators: 1 is the number of thumbnails to optimize, 2 is the opening of a HTML tag that will be hidden on small screens, 3 is the closing tag. */
267 $output .= '<span class="dashicons dashicons-admin-generic"></span>' . sprintf( _n( '%2$sOptimize %3$s%1$d missing thumbnail', '%2$sOptimize %3$s%1$d missing thumbnails', count( $missing_sizes ), 'imagify' ), count( $missing_sizes ), '<span class="imagify-hide-if-small">', '</span>' );
268 $output .= '</a>';
269
270 return $output;
271 }
272
273 /**
274 * Get all data to diplay for a specific attachment.
275 *
276 * @since 1.2
277 * @author Jonathan Buttigieg
278 *
279 * @param object $attachment The attachement object.
280 * @param string $context A context.
281 * @return string The output to print.
282 */
283 function get_imagify_media_column_content( $attachment, $context = 'wp' ) {
284 if ( ! imagify_current_user_can( 'manual-optimize', $attachment->id ) ) {
285 return __( 'You are not allowed to optimize this file.', 'imagify' );
286 }
287
288 // Check if the attachment extension is allowed.
289 if ( ! $attachment->is_extension_supported() ) {
290 $extension = $attachment->get_extension();
291
292 if ( '' === $extension ) {
293 return __( 'With no extension, this file cannot be optimized', 'imagify' );
294 }
295
296 /* translators: %s is a file extension. */
297 return sprintf( __( '%s cannot be optimized', 'imagify' ), strtoupper( $extension ) );
298 }
299
300 // Check if the attachment has the required WP metadata.
301 if ( ! $attachment->has_required_metadata() ) {
302 return __( 'This media lacks the required metadata and can\'t be optimized.', 'imagify' );
303 }
304
305 // Check if the API key is valid.
306 if ( ! imagify_valid_key() && ! $attachment->is_optimized() ) {
307 $output = __( 'Invalid API key', 'imagify' );
308 $output .= '<br/>';
309 $output .= '<a href="' . esc_url( get_imagify_admin_url() ) . '">' . __( 'Check your Settings', 'imagify' ) . '</a>';
310 return $output;
311 }
312
313 $transient_context = ( 'wp' !== $context ) ? strtolower( $context ) . '-' : '';
314 $transient_name = 'imagify-' . $transient_context . 'async-in-progress-' . $attachment->id;
315
316 if ( false !== get_transient( $transient_name ) ) {
317 return '<div class="button"><span class="imagify-spinner"></span>' . __( 'Optimizing...', 'imagify' ) . '</div>';
318 }
319
320 // Check if the image was optimized.
321 if ( ! $attachment->get_status() ) {
322 $args = array(
323 'attachment_id' => $attachment->id,
324 'context' => $context,
325 );
326 $output = '<a id="imagify-upload-' . $attachment->id . '" href="' . esc_url( get_imagify_admin_url( 'manual-upload', $args ) ) . '" class="button-primary button-imagify-manual-upload" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Optimize', 'imagify' ) . '</a>';
327
328 if ( $attachment->has_backup() ) {
329 $output .= '<span class="attachment-has-backup hidden"></span>';
330 }
331
332 return $output;
333 }
334
335 return get_imagify_attachment_optimization_text( $attachment, $context );
336 }
337
338 /**
339 * Get stats data for a specific folder type.
340 *
341 * @since 1.7
342 * @see Imagify_Admin_Ajax_Post::imagify_get_folder_type_data_callback()
343 * @author Grégory Viguier
344 *
345 * @param string $folder_type A folder type.
346 * @return array
347 */
348 function imagify_get_folder_type_data( $folder_type ) {
349 /**
350 * Get the data.
351 */
352 switch ( $folder_type ) {
353 case 'library':
354 $total_saving_data = imagify_count_saving_data();
355 $data = array(
356 'images-optimized' => imagify_count_optimized_attachments(),
357 'errors' => imagify_count_error_attachments(),
358 'optimized' => $total_saving_data['optimized_size'],
359 'original' => $total_saving_data['original_size'],
360 'errors_url' => get_imagify_admin_url( 'folder-errors', $folder_type ),
361 );
362 break;
363
364 case 'custom-folders':
365 $data = array(
366 'images-optimized' => Imagify_Files_Stats::count_optimized_files(),
367 'errors' => Imagify_Files_Stats::count_error_files(),
368 'optimized' => Imagify_Files_Stats::get_optimized_size(),
369 'original' => Imagify_Files_Stats::get_original_size(),
370 'errors_url' => get_imagify_admin_url( 'folder-errors', $folder_type ),
371 );
372 break;
373
374 default:
375 /**
376 * Provide custom folder type data.
377 *
378 * @since 1.7
379 * @author Grégory Viguier
380 *
381 * @param array $data An array with keys corresponding to cell classes, and values formatted with HTML.
382 * @param string $folder_type A folder type.
383 */
384 $data = apply_filters( 'imagify_get_folder_type_data', array(), $folder_type );
385
386 if ( ! $data || ! is_array( $data ) ) {
387 return array();
388 }
389 }
390
391 /**
392 * Format the data.
393 */
394 /* translators: %s is a formatted number, dont use %d. */
395 $data['images-optimized'] = sprintf( _n( '%s Image Optimized', '%s Images Optimized', $data['images-optimized'], 'imagify' ), '<span>' . number_format_i18n( $data['images-optimized'] ) . '</span>' );
396
397 if ( $data['errors'] ) {
398 /* translators: %s is a formatted number, dont use %d. */
399 $data['errors'] = sprintf( _n( '%s Error', '%s Errors', $data['errors'], 'imagify' ), '<span>' . number_format_i18n( $data['errors'] ) . '</span>' );
400 $data['errors'] .= ' <a href="' . esc_url( $data['errors_url'] ) . '">' . __( 'View Errors', 'imagify' ) . '</a>';
401 } else {
402 $data['errors'] = '';
403 }
404
405 if ( $data['optimized'] ) {
406 $data['optimized'] = '<span class="imagify-cell-label">' . __( 'Optimized Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['optimized'], 2 );
407 } else {
408 $data['optimized'] = '';
409 }
410
411 if ( $data['original'] ) {
412 $data['original'] = '<span class="imagify-cell-label">' . __( 'Original Filesize', 'imagify' ) . '</span> ' . imagify_size_format( $data['original'], 2 );
413 } else {
414 $data['original'] = '';
415 }
416
417 unset( $data['errors_url'] );
418
419 return $data;
420 }
421