Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/wp-admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
wp_schedule_event( time(), 'daily', 'delete_expired_transients' );
}

// Schedule collaboration data cleanup.
if ( wp_is_collaboration_enabled()
&& ! wp_next_scheduled( 'wp_delete_old_collaboration_data' )
&& ! wp_installing()
) {
wp_schedule_event( time(), 'daily', 'wp_delete_old_collaboration_data' );
}

set_screen_options();

$date_format = __( 'F j, Y' );
Expand Down
13 changes: 13 additions & 0 deletions src/wp-admin/includes/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ function wp_get_db_schema( $scope = 'all', $blog_id = null ) {
KEY post_parent (post_parent),
KEY post_author (post_author),
KEY type_status_author (post_type,post_status,post_author)
) $charset_collate;
CREATE TABLE $wpdb->collaboration (
id bigint(20) unsigned NOT NULL auto_increment,
room varchar($max_index_length) NOT NULL default '',
type varchar(32) NOT NULL default '',
client_id varchar(32) NOT NULL default '',
user_id bigint(20) unsigned NOT NULL default '0',
data longtext NOT NULL,
date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (id),
KEY type_client_id (type,client_id),
KEY room (room,id),
KEY date_gmt (date_gmt)
) $charset_collate;\n";

// Single site users table. The multisite flavor of the users table is handled below.
Expand Down
2 changes: 1 addition & 1 deletion src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ function upgrade_all() {
upgrade_682();
}

if ( $wp_current_db_version < 61644 ) {
if ( $wp_current_db_version < 61841 ) {
upgrade_700();
}

Expand Down
10 changes: 10 additions & 0 deletions src/wp-includes/class-wpdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ class wpdb {
'term_relationships',
'termmeta',
'commentmeta',
'collaboration',
);

/**
Expand Down Expand Up @@ -404,6 +405,15 @@ class wpdb {
*/
public $posts;

/**
* WordPress Collaboration table.
*
* @since 7.0.0
*
* @var string
*/
public $collaboration;

/**
* WordPress Terms table.
*
Expand Down
79 changes: 39 additions & 40 deletions src/wp-includes/collaboration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,18 @@
*/

/**
* Determines whether real-time collaboration is enabled.
* Checks whether real-time collaboration is enabled.
*
* If the WP_ALLOW_COLLABORATION constant is false,
* collaboration is always disabled regardless of the database option.
* Otherwise, falls back to the 'wp_collaboration_enabled' option.
* The feature requires both the site option and the database schema
* introduced in db_version 61841.
*
* @since 7.0.0
*
* @return bool Whether real-time collaboration is enabled.
* @return bool True if collaboration is enabled, false otherwise.
*/
function wp_is_collaboration_enabled() {
return (
wp_is_collaboration_allowed() &&
(bool) get_option( 'wp_collaboration_enabled' )
);
}

/**
* Determines whether real-time collaboration is allowed.
*
* If the WP_ALLOW_COLLABORATION constant is false,
* collaboration is not allowed and cannot be enabled.
* The constant defaults to true, unless the WP_ALLOW_COLLABORATION
* environment variable is set to string "false".
*
* @since 7.0.0
*
* @return bool Whether real-time collaboration is enabled.
*/
function wp_is_collaboration_allowed() {
if ( ! defined( 'WP_ALLOW_COLLABORATION' ) ) {
$env_value = getenv( 'WP_ALLOW_COLLABORATION' );
if ( false === $env_value ) {
// Environment variable is not defined, default to allowing collaboration.
define( 'WP_ALLOW_COLLABORATION', true );
} else {
/*
* Environment variable is defined, let's confirm it is actually set to
* "true" as it may still have a string value "false" – the preceeding
* `if` branch only tests for the boolean `false`.
*/
define( 'WP_ALLOW_COLLABORATION', 'true' === $env_value );
}
}

return WP_ALLOW_COLLABORATION;
return get_option( 'wp_enable_real_time_collaboration' )
&& get_option( 'db_version' ) >= 61841;
}

/**
Expand Down Expand Up @@ -83,3 +49,36 @@ function wp_collaboration_inject_setting() {
'after'
);
}

/**
* Deletes stale collaboration data from the collaboration table.
*
* Removes non-awareness rows older than 7 days and awareness rows older
* than 60 seconds. Rows left behind by abandoned collaborative editing
* sessions are cleaned up to prevent unbounded table growth.
*
* @since 7.0.0
*/
function wp_delete_old_collaboration_data() {
if ( ! wp_is_collaboration_enabled() ) {
return;
}

global $wpdb;

// Clean up sync rows older than 7 days.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->collaboration} WHERE type != 'awareness' AND date_gmt < %s",
gmdate( 'Y-m-d H:i:s', time() - WEEK_IN_SECONDS )
)
);

// Clean up awareness rows older than 60 seconds.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->collaboration} WHERE type = 'awareness' AND date_gmt < %s",
gmdate( 'Y-m-d H:i:s', time() - 60 )
)
);
}
Loading
Loading