General: Introduce wp_unique_id(), a PHP implementation of Underscore's uniqueId method.

A static variable contains an integer that is incremented with each call. This number is returned with the optional prefix.
As such the returned value is not universally unique, but it is unique across the life of the PHP process.

Props westonruter, dlh.
See #44883.

git-svn-id: https://develop.svn.wordpress.org/trunk@43658 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2018-09-24 20:56:30 +00:00
parent 75a5293460
commit e1ff4cd7cd
2 changed files with 48 additions and 0 deletions

View File

@@ -6167,6 +6167,26 @@ function wp_is_uuid( $uuid, $version = null ) {
return (bool) preg_match( $regex, $uuid );
}
/**
* Get unique ID.
*
* This is a PHP implementation of Underscore's uniqueId method. A static variable
* contains an integer that is incremented with each call. This number is returned
* with the optional prefix. As such the returned value is not universally unique,
* but it is unique across the life of the PHP process.
*
* @since 4.9.9
*
* @staticvar int $id_counter
*
* @param string $prefix Prefix for the returned ID.
* @return string Unique ID.
*/
function wp_unique_id( $prefix = '' ) {
static $id_counter = 0;
return $prefix . (string) ++$id_counter;
}
/**
* Get last changed date for the specified cache group.
*