Skip to content

Commit 9f006f0

Browse files
Apply suggestions from code review
Co-authored-by: Weston Ruter <[email protected]>
1 parent c3fc522 commit 9f006f0

1 file changed

Lines changed: 11 additions & 24 deletions

File tree

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

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ class WP_Collaboration_Table_Storage {
5656
public function add_update( string $room, $update ): bool {
5757
global $wpdb;
5858

59-
if ( '' === $room || empty( $update['type'] ) || empty( $update['client_id'] ) ) {
59+
if ( '' === $room || ! is_array( $update ) || empty( $update['type'] ) || empty( $update['client_id'] ) ) {
6060
return false;
6161
}
6262

6363
$result = $wpdb->insert(
6464
$wpdb->collaboration,
6565
array(
6666
'room' => $room,
67-
'type' => $update['type'] ?? '',
68-
'client_id' => $update['client_id'] ?? '',
67+
'type' => $update['type'],
68+
'client_id' => $update['client_id'],
6969
'data' => wp_json_encode( $update ),
7070
'date_gmt' => gmdate( 'Y-m-d H:i:s' ),
7171
'user_id' => get_current_user_id(),
@@ -120,6 +120,7 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
120120

121121
if ( false !== $cached && is_array( $cached ) ) {
122122
// Deterministic ordering.
123+
/** @var AwarenessState[] $cached_awareness */
123124
$cached_awareness = wp_list_sort( $cached, 'client_id' );
124125

125126
// Remove out of date entries and duplicate entries.
@@ -143,6 +144,7 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
143144
return array();
144145
}
145146

147+
/** @var array<object{ client_id: string, user_id: string, date_gmt: string, data: string }> $rows */
146148
$rows = $wpdb->get_results(
147149
$wpdb->prepare(
148150
"SELECT client_id, user_id, date_gmt, data FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND date_gmt >= %s ORDER BY collaboration_id ASC",
@@ -151,12 +153,6 @@ public function get_awareness_state( string $room, int $timeout = 30 ): array {
151153
)
152154
);
153155

154-
if ( ! is_array( $rows ) ) {
155-
$entries = array();
156-
wp_cache_set( $cache_key, $entries, 'collaboration', HOUR_IN_SECONDS );
157-
return $entries;
158-
}
159-
160156
$entries = array();
161157
foreach ( $rows as $row ) {
162158
$date_time = date_create_from_format( 'Y-m-d H:i:s', $row->date_gmt, new DateTimeZone( 'UTC' ) );
@@ -228,38 +224,33 @@ public function get_updates_after_cursor( string $room, int $cursor ): array {
228224
* separately via get_awareness_state().
229225
*/
230226

231-
/* Snapshot the current max ID and total row count in a single query. */
227+
// Snapshot the current max ID and total row count in a single query.
228+
/** @var object{ max_id: int, total: int } $snapshot */
232229
$snapshot = $wpdb->get_row(
233230
$wpdb->prepare(
234231
"SELECT COALESCE( MAX( collaboration_id ), 0 ) AS max_id, COUNT(*) AS total FROM {$wpdb->collaboration} WHERE room = %s AND type != 'awareness'",
235232
$room
236233
)
237234
);
238235

239-
if ( ! $snapshot ) {
240-
$this->room_cursors[ $room ] = 0;
241-
$this->room_update_counts[ $room ] = 0;
242-
return array();
243-
}
244-
245236
$max_id = (int) $snapshot->max_id;
246237
$total = (int) $snapshot->total;
247238

248239
$this->room_cursors[ $room ] = $max_id;
249240

241+
$this->room_update_counts[ $room ] = $total;
242+
250243
if ( 0 === $max_id || $max_id <= $cursor ) {
251244
/*
252245
* Preserve the real row count so the server can still
253246
* trigger compaction when updates have accumulated but
254247
* no new ones arrived since the client's last poll.
255248
*/
256-
$this->room_update_counts[ $room ] = $total;
257249
return array();
258250
}
259251

260-
$this->room_update_counts[ $room ] = $total;
261-
262-
/* Fetch updates after the cursor up to the snapshot boundary. */
252+
// Fetch updates after the cursor up to the snapshot boundary.
253+
/** @var array<object{ data: string }> $rows */
263254
$rows = $wpdb->get_results(
264255
$wpdb->prepare(
265256
"SELECT data FROM {$wpdb->collaboration} WHERE room = %s AND type != 'awareness' AND collaboration_id > %d AND collaboration_id <= %d ORDER BY collaboration_id ASC",
@@ -269,10 +260,6 @@ public function get_updates_after_cursor( string $room, int $cursor ): array {
269260
)
270261
);
271262

272-
if ( ! is_array( $rows ) ) {
273-
return array();
274-
}
275-
276263
$updates = array();
277264
foreach ( $rows as $row ) {
278265
$decoded = json_decode( $row->data, true );

0 commit comments

Comments
 (0)