From 974517d2bc0e138789187e3fd9ffbfcd1d0acc14 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 14 Sep 2015 18:57:15 +0000 Subject: [PATCH] Accept 'ID' as a valid `$field` in `get_user_by()`. We already accept 'id'. `ID` more closely matches the database and `WP_User` schemas. Props Shelob9. Fixes #33869. git-svn-id: https://develop.svn.wordpress.org/trunk@34125 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-user.php | 8 +++++++- src/wp-includes/pluggable.php | 3 ++- tests/phpunit/tests/user.php | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/class-wp-user.php b/src/wp-includes/class-wp-user.php index 971021a759..b7887c4dc2 100644 --- a/src/wp-includes/class-wp-user.php +++ b/src/wp-includes/class-wp-user.php @@ -165,18 +165,24 @@ class WP_User { * Return only the main user fields * * @since 3.3.0 + * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. * * @static * * @global wpdb $wpdb * - * @param string $field The field to query against: 'id', 'slug', 'email' or 'login' + * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. * @param string|int $value The field value * @return object|false Raw user object */ public static function get_data_by( $field, $value ) { global $wpdb; + // 'ID' is an alias of 'id'. + if ( 'ID' === $field ) { + $field = 'id'; + } + if ( 'id' == $field ) { // Make sure the value is numeric to avoid casting objects, for example, // to int 1. diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index b53a233ea8..3317284381 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -145,8 +145,9 @@ if ( !function_exists('get_user_by') ) : * Retrieve user info by a given field * * @since 2.8.0 + * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. * - * @param string $field The field to retrieve the user with. id | slug | email | login + * @param string $field The field to retrieve the user with. id | ID | slug | email | login. * @param int|string $value A value for $field. A user ID, slug, email address, or login name. * @return WP_User|false WP_User object on success, false on failure. */ diff --git a/tests/phpunit/tests/user.php b/tests/phpunit/tests/user.php index 1e0ed7325b..4dc281cb44 100644 --- a/tests/phpunit/tests/user.php +++ b/tests/phpunit/tests/user.php @@ -445,6 +445,16 @@ class Tests_User extends WP_UnitTestCase { $this->assertEquals( false, $user ); } + /** + * @ticket 33869 + */ + public function test_user_get_data_by_ID_should_alias_to_id() { + $u = $this->factory->user->create(); + + $user = WP_User::get_data_by( 'ID', $u ); + $this->assertEquals( $u, $user->ID ); + } + /** * @ticket 21431 */