| @@ -57,17 +57,19 @@ | ||
| 57 | 57 | public function __construct() { |
| 58 | 58 | |
| 59 | 59 | list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path(); |
| 60 | 60 | |
| 61 | + /* Generate metadata */ | |
| 61 | 62 | add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 ); |
| 62 | - add_action( 'pluswebp_mail_send', array( $this, 'mail_send' ), 10, 1 ); | |
| 63 | + /* Original hook */ | |
| 64 | + add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 1 ); | |
| 65 | + add_filter( 'pluswebp_mail_messages', array( $this, 'mail_messages' ), 10, 2 ); | |
| 66 | + add_action( 'pluswebp_mail_generate_message', array( $this, 'mail_generate_message' ), 10, 2 ); | |
| 63 | 67 | |
| 64 | - /* Ajax */ | |
| 65 | - $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 66 | - $action2 = 'bulkgeneratewebp_message'; | |
| 67 | - add_action( 'wp_ajax_' . $action1, array( $this, 'bulkgeneratewebp_update_callback' ) ); | |
| 68 | - add_action( 'wp_ajax_' . $action2, array( $this, 'bulkgeneratewebp_message_callback' ) ); | |
| 69 | - | |
| 68 | + /* Raise memory */ | |
| 69 | + add_filter( 'pluswebp_memory_limit', array( $this, 'raise_memory_limit' ) ); | |
| 70 | + /* webp generation control */ | |
| 71 | + add_filter( 'wp_upload_image_mime_transforms', array( $this, 'control_mime_type' ), 10, 2 ); | |
| 70 | 72 | } |
| 71 | 73 | |
| 72 | 74 | /** ================================================== |
| 73 | 75 | * Webp generate |
| @@ -86,25 +88,37 @@ | ||
| 86 | 88 | if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { |
| 87 | 89 | $metadata_webp = $metadata; |
| 88 | 90 | $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] ); |
| 89 | 91 | $metadata_webp['file'] = $file_webp; |
| 92 | + if ( '.' === dirname( $file_webp ) ) { | |
| 93 | + $dir_name_url = '/'; | |
| 94 | + $dir_name_path = wp_normalize_path( '/' ); | |
| 95 | + } else { | |
| 96 | + $dir_name_url = '/' . dirname( $file_webp ) . '/'; | |
| 97 | + $dir_name_path = wp_normalize_path( $dir_name_url ); | |
| 98 | + } | |
| 99 | + $url = $this->upload_url . $dir_name_url; | |
| 100 | + $path = $this->upload_dir . $dir_name_path; | |
| 101 | + | |
| 90 | 102 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 91 | 103 | $file_thumb = $value['file']; |
| 92 | 104 | $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] ); |
| 93 | - $path = $this->upload_dir . '/' . dirname( $file_webp ) . '/'; | |
| 94 | - $url = $this->upload_url . '/' . dirname( $file_webp ) . '/'; | |
| 95 | - $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); | |
| 105 | + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); | |
| 96 | 106 | if ( $ret ) { |
| 97 | 107 | $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 98 | 108 | $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; |
| 109 | + $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) ); | |
| 110 | + $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size; | |
| 99 | 111 | if ( $replace ) { |
| 100 | - unlink( $path . $file_thumb ); | |
| 112 | + wp_delete_file( $path . $file_thumb ); | |
| 101 | 113 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 102 | 114 | } |
| 103 | 115 | } |
| 104 | 116 | } |
| 105 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 106 | - $org_img_file = wp_get_original_image_path( $attachment_id, false ); | |
| 117 | + | |
| 118 | + $org_img_file = null; | |
| 119 | + if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { | |
| 120 | + $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) ); | |
| 107 | 121 | $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] ); |
| 108 | 122 | $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file ); |
| 109 | 123 | if ( $ret ) { |
| 110 | 124 | $metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| @@ -110,9 +124,11 @@ | ||
| 110 | 124 | $metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| 111 | 125 | } |
| 112 | 126 | } |
| 113 | 127 | |
| 114 | - $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp ); | |
| 128 | + $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ) ); | |
| 129 | + $webp_size = filesize( $path . wp_basename( $file_webp ) ); | |
| 130 | + $metadata_webp['filesize'] = $webp_size; | |
| 115 | 131 | if ( $ret ) { |
| 116 | 132 | if ( $replace ) { |
| 117 | 133 | $up_post = array( |
| 118 | 134 | 'ID' => $attachment_id, |
| @@ -123,9 +139,12 @@ | ||
| 123 | 139 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 124 | 140 | /* for bulk generate */ |
| 125 | 141 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 126 | 142 | /* delete org file */ |
| 127 | - unlink( $this->upload_dir . '/' . $metadata['file'] ); | |
| 143 | + wp_delete_file( $this->upload_dir . '/' . $metadata['file'] ); | |
| 144 | + if ( $org_img_file ) { | |
| 145 | + wp_delete_file( $org_img_file ); | |
| 146 | + } | |
| 128 | 147 | /* Replace */ |
| 129 | 148 | $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 130 | 149 | /* for hook */ |
| 131 | 150 | $metadata = $metadata_webp; |
| @@ -132,9 +151,9 @@ | ||
| 132 | 151 | /* for mail */ |
| 133 | 152 | $attach_id = $attachment_id; |
| 134 | 153 | } else { |
| 135 | 154 | $post = get_post( $attachment_id ); |
| 136 | - $title = get_the_title( $post ); | |
| 155 | + $title = $post->post_title; | |
| 137 | 156 | $attachment = array( |
| 138 | 157 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 139 | 158 | 'post_mime_type' => 'image/webp', |
| 140 | 159 | 'post_title' => $title, |
| @@ -140,9 +159,15 @@ | ||
| 140 | 159 | 'post_title' => $title, |
| 141 | 160 | 'post_content' => '', |
| 142 | 161 | 'post_status' => 'inherit', |
| 143 | 162 | ); |
| 144 | - $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp ); | |
| 163 | + $file = $this->upload_dir . '/' . $file_webp; | |
| 164 | + $attach_id = wp_insert_attachment( $attachment, $file ); | |
| 165 | + | |
| 166 | + /* for XAMPP [ get_attached_file( $attach_id ): Unable to get correct value ] */ | |
| 167 | + $metapath_name = str_replace( $this->upload_dir . '/', '', $file ); | |
| 168 | + update_post_meta( $attach_id, '_wp_attached_file', $metapath_name ); | |
| 169 | + | |
| 145 | 170 | wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 146 | 171 | |
| 147 | 172 | $author = get_userdata( $post->post_author ); |
| 148 | 173 | $userid = $author->ID; |
| @@ -158,140 +183,23 @@ | ||
| 158 | 183 | ); |
| 159 | 184 | wp_update_post( $up_post ); |
| 160 | 185 | } |
| 161 | 186 | |
| 162 | - $ids = get_option( 'pluswebp_generate' ); | |
| 163 | - $ids[] = $attach_id; | |
| 164 | - update_option( 'pluswebp_generate', $ids ); | |
| 187 | + update_option( 'pluswebp_generate', $attach_id ); | |
| 165 | 188 | |
| 166 | - } | |
| 167 | - } | |
| 189 | + /* for Media Library folders term by Organize Media Folder */ | |
| 190 | + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id ); | |
| 168 | 191 | |
| 169 | - return $metadata; | |
| 192 | + /* for Term filter update by Organize Media Folder */ | |
| 193 | + do_action( 'omf_term_filter_update' ); | |
| 170 | 194 | |
| 171 | - } | |
| 172 | - | |
| 173 | - /** ================================================== | |
| 174 | - * IDs Callback | |
| 175 | - * | |
| 176 | - * @since 2.00 | |
| 177 | - */ | |
| 178 | - public function bulkgeneratewebp_update_callback() { | |
| 179 | - | |
| 180 | - $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 181 | - if ( check_ajax_referer( $action1, 'nonce', false ) ) { | |
| 182 | - if ( current_user_can( 'manage_options' ) ) { | |
| 183 | - if ( ! empty( $_POST['id'] ) ) { | |
| 184 | - $id = absint( $_POST['id'] ); | |
| 185 | - } else { | |
| 186 | - return; | |
| 187 | - } | |
| 188 | - if ( ! wp_get_attachment_url( $id ) ) { | |
| 189 | - $error_string = __( 'No media!', 'plus-webp' ); | |
| 190 | - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; | |
| 191 | - } else { | |
| 192 | - $output_html = $this->output_html( $id ); | |
| 193 | - if ( is_null( $output_html ) ) { | |
| 194 | - $error_string = __( 'WebP was not generated.', 'plus-webp' ); | |
| 195 | - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; | |
| 196 | - } | |
| 197 | - } | |
| 198 | - | |
| 199 | - header( 'Content-type: text/html; charset=UTF-8' ); | |
| 200 | - $allowed_output_html = array( | |
| 201 | - 'a' => array( | |
| 202 | - 'href' => array(), | |
| 203 | - 'target' => array(), | |
| 204 | - 'rel' => array(), | |
| 205 | - 'style' => array(), | |
| 206 | - ), | |
| 207 | - 'img' => array( | |
| 208 | - 'src' => array(), | |
| 209 | - 'width' => array(), | |
| 210 | - 'height' => array(), | |
| 211 | - 'style' => array(), | |
| 212 | - ), | |
| 213 | - 'div' => array( | |
| 214 | - 'style' => array(), | |
| 215 | - 'class' => array(), | |
| 216 | - ), | |
| 217 | - 'font' => array( | |
| 218 | - 'color' => array(), | |
| 219 | - ), | |
| 220 | - 'ul' => array(), | |
| 221 | - 'li' => array(), | |
| 222 | - 'span' => array( | |
| 223 | - 'class' => array(), | |
| 224 | - 'style' => array(), | |
| 225 | - ), | |
| 226 | - ); | |
| 227 | - echo wp_kses( $output_html, $allowed_output_html ); | |
| 228 | 195 | } |
| 229 | - } else { | |
| 230 | - status_header( '403' ); | |
| 231 | - echo 'Forbidden'; | |
| 232 | 196 | } |
| 233 | 197 | |
| 234 | - wp_die(); | |
| 235 | - | |
| 198 | + return $metadata; | |
| 236 | 199 | } |
| 237 | 200 | |
| 238 | 201 | /** ================================================== |
| 239 | - * Output html for Ajax | |
| 240 | - * | |
| 241 | - * @param array $attach_id attach_id. | |
| 242 | - * @since 2.00 | |
| 243 | - */ | |
| 244 | - private function output_html( $attach_id ) { | |
| 245 | - | |
| 246 | - include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 247 | - | |
| 248 | - /* Generate thumbnails */ | |
| 249 | - $metadata_org = wp_get_attachment_metadata( $attach_id ); | |
| 250 | - do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ); | |
| 251 | - if ( get_option( 'pluswebp_generate' ) ) { | |
| 252 | - $ids = get_option( 'pluswebp_generate' ); | |
| 253 | - delete_option( 'pluswebp_generate' ); | |
| 254 | - $webp_id = $ids[0]; | |
| 255 | - $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 256 | - | |
| 257 | - /* Thumbnail urls */ | |
| 258 | - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); | |
| 259 | - /* Output datas*/ | |
| 260 | - list( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id ); | |
| 261 | - | |
| 262 | - $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">'; | |
| 263 | - $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">'; | |
| 264 | - $output_html .= '<div style="overflow: hidden;">'; | |
| 265 | - $output_html .= '<div>ID: ' . $webp_id . '</div>'; | |
| 266 | - $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>'; | |
| 267 | - $output_html .= '<div>' . __( 'Permalink:' ) . ' <a href="' . $attachment_link . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_link . '</a></div>'; | |
| 268 | - $output_html .= '<div>URL: <a href="' . $attachment_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_url . '</a></div>'; | |
| 269 | - $output_html .= '<div>' . __( 'File name:' ) . ' ' . wp_basename( $attachment_url ) . '</div>'; | |
| 270 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 271 | - $output_html .= '<div>' . __( 'Original URL', 'plus-webp' ) . ': <a href="' . $original_image_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $original_image_url . '</a></div>'; | |
| 272 | - $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . wp_basename( $original_image_url ) . '</div>'; | |
| 273 | - } | |
| 274 | - $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>'; | |
| 275 | - $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>'; | |
| 276 | - $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>'; | |
| 277 | - if ( ! empty( $imagethumburls ) ) { | |
| 278 | - $output_html .= '<div>' . __( 'Images' ) . ': '; | |
| 279 | - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 280 | - $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]'; | |
| 281 | - } | |
| 282 | - $output_html .= '</div>'; | |
| 283 | - } | |
| 284 | - $output_html .= '</div></div>'; | |
| 285 | - } else { | |
| 286 | - $output_html = null; | |
| 287 | - } | |
| 288 | - | |
| 289 | - return $output_html; | |
| 290 | - | |
| 291 | - } | |
| 292 | - | |
| 293 | - /** ================================================== | |
| 294 | 202 | * Thumbnail urls |
| 295 | 203 | * |
| 296 | 204 | * @param int $attach_id attach_id. |
| 297 | 205 | * @param array $metadata metadata. |
| @@ -304,15 +212,14 @@ | ||
| 304 | 212 | $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true ); |
| 305 | 213 | $image_thumbnail = $image_attr_thumbnail[0]; |
| 306 | 214 | |
| 307 | 215 | $imagethumburls = array(); |
| 308 | - $media_path = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 309 | - $media_paths = explode( DIRECTORY_SEPARATOR, $media_path ); | |
| 310 | - $str_length = strlen( end( $media_paths ) ); | |
| 311 | - $media_path = substr( $media_path, 0, -$str_length ); | |
| 312 | - $media_url = $upload_url . '/' . $media_path; | |
| 313 | - if ( ! empty( $metadata ) ) { | |
| 314 | - $thumbnails = $metadata['sizes']; | |
| 216 | + if ( ! empty( $metadata ) && array_key_exists( 'sizes', $metadata ) ) { | |
| 217 | + $thumbnails = $metadata['sizes']; | |
| 218 | + $path_file = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 219 | + $filename = wp_basename( $path_file ); | |
| 220 | + $media_path = str_replace( $filename, '', $path_file ); | |
| 221 | + $media_url = $upload_url . '/' . $media_path; | |
| 315 | 222 | foreach ( $thumbnails as $key => $key2 ) { |
| 316 | 223 | $imagethumburls[ $key ] = $media_url . $key2['file']; |
| 317 | 224 | } |
| 318 | 225 | } |
| @@ -317,198 +224,157 @@ | ||
| 317 | 224 | } |
| 318 | 225 | } |
| 319 | 226 | |
| 320 | 227 | return array( $image_thumbnail, $imagethumburls ); |
| 321 | - | |
| 322 | 228 | } |
| 323 | 229 | |
| 324 | 230 | /** ================================================== |
| 325 | 231 | * Output datas |
| 326 | 232 | * |
| 327 | - * @param int $attach_id attach_id. | |
| 328 | - * @return array (string) $attachment_link, $attachment_url, $original_image_url, $mime_type(string), $stamptime, $file_size | |
| 233 | + * @param int $attach_id attach_id. | |
| 234 | + * @param array $metadata metadata. | |
| 235 | + * @return array (string) $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type(string), $stamptime, $file_size | |
| 329 | 236 | * @since 2.00 |
| 330 | 237 | */ |
| 331 | - private function output_datas( $attach_id ) { | |
| 238 | + private function output_datas( $attach_id, $metadata ) { | |
| 332 | 239 | |
| 333 | 240 | $attachment_link = get_attachment_link( $attach_id ); |
| 334 | 241 | $attachment_url = wp_get_attachment_url( $attach_id ); |
| 335 | - $original_image_url = wp_get_original_image_url( $attach_id ); | |
| 336 | - $filetype = wp_check_filetype( get_attached_file( $attach_id ) ); | |
| 337 | - $mime_type = $filetype['type']; | |
| 242 | + $filename = wp_basename( $attachment_url ); | |
| 243 | + if ( ! empty( $metadata ) && array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { | |
| 244 | + $original_image_url = wp_get_original_image_url( $attach_id ); | |
| 245 | + $original_filename = wp_basename( $original_image_url ); | |
| 246 | + } else { | |
| 247 | + $original_image_url = null; | |
| 248 | + $original_filename = null; | |
| 249 | + } | |
| 250 | + $mime_type = get_post_mime_type( $attach_id ); | |
| 338 | 251 | |
| 339 | 252 | $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); |
| 340 | - if ( isset( $metadata['filesize'] ) ) { | |
| 253 | + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) { | |
| 341 | 254 | $file_size = $metadata['filesize']; |
| 342 | 255 | } else { |
| 343 | 256 | $file_size = @filesize( get_attached_file( $attach_id ) ); |
| 344 | 257 | } |
| 345 | 258 | if ( ! $file_size ) { |
| 346 | - $file_size = '<font color="red">' . __( 'Could not retrieve.', 'plus-webp' ) . '</font>'; | |
| 259 | + $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 347 | 260 | } else { |
| 348 | 261 | $file_size = size_format( $file_size ); |
| 349 | 262 | } |
| 350 | 263 | |
| 351 | - return array( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ); | |
| 352 | - | |
| 264 | + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); | |
| 353 | 265 | } |
| 354 | 266 | |
| 355 | 267 | /** ================================================== |
| 356 | - * Messages Callback | |
| 268 | + * Get All Images | |
| 357 | 269 | * |
| 270 | + * @param array $types mime types. | |
| 358 | 271 | * @since 2.00 |
| 359 | 272 | */ |
| 360 | - public function bulkgeneratewebp_message_callback() { | |
| 273 | + public function get_allimages( $types ) { | |
| 361 | 274 | |
| 362 | - $action2 = 'bulkgeneratewebp_message'; | |
| 363 | - if ( check_ajax_referer( $action2, 'nonce', false ) ) { | |
| 364 | - $error_count = 0; | |
| 365 | - $error_update = null; | |
| 366 | - $success_count = 0; | |
| 367 | - if ( ! empty( $_POST['error_count'] ) ) { | |
| 368 | - $error_count = absint( $_POST['error_count'] ); | |
| 369 | - } | |
| 370 | - if ( ! empty( $_POST['error_update'] ) ) { | |
| 371 | - $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) ); | |
| 372 | - } | |
| 373 | - if ( ! empty( $_POST['success_count'] ) ) { | |
| 374 | - $success_count = absint( $_POST['success_count'] ); | |
| 375 | - } | |
| 275 | + $args = array( | |
| 276 | + 'post_type' => 'attachment', | |
| 277 | + 'post_status' => 'inherit', | |
| 278 | + 'post_mime_type' => $types, | |
| 279 | + 'posts_per_page' => -1, | |
| 280 | + 'orderby' => 'date', | |
| 281 | + 'order' => 'DESC', | |
| 282 | + ); | |
| 283 | + $posts = get_posts( $args ); | |
| 376 | 284 | |
| 377 | - $back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>'; | |
| 285 | + global $wpdb; | |
| 286 | + $webp_titles = $wpdb->get_col( | |
| 287 | + " | |
| 288 | + SELECT post_title | |
| 289 | + FROM {$wpdb->prefix}posts | |
| 290 | + WHERE post_type = 'attachment' | |
| 291 | + AND post_mime_type IN ( 'image/webp' ) | |
| 292 | + AND post_status = 'inherit' | |
| 293 | + " | |
| 294 | + ); | |
| 378 | 295 | |
| 379 | - $output_html = null; | |
| 380 | - if ( $error_count > 0 ) { | |
| 381 | - /* translators: error message %1$d: media count %2$s: back link */ | |
| 382 | - $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html ); | |
| 383 | - $output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>'; | |
| 296 | + $post_ids = array(); | |
| 297 | + $no_file_ids = array(); | |
| 298 | + $count = 0; | |
| 299 | + foreach ( $posts as $post ) { | |
| 300 | + if ( ! in_array( $post->post_title, $webp_titles ) ) { | |
| 301 | + if ( file_exists( get_attached_file( $post->ID ) ) ) { | |
| 302 | + $post_ids[] = $post->ID; | |
| 303 | + } else { | |
| 304 | + $no_file_ids[] = $post->ID; | |
| 305 | + } | |
| 384 | 306 | } |
| 385 | - if ( $success_count > 0 ) { | |
| 386 | - /* translators: success message %1$d: media count %2$s: back link */ | |
| 387 | - $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html ); | |
| 388 | - $output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>'; | |
| 389 | - } | |
| 390 | - | |
| 391 | - header( 'Content-type: text/html; charset=UTF-8' ); | |
| 392 | - $allowed_output_html = array( | |
| 393 | - 'div' => array( | |
| 394 | - 'class' => array(), | |
| 395 | - ), | |
| 396 | - 'ul' => array(), | |
| 397 | - 'li' => array(), | |
| 398 | - 'strong' => array(), | |
| 399 | - 'a' => array( | |
| 400 | - 'style' => array(), | |
| 401 | - 'href' => array(), | |
| 402 | - ), | |
| 403 | - ); | |
| 404 | - echo wp_kses( $output_html, $allowed_output_html ); | |
| 405 | 307 | } |
| 406 | 308 | |
| 407 | - wp_die(); | |
| 408 | - | |
| 309 | + return array( $post_ids, $no_file_ids ); | |
| 409 | 310 | } |
| 410 | 311 | |
| 411 | 312 | /** ================================================== |
| 412 | - * Mail send hook | |
| 313 | + * Mail messages hook | |
| 413 | 314 | * |
| 414 | - * @param string $to to. | |
| 415 | - * @since 1.09 | |
| 315 | + * @param array $messages messages. | |
| 316 | + * @param int $webp_id webp_id. | |
| 317 | + * | |
| 318 | + * @since 4.00 | |
| 416 | 319 | */ |
| 417 | - public function mail_send( $to ) { | |
| 320 | + public function mail_messages( $messages, $webp_id ) { | |
| 418 | 321 | |
| 419 | - $post_ids = get_option( 'pluswebp_generate' ); | |
| 420 | - delete_option( 'pluswebp_generate' ); | |
| 322 | + $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 421 | 323 | |
| 422 | - if ( function_exists( 'wp_date' ) ) { | |
| 423 | - $now_date_time = wp_date( 'Y-m-d H:i:s' ); | |
| 424 | - } else { | |
| 425 | - $now_date_time = date_i18n( 'Y-m-d H:i:s' ); | |
| 426 | - } | |
| 427 | - $pluswebp_mail_send = array(); | |
| 428 | - $pluswebp_mail_send['datetime'] = $now_date_time; | |
| 324 | + $message = null; | |
| 325 | + if ( $metadata ) { | |
| 326 | + /* Thumbnail urls */ | |
| 327 | + list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); | |
| 328 | + /* Output datas*/ | |
| 329 | + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata ); | |
| 429 | 330 | |
| 430 | - /* translators: Date and Time */ | |
| 431 | - $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 432 | - | |
| 433 | - if ( ! empty( $post_ids ) ) { | |
| 434 | - | |
| 435 | - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; | |
| 436 | - $pluswebp_mail_send['count'] = count( $post_ids ); | |
| 437 | - | |
| 438 | - $count = 0; | |
| 439 | - foreach ( $post_ids as $attach_id ) { | |
| 440 | - $filename = get_attached_file( $attach_id ); | |
| 441 | - $metadata = wp_get_attachment_metadata( $attach_id ); | |
| 442 | - /* OutputMetaData */ | |
| 443 | - list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url ); | |
| 444 | - $count++; | |
| 445 | - $message .= __( 'Count' ) . ': ' . $count . "\n"; | |
| 446 | - $message .= 'ID: ' . $attach_id . "\n"; | |
| 447 | - $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n"; | |
| 448 | - $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n"; | |
| 449 | - $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n"; | |
| 450 | - $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n"; | |
| 451 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 452 | - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n"; | |
| 453 | - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n"; | |
| 331 | + $message = 'ID: ' . $webp_id . "\n"; | |
| 332 | + $message .= __( 'Title' ) . ': ' . get_the_title( $webp_id ) . "\n"; | |
| 333 | + $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n"; | |
| 334 | + $message .= 'URL: ' . $attachment_url . "\n"; | |
| 335 | + $message .= __( 'File name:' ) . ' ' . $filename . "\n"; | |
| 336 | + if ( ! empty( $original_image_url ) ) { | |
| 337 | + $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n"; | |
| 338 | + $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n"; | |
| 339 | + } | |
| 340 | + $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; | |
| 341 | + $message .= __( 'File size:' ) . ' ' . $file_size . "\n"; | |
| 342 | + if ( ! empty( $imagethumburls ) ) { | |
| 343 | + foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 344 | + $message .= $thumbsize . ': ' . $imagethumburl . "\n"; | |
| 454 | 345 | } |
| 455 | - $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; | |
| 456 | - if ( ! $file_size ) { | |
| 457 | - $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 458 | - } else { | |
| 459 | - $file_size = size_format( $file_size ); | |
| 460 | - } | |
| 461 | - $message .= __( 'File size:' ) . ' ' . $file_size . "\n"; | |
| 462 | - if ( ! empty( $imagethumburls ) ) { | |
| 463 | - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 464 | - $message .= $thumbsize . ': ' . $imagethumburl . "\n"; | |
| 465 | - } | |
| 466 | - } | |
| 467 | - $message .= "\n"; | |
| 468 | 346 | } |
| 469 | - } else { | |
| 470 | - $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n"; | |
| 471 | - $pluswebp_mail_send['count'] = 0; | |
| 347 | + $message .= "\n"; | |
| 348 | + $messages[] = $message; | |
| 472 | 349 | } |
| 473 | 350 | |
| 474 | - update_option( 'pluswebp_generate_mail', $pluswebp_mail_send ); | |
| 475 | - | |
| 476 | - /* translators: blogname for subject */ | |
| 477 | - $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); | |
| 478 | - wp_mail( $to, $subject, $message ); | |
| 479 | - | |
| 351 | + return array( $message, $messages ); | |
| 480 | 352 | } |
| 481 | 353 | |
| 482 | 354 | /** ================================================== |
| 483 | - * Output metadata | |
| 355 | + * Mail sent for Generate Messages | |
| 484 | 356 | * |
| 485 | - * @param int $attach_id attach_id. | |
| 486 | - * @param array $metadata metadata. | |
| 487 | - * @param string $upload_url upload_url. | |
| 488 | - * @return array $imagethumburls(string), $stamptime(string), $file_size(string) | |
| 489 | - * @since 1.09 | |
| 357 | + * @param array $messages mail messages. | |
| 358 | + * @param int $count convert count. | |
| 359 | + * @since 4.02 | |
| 490 | 360 | */ |
| 491 | - private function output_metadata( $attach_id, $metadata, $upload_url ) { | |
| 361 | + public function mail_generate_message( $messages, $count ) { | |
| 492 | 362 | |
| 493 | - $imagethumburls = array(); | |
| 494 | - $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 495 | - $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) ); | |
| 496 | - if ( ! empty( $metadata ) ) { | |
| 497 | - foreach ( $metadata as $key1 => $key2 ) { | |
| 498 | - if ( 'sizes' === $key1 ) { | |
| 499 | - foreach ( $metadata[ $key1 ] as $key2 => $key3 ) { | |
| 500 | - $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file']; | |
| 501 | - } | |
| 502 | - } | |
| 503 | - } | |
| 363 | + if ( function_exists( 'wp_date' ) ) { | |
| 364 | + $now_date_time = wp_date( 'Y-m-d H:i:s' ); | |
| 365 | + } else { | |
| 366 | + $now_date_time = date_i18n( 'Y-m-d H:i:s' ); | |
| 504 | 367 | } |
| 368 | + /* translators: Date and Time */ | |
| 369 | + $message_head = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 370 | + /* translators: Generated count */ | |
| 371 | + $message_head .= sprintf( __( 'Webp generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n"; | |
| 505 | 372 | |
| 506 | - $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); | |
| 507 | - $file_size = @filesize( get_attached_file( $attach_id, false ) ); | |
| 508 | - | |
| 509 | - return array( $imagethumburls, $stamptime, $file_size ); | |
| 510 | - | |
| 373 | + $to = get_option( 'admin_email' ); | |
| 374 | + /* translators: blogname for subject */ | |
| 375 | + $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); | |
| 376 | + wp_mail( $to, $subject, $message_head . implode( $messages ) ); | |
| 511 | 377 | } |
| 512 | 378 | |
| 513 | 379 | /** ================================================== |
| 514 | 380 | * Webp create |
| @@ -529,8 +395,9 @@ | ||
| 529 | 395 | } |
| 530 | 396 | |
| 531 | 397 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 532 | 398 | @set_time_limit( 60 ); |
| 399 | + wp_raise_memory_limit( 'pluswebp' ); | |
| 533 | 400 | |
| 534 | 401 | $ret = false; |
| 535 | 402 | switch ( $mime_type ) { |
| 536 | 403 | case 'image/jpeg': |
| @@ -535,9 +402,10 @@ | ||
| 535 | 402 | switch ( $mime_type ) { |
| 536 | 403 | case 'image/jpeg': |
| 537 | 404 | $src = imagecreatefromjpeg( $filename ); |
| 538 | 405 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 539 | - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); | |
| 406 | + $bgcolor = imagecolorallocate( $img, 255, 255, 255 ); | |
| 407 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 540 | 408 | imagealphablending( $img, true ); |
| 541 | 409 | break; |
| 542 | 410 | case 'image/png': |
| 543 | 411 | $src = imagecreatefrompng( $filename ); |
| @@ -543,8 +411,11 @@ | ||
| 543 | 411 | $src = imagecreatefrompng( $filename ); |
| 544 | 412 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 545 | 413 | imagealphablending( $img, false ); |
| 546 | 414 | imagesavealpha( $img, true ); |
| 415 | + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); | |
| 416 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 417 | + imagecolortransparent( $img, $bgcolor ); | |
| 547 | 418 | break; |
| 548 | 419 | case 'image/bmp': |
| 549 | 420 | $src = imagecreatefrombmp( $filename ); |
| 550 | 421 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| @@ -563,9 +434,8 @@ | ||
| 563 | 434 | $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] ); |
| 564 | 435 | imagedestroy( $img ); |
| 565 | 436 | |
| 566 | 437 | return $ret; |
| 567 | - | |
| 568 | 438 | } |
| 569 | 439 | |
| 570 | 440 | /** ================================================== |
| 571 | 441 | * Change ext |
| @@ -587,9 +457,8 @@ | ||
| 587 | 457 | $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name ); |
| 588 | 458 | } |
| 589 | 459 | |
| 590 | 460 | return $after_file_name; |
| 591 | - | |
| 592 | 461 | } |
| 593 | 462 | |
| 594 | 463 | /** ================================================== |
| 595 | 464 | * Change DB |
| @@ -604,14 +473,19 @@ | ||
| 604 | 473 | |
| 605 | 474 | /* Replace */ |
| 606 | 475 | $wpdb->query( |
| 607 | 476 | $wpdb->prepare( |
| 608 | - "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)", | |
| 477 | + " | |
| 478 | + UPDATE {$wpdb->prefix}posts | |
| 479 | + SET post_content = replace( post_content, %s, %s ) | |
| 480 | + ", | |
| 609 | 481 | $before_url, |
| 610 | 482 | $after_url |
| 611 | 483 | ) |
| 612 | 484 | ); |
| 613 | 485 | |
| 486 | + /* Advanced change database */ | |
| 487 | + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url ); | |
| 614 | 488 | } |
| 615 | 489 | |
| 616 | 490 | /** ================================================== |
| 617 | 491 | * Upload Path |
| @@ -648,9 +522,8 @@ | ||
| 648 | 522 | $upload_url = untrailingslashit( $upload_url ); |
| 649 | 523 | $upload_path = untrailingslashit( $upload_path ); |
| 650 | 524 | |
| 651 | 525 | return array( $upload_dir, $upload_url, $upload_path ); |
| 652 | - | |
| 653 | 526 | } |
| 654 | 527 | |
| 655 | 528 | /** ================================================== |
| 656 | 529 | * Real Url |
| @@ -699,10 +572,34 @@ | ||
| 699 | 572 | } |
| 700 | 573 | $path = implode( '/', $base_path ); |
| 701 | 574 | return $parse['scheme'] . '://' . $parse['host'] . $path; |
| 702 | 575 | } |
| 576 | + } | |
| 703 | 577 | |
| 578 | + /** ================================================== | |
| 579 | + * Filter Raise Memory limit | |
| 580 | + * | |
| 581 | + * @since 3.00 | |
| 582 | + * | |
| 583 | + * @param string $filtered_limit Memory limit. | |
| 584 | + */ | |
| 585 | + public function raise_memory_limit( $filtered_limit ) { | |
| 586 | + | |
| 587 | + return '256M'; | |
| 704 | 588 | } |
| 705 | 589 | |
| 706 | -} | |
| 590 | + /** ================================================== | |
| 591 | + * Filter the output mime types for a given input mime type and image size. | |
| 592 | + * | |
| 593 | + * @since 3.00 | |
| 594 | + * | |
| 595 | + * @param array $image_mime_transforms A map with the valid mime transforms where the key is the source file mime type | |
| 596 | + * and the value is one or more mime file types to generate. | |
| 597 | + * @param int $attachment_id The ID of the attachment where the hook was dispatched. | |
| 598 | + */ | |
| 599 | + public function control_mime_type( $image_mime_transforms, $attachment_id ) { | |
| 707 | 600 | |
| 601 | + $image_mime_transforms = array(); | |
| 708 | 602 | |
| 603 | + return $image_mime_transforms; | |
| 604 | + } | |
| 605 | +} | |