From cd03f868144b99ae0a8767bcd217630ba3e4da27 Mon Sep 17 00:00:00 2001 From: Sergey Mitroshin Date: Fri, 15 Nov 2024 10:54:54 -0500 Subject: [PATCH] Connection: work around a WP user caching bug (#40188) 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. --- .../changelog/fix-connection-dotorg-user-cache-bug | 4 ++++ projects/packages/connection/src/class-manager.php | 7 ++++++- .../tests/php/test-class-connection-notice.php | 9 +++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 projects/packages/connection/changelog/fix-connection-dotorg-user-cache-bug diff --git a/projects/packages/connection/changelog/fix-connection-dotorg-user-cache-bug b/projects/packages/connection/changelog/fix-connection-dotorg-user-cache-bug new file mode 100644 index 0000000000000..5bafeb387ad19 --- /dev/null +++ b/projects/packages/connection/changelog/fix-connection-dotorg-user-cache-bug @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Work around a WP user caching bug (https://core.trac.wordpress.org/ticket/62003). diff --git a/projects/packages/connection/src/class-manager.php b/projects/packages/connection/src/class-manager.php index 8783dbff1126f..5d56946f46609 100644 --- a/projects/packages/connection/src/class-manager.php +++ b/projects/packages/connection/src/class-manager.php @@ -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, ) ); diff --git a/projects/packages/connection/tests/php/test-class-connection-notice.php b/projects/packages/connection/tests/php/test-class-connection-notice.php index 5fdc1ecfb5934..a93f342ba214f 100644 --- a/projects/packages/connection/tests/php/test-class-connection-notice.php +++ b/projects/packages/connection/tests/php/test-class-connection-notice.php @@ -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;