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..46c0165826fb9 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 @@ -25,12 +25,12 @@ class WP_Sync_Post_Meta_Storage implements WP_Sync_Storage { const POST_TYPE = 'wp_sync_storage'; /** - * Meta key for awareness state. + * Transient prefix for awareness state. * * @since 7.0.0 * @var string */ - const AWARENESS_META_KEY = 'wp_sync_awareness'; + const AWARENESS_TRANSIENT_PREFIX = 'wp_sync_awareness'; /** * Meta key for sync updates. @@ -123,6 +123,24 @@ static function ( $update ): bool { return $updates; } + /** + * Gets the transient key for awareness state for a given post ID. + * + * @since 7.0.0 + * + * @param int $post_id Post ID. + * @return string Transient key for awareness state. + */ + public function get_awareness_transient_key( $post_id ) { + $cache_key = self::AWARENESS_TRANSIENT_PREFIX . '_' . $post_id; + if ( strlen( $cache_key ) <= 172 ) { + // Safe length for a transient key. + return $cache_key; + } + // If the cache key is too long, hash it to ensure it fits within limits. + return md5( $cache_key ); + } + /** * Gets awareness state for a given room. * @@ -137,7 +155,7 @@ public function get_awareness_state( string $room ): array { return array(); } - $awareness = get_post_meta( $post_id, self::AWARENESS_META_KEY, true ); + $awareness = get_transient( $this->get_awareness_transient_key( $post_id ) ); if ( ! is_array( $awareness ) ) { return array(); @@ -161,8 +179,8 @@ public function set_awareness_state( string $room, array $awareness ): bool { return false; } - // 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 ); + // set_transient() can return false if the value is the same as the existing value, which is considered a success regardless. + set_transient( $this->get_awareness_transient_key( $post_id ), $awareness, HOUR_IN_SECONDS ); return true; }