Skip to content

Commit

Permalink
Connection: work around a WP user caching bug (#40188)
Browse files Browse the repository at this point in the history
WP bug #62003 leads to inconsistent format of cached user data in certain circumstances.
It happens if only one field gets fetched from the database, so we add ID to work around the issue.
  • Loading branch information
sergeymitr authored Nov 15, 2024
1 parent 5878fb5 commit cd03f86
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Work around a WP user caching bug (https://core.trac.wordpress.org/ticket/62003).
7 changes: 6 additions & 1 deletion projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1620,12 +1620,17 @@ public function get_assumed_site_creation_date() {
return $cached_date;
}

/**
* We don't use the 'ID' field, but need it to overcome a WP caching bug: https://core.trac.wordpress.org/ticket/62003
*
* @todo Remote the 'ID' field from users fetching when the issue is fixed and Jetpack-supported WP versions move beyond it.
*/
$earliest_registered_users = get_users(
array(
'role' => 'administrator',
'orderby' => 'user_registered',
'order' => 'ASC',
'fields' => array( 'user_registered' ),
'fields' => array( 'ID', 'user_registered' ),
'number' => 1,
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ protected function set_up() {
\Jetpack_Options::update_option( 'master_user', $id_admin );

$this->users_query_filter = function ( $result, $query ) {
if ( str_starts_with( trim( $query ), 'SELECT wp_users.user_registered' )
if ( str_starts_with( trim( $query ), 'SELECT wp_users.ID,wp_users.user_registered' )
&& preg_match( '#wp_usermeta\.meta_value LIKE \'\{.*?\}"administrator"\{.*?\}\'#i', $query )
) {
return array( (object) array( 'user_registered' => '2012-03-19 00:00:00' ) );
return array(
(object) array(
'ID' => 12346,
'user_registered' => '2012-03-19 00:00:00',
),
);
}

return $result;
Expand Down

0 comments on commit cd03f86

Please sign in to comment.