diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php
index d8d5d5b34b..ec24d77c00 100644
|
|
function wp_generate_attachment_metadata( $attachment_id, $file ) { |
647 | 647 | * @since 2.5.0 |
648 | 648 | * |
649 | 649 | * @param string $str |
650 | | * @return int|float |
| 650 | * @return int|float Returns 0 on failure. |
651 | 651 | */ |
652 | 652 | function wp_exif_frac2dec( $str ) { |
653 | | if ( false === strpos( $str, '/' ) ) { |
654 | | return $str; |
| 653 | // Fractions must contain a single `/`. |
| 654 | if ( false === strpos( $str, '/' ) || substr_count( $str, '/' ) > 1 ) { |
| 655 | return 0; |
655 | 656 | } |
656 | 657 | |
657 | 658 | list( $numerator, $denominator ) = explode( '/', $str ); |
658 | | if ( ! empty( $denominator ) ) { |
659 | | return $numerator / $denominator; |
| 659 | |
| 660 | // Both the numerator and the denominator must be numbers. |
| 661 | if ( ! is_numeric( $numerator ) || ! is_numeric( $denominator ) ) { |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | // The denominator must not be empty (or 0). |
| 666 | if ( empty( $denominator ) ) { |
| 667 | return 0; |
660 | 668 | } |
661 | | return $str; |
| 669 | |
| 670 | return $numerator / $denominator; |
662 | 671 | } |
663 | 672 | |
664 | 673 | /** |
diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php
index 488731a0e8..5ce882d9c9 100644
|
|
class Tests_Image_Functions extends WP_UnitTestCase { |
700 | 700 | unlink( $temp_dir . $size['file'] ); |
701 | 701 | } |
702 | 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Test for wp_exif_frac2dec verified that it properly handles edge cases |
| 706 | * and always returns an int or float, 0 for failures. |
| 707 | * |
| 708 | * @ticket 54385 |
| 709 | * @dataProvider data_test_wp_exif_frac2dec |
| 710 | */ |
| 711 | public function test_wp_exif_frac2dec( $fraction, $expect ) { |
| 712 | $this->assertSame( $expect, wp_exif_frac2dec( $fraction ) ); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | /** |
| 717 | * Data provider for testing `wp_exif_frac2dec()`. |
| 718 | * |
| 719 | * @return array { |
| 720 | * Arguments for testing `wp_exif_frac2dec()`. |
| 721 | * |
| 722 | * @type string|null $fraction The fraction string to run. |
| 723 | * @type int|float $expected The resulting expected value. |
| 724 | * |
| 725 | */ |
| 726 | public function data_test_wp_exif_frac2dec() { |
| 727 | global $wpdb; |
| 728 | |
| 729 | return array( |
| 730 | array( |
| 731 | '0/0', |
| 732 | 0, |
| 733 | ), |
| 734 | array( |
| 735 | '0/abc', |
| 736 | 0, |
| 737 | ), |
| 738 | array( |
| 739 | '0.0', |
| 740 | 0, |
| 741 | ), |
| 742 | array( |
| 743 | '010', |
| 744 | 0, |
| 745 | ), |
| 746 | array( |
| 747 | 10.123, |
| 748 | 0, |
| 749 | ), |
| 750 | array( |
| 751 | '50/100', |
| 752 | 0.5, |
| 753 | ), |
| 754 | array( |
| 755 | '25/100', |
| 756 | .25, |
| 757 | ), |
| 758 | array( |
| 759 | '0', |
| 760 | 0, |
| 761 | ), |
| 762 | array( |
| 763 | '100/0', |
| 764 | 0, |
| 765 | ), |
| 766 | array( |
| 767 | 'path/to/file', |
| 768 | 0, |
| 769 | ), |
| 770 | array( |
| 771 | '123notafraction', |
| 772 | 0, |
| 773 | ), |
| 774 | array( |
| 775 | '/', |
| 776 | 0, |
| 777 | ), |
| 778 | array( |
| 779 | '1/2/3', |
| 780 | 0, |
| 781 | ), |
| 782 | array( |
| 783 | '///', |
| 784 | 0, |
| 785 | ), |
| 786 | array( |
| 787 | '4/2', |
| 788 | 2, |
| 789 | ), |
| 790 | ); |
| 791 | } |
| 792 | |
703 | 793 | } |