| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bootstraps synchronization (collaborative editing). |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Registers REST API routes for collaborative editing. |
| 10 |
*/ |
| 11 |
function gutenberg_rest_api_register_routes_for_collaborative_editing(): void { |
| 12 |
$sync_storage = new Gutenberg_Sync_Post_Meta_Storage(); |
| 13 |
$sync_storage->init(); |
| 14 |
|
| 15 |
$sse_sync_server = new Gutenberg_HTTP_Polling_Sync_Server( $sync_storage ); |
| 16 |
$sse_sync_server->init(); |
| 17 |
} |
| 18 |
add_action( 'init', 'gutenberg_rest_api_register_routes_for_collaborative_editing' ); |
| 19 |
|
| 20 |
/** |
| 21 |
* Registers post meta for persisting CRDT documents. |
| 22 |
*/ |
| 23 |
function gutenberg_rest_api_crdt_post_meta() { |
| 24 |
// This string must match WORDPRESS_META_KEY_FOR_CRDT_DOC_PERSISTENCE in @wordpress/sync. |
| 25 |
$persisted_crdt_post_meta_key = '_crdt_document'; |
| 26 |
|
| 27 |
register_meta( |
| 28 |
'post', |
| 29 |
$persisted_crdt_post_meta_key, |
| 30 |
array( |
| 31 |
'auth_callback' => function ( bool $_allowed, string $_meta_key, int $object_id, int $user_id ): bool { |
| 32 |
return user_can( $user_id, 'edit_post', $object_id ); |
| 33 |
}, |
| 34 |
// IMPORTANT: Revisions must be disabled because we always want to preserve |
| 35 |
// the latest persisted CRDT document, even when a revision is restored. |
| 36 |
// This ensures that we can continue to apply updates to a shared document |
| 37 |
// and peers can simply merge the restored revision like any other incoming |
| 38 |
// update. |
| 39 |
// |
| 40 |
// If we want to persist CRDT documents alongisde revisions in the |
| 41 |
// future, we should do so in a separate meta key. |
| 42 |
'revisions_enabled' => false, |
| 43 |
'show_in_rest' => true, |
| 44 |
'single' => true, |
| 45 |
'type' => 'string', |
| 46 |
) |
| 47 |
); |
| 48 |
} |
| 49 |
add_action( 'init', 'gutenberg_rest_api_crdt_post_meta' ); |
| 50 |
|