forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollaboration.php
More file actions
130 lines (118 loc) · 3.3 KB
/
collaboration.php
File metadata and controls
130 lines (118 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* Bootstraps collaborative editing.
*
* @package WordPress
* @since 7.0.0
*/
/**
* Determines whether real-time collaboration is enabled.
*
* If the WP_ALLOW_COLLABORATION constant is false,
* collaboration is always disabled regardless of the database option.
*
* @since 7.0.0
*
* @return bool Whether real-time collaboration is enabled.
*/
function wp_is_collaboration_enabled(): bool {
return (
wp_is_collaboration_allowed() &&
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".
*
* Also requires the database schema introduced in db_version 62282.
*
* @since 7.0.0
*
* @return bool Whether real-time collaboration is allowed.
*/
function wp_is_collaboration_allowed(): bool {
if ( get_option( 'db_version' ) < 62282 ) {
return false;
}
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 preceding
* `if` branch only tests for the boolean `false`.
*/
define( 'WP_ALLOW_COLLABORATION', 'true' === $env_value );
}
}
return (bool) WP_ALLOW_COLLABORATION;
}
/**
* Injects the real-time collaboration setting into a global variable.
*
* @since 7.0.0
*
* @access private
*
* @global string $pagenow The filename of the current screen.
*/
function wp_collaboration_inject_setting(): void {
global $pagenow;
if ( ! wp_is_collaboration_enabled() ) {
return;
}
// Disable real-time collaboration on the site editor.
$enabled = true;
if ( 'site-editor.php' === $pagenow ) {
$enabled = false;
}
wp_add_inline_script(
'wp-core-data',
'window._wpCollaborationEnabled = ' . wp_json_encode( $enabled ) . ';',
'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
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
function wp_delete_old_collaboration_data(): void {
global $wpdb;
if ( ! wp_is_collaboration_enabled() ) {
/*
* Collaboration was enabled in the past but has since been disabled.
* Unschedule the cron job prior to clean up so this callback does not
* continue to run.
*/
wp_clear_scheduled_hook( 'wp_delete_old_collaboration_data' );
}
// Clean up rows older than 7 days.
$wpdb->query(
$wpdb->prepare(
"DELETE FROM {$wpdb->collaboration} WHERE 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 )
)
);
}