| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps collaborative editing. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! class_exists( 'WP_Sync_Post_Meta_Storage' ) ) { |
| 9 |
require_once __DIR__ . '/interface-wp-sync-storage.php'; |
| 10 |
require_once __DIR__ . '/class-wp-sync-post-meta-storage.php'; |
| 11 |
require_once __DIR__ . '/class-wp-http-polling-sync-server.php'; |
| 12 |
} |
| 13 |
|
| 14 |
if ( ! function_exists( 'gutenberg_register_sync_storage_post_type' ) ) { |
| 15 |
/** |
| 16 |
* Registers the custom post type for sync storage. |
| 17 |
*/ |
| 18 |
function gutenberg_register_sync_storage_post_type() { |
| 19 |
register_post_type( |
| 20 |
'wp_sync_storage', |
| 21 |
array( |
| 22 |
'labels' => array( |
| 23 |
'name' => __( 'Sync Updates', 'gutenberg' ), |
| 24 |
'singular_name' => __( 'Sync Update', 'gutenberg' ), |
| 25 |
), |
| 26 |
'public' => false, |
| 27 |
'hierarchical' => false, |
| 28 |
'capabilities' => array( |
| 29 |
'read' => 'do_not_allow', |
| 30 |
'read_private_posts' => 'do_not_allow', |
| 31 |
'create_posts' => 'do_not_allow', |
| 32 |
'publish_posts' => 'do_not_allow', |
| 33 |
'edit_posts' => 'do_not_allow', |
| 34 |
'edit_others_posts' => 'do_not_allow', |
| 35 |
'edit_published_posts' => 'do_not_allow', |
| 36 |
'delete_posts' => 'do_not_allow', |
| 37 |
'delete_others_posts' => 'do_not_allow', |
| 38 |
'delete_published_posts' => 'do_not_allow', |
| 39 |
), |
| 40 |
'map_meta_cap' => false, |
| 41 |
'publicly_queryable' => false, |
| 42 |
'query_var' => false, |
| 43 |
'rewrite' => false, |
| 44 |
'show_in_menu' => false, |
| 45 |
'show_in_rest' => false, |
| 46 |
'show_ui' => false, |
| 47 |
'supports' => array( 'custom-fields' ), |
| 48 |
) |
| 49 |
); |
| 50 |
} |
| 51 |
add_action( 'init', 'gutenberg_register_sync_storage_post_type' ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! function_exists( 'gutenberg_register_collaboration_rest_routes' ) ) { |
| 55 |
/** |
| 56 |
* Registers REST API routes for collaborative editing. |
| 57 |
*/ |
| 58 |
function gutenberg_register_collaboration_rest_routes(): void { |
| 59 |
$sync_storage = new WP_Sync_Post_Meta_Storage(); |
| 60 |
$sync_server = new WP_HTTP_Polling_Sync_Server( $sync_storage ); |
| 61 |
$sync_server->register_routes(); |
| 62 |
} |
| 63 |
add_action( 'rest_api_init', 'gutenberg_register_collaboration_rest_routes' ); |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! function_exists( 'wp_collaboration_register_meta' ) ) { |
| 67 |
/** |
| 68 |
* Registers post meta for persisting CRDT documents. |
| 69 |
*/ |
| 70 |
function gutenberg_rest_api_crdt_post_meta() { |
| 71 |
// This string must match WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE in @wordpress/sync. |
| 72 |
$persisted_crdt_post_meta_key = '_crdt_document'; |
| 73 |
|
| 74 |
register_meta( |
| 75 |
'post', |
| 76 |
$persisted_crdt_post_meta_key, |
| 77 |
array( |
| 78 |
'auth_callback' => static function ( bool $_allowed, string $_meta_key, int $object_id, int $user_id ): bool { |
| 79 |
return user_can( $user_id, 'edit_post', $object_id ); |
| 80 |
}, |
| 81 |
/* |
| 82 |
* Revisions must be disabled because we always want to preserve |
| 83 |
* the latest persisted CRDT document, even when a revision is restored. |
| 84 |
* This ensures that we can continue to apply updates to a shared document |
| 85 |
* and peers can simply merge the restored revision like any other incoming |
| 86 |
* update. |
| 87 |
* |
| 88 |
* If we want to persist CRDT documents alongside revisions in the |
| 89 |
* future, we should do so in a separate meta key. |
| 90 |
*/ |
| 91 |
'revisions_enabled' => false, |
| 92 |
'show_in_rest' => true, |
| 93 |
'single' => true, |
| 94 |
'type' => 'string', |
| 95 |
) |
| 96 |
); |
| 97 |
} |
| 98 |
add_action( 'init', 'gutenberg_rest_api_crdt_post_meta' ); |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! function_exists( 'wp_collaboration_inject_setting' ) ) { |
| 102 |
/** |
| 103 |
* Registers the real-time collaboration setting. |
| 104 |
*/ |
| 105 |
function gutenberg_register_real_time_collaboration_setting() { |
| 106 |
$option_name = 'wp_enable_real_time_collaboration'; |
| 107 |
|
| 108 |
register_setting( |
| 109 |
'writing', |
| 110 |
$option_name, |
| 111 |
array( |
| 112 |
'type' => 'boolean', |
| 113 |
'description' => __( 'Enable Real-Time Collaboration', 'gutenberg' ), |
| 114 |
'sanitize_callback' => 'rest_sanitize_boolean', |
| 115 |
'default' => false, |
| 116 |
'show_in_rest' => true, |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
add_settings_field( |
| 121 |
$option_name, |
| 122 |
__( 'Collaboration', 'gutenberg' ), |
| 123 |
function () use ( $option_name ) { |
| 124 |
$option_value = get_option( $option_name ); |
| 125 |
|
| 126 |
?> |
| 127 |
<label for="wp_enable_real_time_collaboration"> |
| 128 |
<input name="wp_enable_real_time_collaboration" type="checkbox" id="wp_enable_real_time_collaboration" value="1" <?php checked( '1', $option_value ); ?>/> |
| 129 |
<?php _e( 'Enable real-time collaboration', 'gutenberg' ); ?> |
| 130 |
</label> |
| 131 |
<?php |
| 132 |
}, |
| 133 |
'writing' |
| 134 |
); |
| 135 |
} |
| 136 |
add_action( 'admin_init', 'gutenberg_register_real_time_collaboration_setting' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Injects the real-time collaboration setting into a global variable. |
| 141 |
*/ |
| 142 |
function gutenberg_inject_real_time_collaboration_setting() { |
| 143 |
if ( get_option( 'wp_enable_real_time_collaboration' ) ) { |
| 144 |
wp_add_inline_script( |
| 145 |
'wp-core-data', |
| 146 |
'window._wpCollaborationEnabled = true;', |
| 147 |
'after' |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
add_action( 'admin_init', 'gutenberg_inject_real_time_collaboration_setting' ); |
| 152 |
add_filter( 'default_option_wp_enable_real_time_collaboration', '__return_true' ); |
| 153 |
|