PluginProbe
Plus WebP or AVIF / 4.11
Plus WebP or AVIF v4.11
5.21 5.20 5.12 5.11 trunk 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 2.00 2.01 2.02 2.03 2.04 2.05 All 47 releases
← All changes | lib/class-pluswebp.php +172 -254 2.034.11 View file →
@@ -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,32 +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 - 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 }
112 - if ( ! empty( $metadata['original_image'] ) ) {
113 - $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 ) );
114 121 $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
115 122 $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
116 123 if ( $ret ) {
117 124 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
@@ -117,9 +124,11 @@
117 124 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
118 125 }
119 126 }
120 127
121 - $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;
122 131 if ( $ret ) {
123 132 if ( $replace ) {
124 133 $up_post = array(
125 134 'ID' => $attachment_id,
@@ -130,9 +139,12 @@
130 139 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
131 140 /* for bulk generate */
132 141 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
133 142 /* delete org file */
134 - 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 + }
135 147 /* Replace */
136 148 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
137 149 /* for hook */
138 150 $metadata = $metadata_webp;
@@ -139,9 +151,9 @@
139 151 /* for mail */
140 152 $attach_id = $attachment_id;
141 153 } else {
142 154 $post = get_post( $attachment_id );
143 - $title = get_the_title( $post );
155 + $title = $post->post_title;
144 156 $attachment = array(
145 157 'guid' => $this->upload_url . '/' . $file_webp,
146 158 'post_mime_type' => 'image/webp',
147 159 'post_title' => $title,
@@ -147,9 +159,15 @@
147 159 'post_title' => $title,
148 160 'post_content' => '',
149 161 'post_status' => 'inherit',
150 162 );
151 - $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 +
152 170 wp_update_attachment_metadata( $attach_id, $metadata_webp );
153 171
154 172 $author = get_userdata( $post->post_author );
155 173 $userid = $author->ID;
@@ -165,137 +183,23 @@
165 183 );
166 184 wp_update_post( $up_post );
167 185 }
168 186
169 - $ids = get_option( 'pluswebp_generate' );
170 - $ids[] = $attach_id;
171 - update_option( 'pluswebp_generate', $ids );
187 + update_option( 'pluswebp_generate', $attach_id );
172 188
173 - }
174 - }
189 + /* for Media Library folders term by Organize Media Folder */
190 + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id );
175 191
176 - return $metadata;
192 + /* for Term filter update by Organize Media Folder */
193 + do_action( 'omf_term_filter_update' );
177 194
178 - }
179 -
180 - /** ==================================================
181 - * IDs Callback
182 - *
183 - * @since 2.00
184 - */
185 - public function bulkgeneratewebp_update_callback() {
186 -
187 - $action1 = 'bulkgeneratewebp-ajax-action';
188 - if ( check_ajax_referer( $action1, 'nonce', false ) ) {
189 - if ( current_user_can( 'manage_options' ) ) {
190 - if ( ! empty( $_POST['id'] ) ) {
191 - $id = absint( $_POST['id'] );
192 - } else {
193 - return;
194 - }
195 - if ( ! wp_get_attachment_url( $id ) ) {
196 - $error_string = __( 'No media!', 'plus-webp' );
197 - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>';
198 - } else {
199 - $output_html = $this->output_html( $id );
200 - }
201 - if ( ! empty( $output_html ) ) {
202 - header( 'Content-type: text/html; charset=UTF-8' );
203 - $allowed_output_html = array(
204 - 'a' => array(
205 - 'href' => array(),
206 - 'target' => array(),
207 - 'rel' => array(),
208 - 'style' => array(),
209 - ),
210 - 'img' => array(
211 - 'src' => array(),
212 - 'width' => array(),
213 - 'height' => array(),
214 - 'style' => array(),
215 - ),
216 - 'div' => array(
217 - 'style' => array(),
218 - 'class' => array(),
219 - ),
220 - 'font' => array(
221 - 'color' => array(),
222 - ),
223 - 'ul' => array(),
224 - 'li' => array(),
225 - 'span' => array(
226 - 'class' => array(),
227 - 'style' => array(),
228 - ),
229 - );
230 - echo wp_kses( $output_html, $allowed_output_html );
231 - }
232 195 }
233 - } else {
234 - status_header( '403' );
235 - echo 'Forbidden';
236 196 }
237 197
238 - wp_die();
239 -
198 + return $metadata;
240 199 }
241 200
242 201 /** ==================================================
243 - * Output html for Ajax
244 - *
245 - * @param array $attach_id attach_id.
246 - * @since 2.00
247 - */
248 - private function output_html( $attach_id ) {
249 -
250 - include_once ABSPATH . 'wp-admin/includes/image.php';
251 -
252 - /* Generate thumbnails */
253 - $metadata_org = wp_get_attachment_metadata( $attach_id );
254 - do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
255 - if ( get_option( 'pluswebp_generate' ) ) {
256 - $ids = get_option( 'pluswebp_generate' );
257 - delete_option( 'pluswebp_generate' );
258 - $webp_id = $ids[0];
259 - $metadata = wp_get_attachment_metadata( $webp_id );
260 -
261 - /* Thumbnail urls */
262 - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url );
263 - /* Output datas*/
264 - list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata );
265 -
266 - $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">';
267 - $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">';
268 - $output_html .= '<div style="overflow: hidden;">';
269 - $output_html .= '<div>ID: ' . $webp_id . '</div>';
270 - $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>';
271 - $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>';
272 - $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>';
273 - $output_html .= '<div>' . __( 'File name:' ) . ' ' . $filename . '</div>';
274 - if ( ! empty( $original_image_url ) ) {
275 - $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>';
276 - $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . $original_filename . '</div>';
277 - }
278 - $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>';
279 - $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>';
280 - $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>';
281 - if ( ! empty( $imagethumburls ) ) {
282 - $output_html .= '<div>' . __( 'Images' ) . ': ';
283 - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
284 - $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]';
285 - }
286 - $output_html .= '</div>';
287 - }
288 - $output_html .= '</div></div>';
289 - } else {
290 - $output_html = null;
291 - }
292 -
293 - return $output_html;
294 -
295 - }
296 -
297 - /** ==================================================
298 202 * Thumbnail urls
299 203 *
300 204 * @param int $attach_id attach_id.
301 205 * @param array $metadata metadata.
@@ -310,13 +214,12 @@
310 214
311 215 $imagethumburls = array();
312 216 if ( ! empty( $metadata ) && array_key_exists( 'sizes', $metadata ) ) {
313 217 $thumbnails = $metadata['sizes'];
314 - $media_path = get_post_meta( $attach_id, '_wp_attached_file', true );
315 - $media_paths = explode( DIRECTORY_SEPARATOR, $media_path );
316 - $str_length = strlen( end( $media_paths ) );
317 - $media_path = substr( $media_path, 0, -$str_length );
318 - $media_url = $upload_url . '/' . $media_path;
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;
319 222 foreach ( $thumbnails as $key => $key2 ) {
320 223 $imagethumburls[ $key ] = $media_url . $key2['file'];
321 224 }
322 225 }
@@ -321,9 +224,8 @@
321 224 }
322 225 }
323 226
324 227 return array( $image_thumbnail, $imagethumburls );
325 -
326 228 }
327 229
328 230 /** ==================================================
329 231 * Output datas
@@ -344,10 +246,9 @@
344 246 } else {
345 247 $original_image_url = null;
346 248 $original_filename = null;
347 249 }
348 - $filetype = wp_check_filetype( get_attached_file( $attach_id ) );
349 - $mime_type = $filetype['type'];
250 + $mime_type = get_post_mime_type( $attach_id );
350 251
351 252 $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
352 253 if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) {
353 254 $file_size = $metadata['filesize'];
@@ -360,134 +261,120 @@
360 261 $file_size = size_format( $file_size );
361 262 }
362 263
363 264 return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size );
364 -
365 265 }
366 266
367 267 /** ==================================================
368 - * Messages Callback
268 + * Get All Images
369 269 *
270 + * @param array $types mime types.
370 271 * @since 2.00
371 272 */
372 - public function bulkgeneratewebp_message_callback() {
273 + public function get_allimages( $types ) {
373 274
374 - $action2 = 'bulkgeneratewebp_message';
375 - if ( check_ajax_referer( $action2, 'nonce', false ) ) {
376 - $error_count = 0;
377 - $error_update = null;
378 - $success_count = 0;
379 - if ( ! empty( $_POST['error_count'] ) ) {
380 - $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 + }
381 306 }
382 - if ( ! empty( $_POST['error_update'] ) ) {
383 - $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) );
384 - }
385 - if ( ! empty( $_POST['success_count'] ) ) {
386 - $success_count = absint( $_POST['success_count'] );
387 - }
307 + }
388 308
389 - $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 + }
390 311
391 - $output_html = null;
392 - if ( $error_count > 0 ) {
393 - /* translators: error message %1$d: media count %2$s: back link */
394 - $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html );
395 - $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";
396 339 }
397 - if ( $success_count > 0 ) {
398 - /* translators: success message %1$d: media count %2$s: back link */
399 - $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html );
400 - $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 + }
401 346 }
402 -
403 - header( 'Content-type: text/html; charset=UTF-8' );
404 - $allowed_output_html = array(
405 - 'div' => array(
406 - 'class' => array(),
407 - ),
408 - 'ul' => array(),
409 - 'li' => array(),
410 - 'strong' => array(),
411 - 'a' => array(
412 - 'style' => array(),
413 - 'href' => array(),
414 - ),
415 - );
416 - echo wp_kses( $output_html, $allowed_output_html );
347 + $message .= "\n";
348 + $messages[] = $message;
417 349 }
418 350
419 - wp_die();
420 -
351 + return array( $message, $messages );
421 352 }
422 353
423 354 /** ==================================================
424 - * Mail send hook
355 + * Mail sent for Generate Messages
425 356 *
426 - * @param string $to to.
427 - * @since 1.09
357 + * @param array $messages mail messages.
358 + * @param int $count convert count.
359 + * @since 4.02
428 360 */
429 - public function mail_send( $to ) {
361 + public function mail_generate_message( $messages, $count ) {
430 362
431 - $post_ids = get_option( 'pluswebp_generate' );
432 - delete_option( 'pluswebp_generate' );
433 -
434 363 if ( function_exists( 'wp_date' ) ) {
435 364 $now_date_time = wp_date( 'Y-m-d H:i:s' );
436 365 } else {
437 366 $now_date_time = date_i18n( 'Y-m-d H:i:s' );
438 367 }
439 - $pluswebp_mail_send = array();
440 - $pluswebp_mail_send['datetime'] = $now_date_time;
441 -
442 368 /* translators: Date and Time */
443 - $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";
444 372
445 - if ( ! empty( $post_ids ) ) {
446 -
447 - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
448 - $pluswebp_mail_send['count'] = count( $post_ids );
449 -
450 - $count = 0;
451 - foreach ( $post_ids as $attach_id ) {
452 - $metadata = wp_get_attachment_metadata( $attach_id );
453 -
454 - /* Thumbnail urls */
455 - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $attach_id, $metadata, $this->upload_url );
456 - /* Output datas*/
457 - list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $attach_id, $metadata );
458 -
459 - $count++;
460 - $message .= __( 'Count' ) . ': ' . $count . "\n";
461 - $message .= 'ID: ' . $attach_id . "\n";
462 - $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n";
463 - $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n";
464 - $message .= 'URL: ' . $attachment_url . "\n";
465 - $message .= __( 'File name:' ) . ' ' . $filename . "\n";
466 - if ( ! empty( $original_image_url ) ) {
467 - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n";
468 - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n";
469 - }
470 - $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
471 - $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
472 - if ( ! empty( $imagethumburls ) ) {
473 - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
474 - $message .= $thumbsize . ': ' . $imagethumburl . "\n";
475 - }
476 - }
477 - $message .= "\n";
478 - }
479 - } else {
480 - $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n";
481 - $pluswebp_mail_send['count'] = 0;
482 - }
483 -
484 - update_option( 'pluswebp_generate_mail', $pluswebp_mail_send );
485 -
373 + $to = get_option( 'admin_email' );
486 374 /* translators: blogname for subject */
487 375 $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
488 - wp_mail( $to, $subject, $message );
489 -
376 + wp_mail( $to, $subject, $message_head . implode( $messages ) );
490 377 }
491 378
492 379 /** ==================================================
493 380 * Webp create
@@ -508,8 +395,9 @@
508 395 }
509 396
510 397 $pluswebp_settings = get_option( 'pluswebp' );
511 398 @set_time_limit( 60 );
399 + wp_raise_memory_limit( 'pluswebp' );
512 400
513 401 $ret = false;
514 402 switch ( $mime_type ) {
515 403 case 'image/jpeg':
@@ -514,9 +402,10 @@
514 402 switch ( $mime_type ) {
515 403 case 'image/jpeg':
516 404 $src = imagecreatefromjpeg( $filename );
517 405 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
518 - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
406 + $bgcolor = imagecolorallocate( $img, 255, 255, 255 );
407 + imagefill( $img, 0, 0, $bgcolor );
519 408 imagealphablending( $img, true );
520 409 break;
521 410 case 'image/png':
522 411 $src = imagecreatefrompng( $filename );
@@ -522,8 +411,11 @@
522 411 $src = imagecreatefrompng( $filename );
523 412 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
524 413 imagealphablending( $img, false );
525 414 imagesavealpha( $img, true );
415 + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
416 + imagefill( $img, 0, 0, $bgcolor );
417 + imagecolortransparent( $img, $bgcolor );
526 418 break;
527 419 case 'image/bmp':
528 420 $src = imagecreatefrombmp( $filename );
529 421 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
@@ -542,9 +434,8 @@
542 434 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
543 435 imagedestroy( $img );
544 436
545 437 return $ret;
546 -
547 438 }
548 439
549 440 /** ==================================================
550 441 * Change ext
@@ -566,9 +457,8 @@
566 457 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
567 458 }
568 459
569 460 return $after_file_name;
570 -
571 461 }
572 462
573 463 /** ==================================================
574 464 * Change DB
@@ -583,14 +473,19 @@
583 473
584 474 /* Replace */
585 475 $wpdb->query(
586 476 $wpdb->prepare(
587 - "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 + ",
588 481 $before_url,
589 482 $after_url
590 483 )
591 484 );
592 485
486 + /* Advanced change database */
487 + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
593 488 }
594 489
595 490 /** ==================================================
596 491 * Upload Path
@@ -627,9 +522,8 @@
627 522 $upload_url = untrailingslashit( $upload_url );
628 523 $upload_path = untrailingslashit( $upload_path );
629 524
630 525 return array( $upload_dir, $upload_url, $upload_path );
631 -
632 526 }
633 527
634 528 /** ==================================================
635 529 * Real Url
@@ -678,10 +572,34 @@
678 572 }
679 573 $path = implode( '/', $base_path );
680 574 return $parse['scheme'] . '://' . $parse['host'] . $path;
681 575 }
576 + }
682 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';
683 588 }
684 589
685 -}
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 ) {
686 600
601 + $image_mime_transforms = array();
687 602
603 + return $image_mime_transforms;
604 + }
605 +}