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