From c11f70878f2e36169b2dab159fcb31c9e5365faf Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 5 Jul 2023 10:44:20 +0000 Subject: [PATCH] General: Compare values as strings in `WP_List_Util::filter()` and `::sort_callback()`. This aims to preserve backward compatibility for code relying on type juggling when using the `wp_list_filter()` function, e.g. comparing a numeric string to an integer. Follow-up to [55908]. Props azaozz, jeremyfelt, david.binda. See #57839. git-svn-id: https://develop.svn.wordpress.org/trunk@56137 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-list-util.php | 10 +++++++--- tests/phpunit/tests/functions/wpListFilter.php | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 8c1d897475..fbc171e0c6 100644 --- a/src/wp-includes/class-wp-list-util.php +++ b/src/wp-includes/class-wp-list-util.php @@ -116,12 +116,16 @@ class WP_List_Util { foreach ( $args as $m_key => $m_value ) { if ( is_array( $obj ) ) { // Treat object as an array. - if ( array_key_exists( $m_key, $obj ) && ( $m_value === $obj[ $m_key ] ) ) { + if ( array_key_exists( $m_key, $obj ) + && ( (string) $m_value === (string) $obj[ $m_key ] ) + ) { $matched++; } } elseif ( is_object( $obj ) ) { // Treat object as an object. - if ( isset( $obj->{$m_key} ) && ( $m_value === $obj->{$m_key} ) ) { + if ( isset( $obj->{$m_key} ) + && ( (string) $m_value === (string) $obj->{$m_key} ) + ) { $matched++; } } @@ -276,7 +280,7 @@ class WP_List_Util { continue; } - if ( $a[ $field ] === $b[ $field ] ) { + if ( (string) $a[ $field ] === (string) $b[ $field ] ) { continue; } diff --git a/tests/phpunit/tests/functions/wpListFilter.php b/tests/phpunit/tests/functions/wpListFilter.php index bc8acdc63a..042325227e 100644 --- a/tests/phpunit/tests/functions/wpListFilter.php +++ b/tests/phpunit/tests/functions/wpListFilter.php @@ -210,6 +210,20 @@ class Tests_Functions_wpListFilter extends WP_UnitTestCase { ), ), ), + 'string to int comparison' => array( + array( + (object) array( + 'foo' => '1', + ), + ), + array( 'foo' => 1 ), + 'AND', + array( + 0 => (object) array( + 'foo' => '1', + ), + ), + ), ); } }