PluginProbe
Plus WebP or AVIF / 2.06
Plus WebP or AVIF v2.06
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 +289 -53 1.132.06 View file →
@@ -60,8 +60,14 @@
60 60
61 61 add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 );
62 62 add_action( 'pluswebp_mail_send', array( $this, 'mail_send' ), 10, 1 );
63 63
64 + /* Ajax */
65 + $action1 = 'bulkgeneratewebp-ajax-action';
66 + $action2 = 'bulkgeneratewebp_message';
67 + add_action( 'wp_ajax_' . $action1, array( $this, 'bulkgeneratewebp_update_callback' ) );
68 + add_action( 'wp_ajax_' . $action2, array( $this, 'bulkgeneratewebp_message_callback' ) );
69 +
64 70 }
65 71
66 72 /** ==================================================
67 73 * Webp generate
@@ -83,11 +89,18 @@
83 89 $metadata_webp['file'] = $file_webp;
84 90 foreach ( (array) $metadata['sizes'] as $key => $value ) {
85 91 $file_thumb = $value['file'];
86 92 $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 );
93 + if ( '.' === dirname( $file_webp ) ) {
94 + $dir_name_url = '/';
95 + $dir_name_path = wp_normalize_path( '/' );
96 + } else {
97 + $dir_name_url = '/' . dirname( $file_webp ) . '/';
98 + $dir_name_path = wp_normalize_path( $dir_name_url );
99 + }
100 + $url = $this->upload_url . $dir_name_url;
101 + $path = $this->upload_dir . $dir_name_path;
102 + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp );
90 103 if ( $ret ) {
91 104 $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
92 105 $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp';
93 106 if ( $replace ) {
@@ -95,10 +108,11 @@
95 108 $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
96 109 }
97 110 }
98 111 }
99 - if ( ! empty( $metadata['original_image'] ) ) {
100 - $org_img_file = wp_get_original_image_path( $attachment_id, false );
112 + $org_img_file = null;
113 + if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) {
114 + $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) );
101 115 $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
102 116 $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
103 117 if ( $ret ) {
104 118 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
@@ -118,8 +132,11 @@
118 132 /* for bulk generate */
119 133 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
120 134 /* delete org file */
121 135 unlink( $this->upload_dir . '/' . $metadata['file'] );
136 + if ( $org_img_file ) {
137 + unlink( $org_img_file );
138 + }
122 139 /* Replace */
123 140 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
124 141 /* for hook */
125 142 $metadata = $metadata_webp;
@@ -134,9 +151,15 @@
134 151 'post_title' => $title,
135 152 'post_content' => '',
136 153 'post_status' => 'inherit',
137 154 );
138 - $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
155 + $file = $this->upload_dir . '/' . $file_webp;
156 + $attach_id = wp_insert_attachment( $attachment, $file );
157 +
158 + /* for XAMPP [ get_attached_file( $attach_id ): Unable to get correct value ] */
159 + $metapath_name = str_replace( $this->upload_dir . '/', '', $file );
160 + update_post_meta( $attach_id, '_wp_attached_file', $metapath_name );
161 +
139 162 wp_update_attachment_metadata( $attach_id, $metadata_webp );
140 163
141 164 $author = get_userdata( $post->post_author );
142 165 $userid = $author->ID;
@@ -164,8 +187,249 @@
164 187
165 188 }
166 189
167 190 /** ==================================================
191 + * IDs Callback
192 + *
193 + * @since 2.00
194 + */
195 + public function bulkgeneratewebp_update_callback() {
196 +
197 + $action1 = 'bulkgeneratewebp-ajax-action';
198 + if ( check_ajax_referer( $action1, 'nonce', false ) ) {
199 + if ( current_user_can( 'manage_options' ) ) {
200 + if ( ! empty( $_POST['id'] ) ) {
201 + $id = absint( $_POST['id'] );
202 + } else {
203 + return;
204 + }
205 + if ( ! wp_get_attachment_url( $id ) ) {
206 + $error_string = __( 'No media!', 'plus-webp' );
207 + $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>';
208 + } else {
209 + $output_html = $this->output_html( $id );
210 + }
211 + if ( ! empty( $output_html ) ) {
212 + header( 'Content-type: text/html; charset=UTF-8' );
213 + $allowed_output_html = array(
214 + 'a' => array(
215 + 'href' => array(),
216 + 'target' => array(),
217 + 'rel' => array(),
218 + 'style' => array(),
219 + ),
220 + 'img' => array(
221 + 'src' => array(),
222 + 'width' => array(),
223 + 'height' => array(),
224 + 'style' => array(),
225 + ),
226 + 'div' => array(
227 + 'style' => array(),
228 + 'class' => array(),
229 + ),
230 + 'font' => array(
231 + 'color' => array(),
232 + ),
233 + 'ul' => array(),
234 + 'li' => array(),
235 + 'span' => array(
236 + 'class' => array(),
237 + 'style' => array(),
238 + ),
239 + );
240 + echo wp_kses( $output_html, $allowed_output_html );
241 + }
242 + }
243 + } else {
244 + status_header( '403' );
245 + echo 'Forbidden';
246 + }
247 +
248 + wp_die();
249 +
250 + }
251 +
252 + /** ==================================================
253 + * Output html for Ajax
254 + *
255 + * @param array $attach_id attach_id.
256 + * @since 2.00
257 + */
258 + private function output_html( $attach_id ) {
259 +
260 + include_once ABSPATH . 'wp-admin/includes/image.php';
261 +
262 + /* Generate thumbnails */
263 + $metadata_org = wp_get_attachment_metadata( $attach_id );
264 + do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
265 + if ( get_option( 'pluswebp_generate' ) ) {
266 + $ids = get_option( 'pluswebp_generate' );
267 + delete_option( 'pluswebp_generate' );
268 + $webp_id = $ids[0];
269 + $metadata = wp_get_attachment_metadata( $webp_id );
270 +
271 + /* Thumbnail urls */
272 + list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url );
273 + /* Output datas*/
274 + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata );
275 +
276 + $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">';
277 + $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">';
278 + $output_html .= '<div style="overflow: hidden;">';
279 + $output_html .= '<div>ID: ' . $webp_id . '</div>';
280 + $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>';
281 + $output_html .= '<div>' . __( 'Permalink:' ) . ' <a href="' . $attachment_link . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_link . '</a></div>';
282 + $output_html .= '<div>URL: <a href="' . $attachment_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_url . '</a></div>';
283 + $output_html .= '<div>' . __( 'File name:' ) . ' ' . $filename . '</div>';
284 + if ( ! empty( $original_image_url ) ) {
285 + $output_html .= '<div>' . __( 'Original URL', 'plus-webp' ) . ': <a href="' . $original_image_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $original_image_url . '</a></div>';
286 + $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . $original_filename . '</div>';
287 + }
288 + $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>';
289 + $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>';
290 + $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>';
291 + if ( ! empty( $imagethumburls ) ) {
292 + $output_html .= '<div>' . __( 'Images' ) . ': ';
293 + foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
294 + $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]';
295 + }
296 + $output_html .= '</div>';
297 + }
298 + $output_html .= '</div></div>';
299 + } else {
300 + $output_html = null;
301 + }
302 +
303 + return $output_html;
304 +
305 + }
306 +
307 + /** ==================================================
308 + * Thumbnail urls
309 + *
310 + * @param int $attach_id attach_id.
311 + * @param array $metadata metadata.
312 + * @param string $upload_url upload_url.
313 + * @return array $image_thumbnail(string), $imagethumburls(array)
314 + * @since 2.00
315 + */
316 + private function thumbnail_urls( $attach_id, $metadata, $upload_url ) {
317 +
318 + $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true );
319 + $image_thumbnail = $image_attr_thumbnail[0];
320 +
321 + $imagethumburls = array();
322 + if ( ! empty( $metadata ) && array_key_exists( 'sizes', $metadata ) ) {
323 + $thumbnails = $metadata['sizes'];
324 + $path_file = get_post_meta( $attach_id, '_wp_attached_file', true );
325 + $filename = wp_basename( $path_file );
326 + $media_path = str_replace( $filename, '', $path_file );
327 + $media_url = $upload_url . '/' . $media_path;
328 + foreach ( $thumbnails as $key => $key2 ) {
329 + $imagethumburls[ $key ] = $media_url . $key2['file'];
330 + }
331 + }
332 +
333 + return array( $image_thumbnail, $imagethumburls );
334 +
335 + }
336 +
337 + /** ==================================================
338 + * Output datas
339 + *
340 + * @param int $attach_id attach_id.
341 + * @param array $metadata metadata.
342 + * @return array (string) $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type(string), $stamptime, $file_size
343 + * @since 2.00
344 + */
345 + private function output_datas( $attach_id, $metadata ) {
346 +
347 + $attachment_link = get_attachment_link( $attach_id );
348 + $attachment_url = wp_get_attachment_url( $attach_id );
349 + $filename = wp_basename( $attachment_url );
350 + if ( ! empty( $metadata ) && array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) {
351 + $original_image_url = wp_get_original_image_url( $attach_id );
352 + $original_filename = wp_basename( $original_image_url );
353 + } else {
354 + $original_image_url = null;
355 + $original_filename = null;
356 + }
357 + $mime_type = get_post_mime_type( $attach_id );
358 +
359 + $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
360 + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) {
361 + $file_size = $metadata['filesize'];
362 + } else {
363 + $file_size = @filesize( get_attached_file( $attach_id ) );
364 + }
365 + if ( ! $file_size ) {
366 + $file_size = __( 'Could not retrieve.', 'plus-webp' );
367 + } else {
368 + $file_size = size_format( $file_size );
369 + }
370 +
371 + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size );
372 +
373 + }
374 +
375 + /** ==================================================
376 + * Messages Callback
377 + *
378 + * @since 2.00
379 + */
380 + public function bulkgeneratewebp_message_callback() {
381 +
382 + $action2 = 'bulkgeneratewebp_message';
383 + if ( check_ajax_referer( $action2, 'nonce', false ) ) {
384 + $error_count = 0;
385 + $error_update = null;
386 + $success_count = 0;
387 + if ( ! empty( $_POST['error_count'] ) ) {
388 + $error_count = absint( $_POST['error_count'] );
389 + }
390 + if ( ! empty( $_POST['error_update'] ) ) {
391 + $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) );
392 + }
393 + if ( ! empty( $_POST['success_count'] ) ) {
394 + $success_count = absint( $_POST['success_count'] );
395 + }
396 +
397 + $back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>';
398 +
399 + $output_html = null;
400 + if ( $error_count > 0 ) {
401 + /* translators: error message %1$d: media count %2$s: back link */
402 + $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html );
403 + $output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>';
404 + }
405 + if ( $success_count > 0 ) {
406 + /* translators: success message %1$d: media count %2$s: back link */
407 + $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html );
408 + $output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>';
409 + }
410 +
411 + header( 'Content-type: text/html; charset=UTF-8' );
412 + $allowed_output_html = array(
413 + 'div' => array(
414 + 'class' => array(),
415 + ),
416 + 'ul' => array(),
417 + 'li' => array(),
418 + 'strong' => array(),
419 + 'a' => array(
420 + 'style' => array(),
421 + 'href' => array(),
422 + ),
423 + );
424 + echo wp_kses( $output_html, $allowed_output_html );
425 + }
426 +
427 + wp_die();
428 +
429 + }
430 +
431 + /** ==================================================
168 432 * Mail send hook
169 433 *
170 434 * @param string $to to.
171 435 * @since 1.09
@@ -192,29 +456,27 @@
192 456 $pluswebp_mail_send['count'] = count( $post_ids );
193 457
194 458 $count = 0;
195 459 foreach ( $post_ids as $attach_id ) {
196 - $filename = get_attached_file( $attach_id );
197 460 $metadata = wp_get_attachment_metadata( $attach_id );
198 - /* OutputMetaData */
199 - list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url );
461 +
462 + /* Thumbnail urls */
463 + list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $attach_id, $metadata, $this->upload_url );
464 + /* Output datas*/
465 + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $attach_id, $metadata );
466 +
200 467 $count++;
201 468 $message .= __( 'Count' ) . ': ' . $count . "\n";
202 469 $message .= 'ID: ' . $attach_id . "\n";
203 470 $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";
471 + $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n";
472 + $message .= 'URL: ' . $attachment_url . "\n";
473 + $message .= __( 'File name:' ) . ' ' . $filename . "\n";
474 + if ( ! empty( $original_image_url ) ) {
475 + $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n";
476 + $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n";
210 477 }
211 478 $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 479 $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
218 480 if ( ! empty( $imagethumburls ) ) {
219 481 foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
220 482 $message .= $thumbsize . ': ' . $imagethumburl . "\n";
@@ -235,39 +497,8 @@
235 497
236 498 }
237 499
238 500 /** ==================================================
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 501 * Webp create
271 502 *
272 503 * @param string $filename input filename for pictures.
273 504 * @param string $mime_type mimetype.
@@ -285,8 +516,9 @@
285 516 }
286 517
287 518 $pluswebp_settings = get_option( 'pluswebp' );
288 519 @set_time_limit( 60 );
520 + @ini_set( 'memory_limit', '256M' );
289 521
290 522 $ret = false;
291 523 switch ( $mime_type ) {
292 524 case 'image/jpeg':
@@ -291,9 +523,10 @@
291 523 switch ( $mime_type ) {
292 524 case 'image/jpeg':
293 525 $src = imagecreatefromjpeg( $filename );
294 526 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
295 - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
527 + $bgcolor = imagecolorallocate( $img, 255, 255, 255 );
528 + imagefill( $img, 0, 0, $bgcolor );
296 529 imagealphablending( $img, true );
297 530 break;
298 531 case 'image/png':
299 532 $src = imagecreatefrompng( $filename );
@@ -360,9 +593,12 @@
360 593
361 594 /* Replace */
362 595 $wpdb->query(
363 596 $wpdb->prepare(
364 - "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
597 + "
598 + UPDATE {$wpdb->prefix}posts
599 + SET post_content = replace( post_content, %s, %s )
600 + ",
365 601 $before_url,
366 602 $after_url
367 603 )
368 604 );