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

Fix Meta boxes saving when they’re not present #67254

Merged
merged 4 commits into from
Dec 2, 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
13 changes: 7 additions & 6 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 @@ -414,6 +415,8 @@ function Layout( {
const { isZoomOut } = unlock( select( blockEditorStore ) );
const { getEditorMode, getRenderingMode } = select( editorStore );
const isRenderingPostOnly = getRenderingMode() === 'post-only';
const isNotDesignPostType =
! DESIGN_POST_TYPES.includes( currentPostType );
t-hamano marked this conversation as resolved.
Show resolved Hide resolved

return {
mode: getEditorMode(),
Expand All @@ -424,9 +427,7 @@ function Layout( {
!! select( blockEditorStore ).getBlockSelectionStart(),
showIconLabels: get( 'core', 'showIconLabels' ),
isDistractionFree: get( 'core', 'distractionFree' ),
showMetaBoxes:
! DESIGN_POST_TYPES.includes( currentPostType ) &&
! isZoomOut(),
showMetaBoxes: isNotDesignPostType && ! isZoomOut(),
isWelcomeGuideVisible: isFeatureActive( 'welcomeGuide' ),
templateId:
supportsTemplateMode &&
Expand All @@ -436,9 +437,7 @@ function Layout( {
? getTemplateId( currentPostType, currentPostId )
: null,
enablePaddingAppender:
! isZoomOut() &&
isRenderingPostOnly &&
! DESIGN_POST_TYPES.includes( currentPostType ),
! isZoomOut() && isRenderingPostOnly && isNotDesignPostType,
};
},
[
Expand All @@ -448,6 +447,8 @@ function Layout( {
settings.supportsTemplateMode,
]
);
useMetaBoxInitialization( hasActiveMetaboxes );

const [ paddingAppenderRef, paddingStyle ] = usePaddingAppender(
enablePaddingAppender
);
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(),
};
},
[ location ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dependency should not have been removed 🤦‍♂️.

const metaBoxes = useSelect(
( select ) =>
select( editPostStore ).getMetaBoxesPerLocation[ location ]
Copy link
Contributor Author

@stokesman stokesman Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a function call getMetaBoxesPerLocation( location ). Bummer this was missed as it seems to break hiding/showing of the meta boxes.

);

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