Skip to content

Commit 4ff1c07

Browse files
committed
Extract md5 hashing to get_room_hash()
1 parent 15893fc commit 4ff1c07

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

src/wp-includes/collaboration/class-wp-sync-post-meta-storage.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ public function add_update( string $room, $update ): bool {
8484
);
8585

8686
if ( $meta_id ) {
87-
$room_hash = md5( $room );
88-
wp_cache_delete( "sync_room_state_{$room_hash}", 'sync' );
87+
wp_cache_delete( "sync_room_state_{$this->get_room_hash( $room )}", 'sync' );
8988
}
9089

9190
return (bool) $meta_id;
@@ -100,8 +99,7 @@ public function add_update( string $room, $update ): bool {
10099
* @return array<int, mixed> Awareness state.
101100
*/
102101
public function get_awareness_state( string $room ): array {
103-
$room_hash = md5( $room );
104-
$awareness = get_transient( "sync_awareness_{$room_hash}" );
102+
$awareness = get_transient( "sync_awareness_{$this->get_room_hash( $room )}" );
105103

106104
if ( ! is_array( $awareness ) ) {
107105
return array();
@@ -120,8 +118,7 @@ public function get_awareness_state( string $room ): array {
120118
* @return bool True on success, false on failure.
121119
*/
122120
public function set_awareness_state( string $room, array $awareness ): bool {
123-
$room_hash = md5( $room );
124-
return set_transient( "sync_awareness_{$room_hash}", $awareness, MINUTE_IN_SECONDS );
121+
return set_transient( "sync_awareness_{$this->get_room_hash( $room )}", $awareness, MINUTE_IN_SECONDS );
125122
}
126123

127124
/**
@@ -151,7 +148,7 @@ public function get_cursor( string $room ): int {
151148
* @return int|null Post ID.
152149
*/
153150
private function get_storage_post_id( string $room ): ?int {
154-
$room_hash = md5( $room );
151+
$room_hash = $this->get_room_hash( $room );
155152

156153
if ( isset( self::$storage_post_ids[ $room_hash ] ) ) {
157154
return self::$storage_post_ids[ $room_hash ];
@@ -216,7 +213,7 @@ public function get_update_count( string $room ): int {
216213
public function get_updates_after_cursor( string $room, int $cursor ): array {
217214
global $wpdb;
218215

219-
$room_hash = md5( $room );
216+
$room_hash = $this->get_room_hash( $room );
220217
$state_cache_key = "sync_room_state_{$room_hash}";
221218
$cached = wp_cache_get( $state_cache_key, 'sync' );
222219

@@ -316,8 +313,7 @@ public function remove_updates_before_cursor( string $room, int $cursor ): bool
316313

317314
if ( $deleted_rows > 0 ) {
318315
wp_cache_delete( $post_id, 'post_meta' );
319-
$room_hash = md5( $room );
320-
wp_cache_delete( "sync_room_state_{$room_hash}", 'sync' );
316+
wp_cache_delete( "sync_room_state_{$this->get_room_hash( $room )}", 'sync' );
321317
}
322318

323319
return true;
@@ -352,4 +348,19 @@ private function with_suspended_posts_last_changed_update( Closure $callback ) {
352348
}
353349
return $return_value;
354350
}
351+
352+
/**
353+
* Returns a hash of the room identifier.
354+
*
355+
* Used as the post slug for storage posts, as a segment in cache keys,
356+
* and as part of transient names.
357+
*
358+
* @since 7.0.0
359+
*
360+
* @param string $room Room identifier.
361+
* @return string MD5 hash of the room.
362+
*/
363+
private function get_room_hash( string $room ): string {
364+
return md5( $room );
365+
}
355366
}

0 commit comments

Comments
 (0)