| @@ -305,21 +305,20 @@ | ||
| 305 | 305 | $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false); |
| 306 | 306 | |
| 307 | 307 | if( $reverse_compatibility ) { |
| 308 | 308 | if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) !== $url ) { |
| 309 | + // Local URL, no replacement needed. | |
| 309 | 310 | return false; |
| 310 | 311 | } |
| 311 | 312 | |
| 312 | 313 | if ( str_replace( Filter::get_remote_domains(), '', $url ) === $url ) { |
| 314 | + // Not a known remote URL, no replacement needed. | |
| 313 | 315 | return false; |
| 314 | 316 | } |
| 315 | 317 | } else { |
| 316 | 318 | if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) { |
| 317 | 319 | // Remote URL, no replacement needed |
| 318 | - if ( str_replace( Filter::get_bare_upload_base_urls(), '', $url ) === $url ) { | |
| 319 | - // Remote URL, no replacement needed | |
| 320 | - return false; | |
| 321 | - } | |
| 320 | + return false; | |
| 322 | 321 | } |
| 323 | 322 | } |
| 324 | 323 | |
| 325 | 324 | // Server URL, perform replacement |
| @@ -358,16 +357,17 @@ | ||
| 358 | 357 | ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'full', $item_source['source_type'] )) |
| 359 | 358 | : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['source_path']); |
| 360 | 359 | |
| 361 | 360 | |
| 362 | - $extras = $wpmcsItem->get_extras( $item_source['id'], false, $item_source['source_type'] ); | |
| 363 | 361 | |
| 364 | - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) { | |
| 362 | + if(isset($item['original_source_path']) && !Utils::is_empty($item['original_source_path'])){ | |
| 365 | 363 | $bare_url[] = $backward_replacement |
| 366 | 364 | ? Utils::reduce_url($wpmcsItem->get_url( $item_source['id'], 'original', $item_source['source_type'] )) |
| 367 | - : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$extras['original']['source_path']); | |
| 365 | + : Utils::reduce_url(trailingslashit($upload_dir['baseurl']).$item['original_source_path']); | |
| 368 | 366 | } |
| 369 | 367 | |
| 368 | + $extras = $wpmcsItem->get_extras( $item_source['id'], false, $item_source['source_type'] ); | |
| 369 | + | |
| 370 | 370 | if(isset($extras['sizes']) && !Utils::is_empty($extras['sizes'])) { |
| 371 | 371 | foreach($extras['sizes'] as $size => $sub_image) { |
| 372 | 372 | if(!Utils::is_empty($sub_image)){ |
| 373 | 373 | $bare_url[] = $backward_replacement |
| @@ -405,8 +405,12 @@ | ||
| 405 | 405 | ) { |
| 406 | 406 | return false; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | + if ( ! apply_filters( 'wpmcs_push_to_url_pairs', true, $item_source['id'], $item_source['source_type'] ) ) { | |
| 410 | + return false; | |
| 411 | + } | |
| 412 | + | |
| 409 | 413 | $base = trailingslashit($upload_dir['baseurl']); |
| 410 | 414 | $backward_replacement = apply_filters('wpmcs_enable_backward_url_replacement', false); |
| 411 | 415 | |
| 412 | 416 | $source_server_file = Utils::remove_scheme($base.$item['source_path']); |
| @@ -416,12 +420,14 @@ | ||
| 416 | 420 | if($backward_replacement) { |
| 417 | 421 | $url_pairs[$source_cloud_file] = $source_server_file; // Backward compatibility |
| 418 | 422 | } |
| 419 | 423 | |
| 420 | - $extras = $wpmcsItem->get_extras($item_source['id'], false, $item_source['source_type']); | |
| 421 | 424 | |
| 422 | - if(isset($extras['original']) && !Utils::is_empty($extras['original'])) { | |
| 423 | - $original_server_file = Utils::remove_scheme($base.$extras['original']['source_path']); | |
| 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']); | |
| 424 | 430 | $original_cloud_file = Utils::remove_scheme($wpmcsItem->get_url($item_source['id'], 'original', $item_source['source_type'])); |
| 425 | 431 | $url_pairs[$original_server_file] = $original_cloud_file; |
| 426 | 432 | if($backward_replacement) { |
| 427 | 433 | $url_pairs[$original_cloud_file] = $original_server_file; // Backward compatibility |
| @@ -426,10 +432,15 @@ | ||
| 426 | 432 | if($backward_replacement) { |
| 427 | 433 | $url_pairs[$original_cloud_file] = $original_server_file; // Backward compatibility |
| 428 | 434 | } |
| 429 | 435 | } |
| 436 | + | |
| 437 | + $extras = $wpmcsItem->get_extras($item_source['id'], false, $item_source['source_type']); | |
| 438 | + | |
| 430 | 439 | if(isset($extras['sizes'])){ |
| 431 | 440 | foreach ($extras['sizes'] as $size => $sub_image) { |
| 441 | + if(Utils::is_empty($sub_image['source_path'])) continue; | |
| 442 | + | |
| 432 | 443 | $sub_server_file = Utils::remove_scheme($base.$sub_image['source_path']); |
| 433 | 444 | $sub_cloud_file = Utils::remove_scheme($wpmcsItem->get_url( $item_source['id'], $size, $item_source['source_type'] )); |
| 434 | 445 | $url_pairs[$sub_server_file] = $sub_cloud_file; |
| 435 | 446 | if($backward_replacement) { |
| @@ -566,9 +577,11 @@ | ||
| 566 | 577 | |
| 567 | 578 | if ( empty( $urls ) ) { |
| 568 | 579 | return $results; |
| 569 | 580 | } |
| 570 | - | |
| 581 | + // Check if backward compatibility is enabled | |
| 582 | + $reverse_compatibility = apply_filters('wpmcs_enable_backward_url_replacement', false); | |
| 583 | + | |
| 571 | 584 | if ( ! is_array( $urls ) ) { |
| 572 | 585 | $urls = array( $urls ); |
| 573 | 586 | } |
| 574 | 587 | |
| @@ -601,9 +614,9 @@ | ||
| 601 | 614 | continue; |
| 602 | 615 | } |
| 603 | 616 | |
| 604 | 617 | |
| 605 | - $path = Utils::get_attachment_source_path( $full_url ); | |
| 618 | + $path = Utils::get_attachment_source_path( $full_url, $reverse_compatibility ? 'key' : 'source' ); | |
| 606 | 619 | |
| 607 | 620 | $paths[ $path ] = $full_url; |
| 608 | 621 | $full_urls[ $full_url ][] = $url; |
| 609 | 622 | } |
| @@ -608,21 +621,32 @@ | ||
| 608 | 621 | $full_urls[ $full_url ][] = $url; |
| 609 | 622 | } |
| 610 | 623 | |
| 611 | 624 | if ( ! empty( $paths ) ) { |
| 612 | - $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 | + } | |
| 613 | 631 | |
| 614 | 632 | if ( ! empty( $wpmcsItems ) ) { |
| 615 | 633 | foreach ( $wpmcsItems as $item ) { |
| 616 | - // 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). | |
| 617 | 638 | // Because the base image file name of a thumbnail might match the primary rather scaled or rotated full image |
| 618 | 639 | // it's possible that both source paths are used by separate URLs. |
| 619 | 640 | $source_id = (int)$item['source_id']; |
| 620 | 641 | $source_type = $item['source_type']; |
| 621 | - $source_paths = [ $item['source_path'] ]; | |
| 622 | - $original = $wpmcsItem->get_extras($source_id, 'original', $source_type); | |
| 623 | - if(isset($original['source_path'])) { | |
| 624 | - $source_paths[] = $original['source_path']; | |
| 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 ]; | |
| 625 | 649 | } |
| 626 | 650 | |
| 627 | 651 | foreach ( $source_paths as $source_path ) { |
| 628 | 652 | if ( ! empty( $paths[ $source_path ] ) ) { |