Make WordPress Core

Ticket #39778: generic.39778.2.diff

File generic.39778.2.diff, 3.4 KB (added by jonathanbardo, 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 1b2d29dc3b..b06ea4242e 100644
    final class WP_Customize_Manager { 
    480480                        return;
    481481                }
    482482
    483                 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 ) ) {
     483                if ( ! wp_is_valid_uuid( $this->_changeset_uuid ) ) {
    484484                        $this->wp_die( -1, __( 'Invalid changeset UUID' ) );
    485485                }
    486486
  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 3923ca92ec..25a4c61e97 100644
    function wp_generate_uuid4() { 
    56105610}
    56115611
    56125612/**
     5613 * Validate if a string is a valid UUID.
     5614 *
     5615 * @param string $uuid
     5616 *
     5617 * @param int $version Specify which version of UUID we should check against.
     5618 *
     5619 * @return bool The string is a valid UUID V4 or false on failure.
     5620 */
     5621function wp_is_valid_uuid( $uuid, $version = null ) {
     5622
     5623        if ( ! is_string( $uuid ) ) {
     5624                return false;
     5625        }
     5626
     5627        $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/';
     5628
     5629        if ( isset( $version ) ) {
     5630                if ( 4 !== $version ) {
     5631                        _doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ) );
     5632                        return false;
     5633                }
     5634                $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
     5635        }
     5636
     5637        return (bool) preg_match( $regex, $uuid );
     5638}
     5639
     5640/**
    56135641 * Get last changed date for the specified cache group.
    56145642 *
    56155643 * @since 4.7.0
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 8932bc63f7..ebab1064a6 100644
    class Tests_Functions extends WP_UnitTestCase { 
    891891                $uuids = array();
    892892                for ( $i = 0; $i < 20; $i += 1 ) {
    893893                        $uuid = wp_generate_uuid4();
    894                         $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 );
     894                        $this->assertTrue( wp_is_valid_uuid( $uuid, 4 ) );
    895895                        $uuids[] = $uuid;
    896896                }
    897897
    898898                $unique_uuids = array_unique( $uuids );
    899899                $this->assertEquals( $uuids, $unique_uuids );
    900900        }
     901
     902        /**
     903         * Tests wp_is_valid_uuid().
     904         *
     905         * @covers ::wp_is_valid_uuid
     906         * @ticket 39778
     907         */
     908        function test_wp_is_valid_uuid() {
     909                $uuids = array(
     910                        '27fe2421-780c-44c5-b39b-fff753092b55',
     911                        'b7c7713a-4ee9-45a1-87ed-944a90390fc7',
     912                        'fbedbe35-7bf5-49cc-a5ac-0343bd94360a',
     913                        '4c58e67e-123b-4290-a41c-5eeb6970fa3e',
     914                        'f54f5b78-e414-4637-84a9-a6cdc94a1beb',
     915                        'd1c533ac-abcf-44b6-9b0e-6477d2c91b09',
     916                        '7fcd683f-e5fd-454a-a8b9-ed15068830da',
     917                        '7962c750-e58c-470a-af0d-ec1eae453ff2',
     918                        'a59878ce-9a67-4493-8ca0-a756b52804b3',
     919                        '6faa519d-1e13-4415-bd6f-905ae3689d1d',
     920                );
     921
     922                foreach ( $uuids as $uuid ) {
     923                        $this->assertTrue( wp_is_valid_uuid( $uuid, 4 ) );
     924                }
     925
     926                $uuids = array(
     927                        'a19d5192-ea41-11e6-b006-92361f002671',
     928                        'a19d5192-ea41-21e6-b006-92361f002671',
     929                        'a19d5192-ea41-31e6-b006-92361f002671',
     930                );
     931
     932                foreach ( $uuids as $uuid ) {
     933                        $this->assertTrue( wp_is_valid_uuid( $uuid ) );
     934                }
     935
     936                $invalid_uuids = array(
     937                        'a19d5192-ea41-41e6-b006',
     938                        'this-is-not-valid',
     939                        1234,
     940                        true,
     941                        [],
     942                );
     943
     944                foreach ( $invalid_uuids as $invalid_uuid ) {
     945                        $this->assertFalse( wp_is_valid_uuid( $invalid_uuid, 4 ) );
     946                        $this->assertFalse( wp_is_valid_uuid( $invalid_uuid ) );
     947                }
     948        }
    901949}