| @@ -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,30 +88,34 @@ | ||
| 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 | - if ( '.' === dirname( $file_webp ) ) { | |
| 94 | - $dir_name_url = '/'; | |
| 95 | - $dir_name_path = wp_normalize_path( '/' ); | |
| 96 | - } else { | |
| 97 | - $dir_name_url = '/' . dirname( $file_webp ) . '/'; | |
| 98 | - $dir_name_path = wp_normalize_path( $dir_name_url ); | |
| 99 | - } | |
| 100 | - $url = $this->upload_url . $dir_name_url; | |
| 101 | - $path = $this->upload_dir . $dir_name_path; | |
| 102 | 105 | $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); |
| 103 | 106 | if ( $ret ) { |
| 104 | 107 | $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 105 | 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; | |
| 106 | 111 | if ( $replace ) { |
| 107 | - unlink( $path . $file_thumb ); | |
| 112 | + wp_delete_file( $path . $file_thumb ); | |
| 108 | 113 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 109 | 114 | } |
| 110 | 115 | } |
| 111 | 116 | } |
| 117 | + | |
| 112 | 118 | $org_img_file = null; |
| 113 | 119 | if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { |
| 114 | 120 | $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) ); |
| 115 | 121 | $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] ); |
| @@ -118,9 +124,11 @@ | ||
| 118 | 124 | $metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| 119 | 125 | } |
| 120 | 126 | } |
| 121 | 127 | |
| 122 | - $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; | |
| 123 | 131 | if ( $ret ) { |
| 124 | 132 | if ( $replace ) { |
| 125 | 133 | $up_post = array( |
| 126 | 134 | 'ID' => $attachment_id, |
| @@ -131,11 +139,11 @@ | ||
| 131 | 139 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 132 | 140 | /* for bulk generate */ |
| 133 | 141 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 134 | 142 | /* delete org file */ |
| 135 | - unlink( $this->upload_dir . '/' . $metadata['file'] ); | |
| 143 | + wp_delete_file( $this->upload_dir . '/' . $metadata['file'] ); | |
| 136 | 144 | if ( $org_img_file ) { |
| 137 | - unlink( $org_img_file ); | |
| 145 | + wp_delete_file( $org_img_file ); | |
| 138 | 146 | } |
| 139 | 147 | /* Replace */ |
| 140 | 148 | $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 141 | 149 | /* for hook */ |
| @@ -143,9 +151,9 @@ | ||
| 143 | 151 | /* for mail */ |
| 144 | 152 | $attach_id = $attachment_id; |
| 145 | 153 | } else { |
| 146 | 154 | $post = get_post( $attachment_id ); |
| 147 | - $title = get_the_title( $post ); | |
| 155 | + $title = $post->post_title; | |
| 148 | 156 | $attachment = array( |
| 149 | 157 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 150 | 158 | 'post_mime_type' => 'image/webp', |
| 151 | 159 | 'post_title' => $title, |
| @@ -175,137 +183,23 @@ | ||
| 175 | 183 | ); |
| 176 | 184 | wp_update_post( $up_post ); |
| 177 | 185 | } |
| 178 | 186 | |
| 179 | - $ids = get_option( 'pluswebp_generate' ); | |
| 180 | - $ids[] = $attach_id; | |
| 181 | - update_option( 'pluswebp_generate', $ids ); | |
| 187 | + update_option( 'pluswebp_generate', $attach_id ); | |
| 182 | 188 | |
| 183 | - } | |
| 184 | - } | |
| 189 | + /* for Media Library folders term by Organize Media Folder */ | |
| 190 | + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id ); | |
| 185 | 191 | |
| 186 | - return $metadata; | |
| 192 | + /* for Term filter update by Organize Media Folder */ | |
| 193 | + do_action( 'omf_term_filter_update' ); | |
| 187 | 194 | |
| 188 | - } | |
| 189 | - | |
| 190 | - /** ================================================== | |
| 191 | - * IDs Callback | |
| 192 | - * | |
| 193 | - * @since 2.00 | |
| 194 | - */ | |
| 195 | - public function bulkgeneratewebp_update_callback() { | |
| 196 | - | |
| 197 | - $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 198 | - if ( check_ajax_referer( $action1, 'nonce', false ) ) { | |
| 199 | - if ( current_user_can( 'manage_options' ) ) { | |
| 200 | - if ( ! empty( $_POST['id'] ) ) { | |
| 201 | - $id = absint( $_POST['id'] ); | |
| 202 | - } else { | |
| 203 | - return; | |
| 204 | - } | |
| 205 | - if ( ! wp_get_attachment_url( $id ) ) { | |
| 206 | - $error_string = __( 'No media!', 'plus-webp' ); | |
| 207 | - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; | |
| 208 | - } else { | |
| 209 | - $output_html = $this->output_html( $id ); | |
| 210 | - } | |
| 211 | - if ( ! empty( $output_html ) ) { | |
| 212 | - header( 'Content-type: text/html; charset=UTF-8' ); | |
| 213 | - $allowed_output_html = array( | |
| 214 | - 'a' => array( | |
| 215 | - 'href' => array(), | |
| 216 | - 'target' => array(), | |
| 217 | - 'rel' => array(), | |
| 218 | - 'style' => array(), | |
| 219 | - ), | |
| 220 | - 'img' => array( | |
| 221 | - 'src' => array(), | |
| 222 | - 'width' => array(), | |
| 223 | - 'height' => array(), | |
| 224 | - 'style' => array(), | |
| 225 | - ), | |
| 226 | - 'div' => array( | |
| 227 | - 'style' => array(), | |
| 228 | - 'class' => array(), | |
| 229 | - ), | |
| 230 | - 'font' => array( | |
| 231 | - 'color' => array(), | |
| 232 | - ), | |
| 233 | - 'ul' => array(), | |
| 234 | - 'li' => array(), | |
| 235 | - 'span' => array( | |
| 236 | - 'class' => array(), | |
| 237 | - 'style' => array(), | |
| 238 | - ), | |
| 239 | - ); | |
| 240 | - echo wp_kses( $output_html, $allowed_output_html ); | |
| 241 | - } | |
| 242 | 195 | } |
| 243 | - } else { | |
| 244 | - status_header( '403' ); | |
| 245 | - echo 'Forbidden'; | |
| 246 | 196 | } |
| 247 | 197 | |
| 248 | - wp_die(); | |
| 249 | - | |
| 198 | + return $metadata; | |
| 250 | 199 | } |
| 251 | 200 | |
| 252 | 201 | /** ================================================== |
| 253 | - * Output html for Ajax | |
| 254 | - * | |
| 255 | - * @param array $attach_id attach_id. | |
| 256 | - * @since 2.00 | |
| 257 | - */ | |
| 258 | - private function output_html( $attach_id ) { | |
| 259 | - | |
| 260 | - include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 261 | - | |
| 262 | - /* Generate thumbnails */ | |
| 263 | - $metadata_org = wp_get_attachment_metadata( $attach_id ); | |
| 264 | - do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ); | |
| 265 | - if ( get_option( 'pluswebp_generate' ) ) { | |
| 266 | - $ids = get_option( 'pluswebp_generate' ); | |
| 267 | - delete_option( 'pluswebp_generate' ); | |
| 268 | - $webp_id = $ids[0]; | |
| 269 | - $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 270 | - | |
| 271 | - /* Thumbnail urls */ | |
| 272 | - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); | |
| 273 | - /* Output datas*/ | |
| 274 | - list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata ); | |
| 275 | - | |
| 276 | - $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">'; | |
| 277 | - $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">'; | |
| 278 | - $output_html .= '<div style="overflow: hidden;">'; | |
| 279 | - $output_html .= '<div>ID: ' . $webp_id . '</div>'; | |
| 280 | - $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>'; | |
| 281 | - $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>'; | |
| 282 | - $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>'; | |
| 283 | - $output_html .= '<div>' . __( 'File name:' ) . ' ' . $filename . '</div>'; | |
| 284 | - if ( ! empty( $original_image_url ) ) { | |
| 285 | - $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>'; | |
| 286 | - $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . $original_filename . '</div>'; | |
| 287 | - } | |
| 288 | - $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>'; | |
| 289 | - $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>'; | |
| 290 | - $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>'; | |
| 291 | - if ( ! empty( $imagethumburls ) ) { | |
| 292 | - $output_html .= '<div>' . __( 'Images' ) . ': '; | |
| 293 | - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 294 | - $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]'; | |
| 295 | - } | |
| 296 | - $output_html .= '</div>'; | |
| 297 | - } | |
| 298 | - $output_html .= '</div></div>'; | |
| 299 | - } else { | |
| 300 | - $output_html = null; | |
| 301 | - } | |
| 302 | - | |
| 303 | - return $output_html; | |
| 304 | - | |
| 305 | - } | |
| 306 | - | |
| 307 | - /** ================================================== | |
| 308 | 202 | * Thumbnail urls |
| 309 | 203 | * |
| 310 | 204 | * @param int $attach_id attach_id. |
| 311 | 205 | * @param array $metadata metadata. |
| @@ -330,9 +224,8 @@ | ||
| 330 | 224 | } |
| 331 | 225 | } |
| 332 | 226 | |
| 333 | 227 | return array( $image_thumbnail, $imagethumburls ); |
| 334 | - | |
| 335 | 228 | } |
| 336 | 229 | |
| 337 | 230 | /** ================================================== |
| 338 | 231 | * Output datas |
| @@ -368,134 +261,120 @@ | ||
| 368 | 261 | $file_size = size_format( $file_size ); |
| 369 | 262 | } |
| 370 | 263 | |
| 371 | 264 | return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); |
| 372 | - | |
| 373 | 265 | } |
| 374 | 266 | |
| 375 | 267 | /** ================================================== |
| 376 | - * Messages Callback | |
| 268 | + * Get All Images | |
| 377 | 269 | * |
| 270 | + * @param array $types mime types. | |
| 378 | 271 | * @since 2.00 |
| 379 | 272 | */ |
| 380 | - public function bulkgeneratewebp_message_callback() { | |
| 273 | + public function get_allimages( $types ) { | |
| 381 | 274 | |
| 382 | - $action2 = 'bulkgeneratewebp_message'; | |
| 383 | - if ( check_ajax_referer( $action2, 'nonce', false ) ) { | |
| 384 | - $error_count = 0; | |
| 385 | - $error_update = null; | |
| 386 | - $success_count = 0; | |
| 387 | - if ( ! empty( $_POST['error_count'] ) ) { | |
| 388 | - $error_count = absint( $_POST['error_count'] ); | |
| 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 ); | |
| 284 | + | |
| 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 | + ); | |
| 295 | + | |
| 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 | + } | |
| 389 | 306 | } |
| 390 | - if ( ! empty( $_POST['error_update'] ) ) { | |
| 391 | - $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) ); | |
| 392 | - } | |
| 393 | - if ( ! empty( $_POST['success_count'] ) ) { | |
| 394 | - $success_count = absint( $_POST['success_count'] ); | |
| 395 | - } | |
| 307 | + } | |
| 396 | 308 | |
| 397 | - $back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>'; | |
| 309 | + return array( $post_ids, $no_file_ids ); | |
| 310 | + } | |
| 398 | 311 | |
| 399 | - $output_html = null; | |
| 400 | - if ( $error_count > 0 ) { | |
| 401 | - /* translators: error message %1$d: media count %2$s: back link */ | |
| 402 | - $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html ); | |
| 403 | - $output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>'; | |
| 312 | + /** ================================================== | |
| 313 | + * Mail messages hook | |
| 314 | + * | |
| 315 | + * @param array $messages messages. | |
| 316 | + * @param int $webp_id webp_id. | |
| 317 | + * | |
| 318 | + * @since 4.00 | |
| 319 | + */ | |
| 320 | + public function mail_messages( $messages, $webp_id ) { | |
| 321 | + | |
| 322 | + $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 323 | + | |
| 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 ); | |
| 330 | + | |
| 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"; | |
| 404 | 339 | } |
| 405 | - if ( $success_count > 0 ) { | |
| 406 | - /* translators: success message %1$d: media count %2$s: back link */ | |
| 407 | - $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html ); | |
| 408 | - $output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>'; | |
| 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"; | |
| 345 | + } | |
| 409 | 346 | } |
| 410 | - | |
| 411 | - header( 'Content-type: text/html; charset=UTF-8' ); | |
| 412 | - $allowed_output_html = array( | |
| 413 | - 'div' => array( | |
| 414 | - 'class' => array(), | |
| 415 | - ), | |
| 416 | - 'ul' => array(), | |
| 417 | - 'li' => array(), | |
| 418 | - 'strong' => array(), | |
| 419 | - 'a' => array( | |
| 420 | - 'style' => array(), | |
| 421 | - 'href' => array(), | |
| 422 | - ), | |
| 423 | - ); | |
| 424 | - echo wp_kses( $output_html, $allowed_output_html ); | |
| 347 | + $message .= "\n"; | |
| 348 | + $messages[] = $message; | |
| 425 | 349 | } |
| 426 | 350 | |
| 427 | - wp_die(); | |
| 428 | - | |
| 351 | + return array( $message, $messages ); | |
| 429 | 352 | } |
| 430 | 353 | |
| 431 | 354 | /** ================================================== |
| 432 | - * Mail send hook | |
| 355 | + * Mail sent for Generate Messages | |
| 433 | 356 | * |
| 434 | - * @param string $to to. | |
| 435 | - * @since 1.09 | |
| 357 | + * @param array $messages mail messages. | |
| 358 | + * @param int $count convert count. | |
| 359 | + * @since 4.02 | |
| 436 | 360 | */ |
| 437 | - public function mail_send( $to ) { | |
| 361 | + public function mail_generate_message( $messages, $count ) { | |
| 438 | 362 | |
| 439 | - $post_ids = get_option( 'pluswebp_generate' ); | |
| 440 | - delete_option( 'pluswebp_generate' ); | |
| 441 | - | |
| 442 | 363 | if ( function_exists( 'wp_date' ) ) { |
| 443 | 364 | $now_date_time = wp_date( 'Y-m-d H:i:s' ); |
| 444 | 365 | } else { |
| 445 | 366 | $now_date_time = date_i18n( 'Y-m-d H:i:s' ); |
| 446 | 367 | } |
| 447 | - $pluswebp_mail_send = array(); | |
| 448 | - $pluswebp_mail_send['datetime'] = $now_date_time; | |
| 449 | - | |
| 450 | 368 | /* translators: Date and Time */ |
| 451 | - $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 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"; | |
| 452 | 372 | |
| 453 | - if ( ! empty( $post_ids ) ) { | |
| 454 | - | |
| 455 | - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; | |
| 456 | - $pluswebp_mail_send['count'] = count( $post_ids ); | |
| 457 | - | |
| 458 | - $count = 0; | |
| 459 | - foreach ( $post_ids as $attach_id ) { | |
| 460 | - $metadata = wp_get_attachment_metadata( $attach_id ); | |
| 461 | - | |
| 462 | - /* Thumbnail urls */ | |
| 463 | - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $attach_id, $metadata, $this->upload_url ); | |
| 464 | - /* Output datas*/ | |
| 465 | - list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $attach_id, $metadata ); | |
| 466 | - | |
| 467 | - $count++; | |
| 468 | - $message .= __( 'Count' ) . ': ' . $count . "\n"; | |
| 469 | - $message .= 'ID: ' . $attach_id . "\n"; | |
| 470 | - $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n"; | |
| 471 | - $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n"; | |
| 472 | - $message .= 'URL: ' . $attachment_url . "\n"; | |
| 473 | - $message .= __( 'File name:' ) . ' ' . $filename . "\n"; | |
| 474 | - if ( ! empty( $original_image_url ) ) { | |
| 475 | - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n"; | |
| 476 | - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n"; | |
| 477 | - } | |
| 478 | - $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; | |
| 479 | - $message .= __( 'File size:' ) . ' ' . $file_size . "\n"; | |
| 480 | - if ( ! empty( $imagethumburls ) ) { | |
| 481 | - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 482 | - $message .= $thumbsize . ': ' . $imagethumburl . "\n"; | |
| 483 | - } | |
| 484 | - } | |
| 485 | - $message .= "\n"; | |
| 486 | - } | |
| 487 | - } else { | |
| 488 | - $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n"; | |
| 489 | - $pluswebp_mail_send['count'] = 0; | |
| 490 | - } | |
| 491 | - | |
| 492 | - update_option( 'pluswebp_generate_mail', $pluswebp_mail_send ); | |
| 493 | - | |
| 373 | + $to = get_option( 'admin_email' ); | |
| 494 | 374 | /* translators: blogname for subject */ |
| 495 | 375 | $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); |
| 496 | - wp_mail( $to, $subject, $message ); | |
| 497 | - | |
| 376 | + wp_mail( $to, $subject, $message_head . implode( $messages ) ); | |
| 498 | 377 | } |
| 499 | 378 | |
| 500 | 379 | /** ================================================== |
| 501 | 380 | * Webp create |
| @@ -516,9 +395,9 @@ | ||
| 516 | 395 | } |
| 517 | 396 | |
| 518 | 397 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 519 | 398 | @set_time_limit( 60 ); |
| 520 | - @ini_set( 'memory_limit', '256M' ); | |
| 399 | + wp_raise_memory_limit( 'pluswebp' ); | |
| 521 | 400 | |
| 522 | 401 | $ret = false; |
| 523 | 402 | switch ( $mime_type ) { |
| 524 | 403 | case 'image/jpeg': |
| @@ -532,8 +411,11 @@ | ||
| 532 | 411 | $src = imagecreatefrompng( $filename ); |
| 533 | 412 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 534 | 413 | imagealphablending( $img, false ); |
| 535 | 414 | imagesavealpha( $img, true ); |
| 415 | + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); | |
| 416 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 417 | + imagecolortransparent( $img, $bgcolor ); | |
| 536 | 418 | break; |
| 537 | 419 | case 'image/bmp': |
| 538 | 420 | $src = imagecreatefrombmp( $filename ); |
| 539 | 421 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| @@ -552,9 +434,8 @@ | ||
| 552 | 434 | $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] ); |
| 553 | 435 | imagedestroy( $img ); |
| 554 | 436 | |
| 555 | 437 | return $ret; |
| 556 | - | |
| 557 | 438 | } |
| 558 | 439 | |
| 559 | 440 | /** ================================================== |
| 560 | 441 | * Change ext |
| @@ -576,9 +457,8 @@ | ||
| 576 | 457 | $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name ); |
| 577 | 458 | } |
| 578 | 459 | |
| 579 | 460 | return $after_file_name; |
| 580 | - | |
| 581 | 461 | } |
| 582 | 462 | |
| 583 | 463 | /** ================================================== |
| 584 | 464 | * Change DB |
| @@ -602,8 +482,10 @@ | ||
| 602 | 482 | $after_url |
| 603 | 483 | ) |
| 604 | 484 | ); |
| 605 | 485 | |
| 486 | + /* Advanced change database */ | |
| 487 | + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url ); | |
| 606 | 488 | } |
| 607 | 489 | |
| 608 | 490 | /** ================================================== |
| 609 | 491 | * Upload Path |
| @@ -640,9 +522,8 @@ | ||
| 640 | 522 | $upload_url = untrailingslashit( $upload_url ); |
| 641 | 523 | $upload_path = untrailingslashit( $upload_path ); |
| 642 | 524 | |
| 643 | 525 | return array( $upload_dir, $upload_url, $upload_path ); |
| 644 | - | |
| 645 | 526 | } |
| 646 | 527 | |
| 647 | 528 | /** ================================================== |
| 648 | 529 | * Real Url |
| @@ -691,10 +572,34 @@ | ||
| 691 | 572 | } |
| 692 | 573 | $path = implode( '/', $base_path ); |
| 693 | 574 | return $parse['scheme'] . '://' . $parse['host'] . $path; |
| 694 | 575 | } |
| 576 | + } | |
| 695 | 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'; | |
| 696 | 588 | } |
| 697 | 589 | |
| 698 | -} | |
| 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 ) { | |
| 699 | 600 | |
| 601 | + $image_mime_transforms = array(); | |
| 700 | 602 | |
| 603 | + return $image_mime_transforms; | |
| 604 | + } | |
| 605 | +} | |