-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hook for initialization of meta boxes
- Loading branch information
Showing
2 changed files
with
53 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/edit-post/src/components/meta-boxes/use-meta-box-initialization.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 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 postboxes (wp core script) and meta box saving for all meta box locations. | ||
* | ||
* @param { boolean } enabled | ||
*/ | ||
export default ( enabled ) => { | ||
const { areInitialized, isEditorReady, hasAny } = useSelect( | ||
( select ) => { | ||
if ( ! enabled ) { | ||
return {}; | ||
} | ||
const { __unstableIsEditorReady } = select( editorStore ); | ||
const { areMetaBoxesInitialized, getMetaBoxesPerLocation } = | ||
select( editPostStore ); | ||
return { | ||
areInitialized: areMetaBoxesInitialized(), | ||
isEditorReady: __unstableIsEditorReady(), | ||
hasAny: | ||
getMetaBoxesPerLocation( 'normal' ).length > 0 || | ||
getMetaBoxesPerLocation( 'advanced' ).length > 0 || | ||
getMetaBoxesPerLocation( 'side' ).length > 0, | ||
}; | ||
}, | ||
[ enabled ] | ||
); | ||
const { initializeMetaBoxes } = useDispatch( editPostStore ); | ||
useEffect( () => { | ||
if ( isEditorReady && hasAny && ! areInitialized ) { | ||
initializeMetaBoxes(); | ||
} | ||
}, [ isEditorReady, hasAny, areInitialized, initializeMetaBoxes ] ); | ||
}; |