Skip to content

Commit d960ba8

Browse files
Mamadukaalecgeatchest-hamano
authored andcommitted
Post Editor: Store metaboxes RTC-compatible flag on location entries (#77361)
* Post Editor: Store metaboxes RTC-compatible flag on location entries * Simplify logic in useMetaBoxInitialization * Update backport changelog entry, remove function check Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: alecgeatches <alecgeatches@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org>
1 parent 464eac7 commit d960ba8

8 files changed

Lines changed: 94 additions & 149 deletions

File tree

backport-changelog/7.0/11566.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
https://github.com/WordPress/wordpress-develop/pull/11566
22

33
* https://github.com/WordPress/gutenberg/pull/76939
4+
* https://github.com/WordPress/gutenberg/pull/77361

docs/reference-guides/data/data-core-edit-post.md

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,6 @@ _Returns_
124124

125125
- `Object`: Preferences Object.
126126

127-
### getRtcCompatibleMetaBoxIds
128-
129-
Returns the list of meta box IDs marked as compatible with real-time collaboration via the add_meta_box() \_\_rtc_compatible_meta_box compatibility flag.
130-
131-
_Parameters_
132-
133-
- _state_ `Object`: Global application state.
134-
135-
_Returns_
136-
137-
- `string[]`: List of RTC-compatible meta box IDs.
138-
139127
### hasMetaBoxes
140128

141129
Returns true if the post is using Meta Boxes
@@ -484,14 +472,6 @@ _Parameters_
484472

485473
- _isOpen_ `boolean`: A boolean representing whether the list view should be opened or closed.
486474

487-
### setRtcCompatibleMetaBoxIds
488-
489-
Stores the IDs of meta boxes marked as compatible with real-time collaboration via the \_\_rtc_compatible_meta_box flag on the server.
490-
491-
_Parameters_
492-
493-
- _ids_ `string[]`: Meta box IDs that are RTC-compatible.
494-
495475
### showBlockTypes
496476

497477
Update the provided block types to be visible.

lib/compat/wordpress-7.0/meta-box-rtc-compat.php

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,77 @@
1010
* @package gutenberg
1111
*/
1212

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;
2625

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+
}
3029

31-
$screen_id = $current_screen->id;
30+
$screen_id = $current_screen->id;
3231

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+
}
3635

37-
$rtc_compatible_ids = array();
36+
$meta_boxes_per_location = array();
3837

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+
}
4544

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;
5047
}
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+
);
5158
}
5259
}
60+
}
5361

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+
} );';
6573

66-
wp_add_inline_script( 'wp-edit-post', $script );
74+
wp_add_inline_script( 'wp-edit-post', $script );
6775

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 ) );
7481
}
75-
76-
return $wp_meta_boxes;
7782
}
7883

79-
add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 );
84+
return $wp_meta_boxes;
8085
}
86+
add_filter( 'filter_block_editor_meta_boxes', 'gutenberg_inject_rtc_compatible_meta_boxes', 100 );

packages/edit-post/src/components/meta-boxes/test/use-meta-box-initialization.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ function createMockStores( {
3939
isEditorReady = true,
4040
isCollaborationEnabled = true,
4141
metaBoxes = [],
42-
rtcCompatibleIds = [],
4342
} = {} ) {
4443
return {
4544
'core/editor': {
@@ -66,7 +65,6 @@ function createMockStores( {
6665
},
6766
selectors: {
6867
getAllMetaBoxes: jest.fn( () => metaBoxes ),
69-
getRtcCompatibleMetaBoxIds: jest.fn( () => rtcCompatibleIds ),
7068
hasMetaBoxes: jest.fn( () => metaBoxes.length > 0 ),
7169
getActiveMetaBoxLocations: jest.fn( () =>
7270
metaBoxes.length > 0 ? [ 'normal' ] : []
@@ -113,10 +111,17 @@ describe( 'useMetaBoxInitialization', () => {
113111
it( 'does not disable collaboration when all metaboxes are rtcCompatible', () => {
114112
const mockStores = createMockStores( {
115113
metaBoxes: [
116-
{ id: 'my-metabox', title: 'My Meta Box' },
117-
{ id: 'another-metabox', title: 'Another' },
114+
{
115+
id: 'my-metabox',
116+
title: 'My Meta Box',
117+
__rtc_compatible: true,
118+
},
119+
{
120+
id: 'another-metabox',
121+
title: 'Another',
122+
__rtc_compatible: true,
123+
},
118124
],
119-
rtcCompatibleIds: [ 'my-metabox', 'another-metabox' ],
120125
} );
121126
const registry = createRegistry( mockStores );
122127

@@ -129,10 +134,13 @@ describe( 'useMetaBoxInitialization', () => {
129134
it( 'disables collaboration when some metaboxes lack rtcCompatible', () => {
130135
const mockStores = createMockStores( {
131136
metaBoxes: [
132-
{ id: 'compatible-metabox', title: 'Compatible' },
137+
{
138+
id: 'compatible-metabox',
139+
title: 'Compatible',
140+
__rtc_compatible: true,
141+
},
133142
{ id: 'incompatible-metabox', title: 'Incompatible' },
134143
],
135-
rtcCompatibleIds: [ 'compatible-metabox' ],
136144
} );
137145
const registry = createRegistry( mockStores );
138146

@@ -143,8 +151,13 @@ describe( 'useMetaBoxInitialization', () => {
143151

144152
it( 'does not disable collaboration when the only metabox is rtcCompatible', () => {
145153
const mockStores = createMockStores( {
146-
metaBoxes: [ { id: 'compatible-metabox', title: 'Compatible' } ],
147-
rtcCompatibleIds: [ 'compatible-metabox' ],
154+
metaBoxes: [
155+
{
156+
id: 'compatible-metabox',
157+
title: 'Compatible',
158+
__rtc_compatible: true,
159+
},
160+
],
148161
} );
149162
const registry = createRegistry( mockStores );
150163

packages/edit-post/src/components/meta-boxes/use-meta-box-initialization.js

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,18 @@ export const useMetaBoxInitialization = ( enabled ) => {
2121
const {
2222
isEnabledAndEditorReady,
2323
isCollaborationEnabled,
24-
hasMetaBoxes,
25-
allMetaBoxes,
26-
rtcCompatibleIds,
24+
hasIncompatibleMetaBoxes,
2725
} = useSelect(
2826
( select ) => ( {
2927
isEnabledAndEditorReady:
3028
enabled && select( editorStore ).__unstableIsEditorReady(),
3129
isCollaborationEnabled:
3230
select( editorStore ).isCollaborationEnabledForCurrentPost(),
33-
hasMetaBoxes: enabled
34-
? select( editPostStore ).hasMetaBoxes()
31+
hasIncompatibleMetaBoxes: enabled
32+
? select( editPostStore )
33+
.getAllMetaBoxes()
34+
.some( ( metaBox ) => ! metaBox.__rtc_compatible )
3535
: false,
36-
allMetaBoxes: enabled
37-
? select( editPostStore ).getAllMetaBoxes()
38-
: undefined,
39-
rtcCompatibleIds:
40-
select( editPostStore ).getRtcCompatibleMetaBoxIds(),
4136
} ),
4237
[ enabled ]
4338
);
@@ -50,26 +45,16 @@ export const useMetaBoxInitialization = ( enabled ) => {
5045
if ( isEnabledAndEditorReady ) {
5146
initializeMetaBoxes();
5247

53-
// Disable real-time collaboration when legacy meta boxes are detected.
54-
// Meta boxes marked with __rtc_compatible_meta_box on the server
55-
// have their IDs stored via setRtcCompatibleMetaBoxIds().
56-
if ( isCollaborationEnabled ) {
57-
const hasIncompatibleMetaBoxes = allMetaBoxes?.some(
58-
( metaBox ) => ! rtcCompatibleIds.includes( metaBox.id )
59-
);
60-
61-
if ( hasIncompatibleMetaBoxes ) {
62-
setCollaborationSupported( false );
63-
}
48+
// Disable real-time collaboration when incompatible meta boxes are detected.
49+
if ( isCollaborationEnabled && hasIncompatibleMetaBoxes ) {
50+
setCollaborationSupported( false );
6451
}
6552
}
6653
}, [
6754
isEnabledAndEditorReady,
6855
initializeMetaBoxes,
6956
isCollaborationEnabled,
7057
setCollaborationSupported,
71-
hasMetaBoxes,
72-
allMetaBoxes,
73-
rtcCompatibleIds,
58+
hasIncompatibleMetaBoxes,
7459
] );
7560
};

packages/edit-post/src/store/actions.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -277,19 +277,6 @@ export function setAvailableMetaBoxesPerLocation( metaBoxesPerLocation ) {
277277
};
278278
}
279279

280-
/**
281-
* Stores the IDs of meta boxes marked as compatible with real-time collaboration
282-
* via the __rtc_compatible_meta_box flag on the server.
283-
*
284-
* @param {string[]} ids Meta box IDs that are RTC-compatible.
285-
*/
286-
export function setRtcCompatibleMetaBoxIds( ids ) {
287-
return {
288-
type: 'SET_RTC_COMPATIBLE_META_BOX_IDS',
289-
ids,
290-
};
291-
}
292-
293280
/**
294281
* Update a metabox.
295282
*/

packages/edit-post/src/store/reducer.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ function mergeMetaboxes( metaboxes = [], newMetaboxes ) {
3232
( box ) => box.id === metabox.id
3333
);
3434
if ( existing !== -1 ) {
35-
mergedMetaboxes[ existing ] = metabox;
35+
mergedMetaboxes[ existing ] = {
36+
...mergedMetaboxes[ existing ],
37+
...metabox,
38+
};
3639
} else {
3740
mergedMetaboxes.push( metabox );
3841
}
@@ -83,28 +86,10 @@ function metaBoxesInitialized( state = false, action ) {
8386
return state;
8487
}
8588

86-
/**
87-
* Reducer tracking meta box IDs marked as compatible with real-time collaboration
88-
* via the add_meta_box() __rtc_compatible_meta_box compatibility flag.
89-
*
90-
* @param {string[]} state Previous state.
91-
* @param {Object} action Action Object.
92-
*
93-
* @return {string[]} Updated state.
94-
*/
95-
export function rtcCompatibleMetaBoxIds( state = [], action ) {
96-
switch ( action.type ) {
97-
case 'SET_RTC_COMPATIBLE_META_BOX_IDS':
98-
return action.ids;
99-
}
100-
return state;
101-
}
102-
10389
const metaBoxes = combineReducers( {
10490
isSaving: isSavingMetaBoxes,
10591
locations: metaBoxLocations,
10692
initialized: metaBoxesInitialized,
107-
rtcCompatibleIds: rtcCompatibleMetaBoxIds,
10893
} );
10994

11095
export default combineReducers( {

packages/edit-post/src/store/selectors.js

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -424,18 +424,6 @@ export const getAllMetaBoxes = createSelector(
424424
( state ) => [ state.metaBoxes.locations ]
425425
);
426426

427-
/**
428-
* Returns the list of meta box IDs marked as compatible with real-time
429-
* collaboration via the add_meta_box() __rtc_compatible_meta_box compatibility flag.
430-
*
431-
* @param {Object} state Global application state.
432-
*
433-
* @return {string[]} List of RTC-compatible meta box IDs.
434-
*/
435-
export function getRtcCompatibleMetaBoxIds( state ) {
436-
return state.metaBoxes.rtcCompatibleIds;
437-
}
438-
439427
/**
440428
* Returns true if the post is using Meta Boxes
441429
*

0 commit comments

Comments
 (0)