| @@ -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 |
| @@ -33,20 +33,15 @@ | ||
| 33 | 33 | * |
| 34 | 34 | * @var $upload_dir DIR. |
| 35 | 35 | */ |
| 36 | 36 | private $upload_dir; |
| 37 | + | |
| 37 | 38 | /** ================================================== |
| 38 | - * Dir | |
| 39 | + * URL | |
| 39 | 40 | * |
| 40 | 41 | * @var $upload_url URL. |
| 41 | 42 | */ |
| 42 | 43 | private $upload_url; |
| 43 | - /** ================================================== | |
| 44 | - * Dir | |
| 45 | - * | |
| 46 | - * @var $upload_path PATH. | |
| 47 | - */ | |
| 48 | - private $upload_path; | |
| 49 | 44 | |
| 50 | 45 | /** ================================================== |
| 51 | 46 | * Construct |
| 52 | 47 | * |
| @@ -53,16 +48,33 @@ | ||
| 53 | 48 | * @since 1.00 |
| 54 | 49 | */ |
| 55 | 50 | public function __construct() { |
| 56 | 51 | |
| 57 | - list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path(); | |
| 52 | + $wp_uploads = wp_upload_dir(); | |
| 58 | 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 */ | |
| 59 | 63 | add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 ); |
| 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 ); | |
| 60 | 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 ); | |
| 61 | 73 | } |
| 62 | 74 | |
| 63 | 75 | /** ================================================== |
| 64 | - * Webp generate | |
| 76 | + * Webp of AVIF generate | |
| 65 | 77 | * |
| 66 | 78 | * @param array $metadata metadata. |
| 67 | 79 | * @param int $attachment_id ID. |
| 68 | 80 | * @return array $metadata metadata. |
| @@ -70,38 +82,70 @@ | ||
| 70 | 82 | */ |
| 71 | 83 | public function generate_webp( $metadata, $attachment_id ) { |
| 72 | 84 | |
| 73 | 85 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 74 | - $replace = $pluswebp_settings['replace']; | |
| 75 | 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 | + | |
| 76 | 98 | $mime_type = get_post_mime_type( $attachment_id ); |
| 77 | - if ( 'image/jpeg' === $mime_type || 'image/png' === $mime_type || 'image/bmp' === $mime_type || 'image/gif' === $mime_type ) { | |
| 99 | + if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { | |
| 78 | 100 | $metadata_webp = $metadata; |
| 79 | - $file_webp = $this->change_ext( $metadata['file'], 'webp' ); | |
| 101 | + $file_webp = $this->change_ext( $metadata['file'], $ext, $pluswebp_settings['addext'] ); | |
| 80 | 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 | + | |
| 81 | 113 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 82 | 114 | $file_thumb = $value['file']; |
| 83 | - $file_thumb_webp = $this->change_ext( $file_thumb, 'webp' ); | |
| 84 | - $path = $this->upload_dir . '/' . dirname( $file_webp ) . '/'; | |
| 85 | - $url = $this->upload_url . '/' . dirname( $file_webp ) . '/'; | |
| 86 | - $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'] ); | |
| 87 | 117 | if ( $ret ) { |
| 88 | 118 | $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 89 | - $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; | |
| 90 | - if ( $replace ) { | |
| 91 | - 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 ); | |
| 92 | 124 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 93 | 125 | } |
| 94 | 126 | } |
| 95 | 127 | } |
| 96 | 128 | |
| 97 | - $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; | |
| 98 | 142 | if ( $ret ) { |
| 99 | - if ( $replace ) { | |
| 143 | + if ( $pluswebp_settings['replace'] ) { | |
| 100 | 144 | $up_post = array( |
| 101 | 145 | 'ID' => $attachment_id, |
| 102 | 146 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 103 | - 'post_mime_type' => 'image/webp', | |
| 147 | + 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 104 | 148 | ); |
| 105 | 149 | wp_update_post( $up_post ); |
| 106 | 150 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 107 | 151 | /* for bulk generate */ |
| @@ -106,24 +150,35 @@ | ||
| 106 | 150 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 107 | 151 | /* for bulk generate */ |
| 108 | 152 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 109 | 153 | /* delete org file */ |
| 110 | - 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 | + } | |
| 111 | 158 | /* Replace */ |
| 112 | 159 | $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 113 | 160 | /* for hook */ |
| 114 | 161 | $metadata = $metadata_webp; |
| 162 | + /* for mail */ | |
| 163 | + $attach_id = $attachment_id; | |
| 115 | 164 | } else { |
| 116 | 165 | $post = get_post( $attachment_id ); |
| 117 | - $title = get_the_title( $post ); | |
| 166 | + $title = $post->post_title; | |
| 118 | 167 | $attachment = array( |
| 119 | 168 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 120 | - 'post_mime_type' => 'image/webp', | |
| 169 | + 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 121 | 170 | 'post_title' => $title, |
| 122 | 171 | 'post_content' => '', |
| 123 | 172 | 'post_status' => 'inherit', |
| 124 | 173 | ); |
| 125 | - $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 | + | |
| 126 | 181 | wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 127 | 182 | |
| 128 | 183 | $author = get_userdata( $post->post_author ); |
| 129 | 184 | $userid = $author->ID; |
| @@ -138,25 +193,221 @@ | ||
| 138 | 193 | 'post_modified_gmt' => $postdategmt, |
| 139 | 194 | ); |
| 140 | 195 | wp_update_post( $up_post ); |
| 141 | 196 | } |
| 197 | + | |
| 198 | + update_option( 'pluswebp_generate', $attach_id ); | |
| 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 | + | |
| 142 | 206 | } |
| 143 | 207 | } |
| 144 | 208 | |
| 145 | 209 | return $metadata; |
| 210 | + } | |
| 146 | 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 ); | |
| 147 | 239 | } |
| 148 | 240 | |
| 149 | 241 | /** ================================================== |
| 150 | - * Webp create | |
| 242 | + * Output datas | |
| 151 | 243 | * |
| 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 | |
| 248 | + */ | |
| 249 | + private function output_datas( $attach_id, $metadata ) { | |
| 250 | + | |
| 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 ); | |
| 262 | + | |
| 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']; | |
| 266 | + } else { | |
| 267 | + $file_size = @filesize( get_attached_file( $attach_id ) ); | |
| 268 | + } | |
| 269 | + if ( ! $file_size ) { | |
| 270 | + $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 271 | + } else { | |
| 272 | + $file_size = size_format( $file_size ); | |
| 273 | + } | |
| 274 | + | |
| 275 | + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); | |
| 276 | + } | |
| 277 | + | |
| 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 ) { | |
| 286 | + | |
| 287 | + if ( empty( $types ) ) { | |
| 288 | + return array( array(), array() ); | |
| 289 | + } | |
| 290 | + | |
| 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; | |
| 322 | + } else { | |
| 323 | + $no_file_ids[] = $post->ID; | |
| 324 | + } | |
| 325 | + } | |
| 326 | + } | |
| 327 | + | |
| 328 | + return array( $post_ids, $no_file_ids ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + /** ================================================== | |
| 332 | + * Mail messages hook | |
| 333 | + * | |
| 334 | + * @param array $messages messages. | |
| 335 | + * @param int $webp_id webp ID or avif ID. | |
| 336 | + * | |
| 337 | + * @since 4.00 | |
| 338 | + */ | |
| 339 | + public function mail_messages( $messages, $webp_id ) { | |
| 340 | + | |
| 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"; | |
| 364 | + } | |
| 365 | + } | |
| 366 | + $message .= "\n"; | |
| 367 | + $messages[] = $message; | |
| 368 | + } | |
| 369 | + | |
| 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' ); | |
| 384 | + } else { | |
| 385 | + $now_date_time = date_i18n( 'Y-m-d H:i:s' ); | |
| 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"; | |
| 391 | + | |
| 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 ) ); | |
| 396 | + } | |
| 397 | + | |
| 398 | + /** ================================================== | |
| 399 | + * WebP or AVIF create | |
| 400 | + * | |
| 152 | 401 | * @param string $filename input filename for pictures. |
| 153 | 402 | * @param string $mime_type mimetype. |
| 154 | - * @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. | |
| 155 | 406 | * @return bool $ret create bool. |
| 156 | 407 | * @since 1.00 |
| 157 | 408 | */ |
| 158 | - private function create_webp( $filename, $mime_type, $filename_webp ) { | |
| 409 | + private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) { | |
| 159 | 410 | |
| 160 | 411 | if ( ! file_exists( $filename ) ) { |
| 161 | 412 | return false; |
| 162 | 413 | } |
| @@ -163,10 +414,10 @@ | ||
| 163 | 414 | if ( file_exists( $filename_webp ) ) { |
| 164 | 415 | return false; |
| 165 | 416 | } |
| 166 | 417 | |
| 167 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 168 | 418 | @set_time_limit( 60 ); |
| 419 | + wp_raise_memory_limit( 'pluswebp' ); | |
| 169 | 420 | |
| 170 | 421 | $ret = false; |
| 171 | 422 | switch ( $mime_type ) { |
| 172 | 423 | case 'image/jpeg': |
| @@ -171,9 +422,10 @@ | ||
| 171 | 422 | switch ( $mime_type ) { |
| 172 | 423 | case 'image/jpeg': |
| 173 | 424 | $src = imagecreatefromjpeg( $filename ); |
| 174 | 425 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 175 | - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); | |
| 426 | + $bgcolor = imagecolorallocate( $img, 255, 255, 255 ); | |
| 427 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 176 | 428 | imagealphablending( $img, true ); |
| 177 | 429 | break; |
| 178 | 430 | case 'image/png': |
| 179 | 431 | $src = imagecreatefrompng( $filename ); |
| @@ -179,8 +431,11 @@ | ||
| 179 | 431 | $src = imagecreatefrompng( $filename ); |
| 180 | 432 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 181 | 433 | imagealphablending( $img, false ); |
| 182 | 434 | imagesavealpha( $img, true ); |
| 435 | + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); | |
| 436 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 437 | + imagecolortransparent( $img, $bgcolor ); | |
| 183 | 438 | break; |
| 184 | 439 | case 'image/bmp': |
| 185 | 440 | $src = imagecreatefrombmp( $filename ); |
| 186 | 441 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| @@ -195,13 +450,23 @@ | ||
| 195 | 450 | } |
| 196 | 451 | |
| 197 | 452 | imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) ); |
| 198 | 453 | imagedestroy( $src ); |
| 199 | - $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 | + | |
| 200 | 466 | imagedestroy( $img ); |
| 201 | 467 | |
| 202 | 468 | return $ret; |
| 203 | - | |
| 204 | 469 | } |
| 205 | 470 | |
| 206 | 471 | /** ================================================== |
| 207 | 472 | * Change ext |
| @@ -207,20 +472,24 @@ | ||
| 207 | 472 | * Change ext |
| 208 | 473 | * |
| 209 | 474 | * @param string $before_file_name before_file_name. |
| 210 | 475 | * @param string $ext ext. |
| 476 | + * @param bool $addext addext. | |
| 211 | 477 | * @return array $after_file_name after_file_name. |
| 212 | 478 | * @since 1.00 |
| 213 | 479 | */ |
| 214 | - private function change_ext( $before_file_name, $ext ) { | |
| 480 | + private function change_ext( $before_file_name, $ext, $addext ) { | |
| 215 | 481 | |
| 216 | - $exts = explode( '.', $before_file_name ); | |
| 217 | - $before_ext = '.' . end( $exts ); | |
| 218 | - $after_ext = '.' . $ext; | |
| 219 | - $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 | + } | |
| 220 | 490 | |
| 221 | 491 | return $after_file_name; |
| 222 | - | |
| 223 | 492 | } |
| 224 | 493 | |
| 225 | 494 | /** ================================================== |
| 226 | 495 | * Change DB |
| @@ -235,54 +504,45 @@ | ||
| 235 | 504 | |
| 236 | 505 | /* Replace */ |
| 237 | 506 | $wpdb->query( |
| 238 | 507 | $wpdb->prepare( |
| 239 | - "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 | + ", | |
| 240 | 512 | $before_url, |
| 241 | 513 | $after_url |
| 242 | 514 | ) |
| 243 | 515 | ); |
| 244 | 516 | |
| 517 | + /* Advanced change database */ | |
| 518 | + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url ); | |
| 245 | 519 | } |
| 246 | 520 | |
| 247 | 521 | /** ================================================== |
| 248 | - * Upload Path | |
| 522 | + * Filter Raise Memory limit | |
| 249 | 523 | * |
| 250 | - * @return array $upload_dir,$upload_url,$upload_path uploadpath. | |
| 251 | - * @since 1.00 | |
| 524 | + * @since 3.00 | |
| 525 | + * | |
| 526 | + * @param string $filtered_limit Memory limit. | |
| 252 | 527 | */ |
| 253 | - public function upload_dir_url_path() { | |
| 528 | + public function raise_memory_limit( $filtered_limit ) { | |
| 254 | 529 | |
| 255 | - $wp_uploads = wp_upload_dir(); | |
| 530 | + return '256M'; | |
| 531 | + } | |
| 256 | 532 | |
| 257 | - $relation_path_true = strpos( $wp_uploads['baseurl'], '../' ); | |
| 258 | - if ( $relation_path_true > 0 ) { | |
| 259 | - $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true ); | |
| 260 | - $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true ); | |
| 261 | - $upload_url = $this->realurl( $basepath, $relationalpath ); | |
| 262 | - $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) ); | |
| 263 | - } else { | |
| 264 | - $upload_url = $wp_uploads['baseurl']; | |
| 265 | - $upload_dir = wp_normalize_path( $wp_uploads['basedir'] ); | |
| 266 | - } | |
| 533 | + /** ================================================== | |
| 534 | + * Filter the output mime types for a given input mime type and image size. | |
| 535 | + * | |
| 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. | |
| 541 | + */ | |
| 542 | + public function control_mime_type( $image_mime_transforms, $attachment_id ) { | |
| 267 | 543 | |
| 268 | - if ( is_ssl() ) { | |
| 269 | - $upload_url = str_replace( 'http:', 'https:', $upload_url ); | |
| 270 | - } | |
| 544 | + $image_mime_transforms = array(); | |
| 271 | 545 | |
| 272 | - if ( $relation_path_true > 0 ) { | |
| 273 | - $upload_path = $relationalpath; | |
| 274 | - } else { | |
| 275 | - $upload_path = str_replace( site_url( '/' ), '', $upload_url ); | |
| 276 | - } | |
| 277 | - | |
| 278 | - $upload_dir = untrailingslashit( $upload_dir ); | |
| 279 | - $upload_url = untrailingslashit( $upload_url ); | |
| 280 | - $upload_path = untrailingslashit( $upload_path ); | |
| 281 | - | |
| 282 | - return array( $upload_dir, $upload_url, $upload_path ); | |
| 283 | - | |
| 546 | + return $image_mime_transforms; | |
| 284 | 547 | } |
| 285 | - | |
| 286 | 548 | } |
| 287 | - | |
| 288 | - | |