From 0d8bdb78722544da62ef9cfa3d65de5f26ad8c81 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Thu, 17 Oct 2024 09:42:44 +0200 Subject: [PATCH] Block Bindings: Use `getEntityConfig` instead of `getPostTypes` to get available slugs (#66101) * Add slug prop to postType entity config * Use `getEntitiesConfig` instead of `getPostTypes` * Only fetch postTypes in templates * Add `post.type` to `useSelect` dependencies * Use `name` instead of `slug` * Rename variable to `postTypeEntities` Co-authored-by: SantosGuillamot Co-authored-by: youknowriad Co-authored-by: draganescu Co-authored-by: gziolo --- .../editor/src/components/provider/index.js | 53 +++++++++++-------- 1 file changed, 30 insertions(+), 23 deletions(-) diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 51b54979cb34aa..87fea30e31cdb9 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -164,40 +164,47 @@ export const ExperimentalEditorProvider = withRegistryProvider( BlockEditorProviderComponent = ExperimentalBlockEditorProvider, __unstableTemplate: template, } ) => { - const { editorSettings, selection, isReady, mode, postTypes } = - useSelect( ( select ) => { - const { - getEditorSettings, - getEditorSelection, - getRenderingMode, - __unstableIsEditorReady, - } = select( editorStore ); - const { getPostTypes } = select( coreStore ); + const { editorSettings, selection, isReady, mode, postTypeEntities } = + useSelect( + ( select ) => { + const { + getEditorSettings, + getEditorSelection, + getRenderingMode, + __unstableIsEditorReady, + } = select( editorStore ); + const { getEntitiesConfig } = select( coreStore ); - return { - editorSettings: getEditorSettings(), - isReady: __unstableIsEditorReady(), - mode: getRenderingMode(), - selection: getEditorSelection(), - postTypes: getPostTypes( { per_page: -1 } ), - }; - }, [] ); + return { + editorSettings: getEditorSettings(), + isReady: __unstableIsEditorReady(), + mode: getRenderingMode(), + selection: getEditorSelection(), + postTypeEntities: + post.type === 'wp_template' + ? getEntitiesConfig( 'postType' ) + : null, + }; + }, + [ post.type ] + ); const shouldRenderTemplate = !! template && mode !== 'post-only'; const rootLevelPost = shouldRenderTemplate ? template : post; const defaultBlockContext = useMemo( () => { const postContext = {}; - // If it is a template, try to inherit the post type from the slug. + // If it is a template, try to inherit the post type from the name. if ( post.type === 'wp_template' ) { if ( post.slug === 'page' ) { postContext.postType = 'page'; } else if ( post.slug === 'single' ) { postContext.postType = 'post'; } else if ( post.slug.split( '-' )[ 0 ] === 'single' ) { - // If the slug is single-{postType}, infer the post type from the slug. - const postTypesSlugs = - postTypes?.map( ( entity ) => entity.slug ) || []; + // If the slug is single-{postType}, infer the post type from the name. + const postTypeNames = + postTypeEntities?.map( ( entity ) => entity.name ) || + []; const match = post.slug.match( - `^single-(${ postTypesSlugs.join( '|' ) })(?:-.+)?$` + `^single-(${ postTypeNames.join( '|' ) })(?:-.+)?$` ); if ( match ) { postContext.postType = match[ 1 ]; @@ -225,7 +232,7 @@ export const ExperimentalEditorProvider = withRegistryProvider( post.slug, rootLevelPost.type, rootLevelPost.slug, - postTypes, + postTypeEntities, ] ); const { id, type } = rootLevelPost; const blockEditorSettings = useBlockEditorSettings(