| @@ -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,22 +55,14 @@ | ||
| 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 ); | |
| 65 | - | |
| 66 | 61 | /* Generate metadata */ |
| 67 | 62 | add_filter( 'wp_generate_attachment_metadata', array( $this, 'generate_webp' ), 10, 2 ); |
| 68 | 63 | /* Original hook */ |
| 69 | - add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 2 ); | |
| 64 | + add_filter( 'pluswebp_get_allimages', array( $this, 'get_allimages' ), 10, 1 ); | |
| 70 | 65 | add_filter( 'pluswebp_mail_messages', array( $this, 'mail_messages' ), 10, 2 ); |
| 71 | 66 | add_action( 'pluswebp_mail_generate_message', array( $this, 'mail_generate_message' ), 10, 2 ); |
| 72 | 67 | |
| 73 | 68 | /* Raise memory */ |
| @@ -76,9 +71,9 @@ | ||
| 76 | 71 | add_filter( 'wp_upload_image_mime_transforms', array( $this, 'control_mime_type' ), 10, 2 ); |
| 77 | 72 | } |
| 78 | 73 | |
| 79 | 74 | /** ================================================== |
| 80 | - * Webp of AVIF generate | |
| 75 | + * Webp generate | |
| 81 | 76 | * |
| 82 | 77 | * @param array $metadata metadata. |
| 83 | 78 | * @param int $attachment_id ID. |
| 84 | 79 | * @return array $metadata metadata. |
| @@ -85,29 +80,15 @@ | ||
| 85 | 80 | * @since 1.00 |
| 86 | 81 | */ |
| 87 | 82 | public function generate_webp( $metadata, $attachment_id ) { |
| 88 | 83 | |
| 89 | - if ( ! is_array( $metadata ) ) { | |
| 90 | - return $metadata; | |
| 91 | - } | |
| 92 | - | |
| 93 | 84 | $pluswebp_settings = get_option( 'pluswebp' ); |
| 85 | + $replace = $pluswebp_settings['replace']; | |
| 94 | 86 | |
| 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 | 87 | $mime_type = get_post_mime_type( $attachment_id ); |
| 107 | 88 | if ( in_array( $mime_type, $pluswebp_settings['types'] ) ) { |
| 108 | 89 | $metadata_webp = $metadata; |
| 109 | - $file_webp = $this->change_ext( $metadata['file'], $ext, $pluswebp_settings['addext'] ); | |
| 90 | + $file_webp = $this->change_ext( $metadata['file'], 'webp', $pluswebp_settings['addext'] ); | |
| 110 | 91 | $metadata_webp['file'] = $file_webp; |
| 111 | 92 | if ( '.' === dirname( $file_webp ) ) { |
| 112 | 93 | $dir_name_url = '/'; |
| 113 | 94 | $dir_name_path = wp_normalize_path( '/' ); |
| @@ -119,18 +100,19 @@ | ||
| 119 | 100 | $path = $this->upload_dir . $dir_name_path; |
| 120 | 101 | |
| 121 | 102 | foreach ( (array) $metadata['sizes'] as $key => $value ) { |
| 122 | 103 | $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 ); | |
| 104 | + $file_thumb_webp = $this->change_ext( $file_thumb, 'webp', $pluswebp_settings['addext'] ); | |
| 105 | + $ret = $this->create_webp( $path . $file_thumb, $mime_type, $path . $file_thumb_webp ); | |
| 106 | + if ( $ret ) { | |
| 107 | + $metadata_webp['sizes'][ $key ]['file'] = $file_thumb_webp; | |
| 108 | + $metadata_webp['sizes'][ $key ]['mime-type'] = 'image/webp'; | |
| 109 | + $webp_size = filesize( $path . wp_basename( $file_thumb_webp ) ); | |
| 110 | + $metadata_webp['sizes'][ $key ]['filesize'] = $webp_size; | |
| 111 | + if ( $replace ) { | |
| 112 | + wp_delete_file( $path . $file_thumb ); | |
| 113 | + $this->change_db( $url . $file_thumb, $url . $file_thumb_webp ); | |
| 114 | + } | |
| 133 | 115 | } |
| 134 | 116 | } |
| 135 | 117 | |
| 136 | 118 | $org_img_file = null; |
| @@ -135,24 +117,24 @@ | ||
| 135 | 117 | |
| 136 | 118 | $org_img_file = null; |
| 137 | 119 | if ( array_key_exists( 'original_image', $metadata ) && ! empty( $metadata['original_image'] ) ) { |
| 138 | 120 | $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'] ); | |
| 121 | + $org_webp_file = $this->change_ext( $org_img_file, 'webp', $pluswebp_settings['addext'] ); | |
| 122 | + $ret = $this->create_webp( $org_img_file, $mime_type, $org_webp_file ); | |
| 141 | 123 | if ( $ret ) { |
| 142 | 124 | $metadata_webp['original_image'] = wp_basename( $org_webp_file ); |
| 143 | 125 | } |
| 144 | 126 | } |
| 145 | 127 | |
| 146 | - $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ), $pluswebp_settings['quality'], $pluswebp_settings['output_mime'] ); | |
| 128 | + $ret = $this->create_webp( $this->upload_dir . '/' . $metadata['file'], $mime_type, $path . wp_basename( $file_webp ) ); | |
| 147 | 129 | $webp_size = filesize( $path . wp_basename( $file_webp ) ); |
| 148 | 130 | $metadata_webp['filesize'] = $webp_size; |
| 149 | 131 | if ( $ret ) { |
| 150 | - if ( $pluswebp_settings['replace'] ) { | |
| 132 | + if ( $replace ) { | |
| 151 | 133 | $up_post = array( |
| 152 | 134 | 'ID' => $attachment_id, |
| 153 | 135 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 154 | - 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 136 | + 'post_mime_type' => 'image/webp', | |
| 155 | 137 | ); |
| 156 | 138 | wp_update_post( $up_post ); |
| 157 | 139 | update_post_meta( $attachment_id, '_wp_attached_file', $file_webp ); |
| 158 | 140 | /* for bulk generate */ |
| @@ -172,9 +154,9 @@ | ||
| 172 | 154 | $post = get_post( $attachment_id ); |
| 173 | 155 | $title = $post->post_title; |
| 174 | 156 | $attachment = array( |
| 175 | 157 | 'guid' => $this->upload_url . '/' . $file_webp, |
| 176 | - 'post_mime_type' => $pluswebp_settings['output_mime'], | |
| 158 | + 'post_mime_type' => 'image/webp', | |
| 177 | 159 | 'post_title' => $title, |
| 178 | 160 | 'post_content' => '', |
| 179 | 161 | 'post_status' => 'inherit', |
| 180 | 162 | ); |
| @@ -284,101 +266,47 @@ | ||
| 284 | 266 | |
| 285 | 267 | /** ================================================== |
| 286 | 268 | * Get All Images |
| 287 | 269 | * |
| 288 | - * @param array $types mime types. | |
| 289 | - * @param string $output_mime output mime type. | |
| 270 | + * @param array $types mime types. | |
| 290 | 271 | * @since 2.00 |
| 291 | 272 | */ |
| 292 | - public function get_allimages( $types, $output_mime ) { | |
| 273 | + public function get_allimages( $types ) { | |
| 293 | 274 | |
| 294 | - if ( empty( $types ) ) { | |
| 295 | - return array( array(), array() ); | |
| 296 | - } | |
| 275 | + $args = array( | |
| 276 | + 'post_type' => 'attachment', | |
| 277 | + 'post_status' => 'inherit', | |
| 278 | + 'post_mime_type' => $types, | |
| 279 | + 'posts_per_page' => -1, | |
| 280 | + 'orderby' => 'date', | |
| 281 | + 'order' => 'DESC', | |
| 282 | + ); | |
| 283 | + $posts = get_posts( $args ); | |
| 297 | 284 | |
| 298 | 285 | global $wpdb; |
| 286 | + $webp_titles = $wpdb->get_col( | |
| 287 | + " | |
| 288 | + SELECT post_title | |
| 289 | + FROM {$wpdb->prefix}posts | |
| 290 | + WHERE post_type = 'attachment' | |
| 291 | + AND post_mime_type IN ( 'image/webp' ) | |
| 292 | + AND post_status = 'inherit' | |
| 293 | + " | |
| 294 | + ); | |
| 299 | 295 | |
| 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 ); | |
| 317 | - } | |
| 318 | - } | |
| 319 | - | |
| 320 | - /* Creating SQL placeholders for $types */ | |
| 321 | - $types_placeholders = implode( ',', array_fill( 0, count( $types ), '%s' ) ); | |
| 322 | - | |
| 323 | - $post_ids = array(); | |
| 296 | + $post_ids = array(); | |
| 324 | 297 | $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; | |
| 352 | - } | |
| 353 | - if ( 0 === $fetched_count ) { | |
| 354 | - break; | |
| 355 | - } | |
| 356 | - | |
| 357 | - foreach ( $chunk_posts as $post ) { | |
| 358 | - if ( ! isset( $webp_titles[ $post->post_title ] ) ) { | |
| 359 | - $file_path = get_attached_file( $post->ID ); | |
| 360 | - | |
| 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 ); | |
| 298 | + $count = 0; | |
| 299 | + foreach ( $posts as $post ) { | |
| 300 | + if ( ! in_array( $post->post_title, $webp_titles ) ) { | |
| 301 | + if ( file_exists( get_attached_file( $post->ID ) ) ) { | |
| 302 | + $post_ids[] = $post->ID; | |
| 303 | + } else { | |
| 304 | + $no_file_ids[] = $post->ID; | |
| 369 | 305 | } |
| 370 | 306 | } |
| 307 | + } | |
| 371 | 308 | |
| 372 | - $offset += $chunk_size; | |
| 373 | - | |
| 374 | - /* Reset the object cache to keep memory usage consistent */ | |
| 375 | - wp_cache_flush(); | |
| 376 | - | |
| 377 | - } while ( $fetched_count === $chunk_size ); | |
| 378 | - | |
| 379 | - unset( $webp_titles, $chunk_posts ); | |
| 380 | - | |
| 381 | 309 | return array( $post_ids, $no_file_ids ); |
| 382 | 310 | } |
| 383 | 311 | |
| 384 | 312 | /** ================================================== |
| @@ -384,9 +312,9 @@ | ||
| 384 | 312 | /** ================================================== |
| 385 | 313 | * Mail messages hook |
| 386 | 314 | * |
| 387 | 315 | * @param array $messages messages. |
| 388 | - * @param int $webp_id webp ID or avif ID. | |
| 316 | + * @param int $webp_id webp_id. | |
| 389 | 317 | * |
| 390 | 318 | * @since 4.00 |
| 391 | 319 | */ |
| 392 | 320 | public function mail_messages( $messages, $webp_id ) { |
| @@ -400,18 +328,18 @@ | ||
| 400 | 328 | /* Output datas*/ |
| 401 | 329 | list( $attachment_link, $attachment_url, $filename, $original_image_url, $original_filename, $mime_type, $stamptime, $file_size ) = $this->output_datas( $webp_id, $metadata ); |
| 402 | 330 | |
| 403 | 331 | $message = 'ID: ' . $webp_id . "\n"; |
| 404 | - $message .= __( 'Title', 'plus-webp' ) . ': ' . get_the_title( $webp_id ) . "\n"; | |
| 405 | - $message .= __( 'Permalink:', 'plus-webp' ) . ' ' . $attachment_link . "\n"; | |
| 332 | + $message .= __( 'Title' ) . ': ' . get_the_title( $webp_id ) . "\n"; | |
| 333 | + $message .= __( 'Permalink:' ) . ' ' . $attachment_link . "\n"; | |
| 406 | 334 | $message .= 'URL: ' . $attachment_url . "\n"; |
| 407 | - $message .= __( 'File name:', 'plus-webp' ) . ' ' . $filename . "\n"; | |
| 335 | + $message .= __( 'File name:' ) . ' ' . $filename . "\n"; | |
| 408 | 336 | if ( ! empty( $original_image_url ) ) { |
| 409 | 337 | $message .= __( 'Original URL:', 'plus-webp' ) . ' ' . $original_image_url . "\n"; |
| 410 | 338 | $message .= __( 'Original File name:', 'plus-webp' ) . ' ' . $original_filename . "\n"; |
| 411 | 339 | } |
| 412 | - $message .= __( 'Date/Time', 'plus-webp' ) . ': ' . $stamptime . "\n"; | |
| 413 | - $message .= __( 'File size:', 'plus-webp' ) . ' ' . $file_size . "\n"; | |
| 340 | + $message .= __( 'Date/Time' ) . ': ' . $stamptime . "\n"; | |
| 341 | + $message .= __( 'File size:' ) . ' ' . $file_size . "\n"; | |
| 414 | 342 | if ( ! empty( $imagethumburls ) ) { |
| 415 | 343 | foreach ( $imagethumburls as $thumbsize => $imagethumburl ) { |
| 416 | 344 | $message .= $thumbsize . ': ' . $imagethumburl . "\n"; |
| 417 | 345 | } |
| @@ -437,30 +365,28 @@ | ||
| 437 | 365 | } else { |
| 438 | 366 | $now_date_time = date_i18n( 'Y-m-d H:i:s' ); |
| 439 | 367 | } |
| 440 | 368 | /* translators: Date and Time */ |
| 441 | - $message_head = sprintf( __( 'Plus WebP or AVIF : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 369 | + $message_head = sprintf( __( 'Plus WebP : %s', 'plus-webp' ), $now_date_time ) . "\r\n\r\n"; | |
| 442 | 370 | /* translators: Generated count */ |
| 443 | - $message_head .= sprintf( __( 'Webp or AVIF generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n"; | |
| 371 | + $message_head .= sprintf( __( 'Webp generated %d.', 'plus-webp' ), $count ) . "\r\n\r\n"; | |
| 444 | 372 | |
| 445 | 373 | $to = get_option( 'admin_email' ); |
| 446 | 374 | /* translators: blogname for subject */ |
| 447 | - $subject = sprintf( __( '[%s] WebP or AVIF generate', 'plus-webp' ), get_option( 'blogname' ) ); | |
| 375 | + $subject = sprintf( __( '[%s] WebP generate', 'plus-webp' ), get_option( 'blogname' ) ); | |
| 448 | 376 | wp_mail( $to, $subject, $message_head . implode( $messages ) ); |
| 449 | 377 | } |
| 450 | 378 | |
| 451 | 379 | /** ================================================== |
| 452 | - * WebP or AVIF create | |
| 380 | + * Webp create | |
| 453 | 381 | * |
| 454 | 382 | * @param string $filename input filename for pictures. |
| 455 | 383 | * @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. | |
| 384 | + * @param string $filename_webp output filename for webp. | |
| 459 | 385 | * @return bool $ret create bool. |
| 460 | 386 | * @since 1.00 |
| 461 | 387 | */ |
| 462 | - private function create_webp( $filename, $mime_type, $filename_webp, $quality, $output_mime ) { | |
| 388 | + private function create_webp( $filename, $mime_type, $filename_webp ) { | |
| 463 | 389 | |
| 464 | 390 | if ( ! file_exists( $filename ) ) { |
| 465 | 391 | return false; |
| 466 | 392 | } |
| @@ -467,8 +393,9 @@ | ||
| 467 | 393 | if ( file_exists( $filename_webp ) ) { |
| 468 | 394 | return false; |
| 469 | 395 | } |
| 470 | 396 | |
| 397 | + $pluswebp_settings = get_option( 'pluswebp' ); | |
| 471 | 398 | @set_time_limit( 60 ); |
| 472 | 399 | wp_raise_memory_limit( 'pluswebp' ); |
| 473 | 400 | |
| 474 | 401 | $ret = false; |
| @@ -502,23 +429,12 @@ | ||
| 502 | 429 | break; |
| 503 | 430 | } |
| 504 | 431 | |
| 505 | 432 | imagecopy( $img, $src, 0, 0, 0, 0, imagesx( $src ), imagesy( $src ) ); |
| 506 | - unset( $src ); | |
| 433 | + imagedestroy( $src ); | |
| 434 | + $ret = imagewebp( $img, $filename_webp, $pluswebp_settings['quality'] ); | |
| 435 | + imagedestroy( $img ); | |
| 507 | 436 | |
| 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 | - } | |
| 518 | - | |
| 519 | - unset( $img ); | |
| 520 | - | |
| 521 | 437 | return $ret; |
| 522 | 438 | } |
| 523 | 439 | |
| 524 | 440 | /** ================================================== |
| @@ -567,13 +483,96 @@ | ||
| 567 | 483 | ) |
| 568 | 484 | ); |
| 569 | 485 | |
| 570 | 486 | /* 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; | |
| 487 | + list( $before_url, $after_url ) = apply_filters( 'plus_webp_advanced_change_db', $before_url, $after_url ); | |
| 488 | + } | |
| 489 | + | |
| 490 | + /** ================================================== | |
| 491 | + * Upload Path | |
| 492 | + * | |
| 493 | + * @return array $upload_dir,$upload_url,$upload_path uploadpath. | |
| 494 | + * @since 1.00 | |
| 495 | + */ | |
| 496 | + public function upload_dir_url_path() { | |
| 497 | + | |
| 498 | + $wp_uploads = wp_upload_dir(); | |
| 499 | + | |
| 500 | + $relation_path_true = strpos( $wp_uploads['baseurl'], '../' ); | |
| 501 | + if ( $relation_path_true > 0 ) { | |
| 502 | + $relationalpath = substr( $wp_uploads['baseurl'], $relation_path_true ); | |
| 503 | + $basepath = substr( $wp_uploads['baseurl'], 0, $relation_path_true ); | |
| 504 | + $upload_url = $this->realurl( $basepath, $relationalpath ); | |
| 505 | + $upload_dir = wp_normalize_path( realpath( $wp_uploads['basedir'] ) ); | |
| 506 | + } else { | |
| 507 | + $upload_url = $wp_uploads['baseurl']; | |
| 508 | + $upload_dir = wp_normalize_path( $wp_uploads['basedir'] ); | |
| 509 | + } | |
| 510 | + | |
| 511 | + if ( is_ssl() ) { | |
| 512 | + $upload_url = str_replace( 'http:', 'https:', $upload_url ); | |
| 513 | + } | |
| 514 | + | |
| 515 | + if ( $relation_path_true > 0 ) { | |
| 516 | + $upload_path = $relationalpath; | |
| 517 | + } else { | |
| 518 | + $upload_path = str_replace( site_url( '/' ), '', $upload_url ); | |
| 519 | + } | |
| 520 | + | |
| 521 | + $upload_dir = untrailingslashit( $upload_dir ); | |
| 522 | + $upload_url = untrailingslashit( $upload_url ); | |
| 523 | + $upload_path = untrailingslashit( $upload_path ); | |
| 524 | + | |
| 525 | + return array( $upload_dir, $upload_url, $upload_path ); | |
| 526 | + } | |
| 527 | + | |
| 528 | + /** ================================================== | |
| 529 | + * Real Url | |
| 530 | + * | |
| 531 | + * @param string $base base. | |
| 532 | + * @param string $relationalpath relationalpath. | |
| 533 | + * @return string $realurl realurl. | |
| 534 | + * @since 1.00 | |
| 535 | + */ | |
| 536 | + private function realurl( $base, $relationalpath ) { | |
| 537 | + | |
| 538 | + $parse = array( | |
| 539 | + 'scheme' => null, | |
| 540 | + 'user' => null, | |
| 541 | + 'pass' => null, | |
| 542 | + 'host' => null, | |
| 543 | + 'port' => null, | |
| 544 | + 'query' => null, | |
| 545 | + 'fragment' => null, | |
| 546 | + ); | |
| 547 | + $parse = wp_parse_url( $base ); | |
| 548 | + | |
| 549 | + if ( strpos( $parse['path'], '/', ( strlen( $parse['path'] ) - 1 ) ) !== false ) { | |
| 550 | + $parse['path'] .= '.'; | |
| 551 | + } | |
| 552 | + | |
| 553 | + if ( preg_match( '#^https?://#', $relationalpath ) ) { | |
| 554 | + return $relationalpath; | |
| 555 | + } elseif ( preg_match( '#^/.*$#', $relationalpath ) ) { | |
| 556 | + return $parse['scheme'] . '://' . $parse['host'] . $relationalpath; | |
| 557 | + } else { | |
| 558 | + $base_path = explode( '/', dirname( $parse['path'] ) ); | |
| 559 | + $rel_path = explode( '/', $relationalpath ); | |
| 560 | + foreach ( $rel_path as $rel_dir_name ) { | |
| 561 | + if ( '.' === $rel_dir_name ) { | |
| 562 | + array_shift( $base_path ); | |
| 563 | + array_unshift( $base_path, '' ); | |
| 564 | + } elseif ( '..' === $rel_dir_name ) { | |
| 565 | + array_pop( $base_path ); | |
| 566 | + if ( count( $base_path ) === 0 ) { | |
| 567 | + $base_path = array( '' ); | |
| 568 | + } | |
| 569 | + } else { | |
| 570 | + array_push( $base_path, $rel_dir_name ); | |
| 571 | + } | |
| 572 | + } | |
| 573 | + $path = implode( '/', $base_path ); | |
| 574 | + return $parse['scheme'] . '://' . $parse['host'] . $path; | |
| 576 | 575 | } |
| 577 | 576 | } |
| 578 | 577 | |
| 579 | 578 | /** ================================================== |