PluginProbe
Plus WebP or AVIF / 1.13
Plus WebP or AVIF v1.13
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
plus-webp / lib / class-pluswebp.php

class-pluswebp.php in Plus WebP or AVIF 1.13, at lib/class-pluswebp.php

465 lines 14.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plus WebP
4 *
5 * @package Plus WebP
6 * @subpackage PlusWebp Main function
7 /* Copyright (c) 2019- Katsushi Kawamori (email : dodesyoswift312@gmail.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 $pluswebp = new PlusWebp();
23
24 /** ==================================================
25 * Class Main function
26 *
27 * @since 1.00
28 */
29 class PlusWebp {
30
31 /** ==================================================
32 * Dir
33 *
34 * @var $upload_dir DIR.
35 */
36 private $upload_dir;
37
38 /** ==================================================
39 * URL
40 *
41 * @var $upload_url URL.
42 */
43 private $upload_url;
44
45 /** ==================================================
46 * Path
47 *
48 * @var $upload_path PATH.
49 */
50 private $upload_path;
51
52 /** ==================================================
53 * Construct
54 *
55 * @since 1.00
56 */
57 public function __construct() {
58
59 list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path();
60
61 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
64 }
65
66 /** ==================================================
67 * Webp generate
68 *
69 * @param array $metadata metadata.
70 * @param int $attachment_id ID.
71 * @return array $metadata metadata.
72 * @since 1.00
73 */
74 public function generate_webp( $metadata, $attachment_id ) {
75
76 $pluswebp_settings = get_option( 'pluswebp' );
77 $replace = $pluswebp_settings['replace'];
78
79 $mime_type = get_post_mime_type( $attachment_id );
80 if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) {
81 $metadata_webp = $metadata;
82 $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] );
83 $metadata_webp['file'] = $file_webp;
84 foreach ( (array) $metadata['sizes'] as $key => $value ) {
85 $file_thumb = $value['file'];
86 $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 );
90 if ( $ret ) {
91 $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
92 $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp';
93 if ( $replace ) {
94 unlink( $path . $file_thumb );
95 $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
96 }
97 }
98 }
99 if ( ! empty( $metadata['original_image'] ) ) {
100 $org_img_file = wp_get_original_image_path( $attachment_id, false );
101 $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
102 $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
103 if ( $ret ) {
104 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
105 }
106 }
107
108 $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp );
109 if ( $ret ) {
110 if ( $replace ) {
111 $up_post = array(
112 'ID' => $attachment_id,
113 'guid' => $this->upload_url . '/' . $file_webp,
114 'post_mime_type' => 'image/webp',
115 );
116 wp_update_post( $up_post );
117 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
118 /* for bulk generate */
119 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
120 /* delete org file */
121 unlink( $this->upload_dir . '/' . $metadata['file'] );
122 /* Replace */
123 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
124 /* for hook */
125 $metadata = $metadata_webp;
126 /* for mail */
127 $attach_id = $attachment_id;
128 } else {
129 $post = get_post( $attachment_id );
130 $title = get_the_title( $post );
131 $attachment = array(
132 'guid' => $this->upload_url . '/' . $file_webp,
133 'post_mime_type' => 'image/webp',
134 'post_title' => $title,
135 'post_content' => '',
136 'post_status' => 'inherit',
137 );
138 $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
139 wp_update_attachment_metadata( $attach_id, $metadata_webp );
140
141 $author = get_userdata( $post->post_author );
142 $userid = $author->ID;
143 $postdate = get_the_date( 'Y-m-d H:i:s', $attachment_id );
144 $postdategmt = get_gmt_from_date( $postdate );
145 $up_post = array(
146 'ID' => $attach_id,
147 'post_author' => $userid,
148 'post_date' => $postdate,
149 'post_date_gmt' => $postdategmt,
150 'post_modified' => $postdate,
151 'post_modified_gmt' => $postdategmt,
152 );
153 wp_update_post( $up_post );
154 }
155
156 $ids = get_option( 'pluswebp_generate' );
157 $ids[] = $attach_id;
158 update_option( 'pluswebp_generate', $ids );
159
160 }
161 }
162
163 return $metadata;
164
165 }
166
167 /** ==================================================
168 * Mail send hook
169 *
170 * @param string $to to.
171 * @since 1.09
172 */
173 public function mail_send( $to ) {
174
175 $post_ids = get_option( 'pluswebp_generate' );
176 delete_option( 'pluswebp_generate' );
177
178 if ( function_exists( 'wp_date' ) ) {
179 $now_date_time = wp_date( 'Y-m-d H:i:s' );
180 } else {
181 $now_date_time = date_i18n( 'Y-m-d H:i:s' );
182 }
183 $pluswebp_mail_send = array();
184 $pluswebp_mail_send['datetime'] = $now_date_time;
185
186 /* translators: Date and Time */
187 $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n";
188
189 if ( ! empty( $post_ids ) ) {
190
191 $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
192 $pluswebp_mail_send['count'] = count( $post_ids );
193
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' );
214 } else {
215 $file_size = size_format( $file_size );
216 }
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 }
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 }
229
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
236 }
237
238 /** ==================================================
239 * Output metadata
240 *
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
246 */
247 private function output_metadata( $attach_id, $metadata, $upload_url ) {
248
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 }
258 }
259 }
260 }
261
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 ) );
264
265 return array( $imagethumburls, $stamptime, $file_size );
266
267 }
268
269 /** ==================================================
270 * Webp create
271 *
272 * @param string $filename input filename for pictures.
273 * @param string $mime_type mimetype.
274 * @param string $filename_webp output filename for webp.
275 * @return bool $ret create bool.
276 * @since 1.00
277 */
278 private function create_webp( $filename, $mime_type, $filename_webp ) {
279
280 if ( ! file_exists( $filename ) ) {
281 return false;
282 }
283 if ( file_exists( $filename_webp ) ) {
284 return false;
285 }
286
287 $pluswebp_settings = get_option( 'pluswebp' );
288 @set_time_limit( 60 );
289
290 $ret = false;
291 switch ( $mime_type ) {
292 case 'image/jpeg':
293 $src = imagecreatefromjpeg( $filename );
294 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
295 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
296 imagealphablending( $img, true );
297 break;
298 case 'image/png':
299 $src = imagecreatefrompng( $filename );
300 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
301 imagealphablending( $img, false );
302 imagesavealpha( $img, true );
303 break;
304 case 'image/bmp':
305 $src = imagecreatefrombmp( $filename );
306 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
307 break;
308 case 'image/gif':
309 $src = imagecreatefromgif( $filename );
310 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
311 $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
312 imagefill( $img, 0, 0, $bgcolor );
313 imagecolortransparent( $img, $bgcolor );
314 break;
315 }
316
317 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
318 imagedestroy( $src );
319 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
320 imagedestroy( $img );
321
322 return $ret;
323
324 }
325
326 /** ==================================================
327 * Change ext
328 *
329 * @param string $before_file_name before_file_name.
330 * @param string $ext ext.
331 * @param bool $addext addext.
332 * @return array $after_file_name after_file_name.
333 * @since 1.00
334 */
335 private function change_ext( $before_file_name, $ext, $addext ) {
336
337 if ( $addext ) {
338 $after_file_name = $before_file_name . '.' . $ext;
339 } else {
340 $exts = explode( '.', $before_file_name );
341 $before_ext = '.' . end( $exts );
342 $after_ext = '.' . $ext;
343 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
344 }
345
346 return $after_file_name;
347
348 }
349
350 /** ==================================================
351 * Change DB
352 *
353 * @param string $before_url before_url.
354 * @param string $after_url after_url.
355 * @since 1.00
356 */
357 private function change_db( $before_url, $after_url ) {
358
359 global $wpdb;
360
361 /* Replace */
362 $wpdb->query(
363 $wpdb->prepare(
364 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
365 $before_url,
366 $after_url
367 )
368 );
369
370 }
371
372 /** ==================================================
373 * Upload Path
374 *
375 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
376 * @since 1.00
377 */
378 public function upload_dir_url_path() {
379
380 $wp_uploads = wp_upload_dir();
381
382 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
383 if ( $relation_path_true > 0 ) {
384 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
385 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
386 $upload_url = $this->realurl( $basepath, $relationalpath );
387 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
388 } else {
389 $upload_url = $wp_uploads['baseurl'];
390 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
391 }
392
393 if ( is_ssl() ) {
394 $upload_url = str_replace( 'http:', 'https:', $upload_url );
395 }
396
397 if ( $relation_path_true > 0 ) {
398 $upload_path = $relationalpath;
399 } else {
400 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
401 }
402
403 $upload_dir = untrailingslashit( $upload_dir );
404 $upload_url = untrailingslashit( $upload_url );
405 $upload_path = untrailingslashit( $upload_path );
406
407 return array( $upload_dir, $upload_url, $upload_path );
408
409 }
410
411 /** ==================================================
412 * Real Url
413 *
414 * @param string $base base.
415 * @param string $relationalpath relationalpath.
416 * @return string $realurl realurl.
417 * @since 1.00
418 */
419 private function realurl( $base, $relationalpath ) {
420
421 $parse = array(
422 'scheme' => null,
423 'user' => null,
424 'pass' => null,
425 'host' => null,
426 'port' => null,
427 'query' => null,
428 'fragment' => null,
429 );
430 $parse = wp_parse_url( $base );
431
432 if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) {
433 $parse['path'] .= '.';
434 }
435
436 if ( preg_match( '#^https?://#', $relationalpath ) ) {
437 return $relationalpath;
438 } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) {
439 return $parse['scheme'] . '://' . $parse['host'] . $relationalpath;
440 } else {
441 $base_path = explode( '/', dirname( $parse['path'] ) );
442 $rel_path = explode( '/', $relationalpath );
443 foreach ( $rel_path as $rel_dir_name ) {
444 if ( '.' === $rel_dir_name ) {
445 array_shift( $base_path );
446 array_unshift( $base_path, '' );
447 } elseif ( '..' === $rel_dir_name ) {
448 array_pop( $base_path );
449 if ( count( $base_path ) === 0 ) {
450 $base_path = array( '' );
451 }
452 } else {
453 array_push( $base_path, $rel_dir_name );
454 }
455 }
456 $path = implode( '/', $base_path );
457 return $parse['scheme'] . '://' . $parse['host'] . $path;
458 }
459
460 }
461
462 }
463
464
465