PluginProbe
Plus WebP or AVIF / 1.09
Plus WebP or AVIF v1.09
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.09, at lib/class-pluswebp.php

453 lines 14.0 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 if ( ! empty( $post_ids ) ) {
179
180 $message = __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
181 $pluswebp_mail_send['count'] = count( $post_ids );
182
183 $count = 0;
184 foreach ( $post_ids as $attach_id ) {
185 $filename = get_attached_file( $attach_id );
186 $metadata = wp_get_attachment_metadata( $attach_id );
187 /* OutputMetaData */
188 list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url );
189 $count++;
190 $message .= __( 'Count' ) . ': ' . $count . "\n";
191 $message .= 'ID: ' . $attach_id . "\n";
192 $message .= __( 'Title' ) . ': ' . $title . "\n";
193 $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n";
194 $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n";
195 $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n";
196 if ( ! empty( $metadata['original_image'] ) ) {
197 $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n";
198 $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n";
199 }
200 $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
201 if ( ! $file_size ) {
202 $file_size = __( 'Could not retrieve.', 'plus-webp' );
203 } else {
204 $file_size = size_format( $file_size );
205 }
206 $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
207 if ( ! empty( $imagethumburls ) ) {
208 foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
209 $message .= $thumbsize . ': ' . $imagethumburl . "\n";
210 }
211 }
212 $message .= "\n";
213 }
214 } else {
215 $message = __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n";
216 $pluswebp_mail_send['count'] = 0;
217 }
218
219 update_option( 'pluswebp_generate_mail', $pluswebp_mail_send );
220
221 /* translators: blogname for subject */
222 $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
223 wp_mail( $to, $subject, $message );
224
225 }
226
227 /** ==================================================
228 * Output metadata
229 *
230 * @param int $attach_id attach_id.
231 * @param array $metadata metadata.
232 * @param string $upload_url upload_url.
233 * @return array $imagethumburls(string), $stamptime(string), $file_size(string)
234 * @since 1.09
235 */
236 private function output_metadata( $attach_id, $metadata, $upload_url ) {
237
238 $imagethumburls = array();
239 $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true );
240 $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) );
241 if ( ! empty( $metadata ) ) {
242 foreach ( $metadata as $key1 => $key2 ) {
243 if ( 'sizes' === $key1 ) {
244 foreach ( $metadata[ $key1 ] as $key2 => $key3 ) {
245 $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file'];
246 }
247 }
248 }
249 }
250
251 $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
252 if ( isset( $metadata['filesize'] ) ) {
253 $file_size = $metadata['filesize'];
254 } else {
255 $file_size = @filesize( get_attached_file( $attach_id ) );
256 }
257
258 return array( $imagethumburls, $stamptime, $file_size );
259
260 }
261
262 /** ==================================================
263 * Webp create
264 *
265 * @param string $filename input filename for pictures.
266 * @param string $mime_type mimetype.
267 * @param string $filename_webp output filename for webp.
268 * @return bool $ret create bool.
269 * @since 1.00
270 */
271 private function create_webp( $filename, $mime_type, $filename_webp ) {
272
273 if ( ! file_exists( $filename ) ) {
274 return false;
275 }
276 if ( file_exists( $filename_webp ) ) {
277 return false;
278 }
279
280 $pluswebp_settings = get_option( 'pluswebp' );
281 @set_time_limit( 60 );
282
283 $ret = false;
284 switch ( $mime_type ) {
285 case 'image/jpeg':
286 $src = imagecreatefromjpeg( $filename );
287 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
288 imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
289 imagealphablending( $img, true );
290 break;
291 case 'image/png':
292 $src = imagecreatefrompng( $filename );
293 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
294 imagealphablending( $img, false );
295 imagesavealpha( $img, true );
296 break;
297 case 'image/bmp':
298 $src = imagecreatefrombmp( $filename );
299 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
300 break;
301 case 'image/gif':
302 $src = imagecreatefromgif( $filename );
303 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
304 $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
305 imagefill( $img, 0, 0, $bgcolor );
306 imagecolortransparent( $img, $bgcolor );
307 break;
308 }
309
310 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
311 imagedestroy( $src );
312 $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
313 imagedestroy( $img );
314
315 return $ret;
316
317 }
318
319 /** ==================================================
320 * Change ext
321 *
322 * @param string $before_file_name before_file_name.
323 * @param string $ext ext.
324 * @return array $after_file_name after_file_name.
325 * @since 1.00
326 */
327 private function change_ext( $before_file_name, $ext ) {
328
329 $exts = explode( '.', $before_file_name );
330 $before_ext = '.' . end( $exts );
331 $after_ext = '.' . $ext;
332 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
333
334 return $after_file_name;
335
336 }
337
338 /** ==================================================
339 * Change DB
340 *
341 * @param string $before_url before_url.
342 * @param string $after_url after_url.
343 * @since 1.00
344 */
345 private function change_db( $before_url, $after_url ) {
346
347 global $wpdb;
348
349 /* Replace */
350 $wpdb->query(
351 $wpdb->prepare(
352 "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
353 $before_url,
354 $after_url
355 )
356 );
357
358 }
359
360 /** ==================================================
361 * Upload Path
362 *
363 * @return array $upload_dir,$upload_url,$upload_path uploadpath.
364 * @since 1.00
365 */
366 public function upload_dir_url_path() {
367
368 $wp_uploads = wp_upload_dir();
369
370 $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
371 if ( $relation_path_true > 0 ) {
372 $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
373 $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
374 $upload_url = $this->realurl( $basepath, $relationalpath );
375 $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
376 } else {
377 $upload_url = $wp_uploads['baseurl'];
378 $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
379 }
380
381 if ( is_ssl() ) {
382 $upload_url = str_replace( 'http:', 'https:', $upload_url );
383 }
384
385 if ( $relation_path_true > 0 ) {
386 $upload_path = $relationalpath;
387 } else {
388 $upload_path = str_replace( site_url( '/' ), '', $upload_url );
389 }
390
391 $upload_dir = untrailingslashit( $upload_dir );
392 $upload_url = untrailingslashit( $upload_url );
393 $upload_path = untrailingslashit( $upload_path );
394
395 return array( $upload_dir, $upload_url, $upload_path );
396
397 }
398
399 /** ==================================================
400 * Real Url
401 *
402 * @param string $base base.
403 * @param string $relationalpath relationalpath.
404 * @return string $realurl realurl.
405 * @since 1.00
406 */
407 private function realurl( $base, $relationalpath ) {
408
409 $parse = array(
410 'scheme' => null,
411 'user' => null,
412 'pass' => null,
413 'host' => null,
414 'port' => null,
415 'query' => null,
416 'fragment' => null,
417 );
418 $parse = wp_parse_url( $base );
419
420 if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) {
421 $parse['path'] .= '.';
422 }
423
424 if ( preg_match( '#^https?://#', $relationalpath ) ) {
425 return $relationalpath;
426 } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) {
427 return $parse['scheme'] . '://' . $parse['host'] . $relationalpath;
428 } else {
429 $base_path = explode( '/', dirname( $parse['path'] ) );
430 $rel_path = explode( '/', $relationalpath );
431 foreach ( $rel_path as $rel_dir_name ) {
432 if ( '.' === $rel_dir_name ) {
433 array_shift( $base_path );
434 array_unshift( $base_path, '' );
435 } elseif ( '..' === $rel_dir_name ) {
436 array_pop( $base_path );
437 if ( count( $base_path ) === 0 ) {
438 $base_path = array( '' );
439 }
440 } else {
441 array_push( $base_path, $rel_dir_name );
442 }
443 }
444 $path = implode( '/', $base_path );
445 return $parse['scheme'] . '://' . $parse['host'] . $path;
446 }
447
448 }
449
450 }
451
452
453