Make WordPress Core

Ticket #39778: 39778.3.diff

File 39778.3.diff, 3.5 KB (added by westonruter, 8 years ago)

Refreshed patch

  • 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..e30432f2b6 100644
    final class WP_Customize_Manager { 
    488488                        return;
    489489                }
    490490
    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_valid_uuid( $this->_changeset_uuid ) ) {
    492492                        $this->wp_die( -1, __( 'Invalid changeset UUID' ) );
    493493                }
    494494
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index bab1f7463b..9ea798342c 100644
    function wp_generate_uuid4() { 
    57055705}
    57065706
    57075707/**
     5708 * Validate if a string is a valid UUID.
     5709 *
     5710 * @param string $uuid
     5711 *
     5712 * @param int $version Specify which version of UUID we should check against.
     5713 *
     5714 * @return bool The string is a valid UUID V4 or false on failure.
     5715 */
     5716function wp_is_valid_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.' ) );
     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/**
    57085736 * Get last changed date for the specified cache group.
    57095737 *
    57105738 * @since 4.7.0
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 252d890bbb..0a90d3de4e 100644
    class Tests_Functions extends WP_UnitTestCase { 
    901901                $uuids = array();
    902902                for ( $i = 0; $i < 20; $i += 1 ) {
    903903                        $uuid = wp_generate_uuid4();
    904                         $this->assertRegExp( '/^[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_valid_uuid( $uuid, 4 ) );
    905905                        $uuids[] = $uuid;
    906906                }
    907907
    class Tests_Functions extends WP_UnitTestCase { 
    910910        }
    911911
    912912        /**
     913         * Tests wp_is_valid_uuid().
     914         *
     915         * @covers ::wp_is_valid_uuid
     916         * @ticket 39778
     917         */
     918        function test_wp_is_valid_uuid() {
     919                $uuids = 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 as $uuid ) {
     933                        $this->assertTrue( wp_is_valid_uuid( $uuid, 4 ) );
     934                }
     935
     936                $uuids = array(
     937                        'a19d5192-ea41-11e6-b006-92361f002671',
     938                        'a19d5192-ea41-21e6-b006-92361f002671',
     939                        'a19d5192-ea41-31e6-b006-92361f002671',
     940                );
     941
     942                foreach ( $uuids as $uuid ) {
     943                        $this->assertTrue( wp_is_valid_uuid( $uuid ) );
     944                }
     945
     946                $invalid_uuids = array(
     947                        'a19d5192-ea41-41e6-b006',
     948                        'this-is-not-valid',
     949                        1234,
     950                        true,
     951                        [],
     952                );
     953
     954                foreach ( $invalid_uuids as $invalid_uuid ) {
     955                        $this->assertFalse( wp_is_valid_uuid( $invalid_uuid, 4 ) );
     956                        $this->assertFalse( wp_is_valid_uuid( $invalid_uuid ) );
     957                }
     958        }
     959
     960        /**
    913961         * @ticket 40017
    914962         * @dataProvider _wp_get_image_mime
    915963         */