| 1 |
<?php |
| 2 |
/** |
| 3 |
* Adds support for the __rtc_compatible_meta_box flag in add_meta_box(). |
| 4 |
* |
| 5 |
* Plugin authors can mark their meta boxes as compatible with real-time |
| 6 |
* collaboration by passing '__rtc_compatible_meta_box' => true in the |
| 7 |
* $callback_args parameter of add_meta_box(). Users can also add this |
| 8 |
* flag to third-party meta boxes via the filter_block_editor_meta_boxes hook. |
| 9 |
* |
| 10 |
* @package gutenberg |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Reads the __rtc_compatible_meta_box flag from registered meta boxes |
| 15 |
* and injects the compatibility data into the block editor via inline script. |
| 16 |
* |
| 17 |
* Hooks into filter_block_editor_meta_boxes at a late priority so that it |
| 18 |
* runs after any developer filters that add the flag to third-party meta boxes. |
| 19 |
* |
| 20 |
* @global WP_Screen $current_screen WordPress current screen object. |
| 21 |
* |
| 22 |
* @param array $wp_meta_boxes Global meta box state. |
| 23 |
* @return array Unmodified meta box state. |
| 24 |
*/ |
| 25 |
function gutenberg_inject_rtc_compatible_meta_boxes( $wp_meta_boxes ) { |
| 26 |
global $current_screen; |
| 27 |
|
| 28 |
if ( ! $current_screen || ! wp_is_collaboration_enabled() ) { |
| 29 |
return $wp_meta_boxes; |
| 30 |
} |
| 31 |
|
| 32 |
$screen_id = $current_screen->id; |
| 33 |
|
| 34 |
if ( ! isset( $wp_meta_boxes[ $screen_id ] ) ) { |
| 35 |
return $wp_meta_boxes; |
| 36 |
} |
| 37 |
|
| 38 |
$meta_boxes_per_location = array(); |
| 39 |
|
| 40 |
foreach ( $wp_meta_boxes[ $screen_id ] as $location => $priorities ) { |
| 41 |
foreach ( $priorities as $priority_boxes ) { |
| 42 |
foreach ( (array) $priority_boxes as $meta_box ) { |
| 43 |
if ( false === $meta_box || ! $meta_box['title'] ) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
|
| 47 |
if ( empty( $meta_box['args']['__rtc_compatible_meta_box'] ) ) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! isset( $meta_boxes_per_location[ $location ] ) ) { |
| 52 |
$meta_boxes_per_location[ $location ] = array(); |
| 53 |
} |
| 54 |
|
| 55 |
$meta_boxes_per_location[ $location ][] = array( |
| 56 |
'id' => $meta_box['id'], |
| 57 |
'title' => $meta_box['title'], |
| 58 |
'__rtc_compatible' => true, |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! empty( $meta_boxes_per_location ) ) { |
| 65 |
// Meta boxes are registered during admin_head, which fires after |
| 66 |
// admin_enqueue_scripts where the editor instance is created. This |
| 67 |
// means the compatibility data cannot be added to editor settings |
| 68 |
// directly. Instead, we inject an inline script that dispatches |
| 69 |
// into the store once the block editor has finished loading. The |
| 70 |
// existing entries are merged by id, so this re-flags meta boxes |
| 71 |
// already registered by WordPress core. |
| 72 |
$script = 'window._wpLoadBlockEditor.then( function() { |
| 73 |
wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ' ); |
| 74 |
} );'; |
| 75 |
|
| 76 |
wp_add_inline_script( 'wp-edit-post', $script ); |
| 77 |
|
| 78 |
// If wp-edit-post is output earlier in <head>, the inline script |
| 79 |
// needs to be manually printed. This mirrors the same fallback |
| 80 |
// used by WordPress core for setAvailableMetaBoxesPerLocation. |
| 81 |
if ( wp_script_is( 'wp-edit-post', 'done' ) ) { |
| 82 |
printf( "<script>\n%s\n</script>\n", trim( $script ) ); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $wp_meta_boxes; |
| 87 |
} |
| 88 |
add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 ); |
| 89 |
|