| 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 |
$admin_attachment_assets = include( QUICKWEBP_PLUGIN_PATH . 'public/assets/build/admin-attachment.asset.php' ); |
| 67 |
wp_enqueue_style( 'quickwebp-admin-attachment', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-attachment.css', array(), $admin_attachment_assets['version'], 'all' ); |
| 68 |
wp_enqueue_script( 'quickwebp-admin-attachment', QUICKWEBP_PLUGIN_URL . 'public/assets/build/admin-attachment.js', $admin_attachment_assets['dependencies'], $admin_attachment_assets['version'], true ); |
| 69 |
wp_localize_script( 'quickwebp-admin-attachment', 'QUICKWEBP_ADMIN_ATTACHMENT', array( |
| 70 |
'nonce' => wp_create_nonce( 'quickwebp_admin_attachment' ), |
| 71 |
'ajaxUrl' => admin_url( 'admin-ajax.php' ) |
| 72 |
)); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add "Quickwebp" column in the Media Uploader |
| 77 |
*/ |
| 78 |
public function add_attachment_fields_to_edit( $form_fields, $post ) { |
| 79 |
global $pagenow; |
| 80 |
|
| 81 |
if ( 'post.php' === $pagenow ) { |
| 82 |
return $form_fields; |
| 83 |
} |
| 84 |
|
| 85 |
if ( ! $this->valid_mimetype( $post->ID ) ) { |
| 86 |
return $form_fields; |
| 87 |
} |
| 88 |
|
| 89 |
$data = get_post_meta( $post->ID, 'quickwebp_data', true ); |
| 90 |
$has_error = get_post_meta( $post->ID, 'quickwebp_has_error', true ); |
| 91 |
|
| 92 |
if ( ! is_array( $data ) ) { |
| 93 |
|
| 94 |
$html = $this->optimize_btn( $post->ID ); |
| 95 |
|
| 96 |
if ( ! empty( $has_error ) ) { |
| 97 |
$html .= '<br>' . esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN ); |
| 98 |
} |
| 99 |
|
| 100 |
} else { |
| 101 |
|
| 102 |
$html = $this->attachment_data( $data, $post->ID ); |
| 103 |
} |
| 104 |
|
| 105 |
$form_fields['quickwebp'] = array( |
| 106 |
'label' => 'Quickwebp', |
| 107 |
'input' => 'html', |
| 108 |
'html' => $html, |
| 109 |
'show_in_edit' => true, |
| 110 |
'show_in_modal' => true |
| 111 |
); |
| 112 |
|
| 113 |
return $form_fields; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Add "quickwebp" column in upload.php |
| 118 |
*/ |
| 119 |
public function add_media_columns( $columns ) { |
| 120 |
|
| 121 |
$columns['quickwebp'] = __( 'Quickwebp', QUICKWEBP_TEXT_DOMAIN ); |
| 122 |
|
| 123 |
return $columns; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Add "Quickwebp" column in the Media list table |
| 128 |
*/ |
| 129 |
public function add_media_custom_column( $column_name, $attachment_id ) { |
| 130 |
|
| 131 |
if ( 'quickwebp' !== $column_name ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! $this->valid_mimetype( $attachment_id ) ) { |
| 136 |
echo __( 'Image type not supported', QUICKWEBP_TEXT_DOMAIN ); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$data = get_post_meta( $attachment_id, 'quickwebp_data', true ); |
| 141 |
$has_error = get_post_meta( $attachment_id, 'quickwebp_has_error', true ); |
| 142 |
|
| 143 |
if ( ! is_array( $data ) ) { |
| 144 |
|
| 145 |
echo $this->optimize_btn( $attachment_id ); |
| 146 |
|
| 147 |
if ( ! empty( $has_error ) ) { |
| 148 |
echo esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN ); |
| 149 |
} |
| 150 |
|
| 151 |
} else { |
| 152 |
|
| 153 |
echo $this->attachment_data( $data, $attachment_id ); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Add a "Optimize" button or the Imagify optimization data in the attachment submit area. |
| 159 |
*/ |
| 160 |
public function add_attachment_submitbox_misc_actions() { |
| 161 |
global $post; |
| 162 |
|
| 163 |
if ( ! $this->valid_mimetype( $post->ID ) ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
$data = get_post_meta( $post->ID, 'quickwebp_data', true ); |
| 168 |
$has_error = get_post_meta( $post->ID, 'quickwebp_has_error', true ); |
| 169 |
|
| 170 |
if ( ! is_array( $data ) ) { |
| 171 |
|
| 172 |
echo '<table><tr><td>'; |
| 173 |
echo $this->optimize_btn( $post->ID ); |
| 174 |
|
| 175 |
if ( ! empty( $has_error ) ) { |
| 176 |
echo esc_html__( 'Error attempting to optimize this image', QUICKWEBP_TEXT_DOMAIN ); |
| 177 |
} |
| 178 |
|
| 179 |
echo '</td></tr></table>'; |
| 180 |
|
| 181 |
} else { |
| 182 |
|
| 183 |
echo '<table><tr><td>' . $this->attachment_data( $data, $post->ID ) . '</td></tr></table>'; |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get all data to display for a specific media |
| 190 |
*/ |
| 191 |
public function attachment_data( $data, $attachment_id ) { |
| 192 |
|
| 193 |
$full_image_data = $data['full'] ?? array(); |
| 194 |
|
| 195 |
if ( ! empty( $full_image_data ) ) { |
| 196 |
|
| 197 |
ob_start(); |
| 198 |
|
| 199 |
?> |
| 200 |
<div> |
| 201 |
<strong><?php _e( 'Original Image: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong> |
| 202 |
<span><?php echo round( $full_image_data['original_size'] / 1024, 2 ) . 'KB'; ?></span> |
| 203 |
</div> |
| 204 |
<div> |
| 205 |
<strong><?php _e( 'Webp: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong> |
| 206 |
<span><?php echo round( $full_image_data['optimized_size'] / 1024, 2 ) . 'KB'; ?></span> |
| 207 |
</div> |
| 208 |
<div> |
| 209 |
<strong><?php _e( 'Save: ', QUICKWEBP_TEXT_DOMAIN ); ?></strong> |
| 210 |
<span><?php echo $full_image_data['percent'] . '%'; ?></span> |
| 211 |
</div> |
| 212 |
<div> |
| 213 |
<button class="button button-sacondary quickwebp-undo-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>"> |
| 214 |
<?php _e( 'Undo optimization', QUICKWEBP_TEXT_DOMAIN ); ?> |
| 215 |
<div class="spinner"></div> |
| 216 |
</button> |
| 217 |
<div class="quickwebp-undo-single-optimization-msg"></div> |
| 218 |
</div> |
| 219 |
<?php |
| 220 |
|
| 221 |
return ob_get_clean(); |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Display optimize button in the media uploader |
| 228 |
*/ |
| 229 |
public function optimize_btn( $attachment_id ) { |
| 230 |
|
| 231 |
ob_start(); |
| 232 |
|
| 233 |
?> |
| 234 |
<button class="button button-sacondary quickwebp-single-optimization-btn" data-attachment-id="<?php echo esc_attr( $attachment_id ); ?>"> |
| 235 |
<?php _e( 'Optimize', QUICKWEBP_TEXT_DOMAIN ); ?> |
| 236 |
<div class="spinner"></div> |
| 237 |
</button> |
| 238 |
<div class="quickwebp-single-optimization-msg"></div> |
| 239 |
<?php |
| 240 |
|
| 241 |
return ob_get_clean(); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Check the mimetype of the file |
| 246 |
*/ |
| 247 |
public function valid_mimetype( $post_id ) { |
| 248 |
|
| 249 |
// check the mime type |
| 250 |
$image_optimizer = new Quickwebp_Image_Optimizer( $this->plugin_name, $this->version ); |
| 251 |
$mime_types = $image_optimizer->allowed_mime_types; |
| 252 |
$index = array_search( 'image/webp', $mime_types ); |
| 253 |
unset( $mime_types[$index] ); |
| 254 |
$mime_type = get_post_mime_type( $post_id ); |
| 255 |
|
| 256 |
return in_array( $mime_type, $mime_types ); |
| 257 |
} |
| 258 |
|
| 259 |
} |