Make WordPress Core

Changeset 60131


Ignore:
Timestamp:
04/05/2025 09:04:52 PM (3 weeks ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Move wp_unique_id_from_values() next to wp_unique_id() and wp_unique_prefixed_id(), for consistency.

Follow-up to [60038].

See #63168.

File:
1 edited

Legend:

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

    r60129 r60131  
    80318031
    80328032/**
     8033 * Generates a unique ID based on the structure and values of a given array.
     8034 *
     8035 * This function serializes the array into a JSON string and generates a hash
     8036 * that serves as a unique identifier. Optionally, a prefix can be added to
     8037 * the generated ID for context or categorization.
     8038 *
     8039 * @since 6.8.0
     8040 *
     8041 * @param array  $data   The input array to generate an ID from.
     8042 * @param string $prefix Optional. A prefix to prepend to the generated ID. Default empty string.
     8043 * @return string The generated unique ID for the array.
     8044 */
     8045function wp_unique_id_from_values( array $data, string $prefix = '' ): string {
     8046    if ( empty( $data ) ) {
     8047        _doing_it_wrong(
     8048            __FUNCTION__,
     8049            sprintf(
     8050                /* translators: %s: The parameter name. */
     8051                __( 'The %s parameter must not be empty.' ),
     8052                '$data'
     8053            ),
     8054            '6.8.0'
     8055        );
     8056    }
     8057
     8058    $serialized = wp_json_encode( $data );
     8059    $hash       = substr( md5( $serialized ), 0, 8 );
     8060
     8061    return $prefix . $hash;
     8062}
     8063
     8064/**
    80338065 * Gets last changed date for the specified cache group.
    80348066 *
     
    91749206    return hash_equals( $hash, wp_fast_hash( $message ) );
    91759207}
    9176 
    9177 /**
    9178  * Generates a unique ID based on the structure and values of a given array.
    9179  *
    9180  * This function serializes the array into a JSON string and generates a hash
    9181  * that serves as a unique identifier. Optionally, a prefix can be added to
    9182  * the generated ID for context or categorization.
    9183  *
    9184  * @since 6.8.0
    9185  *
    9186  * @param array  $data   The input array to generate an ID from.
    9187  * @param string $prefix Optional. A prefix to prepend to the generated ID. Default ''.
    9188  *
    9189  * @return string The generated unique ID for the array.
    9190  */
    9191 function wp_unique_id_from_values( array $data, string $prefix = '' ): string {
    9192     if ( empty( $data ) ) {
    9193         _doing_it_wrong(
    9194             __FUNCTION__,
    9195             sprintf(
    9196                 /* translators: %s: parameter name. */
    9197                 __( 'The %s argument must not be empty.' ),
    9198                 '$data'
    9199             ),
    9200             '6.8.0'
    9201         );
    9202     }
    9203     $serialized = wp_json_encode( $data );
    9204     $hash       = substr( md5( $serialized ), 0, 8 );
    9205     return $prefix . $hash;
    9206 }
Note: See TracChangeset for help on using the changeset viewer.