Skip to content

Commit 7517f0c

Browse files
committed
Prevent duplicate entries.
1 parent d66d532 commit 7517f0c

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

src/wp-includes/collaboration/class-wp-collaboration-table-storage.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,16 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
122122
// Deterministic ordering.
123123
$cached_awareness = wp_list_sort( $cached, 'client_id' );
124124

125-
// Remove out of date entries.
125+
// Remove out of date entries and duplicate entries.
126+
$entries = array();
126127
foreach ( $cached_awareness as $index => $client_awareness ) {
127128
if ( empty( $client_awareness['timestamp'] ) || $client_awareness['timestamp'] < $cutoff_timestamp ) {
128-
unset( $cached_awareness[ $index ] );
129+
continue;
129130
}
131+
$entries[ $client_awareness['client_id'] ] = $client_awareness;
130132
}
131133

132-
return array_values( $cached_awareness );
134+
return array_values( $entries );
133135
} elseif ( false !== $cached ) {
134136
// Cache is corrupted, delete it.
135137
wp_cache_delete( $cache_key, 'collaboration' );
@@ -142,7 +144,7 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
142144

143145
$rows = $wpdb->get_results(
144146
$wpdb->prepare(
145-
"SELECT client_id, user_id, date_gmt, data FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND date_gmt >= %s",
147+
"SELECT client_id, user_id, date_gmt, data FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND date_gmt >= %s ORDER BY id ASC",
146148
$room,
147149
$cutoff_mysql
148150
)
@@ -159,7 +161,7 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
159161
$date_time = date_create_from_format( 'Y-m-d H:i:s', $row->date_gmt, new DateTimeZone( 'UTC' ) );
160162
$decoded_state = json_decode( $row->data, true );
161163
if ( is_array( $decoded_state ) && false !== $date_time ) {
162-
$entries[] = array(
164+
$entries[ $row->client_id ] = array(
163165
'client_id' => $row->client_id,
164166
'state' => $decoded_state,
165167
'user_id' => (int) $row->user_id,
@@ -168,6 +170,9 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
168170
}
169171
}
170172

173+
$entries = array_values( $entries );
174+
// Deterministic ordering.
175+
$entries = wp_list_sort( $entries, 'client_id' );
171176
wp_cache_set( $cache_key, $entries, 'collaboration', HOUR_IN_SECONDS );
172177
return $entries;
173178
}

0 commit comments

Comments
 (0)