Skip to content

Commit bb49e45

Browse files
authored
Address lazy-load WP_User behavior introduced WP 6.9 (#45450)
1 parent 316659f commit bb49e45

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Handle lazy-loading of WP_User object properties.

projects/packages/stats/src/class-main.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ public static function jetpack_is_dnt_enabled() {
120120
public static function map_meta_caps( $caps, $cap, $user_id ) {
121121
// Map view_stats to exists.
122122
if ( 'view_stats' === $cap ) {
123-
$user = new WP_User( $user_id );
124-
$user_role = array_shift( $user->roles );
123+
$user = new WP_User( $user_id );
124+
// WordPress 6.9 introduced lazy-loading of some WP_User properties, including `roles`.
125+
// It also made said properties protected, so we can't modify keys directly.
126+
$user_roles = $user->roles;
127+
$user_role = array_shift( $user_roles ); // Work with the copy
125128
$stats_roles = Options::get_option( 'roles' );
126129

127130
// Is the users role in the available stats roles?
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Handle lazy-loading of WP_User object properties.

projects/packages/sync/src/class-functions.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,21 @@ public static function get_is_fse_theme() {
604604
*/
605605
public static function json_wrap( &$any, $seen_nodes = array() ) {
606606
if ( is_object( $any ) ) {
607-
$input = get_object_vars( $any );
607+
$input = get_object_vars( $any );
608+
609+
// WordPress 6.9 introduced lazy-loading of WP_User `roles`, `caps`, and `allcaps` properties.
610+
// It also made said properties protected, so we need to access them and set them as keys manually.
611+
if ( $any instanceof \WP_User ) {
612+
$roles = $any->roles;
613+
$caps = $any->caps;
614+
$allcaps = $any->allcaps;
615+
616+
// For WordPress <6.8 the below are redundant. :shrug:
617+
$input['roles'] = $roles;
618+
$input['caps'] = $caps;
619+
$input['allcaps'] = $allcaps;
620+
}
621+
608622
$input['__o'] = 1;
609623
} else {
610624
$input = &$any;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: other
3+
4+
Sync: Handle lazy-loading of WP_User object properties.

projects/plugins/jetpack/tests/php/sync/Jetpack_Sync_Users_Test.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ public function test_insert_user_is_synced() {
3535
// The regular user object doesn't have allowed_mime_types
3636
unset( $server_user->data->allowed_mime_types );
3737

38-
unset( $user->allcaps['subscriber'] );
39-
unset( $user->allcaps['level_0'] );
38+
// WordPress 6.9 introduced lazy-loading of some WP_User properties.
39+
// It also made said properties protected, so we can't modify keys directly.
40+
$allcaps = $user->allcaps; // This triggers lazy loading
41+
unset( $allcaps['subscriber'] );
42+
unset( $allcaps['level_0'] );
43+
$user->allcaps = $allcaps;
44+
4045
$this->assertEqualsObject( $user, $server_user, 'The replicastore user must equal the initial user.' );
4146

4247
$event = $this->server_event_storage->get_most_recent_event( 'jetpack_sync_register_user' );

0 commit comments

Comments
 (0)