Skip to content

Commit 6a1b00f

Browse files
committed
Changes that did not make their way over.
1 parent a9ca1bb commit 6a1b00f

7 files changed

Lines changed: 857 additions & 750 deletions

File tree

src/wp-admin/includes/schema.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
198198
PRIMARY KEY (id),
199199
KEY type_client_id (type,client_id),
200200
KEY room (room,id),
201+
KEY room_type_date (room,type,date_gmt),
201202
KEY date_gmt (date_gmt)
202203
) $charset_collate;\n";
203204

src/wp-includes/collaboration.php

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,54 @@
77
*/
88

99
/**
10-
* Checks whether real-time collaboration is enabled.
10+
* Determines whether real-time collaboration is enabled.
1111
*
12-
* The feature requires both the site option and the database schema
13-
* introduced in db_version 61841.
12+
* If the WP_ALLOW_COLLABORATION constant is false,
13+
* collaboration is always disabled regardless of the database option.
14+
* Otherwise, the feature requires both the 'wp_collaboration_enabled'
15+
* option and the database schema introduced in db_version 61841.
1416
*
1517
* @since 7.0.0
1618
*
17-
* @return bool True if collaboration is enabled, false otherwise.
19+
* @return bool Whether real-time collaboration is enabled.
1820
*/
1921
function wp_is_collaboration_enabled() {
20-
return get_option( 'wp_enable_real_time_collaboration' )
21-
&& get_option( 'db_version' ) >= 61841;
22+
return (
23+
wp_is_collaboration_allowed() &&
24+
get_option( 'wp_collaboration_enabled' ) &&
25+
get_option( 'db_version' ) >= 61841
26+
);
27+
}
28+
29+
/**
30+
* Determines whether real-time collaboration is allowed.
31+
*
32+
* If the WP_ALLOW_COLLABORATION constant is false,
33+
* collaboration is not allowed and cannot be enabled.
34+
* The constant defaults to true, unless the WP_ALLOW_COLLABORATION
35+
* environment variable is set to string "false".
36+
*
37+
* @since 7.0.0
38+
*
39+
* @return bool Whether real-time collaboration is allowed.
40+
*/
41+
function wp_is_collaboration_allowed() {
42+
if ( ! defined( 'WP_ALLOW_COLLABORATION' ) ) {
43+
$env_value = getenv( 'WP_ALLOW_COLLABORATION' );
44+
if ( false === $env_value ) {
45+
// Environment variable is not defined, default to allowing collaboration.
46+
define( 'WP_ALLOW_COLLABORATION', true );
47+
} else {
48+
/*
49+
* Environment variable is defined, let's confirm it is actually set to
50+
* "true" as it may still have a string value "false" – the preceeding
51+
* `if` branch only tests for the boolean `false`.
52+
*/
53+
define( 'WP_ALLOW_COLLABORATION', 'true' === $env_value );
54+
}
55+
}
56+
57+
return WP_ALLOW_COLLABORATION;
2258
}
2359

2460
/**
@@ -65,25 +101,10 @@ function wp_delete_old_collaboration_data() {
65101
if ( ! wp_is_collaboration_enabled() ) {
66102
/*
67103
* Collaboration was enabled in the past but has since been disabled.
68-
* Clean up any remaining stale data and unschedule the cron job
69-
* so this callback does not continue to run.
104+
* Unschedule the cron job prior to clean up so this callback does not
105+
* continue to run.
70106
*/
71-
$wpdb->query(
72-
$wpdb->prepare(
73-
"DELETE FROM {$wpdb->collaboration} WHERE date_gmt < %s",
74-
gmdate( 'Y-m-d H:i:s', time() - WEEK_IN_SECONDS )
75-
)
76-
);
77-
78-
$wpdb->query(
79-
$wpdb->prepare(
80-
"DELETE FROM {$wpdb->collaboration} WHERE type = 'awareness' AND date_gmt < %s",
81-
gmdate( 'Y-m-d H:i:s', time() - 60 )
82-
)
83-
);
84-
85107
wp_clear_scheduled_hook( 'wp_delete_old_collaboration_data' );
86-
return;
87108
}
88109

89110
/* Clean up rows older than 7 days. */

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ 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'] ) ) {
60+
return false;
61+
}
62+
5963
$result = $wpdb->insert(
6064
$wpdb->collaboration,
6165
array(
@@ -64,8 +68,9 @@ public function add_update( string $room, $update ): bool {
6468
'client_id' => $update['client_id'] ?? '',
6569
'data' => wp_json_encode( $update ),
6670
'date_gmt' => gmdate( 'Y-m-d H:i:s' ),
71+
'user_id' => get_current_user_id(),
6772
),
68-
array( '%s', '%s', '%s', '%s', '%s' )
73+
array( '%s', '%s', '%s', '%s', '%s', '%d' )
6974
);
7075

7176
return false !== $result;
@@ -286,27 +291,42 @@ public function remove_updates_through_cursor( string $room, int $cursor ): bool
286291
*
287292
* @global wpdb $wpdb WordPress database abstraction object.
288293
*
289-
* @param string $room Room identifier.
294+
* @param string $room Room identifier.
290295
* @param string $client_id Client identifier.
291-
* @param array<string, mixed> $state Serializable awareness state for this client.
292-
* @param int $user_id WordPress user ID that owns this client.
296+
* @param array<string, mixed> $state Serializable awareness state for this client.
297+
* @param int $user_id WordPress user ID that owns this client.
293298
* @return bool True on success, false on failure.
294299
*/
295300
public function set_awareness_state( string $room, string $client_id, array $state, int $user_id ): bool {
296301
global $wpdb;
297302

303+
if ( '' === $room || '' === $client_id ) {
304+
return false;
305+
}
306+
298307
$data = wp_json_encode( $state );
299-
$now = gmdate( 'Y-m-d H:i:s' );
308+
309+
/*
310+
* Bucket the timestamp to 5-second intervals so most polls
311+
* short-circuit without a database write. Ceil is used instead
312+
* of floor to prevent the awareness timeout from being hit early.
313+
*/
314+
$now = gmdate( 'Y-m-d H:i:s', (int) ceil( time() / 5 ) * 5 );
300315

301316
/* Check if a row already exists. */
302-
$exists = $wpdb->get_var(
317+
$exists = $wpdb->get_row(
303318
$wpdb->prepare(
304-
"SELECT id FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND client_id = %s LIMIT 1",
319+
"SELECT id, date_gmt FROM {$wpdb->collaboration} WHERE room = %s AND type = 'awareness' AND client_id = %s LIMIT 1",
305320
$room,
306321
$client_id
307322
)
308323
);
309324

325+
if ( $exists && $exists->date_gmt === $now ) {
326+
// Row already has the current date, consider update a success.
327+
return true;
328+
}
329+
310330
if ( $exists ) {
311331
$result = $wpdb->update(
312332
$wpdb->collaboration,
@@ -315,7 +335,7 @@ public function set_awareness_state( string $room, string $client_id, array $sta
315335
'data' => $data,
316336
'date_gmt' => $now,
317337
),
318-
array( 'id' => $exists )
338+
array( 'id' => $exists->id )
319339
);
320340
} else {
321341
$result = $wpdb->insert(

src/wp-includes/collaboration/class-wp-http-polling-collaboration-server.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,15 @@ public function register_routes(): void {
151151
'required' => true,
152152
'type' => array( 'object', 'null' ),
153153
),
154+
/*
155+
* client_id accepts both string and integer values:
156+
* - 'minimum' bounds the integer form.
157+
* - 'minLength' / 'maxLength' bound the string form.
158+
*/
154159
'client_id' => array(
160+
'minimum' => 1,
161+
'minLength' => 1,
162+
'maxLength' => 32, // Matches the client_id column width in wp-admin/includes/schema.php.
155163
'required' => true,
156164
'type' => array( 'string', 'integer' ),
157165
'sanitize_callback' => function ( $value ) {
@@ -315,7 +323,7 @@ public function handle_request( WP_REST_Request $request ) {
315323
// The lowest client ID is nominated to perform compaction when needed.
316324
$is_compactor = false;
317325
if ( count( $merged_awareness ) > 0 ) {
318-
$is_compactor = (string) min( array_keys( $merged_awareness ) ) === $client_id;
326+
$is_compactor = (string) min( array_keys( $merged_awareness ) ) === (string) $client_id;
319327
}
320328

321329
// Process each update according to its type.

0 commit comments

Comments
 (0)