PluginProbe
Plus WebP or AVIF / 5.03
Plus WebP or AVIF v5.03
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 +234 -398 2.025.03 View file →
@@ -1,9 +1,9 @@
1 1 <?php
2 2 /**
3 - * Plus WebP
3 + * Plus WebP or AVIF
4 4 *
5 - * @package Plus WebP
5 + * @package Plus WebP or AVIF
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
@@ -42,15 +42,8 @@
42 42 */
43 43 private $upload_url;
44 44
45 45 /** ==================================================
46 - * Path
47 - *
48 - * @var $upload_path PATH.
49 - */
50 - private $upload_path;
51 -
52 - /** ==================================================
53 46 * Construct
54 47 *
55 48 * @since 1.00
56 49 */
@@ -55,23 +48,33 @@
55 48 * @since 1.00
56 49 */
57 50 public function __construct() {
58 51
59 - list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path();
52 + $wp_uploads = wp_upload_dir();
60 53
54 + $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
55 + $upload_url = $wp_uploads['baseurl'];
56 + if ( is_ssl() ) {
57 + $upload_url = str_replace( 'http:', 'https:', $upload_url );
58 + }
59 + $this->upload_dir = untrailingslashit( $upload_dir );
60 + $this->upload_url = untrailingslashit( $upload_url );
61 +
62 + /* Generate metadata */
61 63 add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 );
62 - add_action( 'pluswebp_mail_send', array( $this, 'mail_send' ), 10, 1 );
64 + /* Original hook */
65 + add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 2 );
66 + add_filter( 'pluswebp_mail_messages', array( $this, 'mail_messages' ), 10, 2 );
67 + add_action( 'pluswebp_mail_generate_message', array( $this, 'mail_generate_message' ), 10, 2 );
63 68
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 -
69 + /* Raise memory */
70 + add_filter( 'pluswebp_memory_limit', array( $this, 'raise_memory_limit' ) );
71 + /* webp generation control */
72 + add_filter( 'wp_upload_image_mime_transforms', array( $this, 'control_mime_type' ), 10, 2 );
70 73 }
71 74
72 75 /** ==================================================
73 - * Webp generate
76 + * Webp of AVIF generate
74 77 *
75 78 * @param array $metadata metadata.
76 79 * @param int $attachment_id ID.
77 80 * @return array $metadata metadata.
@@ -79,53 +82,70 @@
79 82 */
80 83 public function generate_webp( $metadata, $attachment_id ) {
81 84
82 85 $pluswebp_settings = get_option( 'pluswebp' );
83 - $replace = $pluswebp_settings['replace'];
84 86
87 + switch ( $pluswebp_settings['output_mime'] ) {
88 + case 'image/webp':
89 + $ext = 'webp';
90 + break;
91 + case 'image/avif':
92 + $ext = 'avif';
93 + break;
94 + default:
95 + $ext = 'webp';
96 + }
97 +
85 98 $mime_type = get_post_mime_type( $attachment_id );
86 99 if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) {
87 100 $metadata_webp = $metadata;
88 - $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] );
101 + $file_webp = $this->change_ext( $metadata['file'], $ext, $pluswebp_settings['addext'] );
89 102 $metadata_webp['file'] = $file_webp;
103 + if ( '.' === dirname( $file_webp ) ) {
104 + $dir_name_url = '/';
105 + $dir_name_path = wp_normalize_path( '/' );
106 + } else {
107 + $dir_name_url = '/' . dirname( $file_webp ) . '/';
108 + $dir_name_path = wp_normalize_path( $dir_name_url );
109 + }
110 + $url = $this->upload_url . $dir_name_url;
111 + $path = $this->upload_dir . $dir_name_path;
112 +
90 113 foreach ( (array) $metadata['sizes'] as $key => $value ) {
91 114 $file_thumb = $value['file'];
92 - $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] );
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 );
115 + $file_thumb_webp = $this->change_ext( $file_thumb, $ext, $pluswebp_settings['addext'] );
116 + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp, $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
103 117 if ( $ret ) {
104 118 $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp;
105 - $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp';
106 - if ( $replace ) {
107 - unlink( $path . $file_thumb );
119 + $metadata_webp['sizes'][ $key ]['mime-type'] = $pluswebp_settings['output_mime'];
120 + $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) );
121 + $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size;
122 + if ( $pluswebp_settings['replace'] ) {
123 + wp_delete_file( $path . $file_thumb );
108 124 $this->change_db( $url . $file_thumb, $url . $file_thumb_webp );
109 125 }
110 126 }
111 127 }
112 - if ( ! empty( $metadata['original_image'] ) ) {
113 - $org_img_file = wp_get_original_image_path( $attachment_id, false );
114 - $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] );
115 - $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file );
128 +
129 + $org_img_file = null;
130 + if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) {
131 + $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) );
132 + $org_webp_file = $this->change_ext( $org_img_file, $ext, $pluswebp_settings['addext'] );
133 + $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file, $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
116 134 if ( $ret ) {
117 135 $metadata_webp['original_image'] = wp_basename( $org_webp_file );
118 136 }
119 137 }
120 138
121 - $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp );
139 + $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ), $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] );
140 + $webp_size = filesize( $path . wp_basename( $file_webp ) );
141 + $metadata_webp['filesize'] = $webp_size;
122 142 if ( $ret ) {
123 - if ( $replace ) {
143 + if ( $pluswebp_settings['replace'] ) {
124 144 $up_post = array(
125 145 'ID' => $attachment_id,
126 146 'guid' => $this->upload_url . '/' . $file_webp,
127 - 'post_mime_type' => 'image/webp',
147 + 'post_mime_type' => $pluswebp_settings['output_mime'],
128 148 );
129 149 wp_update_post( $up_post );
130 150 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
131 151 /* for bulk generate */
@@ -130,9 +150,12 @@
130 150 update_post_meta( $attachment_id, '_wp_attached_file', $file_webp );
131 151 /* for bulk generate */
132 152 update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp );
133 153 /* delete org file */
134 - unlink( $this->upload_dir . '/' . $metadata['file'] );
154 + wp_delete_file( $this->upload_dir . '/' . $metadata['file'] );
155 + if ( $org_img_file ) {
156 + wp_delete_file( $org_img_file );
157 + }
135 158 /* Replace */
136 159 $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp );
137 160 /* for hook */
138 161 $metadata = $metadata_webp;
@@ -139,17 +162,23 @@
139 162 /* for mail */
140 163 $attach_id = $attachment_id;
141 164 } else {
142 165 $post = get_post( $attachment_id );
143 - $title = get_the_title( $post );
166 + $title = $post->post_title;
144 167 $attachment = array(
145 168 'guid' => $this->upload_url . '/' . $file_webp,
146 - 'post_mime_type' => 'image/webp',
169 + 'post_mime_type' => $pluswebp_settings['output_mime'],
147 170 'post_title' => $title,
148 171 'post_content' => '',
149 172 'post_status' => 'inherit',
150 173 );
151 - $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp );
174 + $file = $this->upload_dir . '/' . $file_webp;
175 + $attach_id = wp_insert_attachment( $attachment, $file );
176 +
177 + /* for XAMPP [ get_attached_file( $attach_id ): Unable to get correct value ] */
178 + $metapath_name = str_replace( $this->upload_dir . '/', '', $file );
179 + update_post_meta( $attach_id, '_wp_attached_file', $metapath_name );
180 +
152 181 wp_update_attachment_metadata( $attach_id, $metadata_webp );
153 182
154 183 $author = get_userdata( $post->post_author );
155 184 $userid = $author->ID;
@@ -165,137 +194,23 @@
165 194 );
166 195 wp_update_post( $up_post );
167 196 }
168 197
169 - $ids = get_option( 'pluswebp_generate' );
170 - $ids[] = $attach_id;
171 - update_option( 'pluswebp_generate', $ids );
198 + update_option( 'pluswebp_generate', $attach_id );
172 199
173 - }
174 - }
200 + /* for Media Library folders term by Organize Media Folder */
201 + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id );
175 202
176 - return $metadata;
203 + /* for Term filter update by Organize Media Folder */
204 + do_action( 'omf_term_filter_update' );
177 205
178 - }
179 -
180 - /** ==================================================
181 - * IDs Callback
182 - *
183 - * @since 2.00
184 - */
185 - public function bulkgeneratewebp_update_callback() {
186 -
187 - $action1 = 'bulkgeneratewebp-ajax-action';
188 - if ( check_ajax_referer( $action1, 'nonce', false ) ) {
189 - if ( current_user_can( 'manage_options' ) ) {
190 - if ( ! empty( $_POST['id'] ) ) {
191 - $id = absint( $_POST['id'] );
192 - } else {
193 - return;
194 - }
195 - if ( ! wp_get_attachment_url( $id ) ) {
196 - $error_string = __( 'No media!', 'plus-webp' );
197 - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>';
198 - } else {
199 - $output_html = $this->output_html( $id );
200 - }
201 - if ( ! empty( $output_html ) ) {
202 - header( 'Content-type: text/html; charset=UTF-8' );
203 - $allowed_output_html = array(
204 - 'a' => array(
205 - 'href' => array(),
206 - 'target' => array(),
207 - 'rel' => array(),
208 - 'style' => array(),
209 - ),
210 - 'img' => array(
211 - 'src' => array(),
212 - 'width' => array(),
213 - 'height' => array(),
214 - 'style' => array(),
215 - ),
216 - 'div' => array(
217 - 'style' => array(),
218 - 'class' => array(),
219 - ),
220 - 'font' => array(
221 - 'color' => array(),
222 - ),
223 - 'ul' => array(),
224 - 'li' => array(),
225 - 'span' => array(
226 - 'class' => array(),
227 - 'style' => array(),
228 - ),
229 - );
230 - echo wp_kses( $output_html, $allowed_output_html );
231 - }
232 206 }
233 - } else {
234 - status_header( '403' );
235 - echo 'Forbidden';
236 207 }
237 208
238 - wp_die();
239 -
209 + return $metadata;
240 210 }
241 211
242 212 /** ==================================================
243 - * Output html for Ajax
244 - *
245 - * @param array $attach_id attach_id.
246 - * @since 2.00
247 - */
248 - private function output_html( $attach_id ) {
249 -
250 - include_once ABSPATH . 'wp-admin/includes/image.php';
251 -
252 - /* Generate thumbnails */
253 - $metadata_org = wp_get_attachment_metadata( $attach_id );
254 - do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id );
255 - if ( get_option( 'pluswebp_generate' ) ) {
256 - $ids = get_option( 'pluswebp_generate' );
257 - delete_option( 'pluswebp_generate' );
258 - $webp_id = $ids[0];
259 - $metadata = wp_get_attachment_metadata( $webp_id );
260 -
261 - /* Thumbnail urls */
262 - list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url );
263 - /* Output datas*/
264 - list( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id );
265 -
266 - $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">';
267 - $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">';
268 - $output_html .= '<div style="overflow: hidden;">';
269 - $output_html .= '<div>ID: ' . $webp_id . '</div>';
270 - $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>';
271 - $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>';
272 - $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>';
273 - $output_html .= '<div>' . __( 'File name:' ) . ' ' . wp_basename( $attachment_url ) . '</div>';
274 - if ( ! empty( $metadata['original_image'] ) ) {
275 - $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>';
276 - $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . wp_basename( $original_image_url ) . '</div>';
277 - }
278 - $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>';
279 - $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>';
280 - $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>';
281 - if ( ! empty( $imagethumburls ) ) {
282 - $output_html .= '<div>' . __( 'Images' ) . ': ';
283 - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
284 - $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]';
285 - }
286 - $output_html .= '</div>';
287 - }
288 - $output_html .= '</div></div>';
289 - } else {
290 - $output_html = null;
291 - }
292 -
293 - return $output_html;
294 -
295 - }
296 -
297 - /** ==================================================
298 213 * Thumbnail urls
299 214 *
300 215 * @param int $attach_id attach_id.
301 216 * @param array $metadata metadata.
@@ -308,15 +223,14 @@
308 223 $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true );
309 224 $image_thumbnail = $image_attr_thumbnail[0];
310 225
311 226 $imagethumburls = array();
312 - $media_path = get_post_meta( $attach_id, '_wp_attached_file', true );
313 - $media_paths = explode( DIRECTORY_SEPARATOR, $media_path );
314 - $str_length = strlen( end( $media_paths ) );
315 - $media_path = substr( $media_path, 0, -$str_length );
316 - $media_url = $upload_url . '/' . $media_path;
317 - if ( ! empty( $metadata ) ) {
318 - $thumbnails = $metadata['sizes'];
227 + if ( ! empty( $metadata ) && array_key_exists( 'sizes', $metadata ) ) {
228 + $thumbnails = $metadata['sizes'];
229 + $path_file = get_post_meta( $attach_id, '_wp_attached_file', true );
230 + $filename = wp_basename( $path_file );
231 + $media_path = str_replace( $filename, '', $path_file );
232 + $media_url = $upload_url . '/' . $media_path;
319 233 foreach ( $thumbnails as $key => $key2 ) {
320 234 $imagethumburls[ $key ] = $media_url . $key2['file'];
321 235 }
322 236 }
@@ -321,210 +235,179 @@
321 235 }
322 236 }
323 237
324 238 return array( $image_thumbnail, $imagethumburls );
325 -
326 239 }
327 240
328 241 /** ==================================================
329 242 * Output datas
330 243 *
331 - * @param int $attach_id attach_id.
332 - * @return array (string) $attachment_link, $attachment_url, $original_image_url, $mime_type(string), $stamptime, $file_size
244 + * @param int $attach_id attach_id.
245 + * @param array $metadata metadata.
246 + * @return array (string) $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type(string), $stamptime, $file_size
333 247 * @since 2.00
334 248 */
335 - private function output_datas( $attach_id ) {
249 + private function output_datas( $attach_id, $metadata ) {
336 250
337 251 $attachment_link = get_attachment_link( $attach_id );
338 252 $attachment_url = wp_get_attachment_url( $attach_id );
339 - $original_image_url = wp_get_original_image_url( $attach_id );
340 - $filetype = wp_check_filetype( get_attached_file( $attach_id ) );
341 - $mime_type = $filetype['type'];
253 + $filename = wp_basename( $attachment_url );
254 + if ( ! empty( $metadata ) && array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) {
255 + $original_image_url = wp_get_original_image_url( $attach_id );
256 + $original_filename = wp_basename( $original_image_url );
257 + } else {
258 + $original_image_url = null;
259 + $original_filename = null;
260 + }
261 + $mime_type = get_post_mime_type( $attach_id );
342 262
343 263 $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
344 - if ( isset( $metadata['filesize'] ) ) {
264 + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) {
345 265 $file_size = $metadata['filesize'];
346 266 } else {
347 267 $file_size = @filesize( get_attached_file( $attach_id ) );
348 268 }
349 269 if ( ! $file_size ) {
350 - $file_size = '<font color="red">' . __( 'Could not retrieve.', 'plus-webp' ) . '</font>';
270 + $file_size = __( 'Could not retrieve.', 'plus-webp' );
351 271 } else {
352 272 $file_size = size_format( $file_size );
353 273 }
354 274
355 - return array( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size );
356 -
275 + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size );
357 276 }
358 277
359 278 /** ==================================================
360 - * Messages Callback
279 + * Get All Images
361 280 *
281 + * @param array $types mime types.
282 + * @param string $output_mime output mime type.
362 283 * @since 2.00
363 284 */
364 - public function bulkgeneratewebp_message_callback() {
285 + public function get_allimages( $types, $output_mime ) {
365 286
366 - $action2 = 'bulkgeneratewebp_message';
367 - if ( check_ajax_referer( $action2, 'nonce', false ) ) {
368 - $error_count = 0;
369 - $error_update = null;
370 - $success_count = 0;
371 - if ( ! empty( $_POST['error_count'] ) ) {
372 - $error_count = absint( $_POST['error_count'] );
373 - }
374 - if ( ! empty( $_POST['error_update'] ) ) {
375 - $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) );
376 - }
377 - if ( ! empty( $_POST['success_count'] ) ) {
378 - $success_count = absint( $_POST['success_count'] );
379 - }
287 + if ( empty( $types ) ) {
288 + return array( array(), array() );
289 + }
380 290
381 - $back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>';
291 + $args = array(
292 + 'post_type' => 'attachment',
293 + 'post_status' => 'inherit',
294 + 'post_mime_type' => $types,
295 + 'posts_per_page' => -1,
296 + 'orderby' => 'date',
297 + 'order' => 'DESC',
298 + );
299 + $posts = get_posts( $args );
382 300
383 - $output_html = null;
384 - if ( $error_count > 0 ) {
385 - /* translators: error message %1$d: media count %2$s: back link */
386 - $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html );
387 - $output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>';
301 + global $wpdb;
302 + $webp_titles = $wpdb->get_col(
303 + $wpdb->prepare(
304 + "
305 + SELECT post_title
306 + FROM {$wpdb->prefix}posts
307 + WHERE post_type = 'attachment'
308 + AND post_mime_type IN ( %s )
309 + AND post_status = 'inherit'
310 + ",
311 + $output_mime,
312 + )
313 + );
314 +
315 + $post_ids = array();
316 + $no_file_ids = array();
317 + $count = 0;
318 + foreach ( $posts as $post ) {
319 + if ( ! in_array( $post->post_title, $webp_titles ) ) {
320 + if ( file_exists( get_attached_file( $post->ID ) ) ) {
321 + $post_ids[] = $post->ID;
322 + } else {
323 + $no_file_ids[] = $post->ID;
324 + }
388 325 }
389 - if ( $success_count > 0 ) {
390 - /* translators: success message %1$d: media count %2$s: back link */
391 - $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html );
392 - $output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>';
393 - }
394 -
395 - header( 'Content-type: text/html; charset=UTF-8' );
396 - $allowed_output_html = array(
397 - 'div' => array(
398 - 'class' => array(),
399 - ),
400 - 'ul' => array(),
401 - 'li' => array(),
402 - 'strong' => array(),
403 - 'a' => array(
404 - 'style' => array(),
405 - 'href' => array(),
406 - ),
407 - );
408 - echo wp_kses( $output_html, $allowed_output_html );
409 326 }
410 327
411 - wp_die();
412 -
328 + return array( $post_ids, $no_file_ids );
413 329 }
414 330
415 331 /** ==================================================
416 - * Mail send hook
332 + * Mail messages hook
417 333 *
418 - * @param string $to to.
419 - * @since 1.09
334 + * @param array $messages messages.
335 + * @param int $webp_id webp ID or avif ID.
336 + *
337 + * @since 4.00
420 338 */
421 - public function mail_send( $to ) {
339 + public function mail_messages( $messages, $webp_id ) {
422 340
423 - $post_ids = get_option( 'pluswebp_generate' );
424 - delete_option( 'pluswebp_generate' );
341 + $metadata = wp_get_attachment_metadata( $webp_id );
425 342
426 - if ( function_exists( 'wp_date' ) ) {
427 - $now_date_time = wp_date( 'Y-m-d H:i:s' );
428 - } else {
429 - $now_date_time = date_i18n( 'Y-m-d H:i:s' );
430 - }
431 - $pluswebp_mail_send = array();
432 - $pluswebp_mail_send['datetime'] = $now_date_time;
343 + $message = null;
344 + if ( $metadata ) {
345 + /* Thumbnail urls */
346 + list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url );
347 + /* Output datas*/
348 + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata );
433 349
434 - /* translators: Date and Time */
435 - $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n";
436 -
437 - if ( ! empty( $post_ids ) ) {
438 -
439 - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n";
440 - $pluswebp_mail_send['count'] = count( $post_ids );
441 -
442 - $count = 0;
443 - foreach ( $post_ids as $attach_id ) {
444 - $filename = get_attached_file( $attach_id );
445 - $metadata = wp_get_attachment_metadata( $attach_id );
446 - /* OutputMetaData */
447 - list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url );
448 - $count++;
449 - $message .= __( 'Count' ) . ': ' . $count . "\n";
450 - $message .= 'ID: ' . $attach_id . "\n";
451 - $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n";
452 - $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n";
453 - $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n";
454 - $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n";
455 - if ( ! empty( $metadata['original_image'] ) ) {
456 - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n";
457 - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $attach_id ) ) . "\n";
350 + $message = 'ID: ' . $webp_id . "\n";
351 + $message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $webp_id ) . "\n";
352 + $message .= __( 'Permalink:', 'plus-webp' ) . ' ' . $attachment_link . "\n";
353 + $message .= 'URL: ' . $attachment_url . "\n";
354 + $message .= __( 'File name:', 'plus-webp' ) . ' ' . $filename . "\n";
355 + if ( ! empty( $original_image_url ) ) {
356 + $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n";
357 + $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n";
358 + }
359 + $message .= __( 'Date/Time', 'plus-webp' ) . ': ' . $stamptime . "\n";
360 + $message .= __( 'File size:', 'plus-webp' ) . ' ' . $file_size . "\n";
361 + if ( ! empty( $imagethumburls ) ) {
362 + foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
363 + $message .= $thumbsize . ': ' . $imagethumburl . "\n";
458 364 }
459 - $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n";
460 - if ( ! $file_size ) {
461 - $file_size = __( 'Could not retrieve.', 'plus-webp' );
462 - } else {
463 - $file_size = size_format( $file_size );
464 - }
465 - $message .= __( 'File size:' ) . ' ' . $file_size . "\n";
466 - if ( ! empty( $imagethumburls ) ) {
467 - foreach ( $imagethumburls as $thumbsize => $imagethumburl ) {
468 - $message .= $thumbsize . ': ' . $imagethumburl . "\n";
469 - }
470 - }
471 - $message .= "\n";
472 365 }
473 - } else {
474 - $message .= __( 'WebP was not generated. A file with the same name already exists.', 'plus-webp' ) . "\r\n\r\n";
475 - $pluswebp_mail_send['count'] = 0;
366 + $message .= "\n";
367 + $messages[] = $message;
476 368 }
477 369
478 - update_option( 'pluswebp_generate_mail', $pluswebp_mail_send );
479 -
480 - /* translators: blogname for subject */
481 - $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) );
482 - wp_mail( $to, $subject, $message );
483 -
370 + return array( $message, $messages );
484 371 }
485 372
486 373 /** ==================================================
487 - * Output metadata
374 + * Mail sent for Generate Messages
488 375 *
489 - * @param int $attach_id attach_id.
490 - * @param array $metadata metadata.
491 - * @param string $upload_url upload_url.
492 - * @return array $imagethumburls(string), $stamptime(string), $file_size(string)
493 - * @since 1.09
376 + * @param array $messages mail messages.
377 + * @param int $count convert count.
378 + * @since 4.02
494 379 */
495 - private function output_metadata( $attach_id, $metadata, $upload_url ) {
380 + public function mail_generate_message( $messages, $count ) {
496 381
497 - $imagethumburls = array();
498 - $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true );
499 - $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) );
500 - if ( ! empty( $metadata ) ) {
501 - foreach ( $metadata as $key1 => $key2 ) {
502 - if ( 'sizes' === $key1 ) {
503 - foreach ( $metadata[ $key1 ] as $key2 => $key3 ) {
504 - $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file'];
505 - }
506 - }
507 - }
382 + if ( function_exists( 'wp_date' ) ) {
383 + $now_date_time = wp_date( 'Y-m-d H:i:s' );
384 + } else {
385 + $now_date_time = date_i18n( 'Y-m-d H:i:s' );
508 386 }
387 + /* translators: Date and Time */
388 + $message_head = sprintf( __( 'Plus WebP or AVIF : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n";
389 + /* translators: Generated count */
390 + $message_head .= sprintf( __( 'Webp or AVIF generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n";
509 391
510 - $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id );
511 - $file_size = @filesize( get_attached_file( $attach_id, false ) );
512 -
513 - return array( $imagethumburls, $stamptime, $file_size );
514 -
392 + $to = get_option( 'admin_email' );
393 + /* translators: blogname for subject */
394 + $subject = sprintf( __( '[%s] WebP or AVIF generate', 'plus-webp' ), get_option( 'blogname' ) );
395 + wp_mail( $to, $subject, $message_head . implode( $messages ) );
515 396 }
516 397
517 398 /** ==================================================
518 - * Webp create
399 + * WebP or AVIF create
519 400 *
520 401 * @param string $filename input filename for pictures.
521 402 * @param string $mime_type mimetype.
522 - * @param string $filename_webp output filename for webp.
403 + * @param string $filename_webp output filename for webp or avif.
404 + * @param int $quality quality of webp.
405 + * @param string $output_mime output mime type.
523 406 * @return bool $ret create bool.
524 407 * @since 1.00
525 408 */
526 - private function create_webp( $filename, $mime_type, $filename_webp ) {
409 + private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) {
527 410
528 411 if ( ! file_exists( $filename ) ) {
529 412 return false;
530 413 }
@@ -531,10 +414,10 @@
531 414 if ( file_exists( $filename_webp ) ) {
532 415 return false;
533 416 }
534 417
535 - $pluswebp_settings = get_option( 'pluswebp' );
536 418 @set_time_limit( 60 );
419 + wp_raise_memory_limit( 'pluswebp' );
537 420
538 421 $ret = false;
539 422 switch ( $mime_type ) {
540 423 case 'image/jpeg':
@@ -539,9 +422,10 @@
539 422 switch ( $mime_type ) {
540 423 case 'image/jpeg':
541 424 $src = imagecreatefromjpeg( $filename );
542 425 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
543 - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) );
426 + $bgcolor = imagecolorallocate( $img, 255, 255, 255 );
427 + imagefill( $img, 0, 0, $bgcolor );
544 428 imagealphablending( $img, true );
545 429 break;
546 430 case 'image/png':
547 431 $src = imagecreatefrompng( $filename );
@@ -547,8 +431,11 @@
547 431 $src = imagecreatefrompng( $filename );
548 432 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
549 433 imagealphablending( $img, false );
550 434 imagesavealpha( $img, true );
435 + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
436 + imagefill( $img, 0, 0, $bgcolor );
437 + imagecolortransparent( $img, $bgcolor );
551 438 break;
552 439 case 'image/bmp':
553 440 $src = imagecreatefrombmp( $filename );
554 441 $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) );
@@ -563,13 +450,23 @@
563 450 }
564 451
565 452 imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) );
566 453 imagedestroy( $src );
567 - $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] );
454 +
455 + switch ( $output_mime ) {
456 + case 'image/webp':
457 + $ret = imagewebp( $img, $filename_webp, $quality );
458 + break;
459 + case 'image/avif':
460 + $ret = imageavif( $img, $filename_webp, $quality );
461 + break;
462 + default:
463 + $ret = imagewebp( $img, $filename_webp, $quality );
464 + }
465 +
568 466 imagedestroy( $img );
569 467
570 468 return $ret;
571 -
572 469 }
573 470
574 471 /** ==================================================
575 472 * Change ext
@@ -591,9 +488,8 @@
591 488 $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name );
592 489 }
593 490
594 491 return $after_file_name;
595 -
596 492 }
597 493
598 494 /** ==================================================
599 495 * Change DB
@@ -608,105 +504,45 @@
608 504
609 505 /* Replace */
610 506 $wpdb->query(
611 507 $wpdb->prepare(
612 - "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)",
508 + "
509 + UPDATE {$wpdb->prefix}posts
510 + SET post_content = replace( post_content, %s, %s )
511 + ",
613 512 $before_url,
614 513 $after_url
615 514 )
616 515 );
617 516
517 + /* Advanced change database */
518 + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url );
618 519 }
619 520
620 521 /** ==================================================
621 - * Upload Path
522 + * Filter Raise Memory limit
622 523 *
623 - * @return array $upload_dir,$upload_url,$upload_path uploadpath.
624 - * @since 1.00
524 + * @since 3.00
525 + *
526 + * @param string $filtered_limit Memory limit.
625 527 */
626 - public function upload_dir_url_path() {
528 + public function raise_memory_limit( $filtered_limit ) {
627 529
628 - $wp_uploads = wp_upload_dir();
629 -
630 - $relation_path_true = strpos( $wp_uploads['baseurl'], '../' );
631 - if ( $relation_path_true > 0 ) {
632 - $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true );
633 - $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true );
634 - $upload_url = $this->realurl( $basepath, $relationalpath );
635 - $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) );
636 - } else {
637 - $upload_url = $wp_uploads['baseurl'];
638 - $upload_dir = wp_normalize_path( $wp_uploads['basedir'] );
639 - }
640 -
641 - if ( is_ssl() ) {
642 - $upload_url = str_replace( 'http:', 'https:', $upload_url );
643 - }
644 -
645 - if ( $relation_path_true > 0 ) {
646 - $upload_path = $relationalpath;
647 - } else {
648 - $upload_path = str_replace( site_url( '/' ), '', $upload_url );
649 - }
650 -
651 - $upload_dir = untrailingslashit( $upload_dir );
652 - $upload_url = untrailingslashit( $upload_url );
653 - $upload_path = untrailingslashit( $upload_path );
654 -
655 - return array( $upload_dir, $upload_url, $upload_path );
656 -
530 + return '256M';
657 531 }
658 532
659 533 /** ==================================================
660 - * Real Url
534 + * Filter the output mime types for a given input mime type and image size.
661 535 *
662 - * @param string $base base.
663 - * @param string $relationalpath relationalpath.
664 - * @return string $realurl realurl.
665 - * @since 1.00
536 + * @since 3.00
537 + *
538 + * @param array $image_mime_transforms A map with the valid mime transforms where the key is the source file mime type
539 + * and the value is one or more mime file types to generate.
540 + * @param int $attachment_id The ID of the attachment where the hook was dispatched.
666 541 */
667 - private function realurl( $base, $relationalpath ) {
542 + public function control_mime_type( $image_mime_transforms, $attachment_id ) {
668 543
669 - $parse = array(
670 - 'scheme' => null,
671 - 'user' => null,
672 - 'pass' => null,
673 - 'host' => null,
674 - 'port' => null,
675 - 'query' => null,
676 - 'fragment' => null,
677 - );
678 - $parse = wp_parse_url( $base );
544 + $image_mime_transforms = array();
679 545
680 - if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) {
681 - $parse['path'] .= '.';
682 - }
683 -
684 - if ( preg_match( '#^https?://#', $relationalpath ) ) {
685 - return $relationalpath;
686 - } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) {
687 - return $parse['scheme'] . '://' . $parse['host'] . $relationalpath;
688 - } else {
689 - $base_path = explode( '/', dirname( $parse['path'] ) );
690 - $rel_path = explode( '/', $relationalpath );
691 - foreach ( $rel_path as $rel_dir_name ) {
692 - if ( '.' === $rel_dir_name ) {
693 - array_shift( $base_path );
694 - array_unshift( $base_path, '' );
695 - } elseif ( '..' === $rel_dir_name ) {
696 - array_pop( $base_path );
697 - if ( count( $base_path ) === 0 ) {
698 - $base_path = array( '' );
699 - }
700 - } else {
701 - array_push( $base_path, $rel_dir_name );
702 - }
703 - }
704 - $path = implode( '/', $base_path );
705 - return $parse['scheme'] . '://' . $parse['host'] . $path;
706 - }
707 -
546 + return $image_mime_transforms;
708 547 }
709 -
710 548 }
711 -
712 -