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 +138 -84 5.114.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,18 +266,13 @@
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 - }
293 -
294 275 $args = array(
295 276 'post_type' => 'attachment',
296 277 'post_status' => 'inherit',
297 278 'post_mime_type' => $types,
@@ -302,18 +283,15 @@
302 283 $posts = get_posts( $args );
303 284
304 285 global $wpdb;
305 286 $webp_titles = $wpdb->get_col(
306 - $wpdb->prepare(
307 - "
287 + "
308 288 SELECT post_title
309 289 FROM {$wpdb->prefix}posts
310 290 WHERE post_type = 'attachment'
311 - AND post_mime_type IN ( %s )
291 + AND post_mime_type IN ( 'image/webp' )
312 292 AND post_status = 'inherit'
313 - ",
314 - $output_mime,
315 - )
293 + "
316 294 );
317 295
318 296 $post_ids = array();
319 297 $no_file_ids = array();
@@ -334,9 +312,9 @@
334 312 /** ==================================================
335 313 * Mail messages hook
336 314 *
337 315 * @param array $messages messages.
338 - * @param int $webp_id webp ID or avif ID.
316 + * @param int $webp_id webp_id.
339 317 *
340 318 * @since 4.00
341 319 */
342 320 public function mail_messages( $messages, $webp_id ) {
@@ -350,18 +328,18 @@
350 328 /* Output datas*/
351 329 list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata );
352 330
353 331 $message = 'ID: ' . $webp_id . "\n";
354 - $message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $webp_id ) . "\n";
355 - $message .= __( 'Permalink:', 'plus-webp' ) . ' ' . $attachment_link . "\n";
332 + $message .= __( 'Title' ) . ': ' . get_the_title( $webp_id ) . "\n";
333 + $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n";
356 334 $message .= 'URL: ' . $attachment_url . "\n";
357 - $message .= __( 'File name:', 'plus-webp' ) . ' ' . $filename . "\n";
335 + $message .= __( 'File name:' ) . ' ' . $filename . "\n";
358 336 if ( ! empty( $original_image_url ) ) {
359 337 $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n";
360 338 $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n";
361 339 }
362 - $message .= __( 'Date/Time', 'plus-webp' ) . ': ' . $stamptime . "\n";
363 - $message .= __( 'File size:', 'plus-webp' ) . ' ' . $file_size . "\n";
340 + $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
341 + $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
364 342 if ( ! empty( $imagethumburls ) ) {
365 343 foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
366 344 $message .= $thumbsize . ': ' . $imagethumburl . "\n";
367 345 }
@@ -387,30 +365,28 @@
387 365 } else {
388 366 $now_date_time = date_i18n( 'Y-m-d H:i:s' );
389 367 }
390 368 /* translators: Date and Time */
391 - $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";
392 370 /* translators: Generated count */
393 - $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";
394 372
395 373 $to = get_option( 'admin_email' );
396 374 /* translators: blogname for subject */
397 - $subject = sprintf( __( '[%s] WebP or AVIF generate', 'plus-webp' ), get_option( 'blogname' ) );
375 + $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
398 376 wp_mail( $to, $subject, $message_head . implode( $messages ) );
399 377 }
400 378
401 379 /** ==================================================
402 - * WebP or AVIF create
380 + * Webp create
403 381 *
404 382 * @param string $filename input filename for pictures.
405 383 * @param string $mime_type mimetype.
406 - * @param string $filename_webp output filename for webp or avif.
407 - * @param int $quality quality of webp.
408 - * @param string $output_mime output mime type.
384 + * @param string $filename_webp output filename for webp.
409 385 * @return bool $ret create bool.
410 386 * @since 1.00
411 387 */
412 - private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) {
388 + private function create_webp( $filename, $mime_type, $filename_webp ) {
413 389
414 390 if ( ! file_exists( $filename ) ) {
415 391 return false;
416 392 }
@@ -417,8 +393,9 @@
417 393 if ( file_exists( $filename_webp ) ) {
418 394 return false;
419 395 }
420 396
397 + $pluswebp_settings = get_option( 'pluswebp' );
421 398 @set_time_limit( 60 );
422 399 wp_raise_memory_limit( 'pluswebp' );
423 400
424 401 $ret = false;
@@ -453,20 +430,9 @@
453 430 }
454 431
455 432 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
456 433 imagedestroy( $src );
457 -
458 - switch ( $output_mime ) {
459 - case 'image/webp':
460 - $ret = imagewebp( $img, $filename_webp, $quality );
461 - break;
462 - case 'image/avif':
463 - $ret = imageavif( $img, $filename_webp, $quality );
464 - break;
465 - default:
466 - $ret = imagewebp( $img, $filename_webp, $quality );
467 - }
468 -
434 + $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
469 435 imagedestroy( $img );
470 436
471 437 return $ret;
472 438 }
@@ -518,8 +484,96 @@
518 484 );
519 485
520 486 /* Advanced change database */
521 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;
575 + }
522 576 }
523 577
524 578 /** ==================================================
525 579 * Filter Raise Memory limit