| @@ -38,24 +38,280 @@ | ||
| 38 | 38 | public function __construct( $plugin_name, $version ) { |
| 39 | 39 | |
| 40 | 40 | $this->plugin_name = $plugin_name; |
| 41 | 41 | $this->version = $version; |
| 42 | + } | |
| 42 | 43 | |
| 44 | + /** | |
| 45 | + * Enqueue scripts and styles for the media uploader | |
| 46 | + */ | |
| 47 | + public function enqueue_scripts( $hook_suffix ){ | |
| 48 | + global $post_type; | |
| 49 | + | |
| 50 | + if ( 'upload.php' === $hook_suffix || ( ! empty( $post_type ) && 'post.php' === $hook_suffix && 'attachment' === $post_type ) ) { | |
| 51 | + | |
| 52 | + if ( get_option('quickwebp_settings_paste_image', quickwebp_settings_default('quickwebp_settings_paste_image')) == '1' ){ | |
| 53 | + | |
| 54 | + $wp_media_extends_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/wp-media-extends.asset.php' ); | |
| 55 | + wp_enqueue_style( 'quickwebp-wp-media-extends', QUICKWEBP_PLUGIN_URL . 'public/assets/build/wp-media-extends.css', array(), $wp_media_extends_assets['version'], 'all' ); | |
| 56 | + 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 ); | |
| 57 | + wp_localize_script( 'quickwebp-wp-media-extends', 'quickwebp_wp_media_extends', array( | |
| 58 | + 'is_upload_page' => $hook_suffix === 'upload.php', | |
| 59 | + 'nonce' => wp_create_nonce( 'media-form' ), | |
| 60 | + 'tmpl_tab_media_paste' => file_get_contents( QUICKWEBP_PLUGIN_PATH . 'admin/templates/tab-media-paste.php' ), | |
| 61 | + 'tmpl_button_media_paste' => file_get_contents( QUICKWEBP_PLUGIN_PATH . 'admin/templates/button-media-paste.php' ), | |
| 62 | + 'l10n' => array( | |
| 63 | + 'prompt_text' => __( 'Please enter the name of your image', 'quickwebp' ), | |
| 64 | + 'button_text' => __( 'Quickwebp paste image', 'quickwebp' ), | |
| 65 | + 'click_paste' => __( 'Click here, and paste your image.', 'quickwebp' ) | |
| 66 | + ) | |
| 67 | + )); | |
| 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 | + } | |
| 43 | 78 | } |
| 44 | 79 | |
| 45 | - public function enqueue_scripts(){ | |
| 80 | + /** | |
| 81 | + * Add "Quickwebp" column in the Media Uploader | |
| 82 | + */ | |
| 83 | + public function add_attachment_fields_to_edit( $form_fields, $post ) { | |
| 46 | 84 | global $pagenow; |
| 47 | - if(get_option('quickwebp_settings_paste_image', quickwebp_settings_default('quickwebp_settings_paste_image')) != '1'){ | |
| 85 | + | |
| 86 | + if ( 'post.php' === $pagenow ) { | |
| 87 | + return $form_fields; | |
| 88 | + } | |
| 89 | + | |
| 90 | + if ( ! $this->valid_mimetype( $post->ID ) ) { | |
| 91 | + return $form_fields; | |
| 92 | + } | |
| 93 | + | |
| 94 | + $image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version ); | |
| 95 | + $settings = $image_optimizer->get_settings(); | |
| 96 | + | |
| 97 | + $html = ''; | |
| 98 | + $status = '0'; | |
| 99 | + $already_optimized = get_post_meta( $post->ID, 'quickwebp_already_optimized', true ); | |
| 100 | + $data = get_post_meta( $post->ID, 'quickwebp_data', true ); | |
| 101 | + | |
| 102 | + $mode_enabled = $settings['quickwebp_settings_conversion']; | |
| 103 | + | |
| 104 | + if ( '1' == $already_optimized && ( '1' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 105 | + if ( is_array( $data ) ) { | |
| 106 | + $status = '1'; | |
| 107 | + } else { | |
| 108 | + $status = '2'; | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + if ( '2' == $already_optimized && ( '2' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 113 | + if ( is_array( $data ) ) { | |
| 114 | + $status = '1'; | |
| 115 | + } else { | |
| 116 | + $status = '2'; | |
| 117 | + } | |
| 118 | + } | |
| 119 | + | |
| 120 | + switch ( $status ) { | |
| 121 | + case '0': | |
| 122 | + $html = $this->optimize_btn( $post->ID ); | |
| 123 | + break; | |
| 124 | + case '1': | |
| 125 | + $html = $this->attachment_data( $data, $post->ID ); | |
| 126 | + break; | |
| 127 | + case '2': | |
| 128 | + $html = '<br>' . esc_html__( 'Image already optimized.', 'quickwebp' ); | |
| 129 | + break; | |
| 130 | + } | |
| 131 | + | |
| 132 | + $form_fields['wpmtk_media_encoder'] = array( | |
| 133 | + 'label' => 'Quickwebp', | |
| 134 | + 'input' => 'html', | |
| 135 | + 'html' => $html, | |
| 136 | + 'show_in_edit' => true, | |
| 137 | + 'show_in_modal' => true | |
| 138 | + ); | |
| 139 | + | |
| 140 | + return $form_fields; | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Add "quickwebp" column in upload.php | |
| 145 | + */ | |
| 146 | + public function add_media_columns( $columns ) { | |
| 147 | + $columns['quickwebp'] = 'QuickWebP'; | |
| 148 | + return $columns; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Add "Quickwebp" column in the Media list table | |
| 153 | + */ | |
| 154 | + public function add_media_custom_column( $column_name, $attachment_id ) { | |
| 155 | + | |
| 156 | + if ( 'quickwebp' !== $column_name ) { | |
| 48 | 157 | return; |
| 49 | 158 | } |
| 50 | - | |
| 51 | - $wp_media_extends_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/wp-media-extends.asset.php' ); | |
| 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 | - 'prompt_text' => __( 'Please enter the name of your image', QUICKWEBP_TEXT_DOMAIN ), | |
| 57 | - ) ); | |
| 159 | + | |
| 160 | + if ( ! $this->valid_mimetype( $attachment_id ) ) { | |
| 161 | + echo esc_html__( 'Image type not supported', 'quickwebp' ); | |
| 162 | + return; | |
| 163 | + } | |
| 164 | + | |
| 165 | + $image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version ); | |
| 166 | + $settings = $image_optimizer->get_settings(); | |
| 167 | + | |
| 168 | + $html = ''; | |
| 169 | + $status = '0'; | |
| 170 | + $already_optimized = get_post_meta( $attachment_id, 'quickwebp_already_optimized', true ); | |
| 171 | + $data = get_post_meta( $attachment_id, 'quickwebp_data', true ); | |
| 172 | + | |
| 173 | + $mode_enabled = $settings['quickwebp_settings_conversion']; | |
| 174 | + | |
| 175 | + if ( '1' == $already_optimized && ( '1' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 176 | + if ( is_array( $data ) ) { | |
| 177 | + $status = '1'; | |
| 178 | + } else { | |
| 179 | + $status = '2'; | |
| 180 | + } | |
| 181 | + } | |
| 182 | + | |
| 183 | + if ( '2' == $already_optimized && ( '2' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 184 | + if ( is_array( $data ) ) { | |
| 185 | + $status = '1'; | |
| 186 | + } else { | |
| 187 | + $status = '2'; | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + switch ( $status ) { | |
| 192 | + case '0': | |
| 193 | + echo wp_kses_post( $this->optimize_btn( $attachment_id ) ); | |
| 194 | + break; | |
| 195 | + case '1': | |
| 196 | + echo wp_kses_post( $this->attachment_data( $data, $attachment_id ) ); | |
| 197 | + break; | |
| 198 | + case '2': | |
| 199 | + echo wp_kses_post( '<br>' . esc_html__( 'Image already optimized.', 'quickwebp' ) ); | |
| 200 | + break; | |
| 201 | + } | |
| 58 | 202 | } |
| 59 | - | |
| 60 | 203 | |
| 61 | -} | |
| 204 | + /** | |
| 205 | + * Add a "Optimize" button or the Imagify optimization data in the attachment submit area. | |
| 206 | + */ | |
| 207 | + public function add_attachment_submitbox_misc_actions() { | |
| 208 | + global $post; | |
| 209 | + | |
| 210 | + if ( ! $this->valid_mimetype( $post->ID ) ) { | |
| 211 | + return; | |
| 212 | + } | |
| 213 | + | |
| 214 | + $image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version ); | |
| 215 | + $settings = $image_optimizer->get_settings(); | |
| 216 | + | |
| 217 | + $html = ''; | |
| 218 | + $status = '0'; | |
| 219 | + $already_optimized = get_post_meta( $post->ID, 'quickwebp_already_optimized', true ); | |
| 220 | + $data = get_post_meta( $post->ID, 'quickwebp_data', true ); | |
| 221 | + | |
| 222 | + $mode_enabled = $settings['quickwebp_settings_conversion']; | |
| 223 | + | |
| 224 | + if ( '1' == $already_optimized && ( '1' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 225 | + if ( is_array( $data ) ) { | |
| 226 | + $status = '1'; | |
| 227 | + } else { | |
| 228 | + $status = '2'; | |
| 229 | + } | |
| 230 | + } | |
| 231 | + | |
| 232 | + if ( '2' == $already_optimized && ( '2' == $mode_enabled || '0' == $mode_enabled ) ) { | |
| 233 | + if ( is_array( $data ) ) { | |
| 234 | + $status = '1'; | |
| 235 | + } else { | |
| 236 | + $status = '2'; | |
| 237 | + } | |
| 238 | + } | |
| 239 | + | |
| 240 | + echo '<table><tr><td><div><strong>QuickWebP</strong></div>'; | |
| 241 | + switch ( $status ) { | |
| 242 | + case '0': | |
| 243 | + echo wp_kses_post( $this->optimize_btn( $post->ID ) ); | |
| 244 | + break; | |
| 245 | + case '1': | |
| 246 | + echo wp_kses_post( $this->attachment_data( $data, $post->ID ) ); | |
| 247 | + break; | |
| 248 | + case '2': | |
| 249 | + echo esc_html__( 'Image already optimized.', 'quickwebp' ); | |
| 250 | + break; | |
| 251 | + } | |
| 252 | + echo '</td></tr></table>'; | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Get all data to display for a specific media | |
| 257 | + */ | |
| 258 | + public function attachment_data( $data, $attachment_id ) { | |
| 259 | + $available_formats = array( '1' => 'WebP', '2' => 'AVIF' ); | |
| 260 | + $full_image_data = $data['full'] ?? array(); | |
| 261 | + $format = $available_formats[ $full_image_data['format'] ?? '' ] ?? ''; | |
| 262 | + | |
| 263 | + if ( ! empty( $full_image_data ) ) { | |
| 264 | + ob_start(); | |
| 265 | + ?> | |
| 266 | + <div> | |
| 267 | + <strong><?php esc_html_e( 'Original Image: ', 'quickwebp' ); ?></strong> | |
| 268 | + <span><?php echo esc_html( round( $full_image_data['original_size'] / 1024, 2 ) . 'KB' ); ?></span> | |
| 269 | + </div> | |
| 270 | + <div> | |
| 271 | + <strong><?php echo esc_html( $format ); ?>: </strong> | |
| 272 | + <span><?php echo esc_html( round( $full_image_data['optimized_size'] / 1024, 2 ) . 'KB' ); ?></span> | |
| 273 | + </div> | |
| 274 | + <div> | |
| 275 | + <strong><?php esc_html_e( 'Save: ', 'quickwebp' ); ?></strong> | |
| 276 | + <span><?php echo esc_html( $full_image_data['percent'] . '%' ); ?></span> | |
| 277 | + </div> | |
| 278 | + <div> | |
| 279 | + <button class="button button-sacondary quickwebp-undo-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>"> | |
| 280 | + <?php esc_html_e( 'Undo optimization', 'quickwebp' ); ?> | |
| 281 | + <div class="spinner"></div> | |
| 282 | + </button> | |
| 283 | + <div class="quickwebp-undo-single-optimization-msg"></div> | |
| 284 | + </div> | |
| 285 | + <?php | |
| 286 | + | |
| 287 | + return ob_get_clean(); | |
| 288 | + } | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * Display optimize button in the media uploader | |
| 293 | + */ | |
| 294 | + public function optimize_btn( $attachment_id ) { | |
| 295 | + ob_start(); | |
| 296 | + ?> | |
| 297 | + <button class="button button-sacondary quickwebp-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>"> | |
| 298 | + <?php esc_html_e( 'Optimize', 'quickwebp' ); ?> | |
| 299 | + <div class="spinner"></div> | |
| 300 | + </button> | |
| 301 | + <div class="quickwebp-single-optimization-msg"></div> | |
| 302 | + <?php | |
| 303 | + | |
| 304 | + return ob_get_clean(); | |
| 305 | + } | |
| 306 | + | |
| 307 | + /** | |
| 308 | + * Check the mimetype of the file | |
| 309 | + */ | |
| 310 | + private function valid_mimetype( $post_id ) { | |
| 311 | + $image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version ); | |
| 312 | + $allowed_mime_types = $image_optimizer->allowed_mime_types; | |
| 313 | + $post_mime_type = get_post_mime_type( $post_id ); | |
| 314 | + | |
| 315 | + return in_array( $post_mime_type, $allowed_mime_types ); | |
| 316 | + } | |
| 317 | +} | |