diff --git a/src/wp-includes/class-wp-list-util.php b/src/wp-includes/class-wp-list-util.php index 3648cd89f5..c394f2c613 100644 --- a/src/wp-includes/class-wp-list-util.php +++ b/src/wp-includes/class-wp-list-util.php @@ -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' + ); } } diff --git a/tests/phpunit/tests/functions/wpListUtil.php b/tests/phpunit/tests/functions/wpListUtil.php index f455c94936..f40771681e 100644 --- a/tests/phpunit/tests/functions/wpListUtil.php +++ b/tests/phpunit/tests/functions/wpListUtil.php @@ -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 *