diff --git a/lib/compat/wordpress-6.6/rest-api.php b/lib/compat/wordpress-6.6/rest-api.php index f23f9df3771386..ef394ad483cef5 100644 --- a/lib/compat/wordpress-6.6/rest-api.php +++ b/lib/compat/wordpress-6.6/rest-api.php @@ -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; } diff --git a/packages/edit-site/src/components/page-templates/index.js b/packages/edit-site/src/components/page-templates/index.js index 49e454b50ae923..d5dae2efa8ccbe 100644 --- a/packages/edit-site/src/components/page-templates/index.js +++ b/packages/edit-site/src/components/page-templates/index.js @@ -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; @@ -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