| @@ -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,17 +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 | |
| 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 ); | |
| 64 | 73 | } |
| 65 | 74 | |
| 66 | 75 | /** ================================================== |
| 67 | - * Webp generate | |
| 76 | + * Webp of AVIF generate | |
| 68 | 77 | * |
| 69 | 78 | * @param array $metadata metadata. |
| 70 | 79 | * @param int $attachment_id ID. |
| 71 | 80 | * @return array $metadata metadata. |
| @@ -73,38 +82,70 @@ | ||
| 73 | 82 | */ |
| 74 | 83 | public function generate_webp( $metadata, $attachment_id ) { |
| 75 | 84 | |
| 76 | 85 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 77 | - $replace = $pluswebp_settings['replace']; | |
| 78 | 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 | + | |
| 79 | 98 | $mime_type = get_post_mime_type( $attachment_id ); |
| 80 | 99 | if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { |
| 81 | 100 | $metadata_webp = $metadata; |
| 82 | - $file_webp = $this->change_ext( $metadata['file'], 'webp' ); | |
| 101 | + $file_webp = $this->change_ext( $metadata['file'], $ext, $pluswebp_settings['addext'] ); | |
| 83 | 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 | + | |
| 84 | 113 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 85 | 114 | $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 ); | |
| 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'] ); | |
| 90 | 117 | if ( $ret ) { |
| 91 | 118 | $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 ); | |
| 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 ); | |
| 95 | 124 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 96 | 125 | } |
| 97 | 126 | } |
| 98 | 127 | } |
| 99 | 128 | |
| 100 | - $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp ); | |
| 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'] ); | |
| 134 | + if ( $ret ) { | |
| 135 | + $metadata_webp['original_image'] = wp_basename( $org_webp_file ); | |
| 136 | + } | |
| 137 | + } | |
| 138 | + | |
| 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; | |
| 101 | 142 | if ( $ret ) { |
| 102 | - if ( $replace ) { | |
| 143 | + if ( $pluswebp_settings['replace'] ) { | |
| 103 | 144 | $up_post = array( |
| 104 | 145 | 'ID' => $attachment_id, |
| 105 | 146 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 106 | - 'post_mime_type' => 'image/webp', | |
| 147 | + 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 107 | 148 | ); |
| 108 | 149 | wp_update_post( $up_post ); |
| 109 | 150 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 110 | 151 | /* for bulk generate */ |
| @@ -109,9 +150,12 @@ | ||
| 109 | 150 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 110 | 151 | /* for bulk generate */ |
| 111 | 152 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 112 | 153 | /* delete org file */ |
| 113 | - 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 | + } | |
| 114 | 158 | /* Replace */ |
| 115 | 159 | $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 116 | 160 | /* for hook */ |
| 117 | 161 | $metadata = $metadata_webp; |
| @@ -118,17 +162,23 @@ | ||
| 118 | 162 | /* for mail */ |
| 119 | 163 | $attach_id = $attachment_id; |
| 120 | 164 | } else { |
| 121 | 165 | $post = get_post( $attachment_id ); |
| 122 | - $title = get_the_title( $post ); | |
| 166 | + $title = $post->post_title; | |
| 123 | 167 | $attachment = array( |
| 124 | 168 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 125 | - 'post_mime_type' => 'image/webp', | |
| 169 | + 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 126 | 170 | 'post_title' => $title, |
| 127 | 171 | 'post_content' => '', |
| 128 | 172 | 'post_status' => 'inherit', |
| 129 | 173 | ); |
| 130 | - $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 | + | |
| 131 | 181 | wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 132 | 182 | |
| 133 | 183 | $author = get_userdata( $post->post_author ); |
| 134 | 184 | $userid = $author->ID; |
| @@ -144,135 +194,220 @@ | ||
| 144 | 194 | ); |
| 145 | 195 | wp_update_post( $up_post ); |
| 146 | 196 | } |
| 147 | 197 | |
| 148 | - $ids = get_option( 'pluswebp_generate' ); | |
| 149 | - $ids[] = $attach_id; | |
| 150 | - update_option( 'pluswebp_generate', $ids ); | |
| 198 | + update_option( 'pluswebp_generate', $attach_id ); | |
| 151 | 199 | |
| 200 | + /* for Media Library folders term by Organize Media Folder */ | |
| 201 | + do_action( 'omf_folders_term_update', $metadata_webp, $attach_id ); | |
| 202 | + | |
| 203 | + /* for Term filter update by Organize Media Folder */ | |
| 204 | + do_action( 'omf_term_filter_update' ); | |
| 205 | + | |
| 152 | 206 | } |
| 153 | 207 | } |
| 154 | 208 | |
| 155 | 209 | return $metadata; |
| 210 | + } | |
| 156 | 211 | |
| 212 | + /** ================================================== | |
| 213 | + * Thumbnail urls | |
| 214 | + * | |
| 215 | + * @param int $attach_id attach_id. | |
| 216 | + * @param array $metadata metadata. | |
| 217 | + * @param string $upload_url upload_url. | |
| 218 | + * @return array $image_thumbnail(string), $imagethumburls(array) | |
| 219 | + * @since 2.00 | |
| 220 | + */ | |
| 221 | + private function thumbnail_urls( $attach_id, $metadata, $upload_url ) { | |
| 222 | + | |
| 223 | + $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true ); | |
| 224 | + $image_thumbnail = $image_attr_thumbnail[0]; | |
| 225 | + | |
| 226 | + $imagethumburls = array(); | |
| 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; | |
| 233 | + foreach ( $thumbnails as $key => $key2 ) { | |
| 234 | + $imagethumburls[ $key ] = $media_url . $key2['file']; | |
| 235 | + } | |
| 236 | + } | |
| 237 | + | |
| 238 | + return array( $image_thumbnail, $imagethumburls ); | |
| 157 | 239 | } |
| 158 | 240 | |
| 159 | 241 | /** ================================================== |
| 160 | - * Mail send hook | |
| 242 | + * Output datas | |
| 161 | 243 | * |
| 162 | - * @param string $to to. | |
| 163 | - * @since 1.09 | |
| 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 | |
| 247 | + * @since 2.00 | |
| 164 | 248 | */ |
| 165 | - public function mail_send( $to ) { | |
| 249 | + private function output_datas( $attach_id, $metadata ) { | |
| 166 | 250 | |
| 167 | - $post_ids = get_option( 'pluswebp_generate' ); | |
| 168 | - delete_option( 'pluswebp_generate' ); | |
| 251 | + $attachment_link = get_attachment_link( $attach_id ); | |
| 252 | + $attachment_url = wp_get_attachment_url( $attach_id ); | |
| 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 ); | |
| 169 | 262 | |
| 170 | - if ( function_exists( 'wp_date' ) ) { | |
| 171 | - $now_date_time = wp_date( 'Y-m-d H:i:s' ); | |
| 263 | + $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); | |
| 264 | + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) { | |
| 265 | + $file_size = $metadata['filesize']; | |
| 172 | 266 | } else { |
| 173 | - $now_date_time = date_i18n( 'Y-m-d H:i:s' ); | |
| 267 | + $file_size = @filesize( get_attached_file( $attach_id ) ); | |
| 174 | 268 | } |
| 175 | - $pluswebp_mail_send = array(); | |
| 176 | - $pluswebp_mail_send['datetime'] = $now_date_time; | |
| 269 | + if ( ! $file_size ) { | |
| 270 | + $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 271 | + } else { | |
| 272 | + $file_size = size_format( $file_size ); | |
| 273 | + } | |
| 177 | 274 | |
| 178 | - /* translators: Date and Time */ | |
| 179 | - $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 275 | + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); | |
| 276 | + } | |
| 180 | 277 | |
| 181 | - if ( ! empty( $post_ids ) ) { | |
| 278 | + /** ================================================== | |
| 279 | + * Get All Images | |
| 280 | + * | |
| 281 | + * @param array $types mime types. | |
| 282 | + * @param string $output_mime output mime type. | |
| 283 | + * @since 2.00 | |
| 284 | + */ | |
| 285 | + public function get_allimages( $types, $output_mime ) { | |
| 182 | 286 | |
| 183 | - $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; | |
| 184 | - $pluswebp_mail_send['count'] = count( $post_ids ); | |
| 287 | + if ( empty( $types ) ) { | |
| 288 | + return array( array(), array() ); | |
| 289 | + } | |
| 185 | 290 | |
| 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' ); | |
| 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 ); | |
| 300 | + | |
| 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; | |
| 206 | 322 | } else { |
| 207 | - $file_size = size_format( $file_size ); | |
| 323 | + $no_file_ids[] = $post->ID; | |
| 208 | 324 | } |
| 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 | 325 | } |
| 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 | 326 | } |
| 221 | 327 | |
| 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 | - | |
| 328 | + return array( $post_ids, $no_file_ids ); | |
| 228 | 329 | } |
| 229 | 330 | |
| 230 | 331 | /** ================================================== |
| 231 | - * Output metadata | |
| 332 | + * Mail messages hook | |
| 232 | 333 | * |
| 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 | |
| 334 | + * @param array $messages messages. | |
| 335 | + * @param int $webp_id webp ID or avif ID. | |
| 336 | + * | |
| 337 | + * @since 4.00 | |
| 238 | 338 | */ |
| 239 | - private function output_metadata( $attach_id, $metadata, $upload_url ) { | |
| 339 | + public function mail_messages( $messages, $webp_id ) { | |
| 240 | 340 | |
| 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 | - } | |
| 341 | + $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 342 | + | |
| 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 ); | |
| 349 | + | |
| 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"; | |
| 250 | 364 | } |
| 251 | 365 | } |
| 366 | + $message .= "\n"; | |
| 367 | + $messages[] = $message; | |
| 252 | 368 | } |
| 253 | 369 | |
| 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']; | |
| 370 | + return array( $message, $messages ); | |
| 371 | + } | |
| 372 | + | |
| 373 | + /** ================================================== | |
| 374 | + * Mail sent for Generate Messages | |
| 375 | + * | |
| 376 | + * @param array $messages mail messages. | |
| 377 | + * @param int $count convert count. | |
| 378 | + * @since 4.02 | |
| 379 | + */ | |
| 380 | + public function mail_generate_message( $messages, $count ) { | |
| 381 | + | |
| 382 | + if ( function_exists( 'wp_date' ) ) { | |
| 383 | + $now_date_time = wp_date( 'Y-m-d H:i:s' ); | |
| 257 | 384 | } else { |
| 258 | - $file_size = @filesize( get_attached_file( $attach_id ) ); | |
| 385 | + $now_date_time = date_i18n( 'Y-m-d H:i:s' ); | |
| 259 | 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"; | |
| 260 | 391 | |
| 261 | - return array( $imagethumburls, $stamptime, $file_size ); | |
| 262 | - | |
| 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 ) ); | |
| 263 | 396 | } |
| 264 | 397 | |
| 265 | 398 | /** ================================================== |
| 266 | - * Webp create | |
| 399 | + * WebP or AVIF create | |
| 267 | 400 | * |
| 268 | 401 | * @param string $filename input filename for pictures. |
| 269 | 402 | * @param string $mime_type mimetype. |
| 270 | - * @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. | |
| 271 | 406 | * @return bool $ret create bool. |
| 272 | 407 | * @since 1.00 |
| 273 | 408 | */ |
| 274 | - private function create_webp( $filename, $mime_type, $filename_webp ) { | |
| 409 | + private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) { | |
| 275 | 410 | |
| 276 | 411 | if ( ! file_exists( $filename ) ) { |
| 277 | 412 | return false; |
| 278 | 413 | } |
| @@ -279,10 +414,10 @@ | ||
| 279 | 414 | if ( file_exists( $filename_webp ) ) { |
| 280 | 415 | return false; |
| 281 | 416 | } |
| 282 | 417 | |
| 283 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 284 | 418 | @set_time_limit( 60 ); |
| 419 | + wp_raise_memory_limit( 'pluswebp' ); | |
| 285 | 420 | |
| 286 | 421 | $ret = false; |
| 287 | 422 | switch ( $mime_type ) { |
| 288 | 423 | case 'image/jpeg': |
| @@ -287,9 +422,10 @@ | ||
| 287 | 422 | switch ( $mime_type ) { |
| 288 | 423 | case 'image/jpeg': |
| 289 | 424 | $src = imagecreatefromjpeg( $filename ); |
| 290 | 425 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 291 | - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); | |
| 426 | + $bgcolor = imagecolorallocate( $img, 255, 255, 255 ); | |
| 427 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 292 | 428 | imagealphablending( $img, true ); |
| 293 | 429 | break; |
| 294 | 430 | case 'image/png': |
| 295 | 431 | $src = imagecreatefrompng( $filename ); |
| @@ -295,8 +431,11 @@ | ||
| 295 | 431 | $src = imagecreatefrompng( $filename ); |
| 296 | 432 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 297 | 433 | imagealphablending( $img, false ); |
| 298 | 434 | imagesavealpha( $img, true ); |
| 435 | + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); | |
| 436 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 437 | + imagecolortransparent( $img, $bgcolor ); | |
| 299 | 438 | break; |
| 300 | 439 | case 'image/bmp': |
| 301 | 440 | $src = imagecreatefrombmp( $filename ); |
| 302 | 441 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| @@ -311,13 +450,23 @@ | ||
| 311 | 450 | } |
| 312 | 451 | |
| 313 | 452 | imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) ); |
| 314 | 453 | imagedestroy( $src ); |
| 315 | - $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 | + | |
| 316 | 466 | imagedestroy( $img ); |
| 317 | 467 | |
| 318 | 468 | return $ret; |
| 319 | - | |
| 320 | 469 | } |
| 321 | 470 | |
| 322 | 471 | /** ================================================== |
| 323 | 472 | * Change ext |
| @@ -323,20 +472,24 @@ | ||
| 323 | 472 | * Change ext |
| 324 | 473 | * |
| 325 | 474 | * @param string $before_file_name before_file_name. |
| 326 | 475 | * @param string $ext ext. |
| 476 | + * @param bool $addext addext. | |
| 327 | 477 | * @return array $after_file_name after_file_name. |
| 328 | 478 | * @since 1.00 |
| 329 | 479 | */ |
| 330 | - private function change_ext( $before_file_name, $ext ) { | |
| 480 | + private function change_ext( $before_file_name, $ext, $addext ) { | |
| 331 | 481 | |
| 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 ); | |
| 482 | + if ( $addext ) { | |
| 483 | + $after_file_name = $before_file_name . '.' . $ext; | |
| 484 | + } else { | |
| 485 | + $exts = explode( '.', $before_file_name ); | |
| 486 | + $before_ext = '.' . end( $exts ); | |
| 487 | + $after_ext = '.' . $ext; | |
| 488 | + $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name ); | |
| 489 | + } | |
| 336 | 490 | |
| 337 | 491 | return $after_file_name; |
| 338 | - | |
| 339 | 492 | } |
| 340 | 493 | |
| 341 | 494 | /** ================================================== |
| 342 | 495 | * Change DB |
| @@ -351,105 +504,45 @@ | ||
| 351 | 504 | |
| 352 | 505 | /* Replace */ |
| 353 | 506 | $wpdb->query( |
| 354 | 507 | $wpdb->prepare( |
| 355 | - "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 | + ", | |
| 356 | 512 | $before_url, |
| 357 | 513 | $after_url |
| 358 | 514 | ) |
| 359 | 515 | ); |
| 360 | 516 | |
| 517 | + /* Advanced change database */ | |
| 518 | + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url ); | |
| 361 | 519 | } |
| 362 | 520 | |
| 363 | 521 | /** ================================================== |
| 364 | - * Upload Path | |
| 522 | + * Filter Raise Memory limit | |
| 365 | 523 | * |
| 366 | - * @return array $upload_dir,$upload_url,$upload_path uploadpath. | |
| 367 | - * @since 1.00 | |
| 524 | + * @since 3.00 | |
| 525 | + * | |
| 526 | + * @param string $filtered_limit Memory limit. | |
| 368 | 527 | */ |
| 369 | - public function upload_dir_url_path() { | |
| 528 | + public function raise_memory_limit( $filtered_limit ) { | |
| 370 | 529 | |
| 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 | - | |
| 530 | + return '256M'; | |
| 400 | 531 | } |
| 401 | 532 | |
| 402 | 533 | /** ================================================== |
| 403 | - * Real Url | |
| 534 | + * Filter the output mime types for a given input mime type and image size. | |
| 404 | 535 | * |
| 405 | - * @param string $base base. | |
| 406 | - * @param string $relationalpath relationalpath. | |
| 407 | - * @return string $realurl realurl. | |
| 408 | - * @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. | |
| 409 | 541 | */ |
| 410 | - private function realurl( $base, $relationalpath ) { | |
| 542 | + public function control_mime_type( $image_mime_transforms, $attachment_id ) { | |
| 411 | 543 | |
| 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 ); | |
| 544 | + $image_mime_transforms = array(); | |
| 422 | 545 | |
| 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 | - | |
| 546 | + return $image_mime_transforms; | |
| 451 | 547 | } |
| 452 | - | |
| 453 | 548 | } |
| 454 | - | |
| 455 | - | |