| @@ -77,14 +77,14 @@ | ||
| 77 | 77 | */ |
| 78 | 78 | public function filter_post_backward( $content ) { |
| 79 | 79 | |
| 80 | 80 | // Enable backword compatibility |
| 81 | - add_filter( 'wpmcs_enable_backward_url_repalcement', [ $this, 'enable_backward_url_check' ] ); | |
| 81 | + add_filter( 'wpmcs_enable_backward_url_replacement', [ $this, 'enable_backward_url_check' ] ); | |
| 82 | 82 | |
| 83 | 83 | $content = $this->filter_post( $content ); |
| 84 | 84 | |
| 85 | 85 | // remove backword compatibility after process |
| 86 | - remove_filter( 'wpmcs_enable_backward_url_repalcement', [ $this, 'enable_backward_url_check' ] ); | |
| 86 | + remove_filter( 'wpmcs_enable_backward_url_replacement', [ $this, 'enable_backward_url_check' ] ); | |
| 87 | 87 | |
| 88 | 88 | return $content; |
| 89 | 89 | } |
| 90 | 90 | |
| @@ -216,9 +216,9 @@ | ||
| 216 | 216 | protected function pre_filter_content( $content ) { |
| 217 | 217 | $uploads = wp_upload_dir(); |
| 218 | 218 | $base_url = Utils::remove_scheme( $uploads['baseurl'] ); |
| 219 | 219 | |
| 220 | - return Service::instance()->remove_query_strings( $content, $base_url ); | |
| 220 | + return Utils::remove_query_strings( $content, $base_url ); | |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | /** |
| 224 | 224 | * Get URLs from img src. |
| @@ -258,9 +258,9 @@ | ||
| 258 | 258 | if ( ! Utils::is_file_url( $url ) ) { |
| 259 | 259 | continue; |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | - if ( ! $this->url_needs_replacing( $url ) ) { | |
| 262 | + if ( ! self::url_needs_replacing( $url ) ) { | |
| 263 | 263 | // URL already correct, skip |
| 264 | 264 | continue; |
| 265 | 265 | } |
| 266 | 266 | |
| @@ -265,9 +265,12 @@ | ||
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | $bare_url = Utils::reduce_url( $url ); |
| 268 | 268 | |
| 269 | - $item_sources[ $bare_url ] = absint( $class_id[1] ); | |
| 269 | + $item_sources[ $bare_url ] = [ | |
| 270 | + 'id' => absint( $class_id[1] ), | |
| 271 | + 'source_type' => 'media_library' | |
| 272 | + ]; | |
| 270 | 273 | } |
| 271 | 274 | |
| 272 | 275 | if ( count( $item_sources ) > 1 ) { |
| 273 | 276 | /* |
| @@ -275,9 +278,9 @@ | ||
| 275 | 278 | * |
| 276 | 279 | * To avoid making a database call for each image, a single query |
| 277 | 280 | * warms the object cache with the meta information for all images. |
| 278 | 281 | */ |
| 279 | - update_meta_cache( 'post', array_unique( $item_sources ) ); | |
| 282 | + update_meta_cache( 'post', array_unique(array_column($item_sources, 'id'))); | |
| 280 | 283 | } |
| 281 | 284 | |
| 282 | 285 | foreach ( $item_sources as $url => $item_source ) { |
| 283 | 286 | if ( ! $this->item_matches_src( $item_source, $url ) ) { |
| @@ -297,26 +300,25 @@ | ||
| 297 | 300 | * @param string $url |
| 298 | 301 | * |
| 299 | 302 | * @return bool |
| 300 | 303 | */ |
| 301 | - public function url_needs_replacing( $url ) { | |
| 302 | - $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_repalcement', false); | |
| 304 | + public static function url_needs_replacing( $url ) { | |
| 305 | + $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false); | |
| 303 | 306 | |
| 304 | 307 | if( $reverse_compatibility ) { |
| 305 | 308 | if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) !== $url ) { |
| 309 | + // Local URL, no replacement needed. | |
| 306 | 310 | return false; |
| 307 | 311 | } |
| 308 | 312 | |
| 309 | 313 | if ( str_replace( Filter::get_remote_domains(), '', $url ) === $url ) { |
| 314 | + // Not a known remote URL, no replacement needed. | |
| 310 | 315 | return false; |
| 311 | 316 | } |
| 312 | 317 | } else { |
| 313 | 318 | if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) { |
| 314 | 319 | // Remote URL, no replacement needed |
| 315 | - if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) { | |
| 316 | - // Remote URL, no replacement needed | |
| 317 | - return false; | |
| 318 | - } | |
| 320 | + return false; | |
| 319 | 321 | } |
| 320 | 322 | } |
| 321 | 323 | |
| 322 | 324 | // Server URL, perform replacement |
| @@ -333,40 +335,44 @@ | ||
| 333 | 335 | */ |
| 334 | 336 | public function item_matches_src( $item_source, $url ): bool { |
| 335 | 337 | $upload_dir = wp_get_upload_dir(); |
| 336 | 338 | $wpmcsItem = Item::instance(); |
| 337 | - $item = $wpmcsItem->get( $item_source ); | |
| 338 | 339 | |
| 340 | + $backward_replacement = apply_filters('wpmcs_enable_backward_url_replacement', false); | |
| 339 | 341 | |
| 340 | - $backward_replacement = apply_filters('wpmcs_enable_backward_url_repalcement', false); | |
| 341 | - | |
| 342 | 342 | if ( |
| 343 | - Utils::is_empty($item) || | |
| 344 | - get_post_type( $item_source ) !== 'attachment' | |
| 343 | + $this->is_empty_item_source($item_source) || | |
| 344 | + 'media_library' !== $item_source['source_type'] || | |
| 345 | + get_post_type( $item_source['id'] ) !== 'attachment' | |
| 345 | 346 | ) { |
| 346 | 347 | return false; |
| 347 | 348 | } |
| 348 | - | |
| 349 | + | |
| 350 | + $item = $wpmcsItem->get( $item_source['id'], $item_source['source_type'] ); | |
| 351 | + | |
| 352 | + if(!$item) return false; | |
| 353 | + | |
| 349 | 354 | $bare_url = []; |
| 350 | 355 | |
| 351 | 356 | $bare_url[] = $backward_replacement |
| 352 | - ? Utils::reduce_url($wpmcsItem->get_url( $item_source )) | |
| 357 | + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'full', $item_source['source_type'] )) | |
| 353 | 358 | : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['source_path']); |
| 354 | 359 | |
| 355 | 360 | |
| 356 | - $extras = $wpmcsItem->get_extras( $item_source ); | |
| 357 | 361 | |
| 358 | - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) { | |
| 362 | + if(isset($item['original_source_path']) && !Utils::is_empty($item['original_source_path'])){ | |
| 359 | 363 | $bare_url[] = $backward_replacement |
| 360 | - ? Utils::reduce_url($wpmcsItem->get_url( $item_source, 'original' )) | |
| 361 | - : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$extras['original']['source_path']); | |
| 364 | + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'original', $item_source['source_type'] )) | |
| 365 | + : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['original_source_path']); | |
| 362 | 366 | } |
| 363 | 367 | |
| 368 | + $extras = $wpmcsItem->get_extras( $item_source['id'], false, $item_source['source_type'] ); | |
| 369 | + | |
| 364 | 370 | if(isset($extras['sizes']) && !Utils::is_empty($extras['sizes'])) { |
| 365 | 371 | foreach($extras['sizes'] as $size => $sub_image) { |
| 366 | 372 | if(!Utils::is_empty($sub_image)){ |
| 367 | 373 | $bare_url[] = $backward_replacement |
| 368 | - ? Utils::reduce_url($wpmcsItem->get_url( $item_source, $size )) | |
| 374 | + ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], $size, $item_source['source_type'] )) | |
| 369 | 375 | : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$sub_image['source_path']); |
| 370 | 376 | } |
| 371 | 377 | } |
| 372 | 378 | } |
| @@ -390,22 +396,26 @@ | ||
| 390 | 396 | */ |
| 391 | 397 | protected function push_to_url_pairs( &$url_pairs, $item_source, $find, &$to_cache ) { |
| 392 | 398 | $upload_dir = wp_get_upload_dir(); |
| 393 | 399 | $wpmcsItem = Item::instance(); |
| 394 | - $item = $wpmcsItem->get( $item_source ); | |
| 400 | + $item = $wpmcsItem->get( $item_source['id'], $item_source['source_type'] ); | |
| 395 | 401 | |
| 396 | 402 | if ( |
| 397 | 403 | Utils::is_empty($item) || |
| 398 | - get_post_type( $item_source ) !== 'attachment' | |
| 404 | + get_post_type( $item_source['id'] ) !== 'attachment' | |
| 399 | 405 | ) { |
| 400 | 406 | return false; |
| 401 | 407 | } |
| 402 | 408 | |
| 409 | + if ( ! apply_filters( 'wpmcs_push_to_url_pairs', true, $item_source['id'], $item_source['source_type'] ) ) { | |
| 410 | + return false; | |
| 411 | + } | |
| 412 | + | |
| 403 | 413 | $base = trailingslashit($upload_dir['baseurl']); |
| 404 | - $backward_replacement = apply_filters('wpmcs_enable_backward_url_repalcement', false); | |
| 414 | + $backward_replacement = apply_filters('wpmcs_enable_backward_url_replacement', false); | |
| 405 | 415 | |
| 406 | 416 | $source_server_file = Utils::remove_scheme($base.$item['source_path']); |
| 407 | - $source_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source)); | |
| 417 | + $source_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source['id'], 'full', $item_source['source_type'])); | |
| 408 | 418 | |
| 409 | 419 | $url_pairs[$source_server_file] = $source_cloud_file; |
| 410 | 420 | if($backward_replacement) { |
| 411 | 421 | $url_pairs[$source_cloud_file] = $source_server_file; // Backward compatibility |
| @@ -410,22 +420,29 @@ | ||
| 410 | 420 | if($backward_replacement) { |
| 411 | 421 | $url_pairs[$source_cloud_file] = $source_server_file; // Backward compatibility |
| 412 | 422 | } |
| 413 | 423 | |
| 414 | - $extras = $wpmcsItem->get_extras($item_source); | |
| 415 | 424 | |
| 416 | - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) { | |
| 417 | - $original_server_file = Utils::remove_scheme($base.$extras['original']['source_path']); | |
| 418 | - $original_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source, 'original')); | |
| 425 | + if( | |
| 426 | + isset($item['original_source_path']) && | |
| 427 | + !Utils::is_empty($item['original_source_path']) | |
| 428 | + ) { | |
| 429 | + $original_server_file = Utils::remove_scheme($base.$item['original_source_path']); | |
| 430 | + $original_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source['id'], 'original', $item_source['source_type'])); | |
| 419 | 431 | $url_pairs[$original_server_file] = $original_cloud_file; |
| 420 | 432 | if($backward_replacement) { |
| 421 | 433 | $url_pairs[$original_cloud_file] = $original_server_file; // Backward compatibility |
| 422 | 434 | } |
| 423 | 435 | } |
| 436 | + | |
| 437 | + $extras = $wpmcsItem->get_extras($item_source['id'], false, $item_source['source_type']); | |
| 438 | + | |
| 424 | 439 | if(isset($extras['sizes'])){ |
| 425 | 440 | foreach ($extras['sizes'] as $size => $sub_image) { |
| 441 | + if(Utils::is_empty($sub_image['source_path'])) continue; | |
| 442 | + | |
| 426 | 443 | $sub_server_file = Utils::remove_scheme($base.$sub_image['source_path']); |
| 427 | - $sub_cloud_file = Utils::remove_scheme($wpmcsItem->get_url( $item_source, $size )); | |
| 444 | + $sub_cloud_file = Utils::remove_scheme($wpmcsItem->get_url( $item_source['id'], $size, $item_source['source_type'] )); | |
| 428 | 445 | $url_pairs[$sub_server_file] = $sub_cloud_file; |
| 429 | 446 | if($backward_replacement) { |
| 430 | 447 | $url_pairs[$sub_cloud_file ] = $sub_server_file; // Backward compatibility |
| 431 | 448 | } |
| @@ -509,9 +526,9 @@ | ||
| 509 | 526 | if ( ! Utils::is_file_url( $url ) ) { |
| 510 | 527 | continue; |
| 511 | 528 | } |
| 512 | 529 | |
| 513 | - if ( ! $this->url_needs_replacing( $url ) ) { | |
| 530 | + if ( ! self::url_needs_replacing( $url ) ) { | |
| 514 | 531 | // URL already correct, skip |
| 515 | 532 | continue; |
| 516 | 533 | } |
| 517 | 534 | |
| @@ -520,9 +537,9 @@ | ||
| 520 | 537 | |
| 521 | 538 | // If attachment ID recently or previously cached, skip full search. |
| 522 | 539 | if ( isset( $to_cache[ $bare_url ] ) ) { |
| 523 | 540 | $item_source = $to_cache[ $bare_url ]; |
| 524 | - } elseif ( isset( $cache[ $bare_url ] ) ) { | |
| 541 | + } elseif ( isset( $cache[ $bare_url ] ) && is_array($cache[ $bare_url ]) ) { | |
| 525 | 542 | $item_source = $cache[ $bare_url ]; |
| 526 | 543 | } |
| 527 | 544 | |
| 528 | 545 | if ( is_null( $item_source ) ) { |
| @@ -560,9 +577,11 @@ | ||
| 560 | 577 | |
| 561 | 578 | if ( empty( $urls ) ) { |
| 562 | 579 | return $results; |
| 563 | 580 | } |
| 564 | - | |
| 581 | + // Check if backward compatibility is enabled | |
| 582 | + $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false); | |
| 583 | + | |
| 565 | 584 | if ( ! is_array( $urls ) ) { |
| 566 | 585 | $urls = array( $urls ); |
| 567 | 586 | } |
| 568 | 587 | |
| @@ -595,9 +614,9 @@ | ||
| 595 | 614 | continue; |
| 596 | 615 | } |
| 597 | 616 | |
| 598 | 617 | |
| 599 | - $path = Utils::get_attachment_source_path( $full_url ); | |
| 618 | + $path = Utils::get_attachment_source_path( $full_url, $reverse_compatibility ? 'key' : 'source' ); | |
| 600 | 619 | |
| 601 | 620 | $paths[ $path ] = $full_url; |
| 602 | 621 | $full_urls[ $full_url ][] = $url; |
| 603 | 622 | } |
| @@ -602,20 +621,32 @@ | ||
| 602 | 621 | $full_urls[ $full_url ][] = $url; |
| 603 | 622 | } |
| 604 | 623 | |
| 605 | 624 | if ( ! empty( $paths ) ) { |
| 606 | - $wpmcsItems = $wpmcsItem->get_items_by_source_paths( array_keys( $paths ) ); | |
| 625 | + if($reverse_compatibility) { | |
| 626 | + // For backward compatibility, search inside 'key' and 'original_key' columns. | |
| 627 | + $wpmcsItems = $wpmcsItem->get_items_by_paths( array_keys( $paths ), true, false, 'key' ); | |
| 628 | + } else { | |
| 629 | + $wpmcsItems = $wpmcsItem->get_items_by_paths( array_keys( $paths ) ); | |
| 630 | + } | |
| 607 | 631 | |
| 608 | 632 | if ( ! empty( $wpmcsItems ) ) { |
| 609 | 633 | foreach ( $wpmcsItems as $item ) { |
| 610 | - // Each returned item may have matched on either the source_path or original_source_path. | |
| 634 | + // Each returned item may have matched on either the source_path or original_source_path | |
| 635 | + // (or, in backward-compatibility mode, key or original_key — $paths above is keyed by | |
| 636 | + // whichever field type get_attachment_source_path() was asked for, so the lookup below | |
| 637 | + // must compare against that same field, not always source_path). | |
| 611 | 638 | // Because the base image file name of a thumbnail might match the primary rather scaled or rotated full image |
| 612 | 639 | // it's possible that both source paths are used by separate URLs. |
| 613 | 640 | $source_id = (int)$item['source_id']; |
| 614 | - $source_paths = [ $item['source_path'] ]; | |
| 615 | - $original = $wpmcsItem->get_extras($source_id, 'original'); | |
| 616 | - if(isset($original['source_path'])) { | |
| 617 | - $source_paths[] = $original['source_path']; | |
| 641 | + $source_type = $item['source_type']; | |
| 642 | + $path_field = $reverse_compatibility ? 'key' : 'source_path'; | |
| 643 | + $original_field = $reverse_compatibility ? 'original_key' : 'original_source_path'; | |
| 644 | + $source_paths = [ $item[ $path_field ] ]; | |
| 645 | + | |
| 646 | + // If the original path/key is set, add it to source_paths. | |
| 647 | + if(isset($item[ $original_field ]) && !Utils::is_empty($item[ $original_field ])) { | |
| 648 | + $source_paths[] = $item[ $original_field ]; | |
| 618 | 649 | } |
| 619 | 650 | |
| 620 | 651 | foreach ( $source_paths as $source_path ) { |
| 621 | 652 | if ( ! empty( $paths[ $source_path ] ) ) { |
| @@ -621,9 +652,12 @@ | ||
| 621 | 652 | if ( ! empty( $paths[ $source_path ] ) ) { |
| 622 | 653 | $matched_full_url = $paths[ $source_path ]; |
| 623 | 654 | |
| 624 | 655 | if ( ! empty( $full_urls[ $matched_full_url ] ) ) { |
| 625 | - $item_source = $source_id; | |
| 656 | + $item_source = [ | |
| 657 | + 'id' => $source_id, | |
| 658 | + 'source_type' => $source_type | |
| 659 | + ]; | |
| 626 | 660 | |
| 627 | 661 | $this->query_cache[ $matched_full_url ] = $item_source; |
| 628 | 662 | |
| 629 | 663 | foreach ( $full_urls[ $matched_full_url ] as $url ) { |
| @@ -650,8 +684,29 @@ | ||
| 650 | 684 | } |
| 651 | 685 | |
| 652 | 686 | return $results; |
| 653 | 687 | } |
| 688 | + | |
| 689 | + /** | |
| 690 | + * Is the supplied item_source considered to be empty? | |
| 691 | + * | |
| 692 | + * @param array $item_source | |
| 693 | + * | |
| 694 | + * @return bool | |
| 695 | + */ | |
| 696 | + public function is_empty_item_source( $item_source ) { | |
| 697 | + if ( | |
| 698 | + empty( $item_source['source_type'] ) || | |
| 699 | + ! isset( $item_source['id'] ) || | |
| 700 | + ! is_numeric( $item_source['id'] ) || | |
| 701 | + $item_source['id'] < 0 | |
| 702 | + ) { | |
| 703 | + return true; | |
| 704 | + } | |
| 705 | + | |
| 706 | + return false; | |
| 707 | + } | |
| 708 | + | |
| 654 | 709 | |
| 655 | 710 | |
| 656 | 711 | /** |
| 657 | 712 | * Define the method for the backword compatibility filter |