From afc5d2e24bed71eb0c078c4ad7e45e519f47e109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Tue, 19 Sep 2023 13:31:17 +0200 Subject: [PATCH 1/2] fix update useIsSupportedMetaField hook to be context aware --- hooks/use-is-supported-meta-value/index.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) 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]; }; From 94010d5571f9e8d7b5f50876117b4bc08eb12ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Ka=CC=88gy?= Date: Tue, 19 Sep 2023 13:31:29 +0200 Subject: [PATCH 2/2] fix post meta sample block to use post context --- example/src/blocks/post-meta/block.json | 4 ++++ example/src/blocks/post-meta/edit.js | 9 ++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) 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