From 7d15225ad0f722e1ce42f4030a48ecd41636eea4 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 23 Sep 2020 00:25:47 +0000 Subject: [PATCH] Users: Check if the user ID passed as `selected` to `wp_dropdown_users()` corresponds to an existing user. This avoids a few PHP notices if the `include_selected` parameter was specified and a non-existing user ID was passed. Props campusboy1987. Fixes #51370. git-svn-id: https://develop.svn.wordpress.org/trunk@49036 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 6 +++++- tests/phpunit/tests/user/wpDropdownUsers.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index 6a2bf20ca9..284b3d71b2 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -1191,6 +1191,7 @@ function wp_dropdown_users( $args = '' ) { if ( $parsed_args['include_selected'] && ( $parsed_args['selected'] > 0 ) ) { $found_selected = false; $parsed_args['selected'] = (int) $parsed_args['selected']; + foreach ( (array) $users as $user ) { $user->ID = (int) $user->ID; if ( $user->ID === $parsed_args['selected'] ) { @@ -1199,7 +1200,10 @@ function wp_dropdown_users( $args = '' ) { } if ( ! $found_selected ) { - $users[] = get_userdata( $parsed_args['selected'] ); + $selected_user = get_userdata( $parsed_args['selected'] ); + if ( $selected_user ) { + $users[] = $selected_user; + } } } diff --git a/tests/phpunit/tests/user/wpDropdownUsers.php b/tests/phpunit/tests/user/wpDropdownUsers.php index 4f6b89db77..91c35cf63a 100644 --- a/tests/phpunit/tests/user/wpDropdownUsers.php +++ b/tests/phpunit/tests/user/wpDropdownUsers.php @@ -129,6 +129,22 @@ class Tests_User_WpDropdownUsers extends WP_UnitTestCase { $this->assertContains( $user1->user_login, $found ); } + /** + * @ticket 51370 + */ + public function test_include_selected_with_non_existing_user_id() { + $found = wp_dropdown_users( + array( + 'echo' => false, + 'selected' => PHP_INT_MAX, + 'include_selected' => true, + 'show' => 'user_login', + ) + ); + + $this->assertNotContains( (string) PHP_INT_MAX, $found ); + } + /** * @ticket 38135 */