| @@ -33,16 +33,18 @@ | ||
| 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; |
| 44 | + | |
| 43 | 45 | /** ================================================== |
| 44 | - * Dir | |
| 46 | + * Path | |
| 45 | 47 | * |
| 46 | 48 | * @var $upload_path PATH. |
| 47 | 49 | */ |
| 48 | 50 | private $upload_path; |
| @@ -53,12 +55,19 @@ | ||
| 53 | 55 | * @since 1.00 |
| 54 | 56 | */ |
| 55 | 57 | public function __construct() { |
| 56 | 58 | |
| 57 | - list($this->upload_dir, $this->upload_url, $this->upload_path) = $this->upload_dir_url_path(); | |
| 59 | + list( $this->upload_dir, $this->upload_url, $this->upload_path ) = $this->upload_dir_url_path(); | |
| 58 | 60 | |
| 59 | 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 ); | |
| 60 | 63 | |
| 64 | + /* Ajax */ | |
| 65 | + $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 66 | + $action2 = 'bulkgeneratewebp_message'; | |
| 67 | + add_action( 'wp_ajax_' . $action1, array( $this, 'bulkgeneratewebp_update_callback' ) ); | |
| 68 | + add_action( 'wp_ajax_' . $action2, array( $this, 'bulkgeneratewebp_message_callback' ) ); | |
| 69 | + | |
| 61 | 70 | } |
| 62 | 71 | |
| 63 | 72 | /** ================================================== |
| 64 | 73 | * Webp generate |
| @@ -73,18 +82,25 @@ | ||
| 73 | 82 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 74 | 83 | $replace = $pluswebp_settings['replace']; |
| 75 | 84 | |
| 76 | 85 | $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 ) { | |
| 86 | + if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { | |
| 78 | 87 | $metadata_webp = $metadata; |
| 79 | - $file_webp = $this->change_ext( $metadata['file'], 'webp' ); | |
| 88 | + $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] ); | |
| 80 | 89 | $metadata_webp['file'] = $file_webp; |
| 81 | 90 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 82 | 91 | $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 ); | |
| 92 | + $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] ); | |
| 93 | + if ( '.' === dirname( $file_webp ) ) { | |
| 94 | + $dir_name_url = '/'; | |
| 95 | + $dir_name_path = wp_normalize_path( '/' ); | |
| 96 | + } else { | |
| 97 | + $dir_name_url = '/' . dirname( $file_webp ) . '/'; | |
| 98 | + $dir_name_path = wp_normalize_path( $dir_name_url ); | |
| 99 | + } | |
| 100 | + $url = $this->upload_url . $dir_name_url; | |
| 101 | + $path = $this->upload_dir . $dir_name_path; | |
| 102 | + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); | |
| 87 | 103 | if ( $ret ) { |
| 88 | 104 | $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 89 | 105 | $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; |
| 90 | 106 | if ( $replace ) { |
| @@ -92,8 +108,17 @@ | ||
| 92 | 108 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 93 | 109 | } |
| 94 | 110 | } |
| 95 | 111 | } |
| 112 | + $org_img_file = null; | |
| 113 | + if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { | |
| 114 | + $org_img_file = wp_normalize_path( wp_get_original_image_path( $attachment_id, false ) ); | |
| 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 ); | |
| 117 | + if ( $ret ) { | |
| 118 | + $metadata_webp['original_image'] = wp_basename( $org_webp_file ); | |
| 119 | + } | |
| 120 | + } | |
| 96 | 121 | |
| 97 | 122 | $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $this->upload_dir . '/' . $file_webp ); |
| 98 | 123 | if ( $ret ) { |
| 99 | 124 | if ( $replace ) { |
| @@ -107,11 +132,17 @@ | ||
| 107 | 132 | /* for bulk generate */ |
| 108 | 133 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 109 | 134 | /* delete org file */ |
| 110 | 135 | unlink( $this->upload_dir . '/' . $metadata['file'] ); |
| 136 | + if ( $org_img_file ) { | |
| 137 | + unlink( $org_img_file ); | |
| 138 | + } | |
| 139 | + /* Replace */ | |
| 140 | + $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); | |
| 111 | 141 | /* for hook */ |
| 112 | 142 | $metadata = $metadata_webp; |
| 113 | - $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); | |
| 143 | + /* for mail */ | |
| 144 | + $attach_id = $attachment_id; | |
| 114 | 145 | } else { |
| 115 | 146 | $post = get_post( $attachment_id ); |
| 116 | 147 | $title = get_the_title( $post ); |
| 117 | 148 | $attachment = array( |
| @@ -120,9 +151,15 @@ | ||
| 120 | 151 | 'post_title' => $title, |
| 121 | 152 | 'post_content' => '', |
| 122 | 153 | 'post_status' => 'inherit', |
| 123 | 154 | ); |
| 124 | - $attach_id = wp_insert_attachment( $attachment, $this->upload_dir . '/' . $file_webp ); | |
| 155 | + $file = $this->upload_dir . '/' . $file_webp; | |
| 156 | + $attach_id = wp_insert_attachment( $attachment, $file ); | |
| 157 | + | |
| 158 | + /* for XAMPP [ get_attached_file( $attach_id ): Unable to get correct value ] */ | |
| 159 | + $metapath_name = str_replace( $this->upload_dir . '/', '', $file ); | |
| 160 | + update_post_meta( $attach_id, '_wp_attached_file', $metapath_name ); | |
| 161 | + | |
| 125 | 162 | wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 126 | 163 | |
| 127 | 164 | $author = get_userdata( $post->post_author ); |
| 128 | 165 | $userid = $author->ID; |
| @@ -137,8 +174,13 @@ | ||
| 137 | 174 | 'post_modified_gmt' => $postdategmt, |
| 138 | 175 | ); |
| 139 | 176 | wp_update_post( $up_post ); |
| 140 | 177 | } |
| 178 | + | |
| 179 | + $ids = get_option( 'pluswebp_generate' ); | |
| 180 | + $ids[] = $attach_id; | |
| 181 | + update_option( 'pluswebp_generate', $ids ); | |
| 182 | + | |
| 141 | 183 | } |
| 142 | 184 | } |
| 143 | 185 | |
| 144 | 186 | return $metadata; |
| @@ -145,8 +187,318 @@ | ||
| 145 | 187 | |
| 146 | 188 | } |
| 147 | 189 | |
| 148 | 190 | /** ================================================== |
| 191 | + * IDs Callback | |
| 192 | + * | |
| 193 | + * @since 2.00 | |
| 194 | + */ | |
| 195 | + public function bulkgeneratewebp_update_callback() { | |
| 196 | + | |
| 197 | + $action1 = 'bulkgeneratewebp-ajax-action'; | |
| 198 | + if ( check_ajax_referer( $action1, 'nonce', false ) ) { | |
| 199 | + if ( current_user_can( 'manage_options' ) ) { | |
| 200 | + if ( ! empty( $_POST['id'] ) ) { | |
| 201 | + $id = absint( $_POST['id'] ); | |
| 202 | + } else { | |
| 203 | + return; | |
| 204 | + } | |
| 205 | + if ( ! wp_get_attachment_url( $id ) ) { | |
| 206 | + $error_string = __( 'No media!', 'plus-webp' ); | |
| 207 | + $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; | |
| 208 | + } else { | |
| 209 | + $output_html = $this->output_html( $id ); | |
| 210 | + } | |
| 211 | + if ( ! empty( $output_html ) ) { | |
| 212 | + header( 'Content-type: text/html; charset=UTF-8' ); | |
| 213 | + $allowed_output_html = array( | |
| 214 | + 'a' => array( | |
| 215 | + 'href' => array(), | |
| 216 | + 'target' => array(), | |
| 217 | + 'rel' => array(), | |
| 218 | + 'style' => array(), | |
| 219 | + ), | |
| 220 | + 'img' => array( | |
| 221 | + 'src' => array(), | |
| 222 | + 'width' => array(), | |
| 223 | + 'height' => array(), | |
| 224 | + 'style' => array(), | |
| 225 | + ), | |
| 226 | + 'div' => array( | |
| 227 | + 'style' => array(), | |
| 228 | + 'class' => array(), | |
| 229 | + ), | |
| 230 | + 'font' => array( | |
| 231 | + 'color' => array(), | |
| 232 | + ), | |
| 233 | + 'ul' => array(), | |
| 234 | + 'li' => array(), | |
| 235 | + 'span' => array( | |
| 236 | + 'class' => array(), | |
| 237 | + 'style' => array(), | |
| 238 | + ), | |
| 239 | + ); | |
| 240 | + echo wp_kses( $output_html, $allowed_output_html ); | |
| 241 | + } | |
| 242 | + } | |
| 243 | + } else { | |
| 244 | + status_header( '403' ); | |
| 245 | + echo 'Forbidden'; | |
| 246 | + } | |
| 247 | + | |
| 248 | + wp_die(); | |
| 249 | + | |
| 250 | + } | |
| 251 | + | |
| 252 | + /** ================================================== | |
| 253 | + * Output html for Ajax | |
| 254 | + * | |
| 255 | + * @param array $attach_id attach_id. | |
| 256 | + * @since 2.00 | |
| 257 | + */ | |
| 258 | + private function output_html( $attach_id ) { | |
| 259 | + | |
| 260 | + include_once ABSPATH . 'wp-admin/includes/image.php'; | |
| 261 | + | |
| 262 | + /* Generate thumbnails */ | |
| 263 | + $metadata_org = wp_get_attachment_metadata( $attach_id ); | |
| 264 | + do_action( 'wp_generate_attachment_metadata', $metadata_org, $attach_id ); | |
| 265 | + if ( get_option( 'pluswebp_generate' ) ) { | |
| 266 | + $ids = get_option( 'pluswebp_generate' ); | |
| 267 | + delete_option( 'pluswebp_generate' ); | |
| 268 | + $webp_id = $ids[0]; | |
| 269 | + $metadata = wp_get_attachment_metadata( $webp_id ); | |
| 270 | + | |
| 271 | + /* Thumbnail urls */ | |
| 272 | + list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); | |
| 273 | + /* Output datas*/ | |
| 274 | + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata ); | |
| 275 | + | |
| 276 | + $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">'; | |
| 277 | + $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">'; | |
| 278 | + $output_html .= '<div style="overflow: hidden;">'; | |
| 279 | + $output_html .= '<div>ID: ' . $webp_id . '</div>'; | |
| 280 | + $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>'; | |
| 281 | + $output_html .= '<div>' . __( 'Permalink:' ) . ' <a href="' . $attachment_link . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_link . '</a></div>'; | |
| 282 | + $output_html .= '<div>URL: <a href="' . $attachment_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $attachment_url . '</a></div>'; | |
| 283 | + $output_html .= '<div>' . __( 'File name:' ) . ' ' . $filename . '</div>'; | |
| 284 | + if ( ! empty( $original_image_url ) ) { | |
| 285 | + $output_html .= '<div>' . __( 'Original URL', 'plus-webp' ) . ': <a href="' . $original_image_url . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $original_image_url . '</a></div>'; | |
| 286 | + $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . $original_filename . '</div>'; | |
| 287 | + } | |
| 288 | + $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>'; | |
| 289 | + $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>'; | |
| 290 | + $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>'; | |
| 291 | + if ( ! empty( $imagethumburls ) ) { | |
| 292 | + $output_html .= '<div>' . __( 'Images' ) . ': '; | |
| 293 | + foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { | |
| 294 | + $output_html .= '[<a href="' . $imagethumburl . '" target="_blank" rel="noopener noreferrer" style="text-decoration: none; word-break: break-all;">' . $thumbsize . '</a>]'; | |
| 295 | + } | |
| 296 | + $output_html .= '</div>'; | |
| 297 | + } | |
| 298 | + $output_html .= '</div></div>'; | |
| 299 | + } else { | |
| 300 | + $output_html = null; | |
| 301 | + } | |
| 302 | + | |
| 303 | + return $output_html; | |
| 304 | + | |
| 305 | + } | |
| 306 | + | |
| 307 | + /** ================================================== | |
| 308 | + * Thumbnail urls | |
| 309 | + * | |
| 310 | + * @param int $attach_id attach_id. | |
| 311 | + * @param array $metadata metadata. | |
| 312 | + * @param string $upload_url upload_url. | |
| 313 | + * @return array $image_thumbnail(string), $imagethumburls(array) | |
| 314 | + * @since 2.00 | |
| 315 | + */ | |
| 316 | + private function thumbnail_urls( $attach_id, $metadata, $upload_url ) { | |
| 317 | + | |
| 318 | + $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true ); | |
| 319 | + $image_thumbnail = $image_attr_thumbnail[0]; | |
| 320 | + | |
| 321 | + $imagethumburls = array(); | |
| 322 | + if ( ! empty( $metadata ) && array_key_exists( 'sizes', $metadata ) ) { | |
| 323 | + $thumbnails = $metadata['sizes']; | |
| 324 | + $path_file = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 325 | + $filename = wp_basename( $path_file ); | |
| 326 | + $media_path = str_replace( $filename, '', $path_file ); | |
| 327 | + $media_url = $upload_url . '/' . $media_path; | |
| 328 | + foreach ( $thumbnails as $key => $key2 ) { | |
| 329 | + $imagethumburls[ $key ] = $media_url . $key2['file']; | |
| 330 | + } | |
| 331 | + } | |
| 332 | + | |
| 333 | + return array( $image_thumbnail, $imagethumburls ); | |
| 334 | + | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** ================================================== | |
| 338 | + * Output datas | |
| 339 | + * | |
| 340 | + * @param int $attach_id attach_id. | |
| 341 | + * @param array $metadata metadata. | |
| 342 | + * @return array (string) $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type(string), $stamptime, $file_size | |
| 343 | + * @since 2.00 | |
| 344 | + */ | |
| 345 | + private function output_datas( $attach_id, $metadata ) { | |
| 346 | + | |
| 347 | + $attachment_link = get_attachment_link( $attach_id ); | |
| 348 | + $attachment_url = wp_get_attachment_url( $attach_id ); | |
| 349 | + $filename = wp_basename( $attachment_url ); | |
| 350 | + if ( ! empty( $metadata ) && array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { | |
| 351 | + $original_image_url = wp_get_original_image_url( $attach_id ); | |
| 352 | + $original_filename = wp_basename( $original_image_url ); | |
| 353 | + } else { | |
| 354 | + $original_image_url = null; | |
| 355 | + $original_filename = null; | |
| 356 | + } | |
| 357 | + $mime_type = get_post_mime_type( $attach_id ); | |
| 358 | + | |
| 359 | + $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); | |
| 360 | + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) { | |
| 361 | + $file_size = $metadata['filesize']; | |
| 362 | + } else { | |
| 363 | + $file_size = @filesize( get_attached_file( $attach_id ) ); | |
| 364 | + } | |
| 365 | + if ( ! $file_size ) { | |
| 366 | + $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 367 | + } else { | |
| 368 | + $file_size = size_format( $file_size ); | |
| 369 | + } | |
| 370 | + | |
| 371 | + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); | |
| 372 | + | |
| 373 | + } | |
| 374 | + | |
| 375 | + /** ================================================== | |
| 376 | + * Messages Callback | |
| 377 | + * | |
| 378 | + * @since 2.00 | |
| 379 | + */ | |
| 380 | + public function bulkgeneratewebp_message_callback() { | |
| 381 | + | |
| 382 | + $action2 = 'bulkgeneratewebp_message'; | |
| 383 | + if ( check_ajax_referer( $action2, 'nonce', false ) ) { | |
| 384 | + $error_count = 0; | |
| 385 | + $error_update = null; | |
| 386 | + $success_count = 0; | |
| 387 | + if ( ! empty( $_POST['error_count'] ) ) { | |
| 388 | + $error_count = absint( $_POST['error_count'] ); | |
| 389 | + } | |
| 390 | + if ( ! empty( $_POST['error_update'] ) ) { | |
| 391 | + $error_update = sanitize_text_field( wp_unslash( $_POST['error_update'] ) ); | |
| 392 | + } | |
| 393 | + if ( ! empty( $_POST['success_count'] ) ) { | |
| 394 | + $success_count = absint( $_POST['success_count'] ); | |
| 395 | + } | |
| 396 | + | |
| 397 | + $back_html = '<strong><a style="text-decoration: none;" href="' . admin_url( 'tools.php?page=pluswebp' ) . '">' . __( 'Back' ) . '</a></strong>'; | |
| 398 | + | |
| 399 | + $output_html = null; | |
| 400 | + if ( $error_count > 0 ) { | |
| 401 | + /* translators: error message %1$d: media count %2$s: back link */ | |
| 402 | + $error_message = sprintf( __( 'Errored to the generation of %1$d medias.', 'plus-webp' ), $error_count, $back_html ); | |
| 403 | + $output_html .= '<div class="notice notice-error is-dismissible"><ul><li><div>' . $error_message . '</div>' . $error_update . '</li></ul></div>'; | |
| 404 | + } | |
| 405 | + if ( $success_count > 0 ) { | |
| 406 | + /* translators: success message %1$d: media count %2$s: back link */ | |
| 407 | + $success_message = sprintf( __( 'Succeeded to the generation webp of %1$d medias for Media Library. %2$s', 'plus-webp' ), $success_count, $back_html ); | |
| 408 | + $output_html .= '<div class="notice notice-success is-dismissible"><ul><li><div>' . $success_message . '</li></ul></div>'; | |
| 409 | + } | |
| 410 | + | |
| 411 | + header( 'Content-type: text/html; charset=UTF-8' ); | |
| 412 | + $allowed_output_html = array( | |
| 413 | + 'div' => array( | |
| 414 | + 'class' => array(), | |
| 415 | + ), | |
| 416 | + 'ul' => array(), | |
| 417 | + 'li' => array(), | |
| 418 | + 'strong' => array(), | |
| 419 | + 'a' => array( | |
| 420 | + 'style' => array(), | |
| 421 | + 'href' => array(), | |
| 422 | + ), | |
| 423 | + ); | |
| 424 | + echo wp_kses( $output_html, $allowed_output_html ); | |
| 425 | + } | |
| 426 | + | |
| 427 | + wp_die(); | |
| 428 | + | |
| 429 | + } | |
| 430 | + | |
| 431 | + /** ================================================== | |
| 432 | + * Mail send hook | |
| 433 | + * | |
| 434 | + * @param string $to to. | |
| 435 | + * @since 1.09 | |
| 436 | + */ | |
| 437 | + public function mail_send( $to ) { | |
| 438 | + | |
| 439 | + $post_ids = get_option( 'pluswebp_generate' ); | |
| 440 | + delete_option( 'pluswebp_generate' ); | |
| 441 | + | |
| 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; | |
| 449 | + | |
| 450 | + /* translators: Date and Time */ | |
| 451 | + $message = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 452 | + | |
| 453 | + if ( ! empty( $post_ids ) ) { | |
| 454 | + | |
| 455 | + $message .= __( 'Generation of WebP is complete.', 'plus-webp' ) . "\r\n\r\n"; | |
| 456 | + $pluswebp_mail_send['count'] = count( $post_ids ); | |
| 457 | + | |
| 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 | + } | |
| 487 | + } else { | |
| 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; | |
| 490 | + } | |
| 491 | + | |
| 492 | + update_option( 'pluswebp_generate_mail', $pluswebp_mail_send ); | |
| 493 | + | |
| 494 | + /* translators: blogname for subject */ | |
| 495 | + $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); | |
| 496 | + wp_mail( $to, $subject, $message ); | |
| 497 | + | |
| 498 | + } | |
| 499 | + | |
| 500 | + /** ================================================== | |
| 149 | 501 | * Webp create |
| 150 | 502 | * |
| 151 | 503 | * @param string $filename input filename for pictures. |
| 152 | 504 | * @param string $mime_type mimetype. |
| @@ -162,30 +514,40 @@ | ||
| 162 | 514 | if ( file_exists( $filename_webp ) ) { |
| 163 | 515 | return false; |
| 164 | 516 | } |
| 165 | 517 | |
| 518 | + $pluswebp_settings = get_option( 'pluswebp' ); | |
| 519 | + @set_time_limit( 60 ); | |
| 520 | + @ini_set( 'memory_limit', '256M' ); | |
| 521 | + | |
| 166 | 522 | $ret = false; |
| 167 | 523 | switch ( $mime_type ) { |
| 168 | 524 | case 'image/jpeg': |
| 169 | 525 | $src = imagecreatefromjpeg( $filename ); |
| 526 | + $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); | |
| 527 | + $bgcolor = imagecolorallocate( $img, 255, 255, 255 ); | |
| 528 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 529 | + imagealphablending( $img, true ); | |
| 170 | 530 | break; |
| 171 | 531 | case 'image/png': |
| 172 | 532 | $src = imagecreatefrompng( $filename ); |
| 533 | + $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); | |
| 534 | + imagealphablending( $img, false ); | |
| 535 | + imagesavealpha( $img, true ); | |
| 173 | 536 | break; |
| 174 | 537 | case 'image/bmp': |
| 175 | 538 | $src = imagecreatefrombmp( $filename ); |
| 539 | + $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); | |
| 176 | 540 | break; |
| 177 | 541 | case 'image/gif': |
| 178 | 542 | $src = imagecreatefromgif( $filename ); |
| 543 | + $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); | |
| 544 | + $bgcolor = imagecolorallocatealpha( $img, 0, 0, 0, 127 ); | |
| 545 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 546 | + imagecolortransparent( $img, $bgcolor ); | |
| 179 | 547 | break; |
| 180 | 548 | } |
| 181 | 549 | |
| 182 | - $pluswebp_settings = get_option( 'pluswebp' ); | |
| 183 | - @set_time_limit( 60 ); | |
| 184 | - | |
| 185 | - $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); | |
| 186 | - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); | |
| 187 | - imagealphablending( $img, true ); | |
| 188 | 550 | imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) ); |
| 189 | 551 | imagedestroy( $src ); |
| 190 | 552 | $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] ); |
| 191 | 553 | imagedestroy( $img ); |
| @@ -198,16 +560,22 @@ | ||
| 198 | 560 | * Change ext |
| 199 | 561 | * |
| 200 | 562 | * @param string $before_file_name before_file_name. |
| 201 | 563 | * @param string $ext ext. |
| 564 | + * @param bool $addext addext. | |
| 202 | 565 | * @return array $after_file_name after_file_name. |
| 203 | 566 | * @since 1.00 |
| 204 | 567 | */ |
| 205 | - private function change_ext( $before_file_name, $ext ) { | |
| 568 | + private function change_ext( $before_file_name, $ext, $addext ) { | |
| 206 | 569 | |
| 207 | - $exts = explode( '.', wp_basename( $before_file_name ) ); | |
| 208 | - $file_ext = '.' . end( $exts ); | |
| 209 | - $after_file_name = rtrim( $before_file_name, $file_ext ) . '.' . $ext; | |
| 570 | + if ( $addext ) { | |
| 571 | + $after_file_name = $before_file_name . '.' . $ext; | |
| 572 | + } else { | |
| 573 | + $exts = explode( '.', $before_file_name ); | |
| 574 | + $before_ext = '.' . end( $exts ); | |
| 575 | + $after_ext = '.' . $ext; | |
| 576 | + $after_file_name = str_replace( $before_ext, $after_ext, $before_file_name ); | |
| 577 | + } | |
| 210 | 578 | |
| 211 | 579 | return $after_file_name; |
| 212 | 580 | |
| 213 | 581 | } |
| @@ -222,12 +590,15 @@ | ||
| 222 | 590 | private function change_db( $before_url, $after_url ) { |
| 223 | 591 | |
| 224 | 592 | global $wpdb; |
| 225 | 593 | |
| 226 | - // Replace. | |
| 594 | + /* Replace */ | |
| 227 | 595 | $wpdb->query( |
| 228 | 596 | $wpdb->prepare( |
| 229 | - "UPDATE `$wpdb->posts` SET post_content = replace(post_content, %s, %s)", | |
| 597 | + " | |
| 598 | + UPDATE {$wpdb->prefix}posts | |
| 599 | + SET post_content = replace( post_content, %s, %s ) | |
| 600 | + ", | |
| 230 | 601 | $before_url, |
| 231 | 602 | $after_url |
| 232 | 603 | ) |
| 233 | 604 | ); |
| @@ -269,8 +640,59 @@ | ||
| 269 | 640 | $upload_url = untrailingslashit( $upload_url ); |
| 270 | 641 | $upload_path = untrailingslashit( $upload_path ); |
| 271 | 642 | |
| 272 | 643 | return array( $upload_dir, $upload_url, $upload_path ); |
| 644 | + | |
| 645 | + } | |
| 646 | + | |
| 647 | + /** ================================================== | |
| 648 | + * Real Url | |
| 649 | + * | |
| 650 | + * @param string $base base. | |
| 651 | + * @param string $relationalpath relationalpath. | |
| 652 | + * @return string $realurl realurl. | |
| 653 | + * @since 1.00 | |
| 654 | + */ | |
| 655 | + private function realurl( $base, $relationalpath ) { | |
| 656 | + | |
| 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 ); | |
| 667 | + | |
| 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 | + } | |
| 273 | 695 | |
| 274 | 696 | } |
| 275 | 697 | |
| 276 | 698 | } |