Skip to content

Commit 6ac9421

Browse files
committed
Replace get_userdata() with get_authordata() for optimized user data retrieval and introduce short initialization in WP_User.
1 parent e11e9f2 commit 6ac9421

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

src/wp-includes/author-template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function get_the_modified_author() {
9393
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
9494

9595
if ( $last_id ) {
96-
$last_user = get_userdata( $last_id );
96+
$last_user = get_authordata( $last_id );
9797

9898
/**
9999
* Filters the display name of the author who last edited the current post.
@@ -164,7 +164,7 @@ function get_the_author_meta( $field = '', $user_id = false ) {
164164
global $authordata;
165165
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
166166
} else {
167-
$authordata = get_userdata( $user_id );
167+
$authordata = get_authordata( $user_id );
168168
}
169169

170170
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {
@@ -370,7 +370,7 @@ function get_author_posts_url( $author_id, $author_nicename = '' ) {
370370
$link = $file . '?author=' . $author_id;
371371
} else {
372372
if ( '' === $author_nicename ) {
373-
$user = get_userdata( $author_id );
373+
$user = get_authordata( $author_id );
374374
if ( ! empty( $user->user_nicename ) ) {
375375
$author_nicename = $user->user_nicename;
376376
}

src/wp-includes/class-wp-query.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,7 +4051,7 @@ public function get_queried_object() {
40514051
}
40524052
}
40534053

4054-
$this->queried_object = get_userdata( $this->queried_object_id );
4054+
$this->queried_object = get_authordata( $this->queried_object_id );
40554055
}
40564056

40574057
return $this->queried_object;
@@ -4887,7 +4887,7 @@ public function generate_postdata( $post ) {
48874887

48884888
$id = (int) $post->ID;
48894889

4890-
$authordata = get_userdata( $post->post_author );
4890+
$authordata = get_authordata( $post->post_author );
48914891

48924892
$currentday = false;
48934893
$currentmonth = false;

src/wp-includes/class-wp-user.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ class WP_User {
112112
*/
113113
private static $back_compat_keys;
114114

115+
116+
private $short_init = false;
117+
115118
/**
116119
* Constructor.
117120
*
@@ -174,14 +177,17 @@ public function __construct( $id = 0, $name = '', $site_id = 0 ) {
174177
*
175178
* @param object $data User DB row object.
176179
* @param int $site_id Optional. The site ID to initialize for.
180+
* @param bool $short_init Optional. Whether to skip initializing capabilities and roles.
177181
*/
178-
public function init( $data, $site_id = 0 ) {
182+
public function init( $data, $site_id = 0, $short_init = false ) {
179183
if ( ! isset( $data->ID ) ) {
180184
$data->ID = 0;
181185
}
182186
$this->data = $data;
183187
$this->ID = (int) $data->ID;
184188

189+
$this->short_init = $short_init;
190+
185191
$this->for_site( $site_id );
186192
}
187193

@@ -548,6 +554,7 @@ public function add_role( $role ) {
548554
if ( empty( $role ) ) {
549555
return;
550556
}
557+
$this->load_capability_data();
551558

552559
if ( in_array( $role, $this->roles, true ) ) {
553560
return;
@@ -577,6 +584,7 @@ public function add_role( $role ) {
577584
* @param string $role Role name.
578585
*/
579586
public function remove_role( $role ) {
587+
$this->load_capability_data();
580588
if ( ! in_array( $role, $this->roles, true ) ) {
581589
return;
582590
}
@@ -609,6 +617,7 @@ public function remove_role( $role ) {
609617
* @param string $role Role name.
610618
*/
611619
public function set_role( $role ) {
620+
$this->load_capability_data();
612621
if ( 1 === count( $this->roles ) && current( $this->roles ) === $role ) {
613622
return;
614623
}
@@ -710,6 +719,7 @@ public function update_user_level_from_caps() {
710719
* @param bool $grant Whether to grant capability to user.
711720
*/
712721
public function add_cap( $cap, $grant = true ) {
722+
$this->load_capability_data();
713723
$this->caps[ $cap ] = $grant;
714724
update_user_meta( $this->ID, $this->cap_key, $this->caps );
715725
$this->get_role_caps();
@@ -724,6 +734,7 @@ public function add_cap( $cap, $grant = true ) {
724734
* @param string $cap Capability name.
725735
*/
726736
public function remove_cap( $cap ) {
737+
$this->load_capability_data();
727738
if ( ! isset( $this->caps[ $cap ] ) ) {
728739
return;
729740
}
@@ -776,6 +787,7 @@ public function remove_all_caps() {
776787
* the given capability for that object.
777788
*/
778789
public function has_cap( $cap, ...$args ) {
790+
$this->load_capability_data();
779791
if ( is_numeric( $cap ) ) {
780792
_deprecated_argument( __FUNCTION__, '2.0.0', __( 'Usage of user levels is deprecated. Use capabilities instead.' ) );
781793
$cap = $this->translate_level_to_cap( $cap );
@@ -878,6 +890,10 @@ public function for_site( $site_id = 0 ) {
878890

879891
$this->cap_key = $wpdb->get_blog_prefix( $this->site_id ) . 'capabilities';
880892

893+
if ( $this->short_init ) {
894+
return;
895+
}
896+
881897
$this->caps = $this->get_caps_data();
882898

883899
$this->get_role_caps();
@@ -911,4 +927,18 @@ private function get_caps_data() {
911927

912928
return $caps;
913929
}
930+
931+
/**
932+
* Loads capability data if it has not been loaded yet.
933+
*
934+
* @since 6.9.0
935+
*/
936+
private function load_capability_data() {
937+
if ( ! $this->short_init ) {
938+
return;
939+
}
940+
$this->caps = $this->get_caps_data();
941+
$this->get_role_caps();
942+
$this->short_init = false;
943+
}
914944
}

src/wp-includes/class-wp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public function register_globals() {
675675
}
676676

677677
if ( $wp_query->is_author() ) {
678-
$GLOBALS['authordata'] = get_userdata( get_queried_object_id() );
678+
$GLOBALS['authordata'] = get_authordata( get_queried_object_id() );
679679
}
680680
}
681681

src/wp-includes/user.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,25 @@ function sanitize_user_field( $field, $value, $user_id, $context ) {
19741974
return $value;
19751975
}
19761976

1977+
/**
1978+
* Retrieves user info by user ID. Set short init to only load User data not capabilities / roles
1979+
*
1980+
* @since 6.9.0
1981+
*
1982+
* @param int $user_id User ID
1983+
* @return WP_User|false WP_User object on success, false on failure.
1984+
*/
1985+
function get_authordata( $user_id ) {
1986+
$userdata = WP_User::get_data_by( 'id', $user_id );
1987+
if ( ! $userdata ) {
1988+
return false;
1989+
}
1990+
1991+
$user = new WP_User();
1992+
$user->init( $userdata, 0, true );
1993+
return $user;
1994+
}
1995+
19771996
/**
19781997
* Updates all user caches.
19791998
*

0 commit comments

Comments
 (0)