Make WordPress Core

Opened 9 years ago

Closed 4 months ago

Last modified 4 months ago

#35124 closed enhancement (fixed)

Add get_user() function that maps to get_user_by()

Reported by: sc0ttkclark's profile sc0ttkclark Owned by: joedolson's profile joedolson
Milestone: 6.7 Priority: normal
Severity: normal Version:
Component: Users Keywords: good-first-bug has-patch commit
Focuses: Cc:

Description

<?php
/**
 * Retrieve user info by user ID.
 *
 * @since 4.5
 *
 * @param int $user_id User ID
 *
 * @return WP_User|false WP_User object on success, false on failure.
 */
function get_user( $user_id ) {
        return get_user_by( 'id', $user_id );
}

Works like get_userdata but meets the standards of get_{object_type} we've put forth in get_post( $id ) / get_term( $id ) / get_comment( $id ) to make it easier for new developers coming into WordPress.

I imagine at some point we could deprecate get_userdata calls, but not sure that should be done in the same release this is introduced.

Attachments (3)

35124.diff (39.1 KB) - added by kushang78 12 months ago.
Added patch for this ticket.
35124.13.02.2024.diff (718 bytes) - added by kushang78 11 months ago.
Added patch for this ticket.
35124.3.diff (568 bytes) - added by joedolson 4 months ago.
Shift location of function to user.php

Download all attachments as: .zip

Change History (22)

#1 @SergeyBiryukov
9 years ago

Previously suggested in comment:24:ticket:21120.

#2 @joedolson
15 months ago

  • Milestone set to 6.5
  • Owner set to joedolson
  • Status changed from new to accepted

This really makes sense; it may be a fairly minor change, but it enhances the understandability of the code base.

This ticket was mentioned in Slack in #core by rajinsharwar. View the logs.


12 months ago

#4 @rajinsharwar
12 months ago

  • Keywords good-first-bug added

@kushang78
12 months ago

Added patch for this ticket.

#5 @kushang78
12 months ago

  • Keywords has-patch added

I’ve added the latest patch.

Patch includes below changes:

  • Added get_user() - that maps to get_user_by() in the replace of get_userdata().
  • There are plenty of files that contains get_userdata() function. Successfully Replaced with New get_user() function.
  • The root file of this function - src/wp-includes/pluggable.php
Last edited 12 months ago by kushang78 (previous) (diff)

#6 @sc0ttkclark
12 months ago

Renaming the usage everywhere seems ok to me but not required IMO.

The renaming the function itself should be changed, keep the old function in place but have it call get_user() and then bring the logic into the new function with new phpdoc @since for 6.5.

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


11 months ago

#8 @joedolson
11 months ago

  • Milestone changed from 6.5 to 6.6

We can't just remove a function that simply; get_userdata is extremely widely used, and this change would create a lot of breakage. We can alias it, but removing it is not so ideal.

The big question here is whether we might want to deprecate get_userdata when we add get_user. It's a simpler construction, more predictable, matches more other existing function naming patterns.

I'm not sure this will be ready for 6.5; I haven't had time for it, but I'm going to re-milestone to 6.6.

@kushang78
11 months ago

Added patch for this ticket.

#9 @kushang78
11 months ago

@sc0ttkclark ,

As per your feedback, I’ve made the changes accordingly in the attached latest patch.

The changes:

  • Pointing the new get_user function under get_userdata without touching the entire core functions.

@sc0ttkclark @joedolson,

Can you please check my latest patch and drop your feedback on it.

Last edited 11 months ago by kushang78 (previous) (diff)

#10 @oglekler
8 months ago

  • Keywords dev-feedback added

I believe we need a decision if we are doing this or not, and do whatever will be decided. We have like 2 weeks until Beta 1, so, this is the time. New function is an enhancement.

This ticket was mentioned in Slack in #core by nhrrob. View the logs.


8 months ago

#12 @oglekler
8 months ago

  • Milestone changed from 6.6 to 6.7

We have 2 days before Beta 1 and no concrete decision, so, I am moving this ticket to the next milestone for further consideration.

#13 @peterwilsoncc
6 months ago

I think adding an alias is the most that can safely be done with this. The functionality would need to remain in get_userdata() as it's a pluggable function so there's no way of knowing whether the function is Core's or a plugin's.

If an alias is added, it doesn't need to be pluggable and can be placed among the standard user functions. That will ensure that plugins continue to plug the correct code.

I do like the idea, it's a much nicer name and inevitably what I reach for when trying to get a user only to discover that it's not there.

This ticket was mentioned in Slack in #core by chaion07. View the logs.


4 months ago

@joedolson
4 months ago

Shift location of function to user.php

#15 @joedolson
4 months ago

  • Keywords commit added; dev-feedback removed

Updated patch corrects docs formatting and moves the function into user.php. There's no particular need to make use of this immediately; we can do that over time, as there's no functional difference between using this and the existing get_user_by(). This is mostly going to serve developers by providing a more natural and obvious path, similar to all other getter functions.

In my opinion, there's no reason not to commit this, and it's about time. It's actually identical to the pluggable function get_userdata(), but more intuitive.

#17 @joedolson
4 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 59111:

Users: Add get_user() alias.

Add get_user() as an alias for get_user_by( 'id', $user_id ). Add get_user() to user.php so it is not a pluggable function. Bring parity with other standard get_ functions for a more intuitive developer experience.

Props sc0ttkclark, kushang78, joedolson, peterwilsoncc.
Fixes #35124.

#19 @joedolson
4 months ago

In 59112:

Code Standards: Correct @since reference in get_user().

Change @since from 6.7 to 6.7.0, per coding standards. Follow-up to [59111].

Props joedolson, peterwilsoncc.
See #35124.

Note: See TracTickets for help on using tickets.