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.
Built from https://develop.svn.wordpress.org/trunk@55543


git-svn-id: http://core.svn.wordpress.org/trunk@55055 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
spacedmonkey 2023-03-14 15:53:18 +00:00
parent 48deabccbe
commit 8a8216c1c7
2 changed files with 32 additions and 1 deletions

View File

@ -7014,6 +7014,13 @@ function wp_debug_backtrace_summary( $ignore_class = null, $skip_frames = 0, $pr
* @return int[] Array of IDs not present in the cache.
*/
function _get_non_cached_ids( $object_ids, $cache_key ) {
$object_ids = array_filter( $object_ids, '_validate_cache_id' );
$object_ids = array_unique( array_map( 'intval', $object_ids ), SORT_NUMERIC );
if ( empty( $object_ids ) ) {
return array();
}
$non_cached_ids = array();
$cache_values = wp_cache_get_multiple( $object_ids, $cache_key );
@ -7026,6 +7033,30 @@ function _get_non_cached_ids( $object_ids, $cache_key ) {
return $non_cached_ids;
}
/**
* Checks whether the given cache ID is either an integer or iterger-like strings.
* Both `16` and `"16"` are considered valid, other numeric types and numeric
* strings (`16.3` and `"16.3"`) are considered invalid.
*
* @since 6.3.0
*
* @param mixed $object_id The cache id to validate.
* @return bool Whether the given $object_id is a valid cache id.
*/
function _validate_cache_id( $object_id ) {
// Unfortunately filter_var() is considered an optional extension
if ( is_int( $object_id )
|| ( is_string( $object_id ) && (string) (int) $object_id === $object_id ) ) {
return true;
}
/* translators: %s: The type of the given object id. */
$message = sprintf( __( 'Object id must be integer, %s given.' ), gettype( $object_id ) );
_doing_it_wrong( '_get_non_cached_ids', $message, '6.3.0' );
return false;
}
/**
* Tests if the current device has the capability to upload files.
*

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.3-alpha-55542';
$wp_version = '6.3-alpha-55543';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.