Make WordPress Core

Changeset 56137


Ignore:
Timestamp:
07/05/2023 10:44:20 AM (3 years ago)
Author:
SergeyBiryukov
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-list-util.php

    r55908 r56137  
    117117                                if ( is_array( $obj ) ) {
    118118                                        // Treat object as an array.
    119                                         if ( array_key_exists( $m_key, $obj ) && ( $m_value === $obj[ $m_key ] ) ) {
     119                                        if ( array_key_exists( $m_key, $obj )
     120                                                && ( (string) $m_value === (string) $obj[ $m_key ] )
     121                                        ) {
    120122                                                $matched++;
    121123                                        }
    122124                                } elseif ( is_object( $obj ) ) {
    123125                                        // Treat object as an object.
    124                                         if ( isset( $obj->{$m_key} ) && ( $m_value === $obj->{$m_key} ) ) {
     126                                        if ( isset( $obj->{$m_key} )
     127                                                && ( (string) $m_value === (string) $obj->{$m_key} )
     128                                        ) {
    125129                                                $matched++;
    126130                                        }
    … …  
    277281                        }
    278282
    279                         if ( $a[ $field ] === $b[ $field ] ) {
     283                        if ( (string) $a[ $field ] === (string) $b[ $field ] ) {
    280284                                continue;
    281285                        }
  • trunk/tests/phpunit/tests/functions/wpListFilter.php

    r55562 r56137  
    211211                                ),
    212212                        ),
     213                        'string to int comparison' => array(
     214                                array(
     215                                        (object) array(
     216                                                'foo' => '1',
     217                                        ),
     218                                ),
     219                                array( 'foo' => 1 ),
     220                                'AND',
     221                                array(
     222                                        0 => (object) array(
     223                                                'foo' => '1',
     224                                        ),
     225                                ),
     226                        ),
    213227                );
    214228        }
Note: See TracChangeset for help on using the changeset viewer.