Skip to content

Commit

Permalink
getValue: implement logic to target item when it does not have postTy…
Browse files Browse the repository at this point in the history
…pe information
  • Loading branch information
oandregal committed May 29, 2024
1 parent c8fef78 commit 5345174
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/compat/wordpress-6.6/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ function gutenberg_rest_template_post_type_callback( $item ) {

$template_metadata = _get_block_template_file( 'wp_template', $item['slug'] );
if ( null === $template_metadata ) {
return array();
return null;
}

if ( isset( $template_metadata['postTypes'] ) ) {
return $template_metadata['postTypes'];
}

return array();
return null;
}
20 changes: 19 additions & 1 deletion packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ const POST_TYPES = {
page: __( 'Page' ),
};

const SPECIAL_TEMPLATES = {
single: [ 'post' ],
singular: [ 'post', 'page' ],
page: [ 'page' ],
};

export default function PageTemplates() {
const { params } = useLocation();
const { activeView = 'all', layout } = params;
Expand Down Expand Up @@ -326,8 +332,20 @@ export default function PageTemplates() {
{
header: __( 'Post types' ),
id: 'postTypes',
getValue: ( { item } ) => item.post_types,
getValue: ( { item } ) =>
// This logic would be used by the filter
// and replicates what we have in the server at
// https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-template-utils.php#L1077
//
// 1. Consider the post types defined by the item, if any
item.post_types ||
// 2. Otherwise, if a template is custom add it for any CPT.
( item.is_custom ? [ 'post', 'page' ] : item.post_types ) || // TODO: take any CPT.
// 3. Finally, also consider special templates.
( SPECIAL_TEMPLATES[ item.slug ] ?? item.post_types ),
render: ( { item } ) =>
// TODO: note that getValue returns a different value than render.
// getValue is used for filtering, and render is used for display.
item.post_types
?.map(
( postType ) => POST_TYPES[ postType ] || postType
Expand Down

0 comments on commit 5345174

Please sign in to comment.