Make WordPress Core

Changeset 55543


Ignore:
Timestamp:
03/14/2023 03:51:28 PM (14 months ago)
Author:
spacedmonkey
Message:

Cache API: Add a warning when calling _get_non_cached_ids with invalid ids.

Sanitize the array of ids passed to the _get_non_cached_ids function and add a _doing_it_wrong call, if an invalid type is passed.

Props tillkruess, spacedmonkey, peterwilsoncc, flixos90, SergeyBiryukov.
Fixes #57593.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r55540 r55543  
    70157015 */
    70167016function _get_non_cached_ids( $object_ids, $cache_key ) {
     7017    $object_ids = array_filter( $object_ids, '_validate_cache_id' );
     7018    $object_ids = array_unique( array_map( 'intval', $object_ids ), SORT_NUMERIC );
     7019
     7020    if ( empty( $object_ids ) ) {
     7021        return array();
     7022    }
     7023
    70177024    $non_cached_ids = array();
    70187025    $cache_values   = wp_cache_get_multiple( $object_ids, $cache_key );
     
    70257032
    70267033    return $non_cached_ids;
     7034}
     7035
     7036/**
     7037 * Checks whether the given cache ID is either an integer or iterger-like strings.
     7038 * Both `16` and `"16"` are considered valid, other numeric types and numeric
     7039 * strings (`16.3` and `"16.3"`) are considered invalid.
     7040 *
     7041 * @since 6.3.0
     7042 *
     7043 * @param mixed $object_id The cache id to validate.
     7044 * @return bool Whether the given $object_id is a valid cache id.
     7045 */
     7046function _validate_cache_id( $object_id ) {
     7047    // Unfortunately filter_var() is considered an optional extension
     7048    if ( is_int( $object_id )
     7049        || ( is_string( $object_id ) && (string) (int) $object_id === $object_id ) ) {
     7050        return true;
     7051    }
     7052
     7053    /* translators: %s: The type of the given object id. */
     7054    $message = sprintf( __( 'Object id must be integer, %s given.' ), gettype( $object_id ) );
     7055    _doing_it_wrong( '_get_non_cached_ids', $message, '6.3.0' );
     7056
     7057    return false;
    70277058}
    70287059
Note: See TracChangeset for help on using the changeset viewer.