PluginProbe
Plus WebP or AVIF / 5.03
Plus WebP or AVIF v5.03
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 +43 -101 5.215.03 View file →
@@ -18,12 +18,8 @@
18 18 along with this program; if not, write to the Free Software
19 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 20 */
21 21
22 -if ( ! defined( 'ABSPATH' ) ) {
23 - exit;
24 -}
25 -
26 22 $pluswebp = new PlusWebp();
27 23
28 24 /** ==================================================
29 25 * Class Main function
@@ -85,12 +81,8 @@
85 81 * @since 1.00
86 82 */
87 83 public function generate_webp( $metadata, $attachment_id ) {
88 84
89 - if ( ! is_array( $metadata ) ) {
90 - return $metadata;
91 - }
92 -
93 85 $pluswebp_settings = get_option( 'pluswebp' );
94 86
95 87 switch ( $pluswebp_settings['output_mime'] ) {
96 88 case 'image/webp':
@@ -121,16 +113,17 @@
121 113 foreach ( (array) $metadata['sizes'] as $key => $value ) {
122 114 $file_thumb = $value['file'];
123 115 $file_thumb_webp = $this->change_ext( $file_thumb, $ext, $pluswebp_settings['addext'] );
124 116 $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp, $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
125 - /* $ret -> ignore : Some metadata also has duplicate filenames */
126 - $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
127 - $metadata_webp['sizes'][ $key ]['mime-type'] = $pluswebp_settings['output_mime'];
128 - $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) );
129 - $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size;
130 - if ( $pluswebp_settings['replace'] ) {
131 - wp_delete_file( $path . $file_thumb );
132 - $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
117 + if ( $ret ) {
118 + $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
119 + $metadata_webp['sizes'][ $key ]['mime-type'] = $pluswebp_settings['output_mime'];
120 + $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) );
121 + $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size;
122 + if ( $pluswebp_settings['replace'] ) {
123 + wp_delete_file( $path . $file_thumb );
124 + $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
125 + }
133 126 }
134 127 }
135 128
136 129 $org_img_file = null;
@@ -294,91 +287,45 @@
294 287 if ( empty( $types ) ) {
295 288 return array( array(), array() );
296 289 }
297 290
291 + $args = array(
292 + 'post_type' => 'attachment',
293 + 'post_status' => 'inherit',
294 + 'post_mime_type' => $types,
295 + 'posts_per_page' => -1,
296 + 'orderby' => 'date',
297 + 'order' => 'DESC',
298 + );
299 + $posts = get_posts( $args );
300 +
298 301 global $wpdb;
302 + $webp_titles = $wpdb->get_col(
303 + $wpdb->prepare(
304 + "
305 + SELECT post_title
306 + FROM {$wpdb->prefix}posts
307 + WHERE post_type = 'attachment'
308 + AND post_mime_type IN ( %s )
309 + AND post_status = 'inherit'
310 + ",
311 + $output_mime,
312 + )
313 + );
299 314
300 - /* Retrieve a list of converted titles that match $output_mime */
301 - $webp_titles = array();
302 - if ( ! empty( $output_mime ) ) {
303 - $webp_titles_raw = $wpdb->get_col(
304 - $wpdb->prepare(
305 - "SELECT DISTINCT post_title
306 - FROM {$wpdb->posts}
307 - WHERE post_type = 'attachment'
308 - AND post_status = 'inherit'
309 - AND post_mime_type = %s",
310 - $output_mime
311 - )
312 - );
313 -
314 - if ( ! empty( $webp_titles_raw ) ) {
315 - /* Reverse the key and value to speed up searches (O(1) access) */
316 - $webp_titles = array_flip( $webp_titles_raw );
317 - }
318 - }
319 -
320 - /* Creating SQL placeholders for $types */
321 - $types_placeholders = implode( ',', array_fill( 0, count( $types ), '%s' ) );
322 -
323 - $post_ids = array();
315 + $post_ids = array();
324 316 $no_file_ids = array();
325 -
326 - $chunk_size = 2000; /* Retrieve in batches of 2,000 */
327 - $offset = 0;
328 -
329 - do {
330 - /* SQL Parameter Construction (array elements of $types + $chunk_size + $offset) */
331 - $params = array_merge( $types, array( $chunk_size, $offset ) );
332 -
333 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
334 - $chunk_posts = $wpdb->get_results(
335 - $wpdb->prepare(
336 - "SELECT ID, post_title
337 - FROM {$wpdb->posts}
338 - WHERE post_type = 'attachment'
339 - AND post_status = 'inherit'
340 - AND post_mime_type IN ($types_placeholders)
341 - ORDER BY post_date DESC
342 - LIMIT %d OFFSET %d",
343 - $params
344 - )
345 - );
346 - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
347 -
348 - if ( is_array( $chunk_posts ) ) {
349 - $fetched_count = count( $chunk_posts );
350 - } else {
351 - break;
352 - }
353 - if ( 0 === $fetched_count ) {
354 - break;
355 - }
356 -
357 - foreach ( $chunk_posts as $post ) {
358 - if ( ! isset( $webp_titles[ $post->post_title ] ) ) {
359 - $file_path = get_attached_file( $post->ID );
360 -
361 - if ( $file_path && file_exists( $file_path ) ) {
362 - $post_ids[] = (int) $post->ID;
363 - } else {
364 - $no_file_ids[] = (int) $post->ID;
365 - }
366 -
367 - /* Delete the meta-cache generated when retrieving the attachment path on a per-file basis */
368 - clean_post_cache( $post->ID );
317 + $count = 0;
318 + foreach ( $posts as $post ) {
319 + if ( ! in_array( $post->post_title, $webp_titles ) ) {
320 + if ( file_exists( get_attached_file( $post->ID ) ) ) {
321 + $post_ids[] = $post->ID;
322 + } else {
323 + $no_file_ids[] = $post->ID;
369 324 }
370 325 }
326 + }
371 327
372 - $offset += $chunk_size;
373 -
374 - /* Reset the object cache to keep memory usage consistent */
375 - wp_cache_flush();
376 -
377 - } while ( $fetched_count === $chunk_size );
378 -
379 - unset( $webp_titles, $chunk_posts );
380 -
381 328 return array( $post_ids, $no_file_ids );
382 329 }
383 330
384 331 /** ==================================================
@@ -502,9 +449,9 @@
502 449 break;
503 450 }
504 451
505 452 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
506 - unset( $src );
453 + imagedestroy( $src );
507 454
508 455 switch ( $output_mime ) {
509 456 case 'image/webp':
510 457 $ret = imagewebp( $img, $filename_webp, $quality );
@@ -515,9 +462,9 @@
515 462 default:
516 463 $ret = imagewebp( $img, $filename_webp, $quality );
517 464 }
518 465
519 - unset( $img );
466 + imagedestroy( $img );
520 467
521 468 return $ret;
522 469 }
523 470
@@ -567,14 +514,9 @@
567 514 )
568 515 );
569 516
570 517 /* Advanced change database */
571 - $result = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
572 - if ( is_array( $result ) && count( $result ) >= 2 ) {
573 - list( $before_url, $after_url ) = $result;
574 - } else if ( is_string( $result ) ) {
575 - $before_url = $result;
576 - }
518 + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
577 519 }
578 520
579 521 /** ==================================================
580 522 * Filter Raise Memory limit