Skip to content

Commit

Permalink
Fix title and description for single templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jun 22, 2022
1 parent c9ca559 commit 62953df
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion lib/compat/wordpress-6.1/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,42 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
$block_template->id = $id;
$block_template->slug = $slug;
$default_template_types = get_default_block_template_types();
if ( array_key_exists( $slug, $default_template_types ) ) {

$slug_parts = explode( '-', $slug, 3 );
if ( count( $slug_parts ) > 1 ) {
if ( 'single' === $slug_parts [0] ) {
// Get CPT labels
$post_type = get_post_type_object( $slug_parts[1] );
$labels = $post_type->labels;

if ( count( $slug_parts ) > 2 ) {
// Now we look for the CPT with slug as defined in $slug_parts[2]
$post = get_page_by_path( $slug_parts[2], OBJECT, $slug_parts[1] );
$block_template->title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the singular name of a post type and %2$s is the name of the post, e.g. "Post: Hello, WordPress"
__( '%1$s: %2$s' ),
$labels->singular_name,
$post->post_title
);
$block_template->description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Post: Hello, WordPress"
__( 'Template for %1$s' ),
$block_template->title
);
} else {
$block_template->title = sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Single item: %s' ),
$labels->singular_name
);
$block_template->description = sprintf(
// translators: %s: Name of the post type e.g: "Post".
__( 'Displays a single item: %s.' ),
$labels->singular_name
);
}
}
} elseif ( array_key_exists( $slug, $default_template_types ) ) {
$block_template->title = $default_template_types[ $slug ]['title'];
$block_template->description = $default_template_types[ $slug ]['description'];
}
Expand Down

0 comments on commit 62953df

Please sign in to comment.