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 +164 -161 5.124.11 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 - * Plus WebP or AVIF
3 + * Plus WebP
4 4 *
5 - * @package Plus WebP or AVIF
5 + * @package Plus WebP
6 6 * @subpackage PlusWebp Main function
7 7 /* Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 8 This program is free software; you can redistribute it and/or modify
9 9 it under the terms of the GNU General Public License as published by
@@ -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
@@ -46,8 +42,15 @@
46 42 */
47 43 private $upload_url;
48 44
49 45 /** ==================================================
46 + * Path
47 + *
48 + * @var $upload_path PATH.
49 + */
50 + private $upload_path;
51 +
52 + /** ==================================================
50 53 * Construct
51 54 *
52 55 * @since 1.00
53 56 */
@@ -52,22 +55,14 @@
52 55 * @since 1.00
53 56 */
54 57 public function __construct() {
55 58
56 - $wp_uploads = wp_upload_dir();
59 + list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path();
57 60
58 - $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
59 - $upload_url = $wp_uploads['baseurl'];
60 - if ( is_ssl() ) {
61 - $upload_url = str_replace( 'http:', 'https:', $upload_url );
62 - }
63 - $this->upload_dir = untrailingslashit( $upload_dir );
64 - $this->upload_url = untrailingslashit( $upload_url );
65 -
66 61 /* Generate metadata */
67 62 add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 );
68 63 /* Original hook */
69 - add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 2 );
64 + add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 1 );
70 65 add_filter( 'pluswebp_mail_messages', array( $this, 'mail_messages' ), 10, 2 );
71 66 add_action( 'pluswebp_mail_generate_message', array( $this, 'mail_generate_message' ), 10, 2 );
72 67
73 68 /* Raise memory */
@@ -76,9 +71,9 @@
76 71 add_filter( 'wp_upload_image_mime_transforms', array( $this, 'control_mime_type' ), 10, 2 );
77 72 }
78 73
79 74 /** ==================================================
80 - * Webp of AVIF generate
75 + * Webp generate
81 76 *
82 77 * @param array $metadata metadata.
83 78 * @param int $attachment_id ID.
84 79 * @return array $metadata metadata.
@@ -86,24 +81,14 @@
86 81 */
87 82 public function generate_webp( $metadata, $attachment_id ) {
88 83
89 84 $pluswebp_settings = get_option( 'pluswebp' );
85 + $replace = $pluswebp_settings['replace'];
90 86
91 - switch ( $pluswebp_settings['output_mime'] ) {
92 - case 'image/webp':
93 - $ext = 'webp';
94 - break;
95 - case 'image/avif':
96 - $ext = 'avif';
97 - break;
98 - default:
99 - $ext = 'webp';
100 - }
101 -
102 87 $mime_type = get_post_mime_type( $attachment_id );
103 88 if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) {
104 89 $metadata_webp = $metadata;
105 - $file_webp = $this->change_ext( $metadata['file'], $ext, $pluswebp_settings['addext'] );
90 + $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] );
106 91 $metadata_webp['file'] = $file_webp;
107 92 if ( '.' === dirname( $file_webp ) ) {
108 93 $dir_name_url = '/';
109 94 $dir_name_path = wp_normalize_path( '/' );
@@ -115,18 +100,19 @@
115 100 $path = $this->upload_dir . $dir_name_path;
116 101
117 102 foreach ( (array) $metadata['sizes'] as $key => $value ) {
118 103 $file_thumb = $value['file'];
119 - $file_thumb_webp = $this->change_ext( $file_thumb, $ext, $pluswebp_settings['addext'] );
120 - $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp, $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
121 - /* $ret -> ignore : Some metadata also has duplicate filenames */
122 - $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
123 - $metadata_webp['sizes'][ $key ]['mime-type'] = $pluswebp_settings['output_mime'];
124 - $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) );
125 - $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size;
126 - if ( $pluswebp_settings['replace'] ) {
127 - wp_delete_file( $path . $file_thumb );
128 - $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
104 + $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] );
105 + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp );
106 + if ( $ret ) {
107 + $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
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;
111 + if ( $replace ) {
112 + wp_delete_file( $path . $file_thumb );
113 + $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
114 + }
129 115 }
130 116 }
131 117
132 118 $org_img_file = null;
@@ -131,24 +117,24 @@
131 117
132 118 $org_img_file = null;
133 119 if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) {
134 120 $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) );
135 - $org_webp_file = $this->change_ext( $org_img_file, $ext, $pluswebp_settings['addext'] );
136 - $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file, $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
121 + $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
122 + $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
137 123 if ( $ret ) {
138 124 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
139 125 }
140 126 }
141 127
142 - $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ), $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
128 + $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ) );
143 129 $webp_size = filesize( $path . wp_basename( $file_webp ) );
144 130 $metadata_webp['filesize'] = $webp_size;
145 131 if ( $ret ) {
146 - if ( $pluswebp_settings['replace'] ) {
132 + if ( $replace ) {
147 133 $up_post = array(
148 134 'ID' => $attachment_id,
149 135 'guid' => $this->upload_url . '/' . $file_webp,
150 - 'post_mime_type' => $pluswebp_settings['output_mime'],
136 + 'post_mime_type' => 'image/webp',
151 137 );
152 138 wp_update_post( $up_post );
153 139 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
154 140 /* for bulk generate */
@@ -168,9 +154,9 @@
168 154 $post = get_post( $attachment_id );
169 155 $title = $post->post_title;
170 156 $attachment = array(
171 157 'guid' => $this->upload_url . '/' . $file_webp,
172 - 'post_mime_type' => $pluswebp_settings['output_mime'],
158 + 'post_mime_type' => 'image/webp',
173 159 'post_title' => $title,
174 160 'post_content' => '',
175 161 'post_status' => 'inherit',
176 162 );
@@ -280,101 +266,47 @@
280 266
281 267 /** ==================================================
282 268 * Get All Images
283 269 *
284 - * @param array $types mime types.
285 - * @param string $output_mime output mime type.
270 + * @param array $types mime types.
286 271 * @since 2.00
287 272 */
288 - public function get_allimages( $types, $output_mime ) {
273 + public function get_allimages( $types ) {
289 274
290 - if ( empty( $types ) ) {
291 - return array( array(), array() );
292 - }
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 );
293 284
294 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 295
296 - /* Retrieve a list of converted titles that match $output_mime */
297 - $webp_titles = array();
298 - if ( ! empty( $output_mime ) ) {
299 - $webp_titles_raw = $wpdb->get_col(
300 - $wpdb->prepare(
301 - "SELECT DISTINCT post_title
302 - FROM {$wpdb->posts}
303 - WHERE post_type = 'attachment'
304 - AND post_status = 'inherit'
305 - AND post_mime_type = %s",
306 - $output_mime
307 - )
308 - );
309 -
310 - if ( ! empty( $webp_titles_raw ) ) {
311 - /* Reverse the key and value to speed up searches (O(1) access) */
312 - $webp_titles = array_flip( $webp_titles_raw );
313 - }
314 - }
315 -
316 - /* Creating SQL placeholders for $types */
317 - $types_placeholders = implode( ',', array_fill( 0, count( $types ), '%s' ) );
318 -
319 - $post_ids = array();
296 + $post_ids = array();
320 297 $no_file_ids = array();
321 -
322 - $chunk_size = 2000; /* Retrieve in batches of 2,000 */
323 - $offset = 0;
324 -
325 - do {
326 - /* SQL Parameter Construction (array elements of $types + $chunk_size + $offset) */
327 - $params = array_merge( $types, array( $chunk_size, $offset ) );
328 -
329 - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
330 - $chunk_posts = $wpdb->get_results(
331 - $wpdb->prepare(
332 - "SELECT ID, post_title
333 - FROM {$wpdb->posts}
334 - WHERE post_type = 'attachment'
335 - AND post_status = 'inherit'
336 - AND post_mime_type IN ($types_placeholders)
337 - ORDER BY post_date DESC
338 - LIMIT %d OFFSET %d",
339 - $params
340 - )
341 - );
342 - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
343 -
344 - if ( is_array( $chunk_posts ) ) {
345 - $fetched_count = count( $chunk_posts );
346 - } else {
347 - break;
348 - }
349 - if ( 0 === $fetched_count ) {
350 - break;
351 - }
352 -
353 - foreach ( $chunk_posts as $post ) {
354 - if ( ! isset( $webp_titles[ $post->post_title ] ) ) {
355 - $file_path = get_attached_file( $post->ID );
356 -
357 - if ( $file_path && file_exists( $file_path ) ) {
358 - $post_ids[] = (int) $post->ID;
359 - } else {
360 - $no_file_ids[] = (int) $post->ID;
361 - }
362 -
363 - /* Delete the meta-cache generated when retrieving the attachment path on a per-file basis */
364 - clean_post_cache( $post->ID );
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;
365 305 }
366 306 }
307 + }
367 308
368 - $offset += $chunk_size;
369 -
370 - /* Reset the object cache to keep memory usage consistent */
371 - wp_cache_flush();
372 -
373 - } while ( $fetched_count === $chunk_size );
374 -
375 - unset( $webp_titles, $chunk_posts );
376 -
377 309 return array( $post_ids, $no_file_ids );
378 310 }
379 311
380 312 /** ==================================================
@@ -380,9 +312,9 @@
380 312 /** ==================================================
381 313 * Mail messages hook
382 314 *
383 315 * @param array $messages messages.
384 - * @param int $webp_id webp ID or avif ID.
316 + * @param int $webp_id webp_id.
385 317 *
386 318 * @since 4.00
387 319 */
388 320 public function mail_messages( $messages, $webp_id ) {
@@ -396,18 +328,18 @@
396 328 /* Output datas*/
397 329 list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata );
398 330
399 331 $message = 'ID: ' . $webp_id . "\n";
400 - $message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $webp_id ) . "\n";
401 - $message .= __( 'Permalink:', 'plus-webp' ) . ' ' . $attachment_link . "\n";
332 + $message .= __( 'Title' ) . ': ' . get_the_title( $webp_id ) . "\n";
333 + $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n";
402 334 $message .= 'URL: ' . $attachment_url . "\n";
403 - $message .= __( 'File name:', 'plus-webp' ) . ' ' . $filename . "\n";
335 + $message .= __( 'File name:' ) . ' ' . $filename . "\n";
404 336 if ( ! empty( $original_image_url ) ) {
405 337 $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n";
406 338 $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n";
407 339 }
408 - $message .= __( 'Date/Time', 'plus-webp' ) . ': ' . $stamptime . "\n";
409 - $message .= __( 'File size:', 'plus-webp' ) . ' ' . $file_size . "\n";
340 + $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
341 + $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
410 342 if ( ! empty( $imagethumburls ) ) {
411 343 foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
412 344 $message .= $thumbsize . ': ' . $imagethumburl . "\n";
413 345 }
@@ -433,30 +365,28 @@
433 365 } else {
434 366 $now_date_time = date_i18n( 'Y-m-d H:i:s' );
435 367 }
436 368 /* translators: Date and Time */
437 - $message_head = sprintf( __( 'Plus WebP or AVIF : %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";
438 370 /* translators: Generated count */
439 - $message_head .= sprintf( __( 'Webp or AVIF generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n";
371 + $message_head .= sprintf( __( 'Webp generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n";
440 372
441 373 $to = get_option( 'admin_email' );
442 374 /* translators: blogname for subject */
443 - $subject = sprintf( __( '[%s] WebP or AVIF generate', 'plus-webp' ), get_option( 'blogname' ) );
375 + $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
444 376 wp_mail( $to, $subject, $message_head . implode( $messages ) );
445 377 }
446 378
447 379 /** ==================================================
448 - * WebP or AVIF create
380 + * Webp create
449 381 *
450 382 * @param string $filename input filename for pictures.
451 383 * @param string $mime_type mimetype.
452 - * @param string $filename_webp output filename for webp or avif.
453 - * @param int $quality quality of webp.
454 - * @param string $output_mime output mime type.
384 + * @param string $filename_webp output filename for webp.
455 385 * @return bool $ret create bool.
456 386 * @since 1.00
457 387 */
458 - private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) {
388 + private function create_webp( $filename, $mime_type, $filename_webp ) {
459 389
460 390 if ( ! file_exists( $filename ) ) {
461 391 return false;
462 392 }
@@ -463,8 +393,9 @@
463 393 if ( file_exists( $filename_webp ) ) {
464 394 return false;
465 395 }
466 396
397 + $pluswebp_settings = get_option( 'pluswebp' );
467 398 @set_time_limit( 60 );
468 399 wp_raise_memory_limit( 'pluswebp' );
469 400
470 401 $ret = false;
@@ -498,23 +429,12 @@
498 429 break;
499 430 }
500 431
501 432 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
502 - unset( $src );
433 + imagedestroy( $src );
434 + $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
435 + imagedestroy( $img );
503 436
504 - switch ( $output_mime ) {
505 - case 'image/webp':
506 - $ret = imagewebp( $img, $filename_webp, $quality );
507 - break;
508 - case 'image/avif':
509 - $ret = imageavif( $img, $filename_webp, $quality );
510 - break;
511 - default:
512 - $ret = imagewebp( $img, $filename_webp, $quality );
513 - }
514 -
515 - unset( $img );
516 -
517 437 return $ret;
518 438 }
519 439
520 440 /** ==================================================
@@ -563,13 +483,96 @@
563 483 )
564 484 );
565 485
566 486 /* Advanced change database */
567 - $result = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
568 - if ( is_array( $result ) && count( $result ) >= 2 ) {
569 - list( $before_url, $after_url ) = $result;
570 - } else if ( is_string( $result ) ) {
571 - $before_url = $result;
487 + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
488 + }
489 +
490 + /** ==================================================
491 + * Upload Path
492 + *
493 + * @return array $upload_dir,$upload_url,$upload_path uploadpath.
494 + * @since 1.00
495 + */
496 + public function upload_dir_url_path() {
497 +
498 + $wp_uploads = wp_upload_dir();
499 +
500 + $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
501 + if ( $relation_path_true > 0 ) {
502 + $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
503 + $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
504 + $upload_url = $this->realurl( $basepath, $relationalpath );
505 + $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
506 + } else {
507 + $upload_url = $wp_uploads['baseurl'];
508 + $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
509 + }
510 +
511 + if ( is_ssl() ) {
512 + $upload_url = str_replace( 'http:', 'https:', $upload_url );
513 + }
514 +
515 + if ( $relation_path_true > 0 ) {
516 + $upload_path = $relationalpath;
517 + } else {
518 + $upload_path = str_replace( site_url( '/' ), '', $upload_url );
519 + }
520 +
521 + $upload_dir = untrailingslashit( $upload_dir );
522 + $upload_url = untrailingslashit( $upload_url );
523 + $upload_path = untrailingslashit( $upload_path );
524 +
525 + return array( $upload_dir, $upload_url, $upload_path );
526 + }
527 +
528 + /** ==================================================
529 + * Real Url
530 + *
531 + * @param string $base base.
532 + * @param string $relationalpath relationalpath.
533 + * @return string $realurl realurl.
534 + * @since 1.00
535 + */
536 + private function realurl( $base, $relationalpath ) {
537 +
538 + $parse = array(
539 + 'scheme' => null,
540 + 'user' => null,
541 + 'pass' => null,
542 + 'host' => null,
543 + 'port' => null,
544 + 'query' => null,
545 + 'fragment' => null,
546 + );
547 + $parse = wp_parse_url( $base );
548 +
549 + if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) {
550 + $parse['path'] .= '.';
551 + }
552 +
553 + if ( preg_match( '#^https?://#', $relationalpath ) ) {
554 + return $relationalpath;
555 + } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) {
556 + return $parse['scheme'] . '://' . $parse['host'] . $relationalpath;
557 + } else {
558 + $base_path = explode( '/', dirname( $parse['path'] ) );
559 + $rel_path = explode( '/', $relationalpath );
560 + foreach ( $rel_path as $rel_dir_name ) {
561 + if ( '.' === $rel_dir_name ) {
562 + array_shift( $base_path );
563 + array_unshift( $base_path, '' );
564 + } elseif ( '..' === $rel_dir_name ) {
565 + array_pop( $base_path );
566 + if ( count( $base_path ) === 0 ) {
567 + $base_path = array( '' );
568 + }
569 + } else {
570 + array_push( $base_path, $rel_dir_name );
571 + }
572 + }
573 + $path = implode( '/', $base_path );
574 + return $parse['scheme'] . '://' . $parse['host'] . $path;
572 575 }
573 576 }
574 577
575 578 /** ==================================================