Make WordPress Core


Ignore:
Timestamp:
09/18/2017 11:03:06 PM (9 years ago)
Author:
westonruter
Message:

Customize: Add wp_is_uuid() validation function with optional second $version=4 parameter to enforce v4 random UUIDs.

Props jonathanbardo.
Fixes #39778.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r41380 r41388  
    57065706
    57075707/**
     5708 * Validates that a UUID is valid.
     5709 *
     5710 * @since 4.9.0
     5711 *
     5712 * @param mixed $uuid    UUID to check.
     5713 * @param int   $version Specify which version of UUID to check against. Default is none, to accept any UUID version. Otherwise, only version allowed is `4`.
     5714 * @return bool The string is a valid UUID or false on failure.
     5715 */
     5716function wp_is_uuid( $uuid, $version = null ) {
     5717
     5718    if ( ! is_string( $uuid ) ) {
     5719        return false;
     5720    }
     5721
     5722    if ( is_numeric( $version ) ) {
     5723        if ( 4 !== (int) $version ) {
     5724            _doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ), '4.9.0' );
     5725            return false;
     5726        }
     5727        $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
     5728    } else {
     5729        $regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[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 *
Note: See TracChangeset for help on using the changeset viewer.