mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
General: Add more error checking to WP_List_Util::pluck().
Values for the input array in `WP_List_Util::pluck()` or `wp_list_pluck()` must be either objects or arrays. This commit adds a check to ensure that the value retrieved in the loop is an array before treating it as such, and throws a `_doing_it_wrong()` notice if it is neither an object nor an array. Follow-up to [14108], [15686], [18602], [28900], [38928]. Props afragen, costdev, audrasjb. Fixes #56650. git-svn-id: https://develop.svn.wordpress.org/trunk@55423 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -166,8 +166,14 @@ class WP_List_Util {
|
||||
foreach ( $this->output as $key => $value ) {
|
||||
if ( is_object( $value ) ) {
|
||||
$newlist[ $key ] = $value->$field;
|
||||
} else {
|
||||
} elseif ( is_array( $value ) ) {
|
||||
$newlist[ $key ] = $value[ $field ];
|
||||
} else {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'Values for the input array must be either objects or arrays.' ),
|
||||
'6.2.0'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,12 +193,18 @@ class WP_List_Util {
|
||||
} else {
|
||||
$newlist[] = $value->$field;
|
||||
}
|
||||
} else {
|
||||
} elseif ( is_array( $value ) ) {
|
||||
if ( isset( $value[ $index_key ] ) ) {
|
||||
$newlist[ $value[ $index_key ] ] = $value[ $field ];
|
||||
} else {
|
||||
$newlist[] = $value[ $field ];
|
||||
}
|
||||
} else {
|
||||
_doing_it_wrong(
|
||||
__METHOD__,
|
||||
__( 'Values for the input array must be either objects or arrays.' ),
|
||||
'6.2.0'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ class Tests_Functions_wpListUtil extends WP_UnitTestCase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_wp_list_util_pluck_simple().
|
||||
* Data provider for test_wp_list_util_pluck().
|
||||
*
|
||||
* @return array[]
|
||||
*/
|
||||
@@ -108,6 +108,65 @@ class Tests_Functions_wpListUtil extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that wp_list_pluck() throws _doing_it_wrong() with invalid input.
|
||||
*
|
||||
* @ticket 56650
|
||||
*
|
||||
* @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input
|
||||
*
|
||||
* @covers WP_List_Util::pluck
|
||||
* @covers ::wp_list_pluck
|
||||
*
|
||||
* @expectedIncorrectUsage WP_List_Util::pluck
|
||||
*
|
||||
* @param array $input An invalid input array.
|
||||
*/
|
||||
public function test_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input( $input ) {
|
||||
$this->assertSame( array(), wp_list_pluck( $input, 'a_field' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that wp_list_pluck() throws _doing_it_wrong() with an index key and invalid input.
|
||||
*
|
||||
* @ticket 56650
|
||||
*
|
||||
* @dataProvider data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input
|
||||
*
|
||||
* @covers WP_List_Util::pluck
|
||||
* @covers ::wp_list_pluck
|
||||
*
|
||||
* @expectedIncorrectUsage WP_List_Util::pluck
|
||||
*
|
||||
* @param array $input An invalid input array.
|
||||
*/
|
||||
public function test_wp_list_pluck_should_throw_doing_it_wrong_with_index_key_and_invalid_input( $input ) {
|
||||
$this->assertSame( array(), wp_list_pluck( $input, 'a_field', 'an_index_key' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider that provides invalid input arrays.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data_wp_list_pluck_should_throw_doing_it_wrong_with_invalid_input() {
|
||||
return array(
|
||||
'int[] 0' => array( array( 0 ) ),
|
||||
'int[] 1' => array( array( 1 ) ),
|
||||
'int[] -1' => array( array( -1 ) ),
|
||||
'float[] 0.0' => array( array( 0.0 ) ),
|
||||
'float[] 1.0' => array( array( 1.0 ) ),
|
||||
'float[] -1.0' => array( array( -1.0 ) ),
|
||||
'string[] and empty string' => array( array( '' ) ),
|
||||
'string[] and "0"' => array( array( '0' ) ),
|
||||
'string[] and "1"' => array( array( '1' ) ),
|
||||
'string[] and "-1"' => array( array( '-1' ) ),
|
||||
'array and null' => array( array( null ) ),
|
||||
'array and false' => array( array( false ) ),
|
||||
'array and true' => array( array( true ) ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 55300
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user