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..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 @@ -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,42 @@ 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() + * + * @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( + '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; + } }