| @@ -89,11 +89,18 @@ | ||
| 89 | 89 | $metadata_webp['file'] = $file_webp; |
| 90 | 90 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 91 | 91 | $file_thumb = $value['file']; |
| 92 | 92 | $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] ); |
| 93 | - $path = $this->upload_dir . '/' . dirname( $file_webp ) . '/'; | |
| 94 | - $url = $this->upload_url . '/' . dirname( $file_webp ) . '/'; | |
| 95 | - $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); | |
| 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 ); | |
| 96 | 103 | if ( $ret ) { |
| 97 | 104 | $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; |
| 98 | 105 | $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; |
| 99 | 106 | if ( $replace ) { |
| @@ -101,10 +108,11 @@ | ||
| 101 | 108 | $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); |
| 102 | 109 | } |
| 103 | 110 | } |
| 104 | 111 | } |
| 105 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 106 | - $org_img_file = wp_get_original_image_path( $attachment_id, false ); | |
| 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 ) ); | |
| 107 | 115 | $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] ); |
| 108 | 116 | $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file ); |
| 109 | 117 | if ( $ret ) { |
| 110 | 118 | $metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| @@ -124,8 +132,11 @@ | ||
| 124 | 132 | /* for bulk generate */ |
| 125 | 133 | update_post_meta( $attachment_id, '_wp_attachment_metadata', $metadata_webp ); |
| 126 | 134 | /* delete org file */ |
| 127 | 135 | unlink( $this->upload_dir . '/' . $metadata['file'] ); |
| 136 | + if ( $org_img_file ) { | |
| 137 | + unlink( $org_img_file ); | |
| 138 | + } | |
| 128 | 139 | /* Replace */ |
| 129 | 140 | $this->change_db( $this->upload_url . '/' . $metadata['file'], $this->upload_url . '/' . $file_webp ); |
| 130 | 141 | /* for hook */ |
| 131 | 142 | $metadata = $metadata_webp; |
| @@ -140,9 +151,15 @@ | ||
| 140 | 151 | 'post_title' => $title, |
| 141 | 152 | 'post_content' => '', |
| 142 | 153 | 'post_status' => 'inherit', |
| 143 | 154 | ); |
| 144 | - $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 | + | |
| 145 | 162 | wp_update_attachment_metadata( $attach_id, $metadata_webp ); |
| 146 | 163 | |
| 147 | 164 | $author = get_userdata( $post->post_author ); |
| 148 | 165 | $userid = $author->ID; |
| @@ -189,43 +206,40 @@ | ||
| 189 | 206 | $error_string = __( 'No media!', 'plus-webp' ); |
| 190 | 207 | $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; |
| 191 | 208 | } else { |
| 192 | 209 | $output_html = $this->output_html( $id ); |
| 193 | - if ( is_null( $output_html ) ) { | |
| 194 | - $error_string = __( 'WebP was not generated.', 'plus-webp' ); | |
| 195 | - $output_html = '<div>ID: ' . $id . ' <span style="color: red;">' . $error_string . '</span></div>'; | |
| 196 | - } | |
| 197 | 210 | } |
| 198 | - | |
| 199 | - header( 'Content-type: text/html; charset=UTF-8' ); | |
| 200 | - $allowed_output_html = array( | |
| 201 | - 'a' => array( | |
| 202 | - 'href' => array(), | |
| 203 | - 'target' => array(), | |
| 204 | - 'rel' => array(), | |
| 205 | - 'style' => array(), | |
| 206 | - ), | |
| 207 | - 'img' => array( | |
| 208 | - 'src' => array(), | |
| 209 | - 'width' => array(), | |
| 210 | - 'height' => array(), | |
| 211 | - 'style' => array(), | |
| 212 | - ), | |
| 213 | - 'div' => array( | |
| 214 | - 'style' => array(), | |
| 215 | - 'class' => array(), | |
| 216 | - ), | |
| 217 | - 'font' => array( | |
| 218 | - 'color' => array(), | |
| 219 | - ), | |
| 220 | - 'ul' => array(), | |
| 221 | - 'li' => array(), | |
| 222 | - 'span' => array( | |
| 223 | - 'class' => array(), | |
| 224 | - 'style' => array(), | |
| 225 | - ), | |
| 226 | - ); | |
| 227 | - echo wp_kses( $output_html, $allowed_output_html ); | |
| 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 | + } | |
| 228 | 242 | } |
| 229 | 243 | } else { |
| 230 | 244 | status_header( '403' ); |
| 231 | 245 | echo 'Forbidden'; |
| @@ -256,9 +270,9 @@ | ||
| 256 | 270 | |
| 257 | 271 | /* Thumbnail urls */ |
| 258 | 272 | list( $image_thumbnail, $imagethumburls ) = $this->thumbnail_urls( $webp_id, $metadata, $this->upload_url ); |
| 259 | 273 | /* Output datas*/ |
| 260 | - list( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id ); | |
| 274 | + list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata ); | |
| 261 | 275 | |
| 262 | 276 | $output_html = '<div style="border-bottom: 1px solid; padding-top: 5px; padding-bottom: 5px;">'; |
| 263 | 277 | $output_html .= '<img width="40" height="40" src="' . $image_thumbnail . '" style="float: left; margin: 5px;">'; |
| 264 | 278 | $output_html .= '<div style="overflow: hidden;">'; |
| @@ -265,12 +279,12 @@ | ||
| 265 | 279 | $output_html .= '<div>ID: ' . $webp_id . '</div>'; |
| 266 | 280 | $output_html .= '<div>' . __( 'Title' ) . ': ' . get_the_title( $webp_id ) . '</div>'; |
| 267 | 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>'; |
| 268 | 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>'; |
| 269 | - $output_html .= '<div>' . __( 'File name:' ) . ' ' . wp_basename( $attachment_url ) . '</div>'; | |
| 270 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 283 | + $output_html .= '<div>' . __( 'File name:' ) . ' ' . $filename . '</div>'; | |
| 284 | + if ( ! empty( $original_image_url ) ) { | |
| 271 | 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>'; |
| 272 | - $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . wp_basename( $original_image_url ) . '</div>'; | |
| 286 | + $output_html .= '<div>' . __( 'Original File name', 'plus-webp' ) . ': ' . $original_filename . '</div>'; | |
| 273 | 287 | } |
| 274 | 288 | $output_html .= '<div>' . __( 'Date/Time' ) . ': ' . $stamptime . '</div>'; |
| 275 | 289 | $output_html .= '<div>' . __( 'File type:' ) . ' ' . $mime_type . '</div>'; |
| 276 | 290 | $output_html .= '<div>' . __( 'File size:' ) . ' ' . $file_size . '</div>'; |
| @@ -304,15 +318,14 @@ | ||
| 304 | 318 | $image_attr_thumbnail = wp_get_attachment_image_src( $attach_id, 'thumbnail', true ); |
| 305 | 319 | $image_thumbnail = $image_attr_thumbnail[0]; |
| 306 | 320 | |
| 307 | 321 | $imagethumburls = array(); |
| 308 | - $media_path = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 309 | - $media_paths = explode( DIRECTORY_SEPARATOR, $media_path ); | |
| 310 | - $str_length = strlen( end( $media_paths ) ); | |
| 311 | - $media_path = substr( $media_path, 0, -$str_length ); | |
| 312 | - $media_url = $upload_url . '/' . $media_path; | |
| 313 | - if ( ! empty( $metadata ) ) { | |
| 314 | - $thumbnails = $metadata['sizes']; | |
| 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; | |
| 315 | 328 | foreach ( $thumbnails as $key => $key2 ) { |
| 316 | 329 | $imagethumburls[ $key ] = $media_url . $key2['file']; |
| 317 | 330 | } |
| 318 | 331 | } |
| @@ -323,33 +336,40 @@ | ||
| 323 | 336 | |
| 324 | 337 | /** ================================================== |
| 325 | 338 | * Output datas |
| 326 | 339 | * |
| 327 | - * @param int $attach_id attach_id. | |
| 328 | - * @return array (string) $attachment_link, $attachment_url, $original_image_url, $mime_type(string), $stamptime, $file_size | |
| 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 | |
| 329 | 343 | * @since 2.00 |
| 330 | 344 | */ |
| 331 | - private function output_datas( $attach_id ) { | |
| 345 | + private function output_datas( $attach_id, $metadata ) { | |
| 332 | 346 | |
| 333 | 347 | $attachment_link = get_attachment_link( $attach_id ); |
| 334 | 348 | $attachment_url = wp_get_attachment_url( $attach_id ); |
| 335 | - $original_image_url = wp_get_original_image_url( $attach_id ); | |
| 336 | - $filetype = wp_check_filetype( get_attached_file( $attach_id ) ); | |
| 337 | - $mime_type = $filetype['type']; | |
| 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 ); | |
| 338 | 358 | |
| 339 | 359 | $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); |
| 340 | - if ( isset( $metadata['filesize'] ) ) { | |
| 360 | + if ( ! empty( $metadata ) && array_key_exists( 'filesize', $metadata ) && ! empty( $metadata['filesize'] ) ) { | |
| 341 | 361 | $file_size = $metadata['filesize']; |
| 342 | 362 | } else { |
| 343 | 363 | $file_size = @filesize( get_attached_file( $attach_id ) ); |
| 344 | 364 | } |
| 345 | 365 | if ( ! $file_size ) { |
| 346 | - $file_size = '<font color="red">' . __( 'Could not retrieve.', 'plus-webp' ) . '</font>'; | |
| 366 | + $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 347 | 367 | } else { |
| 348 | 368 | $file_size = size_format( $file_size ); |
| 349 | 369 | } |
| 350 | 370 | |
| 351 | - return array( $attachment_link, $attachment_url, $original_image_url, $mime_type, $stamptime, $file_size ); | |
| 371 | + return array( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ); | |
| 352 | 372 | |
| 353 | 373 | } |
| 354 | 374 | |
| 355 | 375 | /** ================================================== |
| @@ -436,29 +456,27 @@ | ||
| 436 | 456 | $pluswebp_mail_send['count'] = count( $post_ids ); |
| 437 | 457 | |
| 438 | 458 | $count = 0; |
| 439 | 459 | foreach ( $post_ids as $attach_id ) { |
| 440 | - $filename = get_attached_file( $attach_id ); | |
| 441 | 460 | $metadata = wp_get_attachment_metadata( $attach_id ); |
| 442 | - /* OutputMetaData */ | |
| 443 | - list( $imagethumburls, $stamptime, $file_size ) = $this->output_metadata( $attach_id, $metadata, $this->upload_url ); | |
| 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 | + | |
| 444 | 467 | $count++; |
| 445 | 468 | $message .= __( 'Count' ) . ': ' . $count . "\n"; |
| 446 | 469 | $message .= 'ID: ' . $attach_id . "\n"; |
| 447 | 470 | $message .= __( 'Title' ) . ': ' . get_the_title( $attach_id ) . "\n"; |
| 448 | - $message .= __( 'Permalink:' ) . ' ' . get_attachment_link( $attach_id ) . "\n"; | |
| 449 | - $message .= 'URL: ' . wp_get_attachment_url( $attach_id ) . "\n"; | |
| 450 | - $message .= __( 'File name:' ) . ' ' . wp_basename( wp_get_attachment_url( $attach_id ) ) . "\n"; | |
| 451 | - if ( ! empty( $metadata['original_image'] ) ) { | |
| 452 | - $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . wp_get_original_image_url( $attach_id ) . "\n"; | |
| 453 | - $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . wp_basename( wp_get_original_image_url( $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"; | |
| 454 | 477 | } |
| 455 | 478 | $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; |
| 456 | - if ( ! $file_size ) { | |
| 457 | - $file_size = __( 'Could not retrieve.', 'plus-webp' ); | |
| 458 | - } else { | |
| 459 | - $file_size = size_format( $file_size ); | |
| 460 | - } | |
| 461 | 479 | $message .= __( 'File size:' ) . ' ' . $file_size . "\n"; |
| 462 | 480 | if ( ! empty( $imagethumburls ) ) { |
| 463 | 481 | foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { |
| 464 | 482 | $message .= $thumbsize . ': ' . $imagethumburl . "\n"; |
| @@ -479,39 +497,8 @@ | ||
| 479 | 497 | |
| 480 | 498 | } |
| 481 | 499 | |
| 482 | 500 | /** ================================================== |
| 483 | - * Output metadata | |
| 484 | - * | |
| 485 | - * @param int $attach_id attach_id. | |
| 486 | - * @param array $metadata metadata. | |
| 487 | - * @param string $upload_url upload_url. | |
| 488 | - * @return array $imagethumburls(string), $stamptime(string), $file_size(string) | |
| 489 | - * @since 1.09 | |
| 490 | - */ | |
| 491 | - private function output_metadata( $attach_id, $metadata, $upload_url ) { | |
| 492 | - | |
| 493 | - $imagethumburls = array(); | |
| 494 | - $wp_attached_file = get_post_meta( $attach_id, '_wp_attached_file', true ); | |
| 495 | - $imagethumburl_base = $upload_url . '/' . rtrim( $wp_attached_file, wp_basename( $wp_attached_file ) ); | |
| 496 | - if ( ! empty( $metadata ) ) { | |
| 497 | - foreach ( $metadata as $key1 => $key2 ) { | |
| 498 | - if ( 'sizes' === $key1 ) { | |
| 499 | - foreach ( $metadata[ $key1 ] as $key2 => $key3 ) { | |
| 500 | - $imagethumburls[ $key2 ] = $imagethumburl_base . $metadata['sizes'][ $key2 ]['file']; | |
| 501 | - } | |
| 502 | - } | |
| 503 | - } | |
| 504 | - } | |
| 505 | - | |
| 506 | - $stamptime = get_the_time( 'Y-n-j ', $attach_id ) . get_the_time( 'G:i:s', $attach_id ); | |
| 507 | - $file_size = @filesize( get_attached_file( $attach_id, false ) ); | |
| 508 | - | |
| 509 | - return array( $imagethumburls, $stamptime, $file_size ); | |
| 510 | - | |
| 511 | - } | |
| 512 | - | |
| 513 | - /** ================================================== | |
| 514 | 501 | * Webp create |
| 515 | 502 | * |
| 516 | 503 | * @param string $filename input filename for pictures. |
| 517 | 504 | * @param string $mime_type mimetype. |
| @@ -529,8 +516,9 @@ | ||
| 529 | 516 | } |
| 530 | 517 | |
| 531 | 518 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 532 | 519 | @set_time_limit( 60 ); |
| 520 | + @ini_set( 'memory_limit', '256M' ); | |
| 533 | 521 | |
| 534 | 522 | $ret = false; |
| 535 | 523 | switch ( $mime_type ) { |
| 536 | 524 | case 'image/jpeg': |
| @@ -535,9 +523,10 @@ | ||
| 535 | 523 | switch ( $mime_type ) { |
| 536 | 524 | case 'image/jpeg': |
| 537 | 525 | $src = imagecreatefromjpeg( $filename ); |
| 538 | 526 | $img = imagecreatetruecolor( imagesx( $src ), imagesy( $src ) ); |
| 539 | - imagefill( $img, 0, 0, imagecolorallocate( $img, 255, 255, 255 ) ); | |
| 527 | + $bgcolor = imagecolorallocate( $img, 255, 255, 255 ); | |
| 528 | + imagefill( $img, 0, 0, $bgcolor ); | |
| 540 | 529 | imagealphablending( $img, true ); |
| 541 | 530 | break; |
| 542 | 531 | case 'image/png': |
| 543 | 532 | $src = imagecreatefrompng( $filename ); |
| @@ -604,9 +593,12 @@ | ||
| 604 | 593 | |
| 605 | 594 | /* Replace */ |
| 606 | 595 | $wpdb->query( |
| 607 | 596 | $wpdb->prepare( |
| 608 | - "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 | + ", | |
| 609 | 601 | $before_url, |
| 610 | 602 | $after_url |
| 611 | 603 | ) |
| 612 | 604 | ); |