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 +233 -92 1.134.11 View file →
@@ -57,11 +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
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 );
64 72 }
65 73
66 74 /** ==================================================
67 75 * Webp generate
@@ -80,25 +88,37 @@
80 88 if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) {
81 89 $metadata_webp = $metadata;
82 90 $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] );
83 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 +
84 102 foreach ( (array) $metadata['sizes'] as $key => $value ) {
85 103 $file_thumb = $value['file'];
86 104 $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] );
87 - $path = $this->upload_dir . '/' . dirname( $file_webp ) . '/';
88 - $url = $this->upload_url . '/' . dirname( $file_webp ) . '/';
89 - $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 );
90 106 if ( $ret ) {
91 107 $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
92 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;
93 111 if ( $replace ) {
94 - unlink( $path . $file_thumb );
112 + wp_delete_file( $path . $file_thumb );
95 113 $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
96 114 }
97 115 }
98 116 }
99 - if ( ! empty( $metadata['original_image'] ) ) {
100 - $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 ) );
101 121 $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
102 122 $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
103 123 if ( $ret ) {
104 124 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
@@ -104,9 +124,11 @@
104 124 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
105 125 }
106 126 }
107 127
108 - $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;
109 131 if ( $ret ) {
110 132 if ( $replace ) {
111 133 $up_post = array(
112 134 'ID' => $attachment_id,
@@ -117,9 +139,12 @@
117 139 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
118 140 /* for bulk generate */
119 141 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
120 142 /* delete org file */
121 - 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 + }
122 147 /* Replace */
123 148 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
124 149 /* for hook */
125 150 $metadata = $metadata_webp;
@@ -126,9 +151,9 @@
126 151 /* for mail */
127 152 $attach_id = $attachment_id;
128 153 } else {
129 154 $post = get_post( $attachment_id );
130 - $title = get_the_title( $post );
155 + $title = $post->post_title;
131 156 $attachment = array(
132 157 'guid' => $this->upload_url . '/' . $file_webp,
133 158 'post_mime_type' => 'image/webp',
134 159 'post_title' => $title,
@@ -134,9 +159,15 @@
134 159 'post_title' => $title,
135 160 'post_content' => '',
136 161 'post_status' => 'inherit',
137 162 );
138 - $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 +
139 170 wp_update_attachment_metadata( $attach_id, $metadata_webp );
140 171
141 172 $author = get_userdata( $post->post_author );
142 173 $userid = $author->ID;
@@ -152,119 +183,198 @@
152 183 );
153 184 wp_update_post( $up_post );
154 185 }
155 186
156 - $ids = get_option( 'pluswebp_generate' );
157 - $ids[] = $attach_id;
158 - update_option( 'pluswebp_generate', $ids );
187 + update_option( 'pluswebp_generate', $attach_id );
159 188
189 + /* for Media Library folders term by Organize Media Folder */
190 + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id );
191 +
192 + /* for Term filter update by Organize Media Folder */
193 + do_action( 'omf_term_filter_update' );
194 +
160 195 }
161 196 }
162 197
163 198 return $metadata;
199 + }
164 200
201 + /** ==================================================
202 + * Thumbnail urls
203 + *
204 + * @param int $attach_id attach_id.
205 + * @param array $metadata metadata.
206 + * @param string $upload_url upload_url.
207 + * @return array $image_thumbnail(string), $imagethumburls(array)
208 + * @since 2.00
209 + */
210 + private function thumbnail_urls( $attach_id, $metadata, $upload_url ) {
211 +
212 + $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true );
213 + $image_thumbnail = $image_attr_thumbnail[0];
214 +
215 + $imagethumburls = array();
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;
222 + foreach ( $thumbnails as $key => $key2 ) {
223 + $imagethumburls[ $key ] = $media_url . $key2['file'];
224 + }
225 + }
226 +
227 + return array( $image_thumbnail, $imagethumburls );
165 228 }
166 229
167 230 /** ==================================================
168 - * Mail send hook
231 + * Output datas
169 232 *
170 - * @param string $to to.
171 - * @since 1.09
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
236 + * @since 2.00
172 237 */
173 - public function mail_send( $to ) {
238 + private function output_datas( $attach_id, $metadata ) {
174 239
175 - $post_ids = get_option( 'pluswebp_generate' );
176 - delete_option( 'pluswebp_generate' );
240 + $attachment_link = get_attachment_link( $attach_id );
241 + $attachment_url = wp_get_attachment_url( $attach_id );
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 );
177 251
178 - if ( function_exists( 'wp_date' ) ) {
179 - $now_date_time = wp_date( 'Y-m-d H:i:s' );
252 + $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
253 + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) {
254 + $file_size = $metadata['filesize'];
180 255 } else {
181 - $now_date_time = date_i18n( 'Y-m-d H:i:s' );
256 + $file_size = @filesize( get_attached_file( $attach_id ) );
182 257 }
183 - $pluswebp_mail_send = array();
184 - $pluswebp_mail_send['datetime'] = $now_date_time;
258 + if ( ! $file_size ) {
259 + $file_size = __( 'Could not retrieve.', 'plus-webp' );
260 + } else {
261 + $file_size = size_format( $file_size );
262 + }
185 263
186 - /* translators: Date and Time */
187 - $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n";
264 + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size );
265 + }
188 266
189 - if ( ! empty( $post_ids ) ) {
267 + /** ==================================================
268 + * Get All Images
269 + *
270 + * @param array $types mime types.
271 + * @since 2.00
272 + */
273 + public function get_allimages( $types ) {
190 274
191 - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
192 - $pluswebp_mail_send['count'] = count( $post_ids );
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 );
193 284
194 - $count = 0;
195 - foreach ( $post_ids as $attach_id ) {
196 - $filename = get_attached_file( $attach_id );
197 - $metadata = wp_get_attachment_metadata( $attach_id );
198 - /* OutputMetaData */
199 - list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url );
200 - $count++;
201 - $message .= __( 'Count' ) . ': ' . $count . "\n";
202 - $message .= 'ID: ' . $attach_id . "\n";
203 - $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n";
204 - $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n";
205 - $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n";
206 - $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n";
207 - if ( ! empty( $metadata['original_image'] ) ) {
208 - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n";
209 - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n";
210 - }
211 - $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
212 - if ( ! $file_size ) {
213 - $file_size = __( 'Could not retrieve.', 'plus-webp' );
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;
214 303 } else {
215 - $file_size = size_format( $file_size );
304 + $no_file_ids[] = $post->ID;
216 305 }
217 - $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
218 - if ( ! empty( $imagethumburls ) ) {
219 - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
220 - $message .= $thumbsize . ': ' . $imagethumburl . "\n";
221 - }
222 - }
223 - $message .= "\n";
224 306 }
225 - } else {
226 - $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n";
227 - $pluswebp_mail_send['count'] = 0;
228 307 }
229 308
230 - update_option( 'pluswebp_generate_mail', $pluswebp_mail_send );
231 -
232 - /* translators: blogname for subject */
233 - $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
234 - wp_mail( $to, $subject, $message );
235 -
309 + return array( $post_ids, $no_file_ids );
236 310 }
237 311
238 312 /** ==================================================
239 - * Output metadata
313 + * Mail messages hook
240 314 *
241 - * @param int $attach_id attach_id.
242 - * @param array $metadata metadata.
243 - * @param string $upload_url upload_url.
244 - * @return array $imagethumburls(string), $stamptime(string), $file_size(string)
245 - * @since 1.09
315 + * @param array $messages messages.
316 + * @param int $webp_id webp_id.
317 + *
318 + * @since 4.00
246 319 */
247 - private function output_metadata( $attach_id, $metadata, $upload_url ) {
320 + public function mail_messages( $messages, $webp_id ) {
248 321
249 - $imagethumburls = array();
250 - $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true );
251 - $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) );
252 - if ( ! empty( $metadata ) ) {
253 - foreach ( $metadata as $key1 => $key2 ) {
254 - if ( 'sizes' === $key1 ) {
255 - foreach ( $metadata[ $key1 ] as $key2 => $key3 ) {
256 - $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file'];
257 - }
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";
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";
258 345 }
259 346 }
347 + $message .= "\n";
348 + $messages[] = $message;
260 349 }
261 350
262 - $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
263 - $file_size = @filesize( get_attached_file( $attach_id, false ) );
351 + return array( $message, $messages );
352 + }
264 353
265 - return array( $imagethumburls, $stamptime, $file_size );
354 + /** ==================================================
355 + * Mail sent for Generate Messages
356 + *
357 + * @param array $messages mail messages.
358 + * @param int $count convert count.
359 + * @since 4.02
360 + */
361 + public function mail_generate_message( $messages, $count ) {
266 362
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' );
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";
372 +
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 ) );
267 377 }
268 378
269 379 /** ==================================================
270 380 * Webp create
@@ -285,8 +395,9 @@
285 395 }
286 396
287 397 $pluswebp_settings = get_option( 'pluswebp' );
288 398 @set_time_limit( 60 );
399 + wp_raise_memory_limit( 'pluswebp' );
289 400
290 401 $ret = false;
291 402 switch ( $mime_type ) {
292 403 case 'image/jpeg':
@@ -291,9 +402,10 @@
291 402 switch ( $mime_type ) {
292 403 case 'image/jpeg':
293 404 $src = imagecreatefromjpeg( $filename );
294 405 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
295 - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
406 + $bgcolor = imagecolorallocate( $img, 255, 255, 255 );
407 + imagefill( $img, 0, 0, $bgcolor );
296 408 imagealphablending( $img, true );
297 409 break;
298 410 case 'image/png':
299 411 $src = imagecreatefrompng( $filename );
@@ -299,8 +411,11 @@
299 411 $src = imagecreatefrompng( $filename );
300 412 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
301 413 imagealphablending( $img, false );
302 414 imagesavealpha( $img, true );
415 + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
416 + imagefill( $img, 0, 0, $bgcolor );
417 + imagecolortransparent( $img, $bgcolor );
303 418 break;
304 419 case 'image/bmp':
305 420 $src = imagecreatefrombmp( $filename );
306 421 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
@@ -319,9 +434,8 @@
319 434 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
320 435 imagedestroy( $img );
321 436
322 437 return $ret;
323 -
324 438 }
325 439
326 440 /** ==================================================
327 441 * Change ext
@@ -343,9 +457,8 @@
343 457 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
344 458 }
345 459
346 460 return $after_file_name;
347 -
348 461 }
349 462
350 463 /** ==================================================
351 464 * Change DB
@@ -360,14 +473,19 @@
360 473
361 474 /* Replace */
362 475 $wpdb->query(
363 476 $wpdb->prepare(
364 - "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 + ",
365 481 $before_url,
366 482 $after_url
367 483 )
368 484 );
369 485
486 + /* Advanced change database */
487 + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
370 488 }
371 489
372 490 /** ==================================================
373 491 * Upload Path
@@ -404,9 +522,8 @@
404 522 $upload_url = untrailingslashit( $upload_url );
405 523 $upload_path = untrailingslashit( $upload_path );
406 524
407 525 return array( $upload_dir, $upload_url, $upload_path );
408 -
409 526 }
410 527
411 528 /** ==================================================
412 529 * Real Url
@@ -455,10 +572,34 @@
455 572 }
456 573 $path = implode( '/', $base_path );
457 574 return $parse['scheme'] . '://' . $parse['host'] . $path;
458 575 }
576 + }
459 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';
460 588 }
461 589
462 -}
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 ) {
463 600
601 + $image_mime_transforms = array();
464 602
603 + return $image_mime_transforms;
604 + }
605 +}