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

197 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 /*
5 * Get the optimization data list for a specific attachment.
6 *
7 * @since 1.0
8 *
9 * @param int $attachment_id The attachment ID.
10 * @return string The output to print.
11 */
12 function get_imagify_attachment_optimization_text( $attachment_id ) {
13 global $pagenow;
14
15 $attachment = new Imagify_Attachment( $attachment_id );
16 $data = $attachment->get_data();
17 $output = ( 'post.php' != $pagenow ) ? '<ul class="imagify-datas-list">' : '';
18 $output_before = ( 'post.php' != $pagenow ) ? '<li class="imagify-data-item">' : '<div class="misc-pub-section misc-pub-imagify imagify-data-item">';
19 $output_after = ( 'post.php' != $pagenow ) ? '</li>' : '</div>';
20 $reoptimize_output = '';
21
22 if ( $error = get_imagify_attachment_error_text( $attachment_id ) ) {
23 $error = ( 'post.php' === $pagenow ) ? $output_before . $error . $output_after : $error;
24 return $error;
25 }
26
27 $optimization_level = $attachment->get_optimization_level_label();
28
29 if ( imagify_valid_key() && $attachment->has_backup() ) {
30 $reoptimize_link = get_imagify_attachment_reoptimize_link( $attachment_id );
31 $reoptimize_output = ( $reoptimize_link ) ? $reoptimize_link : '';
32 }
33
34 if ( 'post.php' !== $pagenow ) {
35 $output .= $output_before . '<span class="data">' . __( 'New Filesize:', 'imagify' ) . '</span> <strong class="big">' . size_format( $data['sizes']['full']['optimized_size'], 2 ) . '</strong>' . $output_after;
36 }
37
38 $chart = '<span class="imagify-chart">
39 <span class="imagify-chart-container">
40 <canvas id="imagify-consumption-chart" width="15" height="15"></canvas>
41 </span>
42 </span>';
43
44 $output .= $output_before . '<span class="data">' . __( 'Original Saving:', 'imagify' ) . '</span> <strong>
45 ' . ( ( 'post.php' != $pagenow ) ? $chart : '' ) . '<span class="imagify-chart-value">' . $data['sizes']['full']['percent'] . '</span>%</strong>' . $output_after;
46
47 // more details section
48 if ( 'post.php' != $pagenow ) {
49 // new list
50 $output .= '</ul>';
51 $output .= '<p class="imagify-datas-more-action"><a href="#imagify-view-details-' . $attachment_id . '" data-close="' . __( 'Close details', 'imagify' ) . '" data-open="' . __( 'View details', 'imagify' ) . '"><span class="the-text">' . __( 'View details', 'imagify' ) . '</span><span class="dashicons dashicons-arrow-down-alt2"></span></a></p>';
52 $output .= '<ul id="imagify-view-details-' . $attachment_id . '" class="imagify-datas-list imagify-datas-details">';
53
54 // not in metabox
55 $output .= $output_before . '<span class="data">' . __( 'Original Filesize:', 'imagify' ) . '</span> <strong class="original">' . $attachment->get_original_size() . '</strong>' . $output_after;
56 }
57
58 $output .= $output_before . '<span class="data">' . __( 'Level:', 'imagify' ) . '</span> <strong>' . $optimization_level . '</strong>' . $output_after;
59
60 if ( $total_optimized_thumbnails = $attachment->get_optimized_sizes_count() ) {
61 $output .= $output_before . '<span class="data">' . __( 'Thumbnails Optimized:', 'imagify' ) . '</span> <strong>' . $total_optimized_thumbnails . '</strong>' . $output_after;
62 $output .= $output_before . '<span class="data">' . __( 'Overall Saving:', 'imagify' ) . '</span> <strong>' . $data['stats']['percent'] . '%</strong>' . $output_after;
63 }
64
65 // end of list
66 $output .= ( 'post.php' != $pagenow ) ? '</ul>' : '';
67
68 // actions section
69 $output .= ( 'post.php' != $pagenow ) ? '' : $output_before;
70 $output .= '<div class="imagify-datas-actions-links">';
71 $output .= $reoptimize_output;
72
73 if ( $attachment->has_backup() ) {
74 $class = ( 'post.php' !== $pagenow ) ? 'button-imagify-restore' : '';
75 $output .= '<a id="imagify-restore-' . $attachment_id . '" href="' . get_imagify_admin_url( 'restore-upload', $attachment_id ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Restoring...', 'imagify' ) . '"><span class="dashicons dashicons-image-rotate"></span>' . __( 'Restore Original', 'imagify' ) . '</a>';
76
77 if ( 'upload.php' != $pagenow ) {
78 $image = wp_get_attachment_image_src( $attachment_id, 'full' );
79
80 $output .= '<input id="imagify-original-src" type="hidden" value="' . $attachment->get_backup_url() . '">';
81 $output .= '<input id="imagify-original-size" type="hidden" value="' . $attachment->get_original_size() . '">';
82 $output .= '<input id="imagify-full-src" type="hidden" value="' . $image[0] . '">';
83 $output .= '<input id="imagify-full-width" type="hidden" value="' . $image[1] . '">';
84 $output .= '<input id="imagify-full-height" type="hidden" value="' . $image[2] . '">';
85 }
86 }
87
88 $output .= '</div><!-- .imagify-datas-actions-links -->';
89 $output .= ( 'post.php' != $pagenow ) ? '' : $output_after;
90
91 return $output;
92 }
93
94 /*
95 * Get the error message for a specific attachment.
96 *
97 * @since 1.0
98 *
99 * @param int $attachment_id The attachement ID.
100 * @return string The output to print.
101 */
102 function get_imagify_attachment_error_text( $attachment_id ) {
103 global $pagenow;
104 $data = get_post_meta( $attachment_id, '_imagify_data', true );
105 $output = '';
106
107 if ( isset( $data['sizes']['full']['success'] ) && ! $data['sizes']['full']['success'] ) {
108 $class = ( 'post.php' !== $pagenow ) ? 'button-imagify-manual-upload' : '';
109 $output .= '<strong>' . $data['sizes']['full']['error'] . '</strong><br/><a id="imagify-upload-' . $attachment_id . '" class="button ' . $class . '" href="' . get_imagify_admin_url( 'manual-upload', $attachment_id ) . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Try again', 'imagify' ) . '</a>';
110 }
111
112 return $output;
113 }
114
115 /*
116 * Get the re-optimize link for a specific attachment.
117 *
118 * @since 1.0
119 *
120 * @param int $attachment_id The attachement ID.
121 * @return string The output to print.
122 */
123 function get_imagify_attachment_reoptimize_link( $attachment_id ) {
124 global $pagenow;
125
126 $attachment = new Imagify_Attachment( $attachment_id );
127 $level = (int) $attachment->get_optimization_level();
128 $args = array( 'attachment_id' => $attachment_id );
129 $output = '';
130
131 if ( $attachment->has_backup() ) {
132 $class = ( 'post.php' !== $pagenow ) ? 'button-imagify-manual-override-upload' : '';
133
134 // Re-optimize to Ultra
135 if ( 1 === $level || 0 === $level ) {
136 $args['optimization_level'] = 2;
137 $output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Ultra', 'imagify' ) ) . '</a>';
138 }
139
140 // Re-optimize to Aggressive
141 if ( 2 === $level || 0 === $level ) {
142 $args['optimization_level'] = 1;
143 $output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Aggressive', 'imagify' ) ) . '</a>';
144 }
145
146 // Re-optimize to Normal
147 if ( 2 === $level || 1 === $level ) {
148 $args['optimization_level'] = 0;
149 $output .= '<a href="' . get_imagify_admin_url( 'manual-override-upload', $args ) . '" class="' . $class . '" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '"><span class="dashicons dashicons-admin-generic"></span>' . sprintf( __( 'Re-Optimize to %s', 'imagify' ), __( 'Normal', 'imagify' ) ) . '</a>';
150 }
151
152 }
153
154 return $output;
155 }
156
157 /*
158 * Get all data to diplay for a specific attachment.
159 *
160 * @since 1.2
161 *
162 * @param int $attachment_id The attachement ID.
163 * @return string The output to print.
164 */
165 function get_imagify_media_column_content( $attachment_id ) {
166 $output = '';
167 $attachment = new Imagify_Attachment( $attachment_id );
168 $attachment_ext = $attachment->get_extension();
169
170 // Check if the attachment extension is allowed
171 if ( ! wp_attachment_is_image( $attachment_id ) ) {
172 $output = sprintf( __( '%s can\'t be optimized', 'imagify' ), strtoupper( $attachment_ext ) );
173 return $output;
174 }
175
176 // Check if the API key is valid
177 if ( ! imagify_valid_key() && ! $attachment->is_optimized() ) {
178 $output .= __( 'Invalid API key', 'imagify' );
179 $output .= '<br/>';
180 $output .= '<a href="' . get_imagify_admin_url( 'options-general' ) . '">' . __( 'Check your Settings', 'imagify' ) . '</a>';
181 return $output;
182 }
183
184 if ( false !== get_transient( 'imagify-async-in-progress-' . $attachment_id ) ) {
185 $output = '<div class="button"><span class="imagify-spinner"></span>' . __( 'Optimizing...', 'imagify' ) . '</div>';
186 return $output;
187 }
188
189 // Check if the image was optimized
190 if ( ! $attachment->is_optimized() && ! $attachment->has_error() ) {
191 $output .= '<a id="imagify-upload-' . $attachment_id . '" href="' . get_imagify_admin_url( 'manual-upload', $attachment_id ) . '" class="button-primary button-imagify-manual-upload" data-waiting-label="' . esc_attr__( 'Optimizing...', 'imagify' ) . '">' . __( 'Optimize', 'imagify' ) . '</a>';
192 return $output;
193 }
194
195 $output .= get_imagify_attachment_optimization_text( $attachment_id );
196 return $output;
197 }