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

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

456 lines 14.1 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' );
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' );
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
100 $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp );
101 if ( $ret ) {
102 if ( $replace ) {
103 $up_post = array(
104 'ID' => $attachment_id,
105 'guid' => $this->upload_url . '/' . $file_webp,
106 'post_mime_type' => 'image/webp',
107 );
108 wp_update_post( $up_post );
109 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
110 /* for bulk generate */
111 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
112 /* delete org file */
113 unlink( $this->upload_dir . '/' . $metadata['file'] );
114 /* Replace */
115 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
116 /* for hook */
117 $metadata = $metadata_webp;
118 /* for mail */
119 $attach_id = $attachment_id;
120 } else {
121 $post = get_post( $attachment_id );
122 $title = get_the_title( $post );
123 $attachment = array(
124 'guid' => $this->upload_url . '/' . $file_webp,
125 'post_mime_type' => 'image/webp',
126 'post_title' => $title,
127 'post_content' => '',
128 'post_status' => 'inherit',
129 );
130 $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
131 wp_update_attachment_metadata( $attach_id, $metadata_webp );
132
133 $author = get_userdata( $post->post_author );
134 $userid = $author->ID;
135 $postdate = get_the_date( 'Y-m-d H:i:s', $attachment_id );
136 $postdategmt = get_gmt_from_date( $postdate );
137 $up_post = array(
138 'ID' => $attach_id,
139 'post_author' => $userid,
140 'post_date' => $postdate,
141 'post_date_gmt' => $postdategmt,
142 'post_modified' => $postdate,
143 'post_modified_gmt' => $postdategmt,
144 );
145 wp_update_post( $up_post );
146 }
147
148 $ids = get_option( 'pluswebp_generate' );
149 $ids[] = $attach_id;
150 update_option( 'pluswebp_generate', $ids );
151
152 }
153 }
154
155 return $metadata;
156
157 }
158
159 /** ==================================================
160 * Mail send hook
161 *
162 * @param string $to to.
163 * @since 1.09
164 */
165 public function mail_send( $to ) {
166
167 $post_ids = get_option( 'pluswebp_generate' );
168 delete_option( 'pluswebp_generate' );
169
170 if ( function_exists( 'wp_date' ) ) {
171 $now_date_time = wp_date( 'Y-m-d H:i:s' );
172 } else {
173 $now_date_time = date_i18n( 'Y-m-d H:i:s' );
174 }
175 $pluswebp_mail_send = array();
176 $pluswebp_mail_send['datetime'] = $now_date_time;
177
178 /* translators: Date and Time */
179 $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n";
180
181 if ( ! empty( $post_ids ) ) {
182
183 $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
184 $pluswebp_mail_send['count'] = count( $post_ids );
185
186 $count = 0;
187 foreach ( $post_ids as $attach_id ) {
188 $filename = get_attached_file( $attach_id );
189 $metadata = wp_get_attachment_metadata( $attach_id );
190 /* OutputMetaData */
191 list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url );
192 $count++;
193 $message .= __( 'Count' ) . ': ' . $count . "\n";
194 $message .= 'ID: ' . $attach_id . "\n";
195 $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n";
196 $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n";
197 $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n";
198 $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n";
199 if ( ! empty( $metadata['original_image'] ) ) {
200 $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n";
201 $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n";
202 }
203 $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
204 if ( ! $file_size ) {
205 $file_size = __( 'Could not retrieve.', 'plus-webp' );
206 } else {
207 $file_size = size_format( $file_size );
208 }
209 $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
210 if ( ! empty( $imagethumburls ) ) {
211 foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
212 $message .= $thumbsize . ': ' . $imagethumburl . "\n";
213 }
214 }
215 $message .= "\n";
216 }
217 } else {
218 $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n";
219 $pluswebp_mail_send['count'] = 0;
220 }
221
222 update_option( 'pluswebp_generate_mail', $pluswebp_mail_send );
223
224 /* translators: blogname for subject */
225 $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
226 wp_mail( $to, $subject, $message );
227
228 }
229
230 /** ==================================================
231 * Output metadata
232 *
233 * @param int $attach_id attach_id.
234 * @param array $metadata metadata.
235 * @param string $upload_url upload_url.
236 * @return array $imagethumburls(string), $stamptime(string), $file_size(string)
237 * @since 1.09
238 */
239 private function output_metadata( $attach_id, $metadata, $upload_url ) {
240
241 $imagethumburls = array();
242 $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true );
243 $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) );
244 if ( ! empty( $metadata ) ) {
245 foreach ( $metadata as $key1 => $key2 ) {
246 if ( 'sizes' === $key1 ) {
247 foreach ( $metadata[ $key1 ] as $key2 => $key3 ) {
248 $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file'];
249 }
250 }
251 }
252 }
253
254 $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
255 if ( isset( $metadata['filesize'] ) ) {
256 $file_size = $metadata['filesize'];
257 } else {
258 $file_size = @filesize( get_attached_file( $attach_id ) );
259 }
260
261 return array( $imagethumburls, $stamptime, $file_size );
262
263 }
264
265 /** ==================================================
266 * Webp create
267 *
268 * @param string $filename input filename for pictures.
269 * @param string $mime_type mimetype.
270 * @param string $filename_webp output filename for webp.
271 * @return bool $ret create bool.
272 * @since 1.00
273 */
274 private function create_webp( $filename, $mime_type, $filename_webp ) {
275
276 if ( ! file_exists( $filename ) ) {
277 return false;
278 }
279 if ( file_exists( $filename_webp ) ) {
280 return false;
281 }
282
283 $pluswebp_settings = get_option( 'pluswebp' );
284 @set_time_limit( 60 );
285
286 $ret = false;
287 switch ( $mime_type ) {
288 case 'image/jpeg':
289 $src = imagecreatefromjpeg( $filename );
290 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
291 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
292 imagealphablending( $img, true );
293 break;
294 case 'image/png':
295 $src = imagecreatefrompng( $filename );
296 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
297 imagealphablending( $img, false );
298 imagesavealpha( $img, true );
299 break;
300 case 'image/bmp':
301 $src = imagecreatefrombmp( $filename );
302 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
303 break;
304 case 'image/gif':
305 $src = imagecreatefromgif( $filename );
306 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
307 $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
308 imagefill( $img, 0, 0, $bgcolor );
309 imagecolortransparent( $img, $bgcolor );
310 break;
311 }
312
313 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
314 imagedestroy( $src );
315 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
316 imagedestroy( $img );
317
318 return $ret;
319
320 }
321
322 /** ==================================================
323 * Change ext
324 *
325 * @param string $before_file_name before_file_name.
326 * @param string $ext ext.
327 * @return array $after_file_name after_file_name.
328 * @since 1.00
329 */
330 private function change_ext( $before_file_name, $ext ) {
331
332 $exts = explode( '.', $before_file_name );
333 $before_ext = '.' . end( $exts );
334 $after_ext = '.' . $ext;
335 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
336
337 return $after_file_name;
338
339 }
340
341 /** ==================================================
342 * Change DB
343 *
344 * @param string $before_url before_url.
345 * @param string $after_url after_url.
346 * @since 1.00
347 */
348 private function change_db( $before_url, $after_url ) {
349
350 global $wpdb;
351
352 /* Replace */
353 $wpdb->query(
354 $wpdb->prepare(
355 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
356 $before_url,
357 $after_url
358 )
359 );
360
361 }
362
363 /** ==================================================
364 * Upload Path
365 *
366 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
367 * @since 1.00
368 */
369 public function upload_dir_url_path() {
370
371 $wp_uploads = wp_upload_dir();
372
373 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
374 if ( $relation_path_true > 0 ) {
375 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
376 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
377 $upload_url = $this->realurl( $basepath, $relationalpath );
378 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
379 } else {
380 $upload_url = $wp_uploads['baseurl'];
381 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
382 }
383
384 if ( is_ssl() ) {
385 $upload_url = str_replace( 'http:', 'https:', $upload_url );
386 }
387
388 if ( $relation_path_true > 0 ) {
389 $upload_path = $relationalpath;
390 } else {
391 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
392 }
393
394 $upload_dir = untrailingslashit( $upload_dir );
395 $upload_url = untrailingslashit( $upload_url );
396 $upload_path = untrailingslashit( $upload_path );
397
398 return array( $upload_dir, $upload_url, $upload_path );
399
400 }
401
402 /** ==================================================
403 * Real Url
404 *
405 * @param string $base base.
406 * @param string $relationalpath relationalpath.
407 * @return string $realurl realurl.
408 * @since 1.00
409 */
410 private function realurl( $base, $relationalpath ) {
411
412 $parse = array(
413 'scheme' => null,
414 'user' => null,
415 'pass' => null,
416 'host' => null,
417 'port' => null,
418 'query' => null,
419 'fragment' => null,
420 );
421 $parse = wp_parse_url( $base );
422
423 if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) {
424 $parse['path'] .= '.';
425 }
426
427 if ( preg_match( '#^https?://#', $relationalpath ) ) {
428 return $relationalpath;
429 } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) {
430 return $parse['scheme'] . '://' . $parse['host'] . $relationalpath;
431 } else {
432 $base_path = explode( '/', dirname( $parse['path'] ) );
433 $rel_path = explode( '/', $relationalpath );
434 foreach ( $rel_path as $rel_dir_name ) {
435 if ( '.' === $rel_dir_name ) {
436 array_shift( $base_path );
437 array_unshift( $base_path, '' );
438 } elseif ( '..' === $rel_dir_name ) {
439 array_pop( $base_path );
440 if ( count( $base_path ) === 0 ) {
441 $base_path = array( '' );
442 }
443 } else {
444 array_push( $base_path, $rel_dir_name );
445 }
446 }
447 $path = implode( '/', $base_path );
448 return $parse['scheme'] . '://' . $parse['host'] . $path;
449 }
450
451 }
452
453 }
454
455
456