PluginProbe
Gutenberg / 23.3.2
Gutenberg v23.3.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-7.0 / meta-box-rtc-compat.php

meta-box-rtc-compat.php in Gutenberg 23.3.2, at lib/compat/wordpress-7.0/meta-box-rtc-compat.php

87 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @param array $wp_meta_boxes Global meta box state.
21 * @return array Unmodified meta box state.
22 */
23 function gutenberg_inject_rtc_compatible_meta_boxes( $wp_meta_boxes ) {
24 global $current_screen;
25
26 if ( ! $current_screen || ! wp_is_collaboration_enabled() ) {
27 return $wp_meta_boxes;
28 }
29
30 $screen_id = $current_screen->id;
31
32 if ( ! isset( $wp_meta_boxes[ $screen_id ] ) ) {
33 return $wp_meta_boxes;
34 }
35
36 $meta_boxes_per_location = array();
37
38 foreach ( $wp_meta_boxes[ $screen_id ] as $location => $priorities ) {
39 foreach ( $priorities as $priority_boxes ) {
40 foreach ( (array) $priority_boxes as $meta_box ) {
41 if ( false === $meta_box || ! $meta_box['title'] ) {
42 continue;
43 }
44
45 if ( empty( $meta_box['args']['__rtc_compatible_meta_box'] ) ) {
46 continue;
47 }
48
49 if ( ! isset( $meta_boxes_per_location[ $location ] ) ) {
50 $meta_boxes_per_location[ $location ] = array();
51 }
52
53 $meta_boxes_per_location[ $location ][] = array(
54 'id' => $meta_box['id'],
55 'title' => $meta_box['title'],
56 '__rtc_compatible' => true,
57 );
58 }
59 }
60 }
61
62 if ( ! empty( $meta_boxes_per_location ) ) {
63 // Meta boxes are registered during admin_head, which fires after
64 // admin_enqueue_scripts where the editor instance is created. This
65 // means the compatibility data cannot be added to editor settings
66 // directly. Instead, we inject an inline script that dispatches
67 // into the store once the block editor has finished loading. The
68 // existing entries are merged by id, so this re-flags meta boxes
69 // already registered by WordPress core.
70 $script = 'window._wpLoadBlockEditor.then( function() {
71 wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ' );
72 } );';
73
74 wp_add_inline_script( 'wp-edit-post', $script );
75
76 // If wp-edit-post is output earlier in <head>, the inline script
77 // needs to be manually printed. This mirrors the same fallback
78 // used by WordPress core for setAvailableMetaBoxesPerLocation.
79 if ( wp_script_is( 'wp-edit-post', 'done' ) ) {
80 printf( "<script>\n%s\n</script>\n", trim( $script ) );
81 }
82 }
83
84 return $wp_meta_boxes;
85 }
86 add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 );
87