diff --git a/example/src/blocks/post-meta/block.json b/example/src/blocks/post-meta/block.json index 78afc849..12ad61ed 100644 --- a/example/src/blocks/post-meta/block.json +++ b/example/src/blocks/post-meta/block.json @@ -9,5 +9,9 @@ "default": "" } }, + "usesContext": [ + "postId", + "postType" + ], "editorScript": "file:./index.js" } \ No newline at end of file diff --git a/example/src/blocks/post-meta/edit.js b/example/src/blocks/post-meta/edit.js index ae9d8999..bf11bedd 100644 --- a/example/src/blocks/post-meta/edit.js +++ b/example/src/blocks/post-meta/edit.js @@ -5,10 +5,11 @@ import { store as blockEditorStore, } from '@wordpress/block-editor'; import { useSelect, useDispatch } from '@wordpress/data'; -import { PostMeta } from '@10up/block-components'; +import { PostMeta, PostContext } from '@10up/block-components'; export const BlockEdit = (props) => { - const { attributes, setAttributes, name } = props; + const { attributes, setAttributes, name, context } = props; + const { postId, postType } = context; const { metaKey } = attributes; const blockProps = useBlockProps(); @@ -46,7 +47,9 @@ export const BlockEdit = (props) => { return (
- + + +
); }; \ No newline at end of file diff --git a/hooks/use-is-supported-meta-value/index.js b/hooks/use-is-supported-meta-value/index.js index 97b07c29..b045e5ef 100644 --- a/hooks/use-is-supported-meta-value/index.js +++ b/hooks/use-is-supported-meta-value/index.js @@ -1,14 +1,11 @@ -import { useSelect } from '@wordpress/data'; -import { store as editorStore } from '@wordpress/editor'; +import { useEntityRecord } from '@wordpress/core-data'; +import { usePost } from '../use-post'; export const useIsSupportedMetaField = (metaKey) => { - return useSelect( - (select) => { - const meta = select(editorStore).getCurrentPostAttribute('meta'); - const supportedMetaKeys = Object.keys(meta || {}); - const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey); - return [!!isSupportedMetaField]; - }, - [metaKey], - ); + const { postId, postType } = usePost(); + const { record } = useEntityRecord('postType', postType, postId); + const { meta } = record || {}; + const supportedMetaKeys = Object.keys(meta || {}); + const isSupportedMetaField = supportedMetaKeys?.some((name) => name === metaKey); + return [!!isSupportedMetaField]; };