Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WP 6.7] Fix meta boxes saving when they’re not present #67503

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/edit-post/src/components/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import useEditPostCommands from '../../commands/use-commands';
import { usePaddingAppender } from './use-padding-appender';
import { useShouldIframe } from './use-should-iframe';
import useNavigateToEntityRecord from '../../hooks/use-navigate-to-entity-record';
import { useMetaBoxInitialization } from '../meta-boxes/use-meta-box-initialization';

const { getLayoutStyles } = unlock( blockEditorPrivateApis );
const { useCommands } = unlock( coreCommandsPrivateApis );
Expand Down Expand Up @@ -462,6 +463,7 @@ function Layout( {
},
[ currentPostType, isEditingTemplate, settings.supportsTemplateMode ]
);
useMetaBoxInitialization( hasActiveMetaboxes );

// Set the right context for the command palette
const commandContext = hasBlockSelected
Expand Down
37 changes: 4 additions & 33 deletions packages/edit-post/src/components/meta-boxes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect, useRegistry } from '@wordpress/data';
import { useEffect } from '@wordpress/element';
import { store as editorStore } from '@wordpress/editor';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -13,38 +11,11 @@ import MetaBoxVisibility from './meta-box-visibility';
import { store as editPostStore } from '../../store';

export default function MetaBoxes( { location } ) {
const registry = useRegistry();
const { metaBoxes, areMetaBoxesInitialized, isEditorReady } = useSelect(
( select ) => {
const { __unstableIsEditorReady } = select( editorStore );
const {
getMetaBoxesPerLocation,
areMetaBoxesInitialized: _areMetaBoxesInitialized,
} = select( editPostStore );
return {
metaBoxes: getMetaBoxesPerLocation( location ),
areMetaBoxesInitialized: _areMetaBoxesInitialized(),
isEditorReady: __unstableIsEditorReady(),
};
},
const metaBoxes = useSelect(
( select ) =>
select( editPostStore ).getMetaBoxesPerLocation( location ),
[ location ]
);

const hasMetaBoxes = !! metaBoxes?.length;

// When editor is ready, initialize postboxes (wp core script) and metabox
// saving. This initializes all meta box locations, not just this specific
// one.
useEffect( () => {
if ( isEditorReady && hasMetaBoxes && ! areMetaBoxesInitialized ) {
registry.dispatch( editPostStore ).initializeMetaBoxes();
}
}, [ isEditorReady, hasMetaBoxes, areMetaBoxesInitialized ] );

if ( ! areMetaBoxesInitialized ) {
return null;
}

return (
<>
{ ( metaBoxes ?? [] ).map( ( { id } ) => (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { store as editorStore } from '@wordpress/editor';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as editPostStore } from '../../store';

/**
* Initializes WordPress `postboxes` script and the logic for saving meta boxes.
*
* @param { boolean } enabled
*/
export const useMetaBoxInitialization = ( enabled ) => {
const isEnabledAndEditorReady = useSelect(
( select ) =>
enabled && select( editorStore ).__unstableIsEditorReady(),
[ enabled ]
);
const { initializeMetaBoxes } = useDispatch( editPostStore );
// The effect has to rerun when the editor is ready because initializeMetaBoxes
// will noop until then.
useEffect( () => {
if ( isEnabledAndEditorReady ) {
initializeMetaBoxes();
}
}, [ isEnabledAndEditorReady, initializeMetaBoxes ] );
};
Loading