|
10 | 10 | * @package gutenberg |
11 | 11 | */ |
12 | 12 |
|
13 | | -if ( ! function_exists( 'gutenberg_inject_rtc_compatible_meta_boxes' ) ) { |
14 | | - /** |
15 | | - * Reads the __rtc_compatible_meta_box flag from registered meta boxes |
16 | | - * and injects the compatibility data into the block editor via inline script. |
17 | | - * |
18 | | - * Hooks into filter_block_editor_meta_boxes at a late priority so that it |
19 | | - * runs after any developer filters that add the flag to third-party meta boxes. |
20 | | - * |
21 | | - * @param array $wp_meta_boxes Global meta box state. |
22 | | - * @return array Unmodified meta box state. |
23 | | - */ |
24 | | - function gutenberg_inject_rtc_compatible_meta_boxes( $wp_meta_boxes ) { |
25 | | - global $current_screen; |
| 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; |
26 | 25 |
|
27 | | - if ( ! $current_screen || ! wp_is_collaboration_enabled() ) { |
28 | | - return $wp_meta_boxes; |
29 | | - } |
| 26 | + if ( ! $current_screen || ! wp_is_collaboration_enabled() ) { |
| 27 | + return $wp_meta_boxes; |
| 28 | + } |
30 | 29 |
|
31 | | - $screen_id = $current_screen->id; |
| 30 | + $screen_id = $current_screen->id; |
32 | 31 |
|
33 | | - if ( ! isset( $wp_meta_boxes[ $screen_id ] ) ) { |
34 | | - return $wp_meta_boxes; |
35 | | - } |
| 32 | + if ( ! isset( $wp_meta_boxes[ $screen_id ] ) ) { |
| 33 | + return $wp_meta_boxes; |
| 34 | + } |
36 | 35 |
|
37 | | - $rtc_compatible_ids = array(); |
| 36 | + $meta_boxes_per_location = array(); |
38 | 37 |
|
39 | | - foreach ( $wp_meta_boxes[ $screen_id ] as $priorities ) { |
40 | | - foreach ( $priorities as $priority_boxes ) { |
41 | | - foreach ( (array) $priority_boxes as $meta_box ) { |
42 | | - if ( false === $meta_box || ! $meta_box['title'] ) { |
43 | | - continue; |
44 | | - } |
| 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 | + } |
45 | 44 |
|
46 | | - if ( isset( $meta_box['args']['__rtc_compatible_meta_box'] ) |
47 | | - && $meta_box['args']['__rtc_compatible_meta_box'] ) { |
48 | | - $rtc_compatible_ids[] = $meta_box['id']; |
49 | | - } |
| 45 | + if ( empty( $meta_box['args']['__rtc_compatible_meta_box'] ) ) { |
| 46 | + continue; |
50 | 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 | + ); |
51 | 58 | } |
52 | 59 | } |
| 60 | + } |
53 | 61 |
|
54 | | - if ( ! empty( $rtc_compatible_ids ) ) { |
55 | | - // Meta boxes are registered during admin_head, which fires after |
56 | | - // admin_enqueue_scripts where the editor instance is created. This |
57 | | - // means the compatibility data cannot be added to editor settings |
58 | | - // directly. Instead, we inject an inline script that dispatches |
59 | | - // into the store once the block editor has finished loading. |
60 | | - $script = 'window._wpLoadBlockEditor.then( function() { |
61 | | - wp.data.dispatch( \'core/edit-post\' ).setRtcCompatibleMetaBoxIds( ' |
62 | | - . wp_json_encode( array_values( array_unique( $rtc_compatible_ids ) ) ) |
63 | | - . ' ); |
64 | | - } );'; |
| 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 | + } );'; |
65 | 73 |
|
66 | | - wp_add_inline_script( 'wp-edit-post', $script ); |
| 74 | + wp_add_inline_script( 'wp-edit-post', $script ); |
67 | 75 |
|
68 | | - // If wp-edit-post is output earlier in <head>, the inline script |
69 | | - // needs to be manually printed. This mirrors the same fallback |
70 | | - // used by WordPress core for setAvailableMetaBoxesPerLocation. |
71 | | - if ( wp_script_is( 'wp-edit-post', 'done' ) ) { |
72 | | - printf( "<script>\n%s\n</script>\n", trim( $script ) ); |
73 | | - } |
| 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 ) ); |
74 | 81 | } |
75 | | - |
76 | | - return $wp_meta_boxes; |
77 | 82 | } |
78 | 83 |
|
79 | | - add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 ); |
| 84 | + return $wp_meta_boxes; |
80 | 85 | } |
| 86 | +add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 ); |
0 commit comments