[wp-trac] [WordPress Trac] #8911: Use $WP_User as the standard unit of user information

WordPress Trac wp-trac at lists.automattic.com
Wed Jan 21 19:51:15 GMT 2009


#8911: Use $WP_User as the standard unit of user information
--------------------------+-------------------------------------------------
 Reporter:  jeremyclarke  |       Owner:  jeremyclarke
     Type:  defect (bug)  |      Status:  new         
 Priority:  normal        |   Milestone:  2.8         
Component:  General       |     Version:              
 Severity:  normal        |    Keywords:              
--------------------------+-------------------------------------------------
 Right now much of the core code and most plugins use the old
 get_userdata() function to fetch information about users. This works okay
 for most cases but fails to take advantage of the new $WP_User object
 type. Using the objects makes the user information much more powerful
 because you can immediately call methods like $user->has_cap('edit_posts')
 without messing around. It also just makes sense, and not using the pretty
 object is silly.

 In some places, like edit-user.php, where more functionality is needed
 there are functions like get_user_to_edit() that make use of the $WP_User
 object. I think that WP should clearly move towards always using the
 modeled object version rather than the straight db version in all places,
 and encourage plugin authors to do the same.

 Luckily the $WP_User wrapper uses get_userinfo to fetch its data and
 rewrites all the elements into its first level, so effectively the
 resulting object from $WP_User has all the same data as get_userinfo() and
 thus deprecates perfectly in all situations.

 My proposal would be to create a new wrapper function to be used instead
 of get_userinfo():


 {{{
 get_user($id, $name = '')
 }}}


 This would just initiate the object and return it, similar to get_post.
 It's also fits much better in the overall naming conventions of wp with
 friends like get_term.

 If possible, it might also be good to move the actual database sql from
 the get_userdata() function into the $WP_User object definition somewhere,
 that way its all in one place. Looking at it now it also seems like the
 whole _fill_user() function thing could be done more elegantly to explain
 itself and the cache better. [problem: pluggable.php has get_userdata(),
 which complicates things]

 Finally, since get_userdata() currently calls _fill_user() every time then
 fetches all meta_value/key's for the user, wouldn't it be faster to make
 just one db call that fetches the row from wp_users AND the values from
 the usermeta table? If you are checking 30 users on your page for some
 reason (say, a twitter style list of your authors in the sidebar) that
 would save 30 database connections, which makes a difference no matter
 what!

 [probably should be in another ticket, but it would also be great to have
 access to a global function get_users() that fetched a set of users (say,
 active ones or a specific role) all at once based on criteria like
 get_terms() or get_posts(), it would save time if you are getting many
 users and if it added them all to the cache after fetching it would
 probably be a lot faster than checking each user individually]

 I'm very interested in feedback. I'll try to work on a patch at some point
 soon (mental note: before 2.8 feature freeze)

-- 
Ticket URL: <http://trac.wordpress.org/ticket/8911>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list