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;