Ticket #39778: 39778.4.diff
File 39778.4.diff, 3.6 KB (added by , 8 years ago) |
---|
-
src/wp-includes/class-wp-customize-manager.php
diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php index cead2d0ce9..7bd1489543 100644
final class WP_Customize_Manager { 488 488 return; 489 489 } 490 490 491 if ( ! preg_match( '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/',$this->_changeset_uuid ) ) {491 if ( ! wp_is_uuid( $this->_changeset_uuid ) ) { 492 492 $this->wp_die( -1, __( 'Invalid changeset UUID' ) ); 493 493 } 494 494 -
src/wp-includes/functions.php
diff --git src/wp-includes/functions.php src/wp-includes/functions.php index bab1f7463b..5ee547dad6 100644
function wp_generate_uuid4() { 5705 5705 } 5706 5706 5707 5707 /** 5708 * Validate if a string is a valid UUID. 5709 * 5710 * @since 4.9.0 5711 * 5712 * @param string $uuid UUID to check. 5713 * @param int $version Specify which version of UUID we should check against. 5714 * @return bool The string is a valid UUID or false on failure. 5715 */ 5716 function wp_is_uuid( $uuid, $version = null ) { 5717 5718 if ( ! is_string( $uuid ) ) { 5719 return false; 5720 } 5721 5722 $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/'; 5723 5724 if ( isset( $version ) ) { 5725 if ( 4 !== $version ) { 5726 _doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ), '4.9.0' ); 5727 return false; 5728 } 5729 $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/'; 5730 } 5731 5732 return (bool) preg_match( $regex, $uuid ); 5733 } 5734 5735 /** 5708 5736 * Get last changed date for the specified cache group. 5709 5737 * 5710 5738 * @since 4.7.0 -
tests/phpunit/tests/functions.php
diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php index 252d890bbb..3fa9fbcc74 100644
class Tests_Functions extends WP_UnitTestCase { 901 901 $uuids = array(); 902 902 for ( $i = 0; $i < 20; $i += 1 ) { 903 903 $uuid = wp_generate_uuid4(); 904 $this->assert RegExp( '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/', $uuid);904 $this->assertTrue( wp_is_uuid( $uuid, 4 ) ); 905 905 $uuids[] = $uuid; 906 906 } 907 907 … … class Tests_Functions extends WP_UnitTestCase { 910 910 } 911 911 912 912 /** 913 * Tests wp_is_uuid(). 914 * 915 * @covers ::wp_is_uuid 916 * @ticket 39778 917 */ 918 function test_wp_is_valid_uuid() { 919 $uuids_v4 = array( 920 '27fe2421-780c-44c5-b39b-fff753092b55', 921 'b7c7713a-4ee9-45a1-87ed-944a90390fc7', 922 'fbedbe35-7bf5-49cc-a5ac-0343bd94360a', 923 '4c58e67e-123b-4290-a41c-5eeb6970fa3e', 924 'f54f5b78-e414-4637-84a9-a6cdc94a1beb', 925 'd1c533ac-abcf-44b6-9b0e-6477d2c91b09', 926 '7fcd683f-e5fd-454a-a8b9-ed15068830da', 927 '7962c750-e58c-470a-af0d-ec1eae453ff2', 928 'a59878ce-9a67-4493-8ca0-a756b52804b3', 929 '6faa519d-1e13-4415-bd6f-905ae3689d1d', 930 ); 931 932 foreach ( $uuids_v4 as $uuid ) { 933 $this->assertTrue( wp_is_uuid( $uuid, 4 ) ); 934 } 935 936 $uuids = array( 937 '00000000-0000-0000-0000-000000000000', // Nil. 938 '9e3a0460-d72d-11e4-a631-c8e0eb141dab', // Version 1. 939 '2c1d43b8-e6d7-376e-af7f-d4bde997cc3f', // Version 3. 940 '39888f87-fb62-5988-a425-b2ea63f5b81e', // Version 5. 941 ); 942 943 foreach ( $uuids as $uuid ) { 944 $this->assertTrue( wp_is_uuid( $uuid ) ); 945 $this->assertFalse( wp_is_uuid( $uuid, 4 ) ); 946 } 947 948 $invalid_uuids = array( 949 'a19d5192-ea41-41e6-b006', 950 'this-is-not-valid', 951 1234, 952 true, 953 array(), 954 ); 955 956 foreach ( $invalid_uuids as $invalid_uuid ) { 957 $this->assertFalse( wp_is_uuid( $invalid_uuid, 4 ) ); 958 $this->assertFalse( wp_is_uuid( $invalid_uuid ) ); 959 } 960 } 961 962 /** 913 963 * @ticket 40017 914 964 * @dataProvider _wp_get_image_mime 915 965 */