| 2650 | * Helper function for wp_unique_filename to check potential alternate output formats for images. |
| 2651 | * |
| 2652 | * @since 5.8.1 |
| 2653 | * |
| 2654 | * @param string $filename |
| 2655 | * @param string $ext |
| 2656 | * @param string $dir |
| 2657 | * |
| 2658 | * @return string |
| 2659 | */ |
| 2660 | function _wp_check_alternate_output_format_uniqueness( $filename, $ext, $dir ) { |
| 2661 | static $checking_alternates; |
| 2662 | |
| 2663 | if ( empty( $checking_alternates ) ) { |
| 2664 | $checking_alternates = true; |
| 2665 | $file_type = wp_check_filetype_and_ext( trailingslashit( $dir ) . $filename, $filename ); |
| 2666 | $mime_type = $file_type['type']; |
| 2667 | |
| 2668 | if ( ! empty( $mime_type ) && 0 === strpos( $mime_type, 'image/' ) ) { |
| 2669 | $output_formats = apply_filters( 'image_editor_output_format', array(), trailingslashit( $dir ) . $filename, $mime_type ); |
| 2670 | |
| 2671 | if ( |
| 2672 | ! empty( $output_formats ) && |
| 2673 | is_array( $output_formats ) && |
| 2674 | ( ! empty( $output_formats[ $mime_type ] ) || in_array( $mime_type, $output_formats ) ) |
| 2675 | ) { |
| 2676 | $alt_mime_types = array(); |
| 2677 | |
| 2678 | foreach ( $output_formats as $source => $target ) { |
| 2679 | if ( $source === $mime_type && $target === $mime_type ) { |
| 2680 | continue; |
| 2681 | } elseif ( $source === $mime_type ) { |
| 2682 | $alt_mime_types[] = $target; |
| 2683 | } elseif ( $target === $mime_type ) { |
| 2684 | $alt_mime_types[] = $source; |
| 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | $alt_mime_types = array_unique( $alt_mime_types ); |
| 2689 | |
| 2690 | foreach ( $alt_mime_types as $alt_mime_type ) { |
| 2691 | $alt_ext = wp_default_extension_for_mine_type( $alt_mime_type ); |
| 2692 | |
| 2693 | if ( ! empty( $alt_ext ) && ".{$alt_ext}" !== $ext ) { |
| 2694 | $alt_filename = wp_basename( $filename, $ext ) . ".{$alt_ext}"; |
| 2695 | $alt_filename2 = wp_unique_filename( $dir, $alt_filename ); |
| 2696 | |
| 2697 | // If a potential clash was found for alternate format, use its unique filename. |
| 2698 | if ( $alt_filename2 !== $alt_filename ) { |
| 2699 | $filename = wp_basename( $alt_filename2, ".{$alt_ext}" ) . $ext; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | } |
| 2704 | } |
| 2705 | $checking_alternates = false; |
| 2706 | } |
| 2707 | |
| 2708 | return $filename; |
| 2709 | } |
| 2710 | |
| 2711 | /** |