Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate block context with inherited post type from template slug #65062

Merged
merged 2 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 42 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,59 @@ 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 } ),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only concern I may have. Sites with tons of postTypes could make the site editor slower.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be further investigated. We should see the results soon at https://codehealth.vercel.app/project/gutenberg.

In this PR on CI, I see the following metrics:
Screenshot 2024-09-11 at 11 52 32

firstBlock is substantially slower for both editors and needs to be double checked on trunk.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we have the following abstracted:

const postTypes = await apiFetch( {
	path: '/wp/v2/types?context=view',
} );

It's preloaded on the server, and it's enough to collect all post types and their slugs. Maybe it's exactly what getPostTypes does.

Copy link
Member

@gziolo gziolo Oct 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The performance regressions was confirmed by @SantosGuillamot in #65705 (comment). @WordPress/gutenberg-core, I would appreciate help during the beta phase to address this issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This issue should be better after merging this pull request. It is already reflected in the first results from https://codehealth.vercel.app/project/gutenberg/site-editor-firstBlock:

Screenshot 2024-10-17 at 14 27 28

};
},
[]
);
}, [] );
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 postTypesSlugs =
postTypes?.map( ( entity ) => entity.slug ) ||
[];
const match = post.slug.match(
`^single-(${ postTypesSlugs.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 +231,7 @@ export const ExperimentalEditorProvider = withRegistryProvider(
post.type,
rootLevelPost.type,
rootLevelPost.slug,
postTypes,
] );
const { id, type } = rootLevelPost;
const blockEditorSettings = useBlockEditorSettings(
Expand Down
Loading