PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 3.3.0
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v3.3.0
4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / admin / class-wp-media-extends.php

class-wp-media-extends.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 3.3.0, at admin/class-wp-media-extends.php

263 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The class for wp_media extends
4 *
5 * @link http://webdeclic.com
6 * @since 1.0.0
7 *
8 * @package Quickwebp
9 * @subpackage Quickwebp/admin
10 */
11 class Quickwebp_Wp_Media_Extends {
12
13 /**
14 * The ID of this plugin.
15 *
16 * @since 1.0.0
17 * @access private
18 * @var string $plugin_name The ID of this plugin.
19 */
20 private $plugin_name;
21
22 /**
23 * The version of this plugin.
24 *
25 * @since 1.0.0
26 * @access private
27 * @var string $version The current version of this plugin.
28 */
29 private $version;
30
31 /**
32 * Initialize the class and set its properties.
33 *
34 * @since 1.0.0
35 * @param string $plugin_name The name of this plugin.
36 * @param string $version The version of this plugin.
37 */
38 public function __construct( $plugin_name, $version ) {
39
40 $this->plugin_name = $plugin_name;
41 $this->version = $version;
42
43 }
44
45 public function enqueue_scripts(){
46 global $pagenow;
47
48 if ( get_option('quickwebp_settings_paste_image', quickwebp_settings_default('quickwebp_settings_paste_image')) == '1' ){
49
50 $wp_media_extends_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/wp-media-extends.asset.php' );
51 wp_enqueue_style( 'quickwebp-wp-media-extends', QUICKWEBP_PLUGIN_URL . 'public/assets/build/wp-media-extends.css', array(), $wp_media_extends_assets['version'], 'all' );
52 wp_enqueue_script( 'quickwebp-wp-media-extends', QUICKWEBP_PLUGIN_URL . 'public/assets/build/wp-media-extends.js', $wp_media_extends_assets['dependencies'], $wp_media_extends_assets['version'], true );
53 wp_localize_script( 'quickwebp-wp-media-extends', 'quickwebp_wp_media_extends', array(
54 'is_upload_page' => $pagenow ? $pagenow === 'upload.php' : false,
55 'nonce' => wp_create_nonce( 'media-form' ),
56 'tmpl_tab_media_paste' => file_get_contents( QUICKWEBP_PLUGIN_PATH . 'admin/templates/tab-media-paste.php' ),
57 'tmpl_button_media_paste' => file_get_contents( QUICKWEBP_PLUGIN_PATH . 'admin/templates/button-media-paste.php' ),
58 'l10n' => array(
59 'prompt_text' => __( 'Please enter the name of your image', QUICKWEBP_TEXT_DOMAIN ),
60 'button_text' => __( 'Quickwebp paste image', QUICKWEBP_TEXT_DOMAIN ),
61 'click_paste' => __( 'Click here, and paste your image.', QUICKWEBP_TEXT_DOMAIN )
62 )
63 ));
64 }
65
66 if ( ! current_user_can( 'upload_files' ) ) {
67 return;
68 }
69
70 $admin_attachment_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/admin-attachment.asset.php' );
71 wp_enqueue_style( 'quickwebp-admin-attachment', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-attachment.css', array(), $admin_attachment_assets['version'], 'all' );
72 wp_enqueue_script( 'quickwebp-admin-attachment', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-attachment.js', $admin_attachment_assets['dependencies'], $admin_attachment_assets['version'], true );
73 wp_localize_script( 'quickwebp-admin-attachment', 'QUICKWEBP_ADMIN_ATTACHMENT', array(
74 'nonce' => wp_create_nonce( 'quickwebp_admin_attachment' ),
75 'ajaxUrl' => admin_url( 'admin-ajax.php' )
76 ));
77 }
78
79 /**
80 * Add "Quickwebp" column in the Media Uploader
81 */
82 public function add_attachment_fields_to_edit( $form_fields, $post ) {
83 global $pagenow;
84
85 if ( 'post.php' === $pagenow ) {
86 return $form_fields;
87 }
88
89 if ( ! $this->valid_mimetype( $post->ID ) ) {
90 return $form_fields;
91 }
92
93 $data = get_post_meta( $post->ID, 'quickwebp_data', true );
94 $has_error = get_post_meta( $post->ID, 'quickwebp_has_error', true );
95
96 if ( ! is_array( $data ) ) {
97
98 $html = $this->optimize_btn( $post->ID );
99
100 if ( ! empty( $has_error ) ) {
101 $html .= '<br>' . esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN );
102 }
103
104 } else {
105
106 $html = $this->attachment_data( $data, $post->ID );
107 }
108
109 $form_fields['quickwebp'] = array(
110 'label' => 'Quickwebp',
111 'input' => 'html',
112 'html' => $html,
113 'show_in_edit' => true,
114 'show_in_modal' => true
115 );
116
117 return $form_fields;
118 }
119
120 /**
121 * Add "quickwebp" column in upload.php
122 */
123 public function add_media_columns( $columns ) {
124
125 $columns['quickwebp'] = __( 'Quickwebp', QUICKWEBP_TEXT_DOMAIN );
126
127 return $columns;
128 }
129
130 /**
131 * Add "Quickwebp" column in the Media list table
132 */
133 public function add_media_custom_column( $column_name, $attachment_id ) {
134
135 if ( 'quickwebp' !== $column_name ) {
136 return;
137 }
138
139 if ( ! $this->valid_mimetype( $attachment_id ) ) {
140 echo esc_html__( 'Image type not supported', QUICKWEBP_TEXT_DOMAIN );
141 return;
142 }
143
144 $data = get_post_meta( $attachment_id, 'quickwebp_data', true );
145 $has_error = get_post_meta( $attachment_id, 'quickwebp_has_error', true );
146
147 if ( ! is_array( $data ) ) {
148
149 echo $this->optimize_btn( $attachment_id );
150
151 if ( ! empty( $has_error ) ) {
152 echo esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN );
153 }
154
155 } else {
156
157 echo $this->attachment_data( $data, $attachment_id );
158 }
159 }
160
161 /**
162 * Add a "Optimize" button or the Imagify optimization data in the attachment submit area.
163 */
164 public function add_attachment_submitbox_misc_actions() {
165 global $post;
166
167 if ( ! $this->valid_mimetype( $post->ID ) ) {
168 return;
169 }
170
171 $data = get_post_meta( $post->ID, 'quickwebp_data', true );
172 $has_error = get_post_meta( $post->ID, 'quickwebp_has_error', true );
173
174 if ( ! is_array( $data ) ) {
175
176 echo '<table><tr><td>';
177 echo $this->optimize_btn( $post->ID );
178
179 if ( ! empty( $has_error ) ) {
180 echo esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN );
181 }
182
183 echo '</td></tr></table>';
184
185 } else {
186
187 echo '<table><tr><td>' . $this->attachment_data( $data, $post->ID ) . '</td></tr></table>';
188 }
189
190 }
191
192 /**
193 * Get all data to display for a specific media
194 */
195 public function attachment_data( $data, $attachment_id ) {
196
197 $full_image_data = $data['full'] ?? array();
198
199 if ( ! empty( $full_image_data ) ) {
200
201 ob_start();
202
203 ?>
204 <div>
205 <strong><?php esc_html_e( 'Original Image: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong>
206 <span><?php echo esc_html( round( $full_image_data['original_size'] / 1024, 2 ) . 'KB' ); ?></span>
207 </div>
208 <div>
209 <strong><?php esc_html_e( 'Webp: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong>
210 <span><?php echo esc_html( round( $full_image_data['optimized_size'] / 1024, 2 ) . 'KB' ); ?></span>
211 </div>
212 <div>
213 <strong><?php esc_html_e( 'Save: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong>
214 <span><?php echo esc_html( $full_image_data['percent'] . '%' ); ?></span>
215 </div>
216 <div>
217 <button class="button button-sacondary quickwebp-undo-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>">
218 <?php esc_html_e( 'Undo optimization', QUICKWEBP_TEXT_DOMAIN ); ?>
219 <div class="spinner"></div>
220 </button>
221 <div class="quickwebp-undo-single-optimization-msg"></div>
222 </div>
223 <?php
224
225 return ob_get_clean();
226 }
227
228 }
229
230 /**
231 * Display optimize button in the media uploader
232 */
233 public function optimize_btn( $attachment_id ) {
234
235 ob_start();
236
237 ?>
238 <button class="button button-sacondary quickwebp-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>">
239 <?php esc_html_e( 'Optimize', QUICKWEBP_TEXT_DOMAIN ); ?>
240 <div class="spinner"></div>
241 </button>
242 <div class="quickwebp-single-optimization-msg"></div>
243 <?php
244
245 return ob_get_clean();
246 }
247
248 /**
249 * Check the mimetype of the file
250 */
251 public function valid_mimetype( $post_id ) {
252
253 // check the mime type
254 $image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version );
255 $mime_types = $image_optimizer->allowed_mime_types;
256 $index = array_search( 'image/webp', $mime_types );
257 unset( $mime_types[$index] );
258 $mime_type = get_post_mime_type( $post_id );
259
260 return in_array( $mime_type, $mime_types );
261 }
262
263 }