From 19274ba3b51d27ae5ace7ed2e9a9308653bbf71f Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 22 Feb 2026 20:23:20 -0800 Subject: [PATCH 1/2] Prevent calling wp_cache_set_posts_last_changed() when touching post meta in WP_Sync_Post_Meta_Storage --- .../class-wp-sync-post-meta-storage.php | 43 +++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php b/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php index d0f1c99736fe0..30eee0f4444a0 100644 --- a/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php +++ b/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php @@ -85,7 +85,9 @@ public function add_update( string $room, $update ): bool { 'value' => $update, ); - return (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false ); + return $this->with_suspended_posts_last_changed_update( + fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false ) + ); } /** @@ -162,7 +164,9 @@ public function set_awareness_state( string $room, array $awareness ): bool { } // update_post_meta returns false if the value is the same as the existing value. - update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness ); + $this->with_suspended_posts_last_changed_update( + fn() => update_post_meta( $post_id, self::AWARENESS_META_KEY, $awareness ) + ); return true; } @@ -305,7 +309,7 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool $all_updates = $this->get_all_updates( $room ); // Remove all updates for the room and re-store only those that are newer than the cursor. - if ( ! delete_post_meta( $post_id, self::SYNC_UPDATE_META_KEY ) ) { + if ( ! $this->with_suspended_posts_last_changed_update( fn() => delete_post_meta( $post_id, self::SYNC_UPDATE_META_KEY ) ) ) { return false; } @@ -313,10 +317,41 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool $add_result = true; foreach ( $all_updates as $envelope ) { if ( $add_result && $envelope['timestamp'] >= $cursor ) { - $add_result = (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false ); + $add_result = $this->with_suspended_posts_last_changed_update( + fn() => (bool) add_post_meta( $post_id, self::SYNC_UPDATE_META_KEY, $envelope, false ) + ); } } return $add_result; } + + /** + * Invokes the provided callback while the suspending setting the posts last_changed cache key. + * + * @since 7.0.0 + * @see wp_cache_set_posts_last_changed() + * + * @param Closure $callback Callback + * @return mixed Return value from the callback. + */ + private function with_suspended_posts_last_changed_update( Closure $callback ) { + $priorities = array( + 'added_post_meta' => has_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' ), + 'updated_post_meta' => has_action( 'updated_post_meta', 'wp_cache_set_posts_last_changed' ), + 'deleted_post_meta' => has_action( 'deleted_post_meta', 'wp_cache_set_posts_last_changed' ), + ); + foreach ( $priorities as $action => $priority ) { + if ( false !== $priority ) { + remove_action( $action, 'wp_cache_set_posts_last_changed', $priority ); + } + } + $return_value = $callback(); + foreach ( $priorities as $action => $priority ) { + if ( false !== $priority ) { + add_action( $action, 'wp_cache_set_posts_last_changed', $priority ); + } + } + return $return_value; + } } From 20560be5e072375554cd0d9b2365d2184b49fa80 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Sun, 22 Feb 2026 20:31:07 -0800 Subject: [PATCH 2/2] Add PHPStan typing for return value of WP_Sync_Post_Meta_Storage::with_suspended_posts_last_changed_update() Co-authored-by: gemini-cli <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- .../collaboration/class-wp-sync-post-meta-storage.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php b/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php index 30eee0f4444a0..fff8952f5a4de 100644 --- a/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php +++ b/src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php @@ -332,8 +332,9 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool * @since 7.0.0 * @see wp_cache_set_posts_last_changed() * - * @param Closure $callback Callback - * @return mixed Return value from the callback. + * @template T + * @param Closure(): T $callback Callback. + * @return T Return value from the callback. */ private function with_suspended_posts_last_changed_update( Closure $callback ) { $priorities = array(