Skip to content

Commit

Permalink
Inherit post type from template slug
Browse files Browse the repository at this point in the history
  • Loading branch information
SantosGuillamot authored and artemiomorales committed Sep 5, 2024
1 parent 36a08ae commit 4b3f87e
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import { useEffect, useLayoutEffect, useMemo } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { EntityProvider, useEntityBlockEditor } from '@wordpress/core-data';
import {
EntityProvider,
useEntityBlockEditor,
store as coreStore,
} from '@wordpress/core-data';
import {
BlockEditorProvider,
BlockContextProvider,
Expand Down Expand Up @@ -48,7 +52,6 @@ const noop = () => {};
*/
const NON_CONTEXTUAL_POST_TYPES = [
'wp_block',
'wp_template',
'wp_navigation',
'wp_template_part',
];
Expand Down Expand Up @@ -161,31 +164,57 @@ export const ExperimentalEditorProvider = withRegistryProvider(
BlockEditorProviderComponent = ExperimentalBlockEditorProvider,
__unstableTemplate: template,
} ) => {
const { editorSettings, selection, isReady, mode } = useSelect(
( select ) => {
const { editorSettings, selection, isReady, mode, postTypes } =
useSelect( ( select ) => {
const {
getEditorSettings,
getEditorSelection,
getRenderingMode,
__unstableIsEditorReady,
} = select( editorStore );
const { getPostTypes } = select( coreStore );

return {
editorSettings: getEditorSettings(),
isReady: __unstableIsEditorReady(),
mode: getRenderingMode(),
selection: getEditorSelection(),
postTypes:
getPostTypes( { per_page: -1 } )?.map(
( entity ) => entity.slug
) || [],
};
},
[]
);
}, [] );
const shouldRenderTemplate = !! template && mode !== 'post-only';
const rootLevelPost = shouldRenderTemplate ? template : post;
const defaultBlockContext = useMemo( () => {
const postContext =
const postContext = {};
// If it is a template, try to inherit the post type from the slug.
if ( post.type === 'wp_template' ) {
if ( ! post.is_custom ) {
const [ kind ] = post.slug.split( '-' );
switch ( kind ) {
case 'page':
postContext.postType = 'page';
break;
case 'single':
// Infer the post type from the slug.
const match = post.slug.match(
`^single-(${ postTypes.join( '|' ) })(?:-.+)?$`
);
if ( match ) {
postContext.postType = match[ 1 ];
}
break;
}
}
} else if (
! NON_CONTEXTUAL_POST_TYPES.includes( rootLevelPost.type ) ||
shouldRenderTemplate
? { postId: post.id, postType: post.type }
: {};
) {
postContext.postId = post.id;
postContext.postType = post.type;
}

return {
...postContext,
Expand All @@ -200,6 +229,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
post.type,
rootLevelPost.type,
rootLevelPost.slug,
postTypes,
] );
const { id, type } = rootLevelPost;
const blockEditorSettings = useBlockEditorSettings(
Expand Down

0 comments on commit 4b3f87e

Please sign in to comment.