Skip to content

Commit

Permalink
Update: Make template names and descriptions of author, single, tag, …
Browse files Browse the repository at this point in the history
…category, and taxonomy dynamic.
  • Loading branch information
jorgefilipecosta committed Sep 6, 2022
1 parent 0ea7c4a commit 4b5cf9c
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 88 deletions.
187 changes: 187 additions & 0 deletions lib/compat/wordpress-6.1/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,115 @@ function gutenberg_get_block_template( $id, $template_type = 'wp_template' ) {
return apply_filters( 'get_block_template', $block_template, $id, $template_type );
}

/**
* Builds the title and description of a post specific template based on the underlying post referenced.
*
* @access private
* @internal
*
* @param string $post_type Post type e.g:page, post, product.
* @param string $slug Slug of the post e.g:a-story-about-shoes.
* @param Gutenberg_Block_Template $template Template whose description and title are going to be computed.
*/
function _gutenberg_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, &$template ) {
$post_type_object = get_post_type_object( $post_type );

$posts = get_posts(
array(
'name' => $slug,
'post_type' => $post_type,
)
);
if ( empty( $posts ) ) {
return;
}

$post_title = $posts[0]->post_title;

$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. "Page: Hello".
__( '%1$s: %2$s', 'gutenberg' ),
$post_type_object->labels->singular_name,
$post_title
);
$template->description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Page: Hello".
__( 'Template for %1$s', 'gutenberg' ),
$post_title
);

$posts_with_same_title = get_posts(
array(
'title' => $post_title,
'post_type' => $post_type,
'post_status' => 'publish',
)
);
if ( count( $posts_with_same_title ) > 1 ) {
$template->title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the post type, e.g. "Project: Hello (project_type)".
__( '%1$s (%2$s)', 'gutenberg' ),
$template->title,
$slug
);
}
}

/**
* Builds the title and description of a taxonomy specific template based on the underlying entity referenced.
*
* @access private
* @internal
*
* @param string $taxonomy Idenfitier of the taxonomy e.g:category.
* @param string $slug Slug of the term e.g:shoes.
* @param Gutenberg_Block_Template $template Template whose description and title are going to be computed.
*/
function _gutenberg_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, &$template ) {
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'slug' => $slug,
)
);
if ( empty( $terms ) ) {
return;
}

$term_title = $terms[0]->name;

$taxonomy_object = get_taxonomy( $taxonomy );

$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 taxonomy and %2$s is the name of the term, e.g. "Category: shoes".
__( '%1$s: %2$s', 'gutenberg' ),
$taxonomy_object->labels->singular_name,
$term_title
);
$template->description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Category: shoes".
__( 'Template for %1$s', 'gutenberg' ),
$term_title
);

$terms_with_same_title = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'name' => $term_title,
)
);
if ( count( $terms_with_same_title ) > 1 ) {
$template->title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the taxonomy, e.g. "Category: shoes (product_tag)".
__( '%1$s (%2$s)', 'gutenberg' ),
$template->title,
$slug
);
}
}

/**
* Build a unified template object based a post Object.
*
Expand Down Expand Up @@ -305,6 +414,84 @@ function gutenberg_build_block_template_result_from_post( $post ) {
$template->area = $type_terms[0]->name;
}
}
if ( 'wp_template' === $post->post_type && empty( $template->description ) && ( empty( $template->title ) || $template->title === $template->slug ) ) {
$matches = array();
if ( preg_match( '/(author|page|single|tag|category|taxonomy)-(.+)/', $template->slug, $matches ) ) {
$type = $matches[1];
$slug_remaining = $matches[2];
switch ( $type ) {
case 'author':
$nice_name = $slug_remaining;
$users = get_users(
array(
'capability' => 'publish_posts',
'search' => $nice_name,
'search_columns' => array( 'user_nicename' ),
'fields' => 'display_name',
)
);

if ( ! empty( $users ) ) {
$author_name = $users[0];

$template->title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %s is the author's name, e.g. "Author: Jane Doe".
__( 'Author: %s', 'gutenberg' ),
$author_name
);
$template->description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Author: Jane Doe".
__( 'Template for %1$s', 'gutenberg' ),
$author_name
);

$users_with_same_name = get_users(
array(
'capability' => 'publish_posts',
'search' => $author_name,
'search_columns' => array( 'display_name' ),
'fields' => 'display_name',
)
);
if ( count( $users_with_same_name ) > 1 ) {
$template->title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title of an author template and %2$s is the author nice name of the author, e.g. "Author: Jorge (jorge-costa)".
__( '%1$s (%2$s)', 'gutenberg' ),
$template->title,
$nice_name
);
}
}
break;
case 'page':
_gutenberg_build_title_and_description_for_single_post_type_block_template( 'page', $slug_remaining, $template );
break;
case 'single':
$single_matches = array();
$regex = '/(' . implode( '|', array_values( get_post_types() ) ) . ')-(.+)/';
if ( preg_match( $regex, $slug_remaining, $single_matches ) ) {
$post_type = $single_matches[1];
$slug = $single_matches[2];
_gutenberg_build_title_and_description_for_single_post_type_block_template( $post_type, $slug, $template );
}
break;
case 'tag':
_gutenberg_build_title_and_description_for_taxonomy_block_template( 'post_tag', $slug_remaining, $template );
break;
case 'category':
_gutenberg_build_title_and_description_for_taxonomy_block_template( 'category', $slug_remaining, $template );
break;
case 'taxonomy':
$taxonomy_matches = array();
if ( preg_match( '/(' . implode( '|', array_values( get_taxonomies() ) ) . ')-(.+)/', $slug_remaining, $taxonomy_matches ) ) {
$taxonomy = $taxonomy_matches[1];
$slug = $taxonomy_matches[2];
_gutenberg_build_title_and_description_for_taxonomy_block_template( $taxonomy, $slug, $template );
}
break;
}
}
}
return $template;
}

Expand Down
97 changes: 9 additions & 88 deletions packages/edit-site/src/components/add-new-template/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,29 +253,10 @@ export const usePostTypeMenuItems = ( onClickMenuItem ) => {
};
},
getSpecificTemplate: ( suggestion ) => {
let 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. "Page: Hello".
__( '%1$s: %2$s' ),
labels.singular_name,
suggestion.name
);
const description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Page: Hello"
__( 'Template for %1$s' ),
title
);
if ( _needsUniqueIdentifier ) {
title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the post type, e.g. "Project: Hello (project_type)"
__( '%1$s (%2$s)' ),
title,
slug
);
}
const templateSlug = `${ templatePrefixes[ slug ] }-${ suggestion.slug }`;
return {
title,
description,
slug: `${ templatePrefixes[ slug ] }-${ suggestion.slug }`,
title: templateSlug,
slug: templateSlug,
templatePrefix: templatePrefixes[ slug ],
};
},
Expand Down Expand Up @@ -417,29 +398,10 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
};
},
getSpecificTemplate: ( suggestion ) => {
let 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 taxonomy and %2$s is the name of the term, e.g. "Category: shoes".
__( '%1$s: %2$s' ),
labels.singular_name,
suggestion.name
);
const description = sprintf(
// translators: Represents the description of a user's custom template in the Site Editor, e.g. "Template for Category: shoes"
__( 'Template for %1$s' ),
title
);
if ( _needsUniqueIdentifier ) {
title = sprintf(
// translators: Represents the title of a user's custom template in the Site Editor, where %1$s is the template title and %2$s is the slug of the taxonomy, e.g. "Category: shoes (product_tag)"
__( '%1$s (%2$s)' ),
title,
slug
);
}
const templateSlug = `${ templatePrefixes[ slug ] }-${ suggestion.slug }`;
return {
title,
description,
slug: `${ templatePrefixes[ slug ] }-${ suggestion.slug }`,
title: templateSlug,
slug: templateSlug,
templatePrefix: templatePrefixes[ slug ],
};
},
Expand Down Expand Up @@ -480,26 +442,6 @@ export const useTaxonomiesMenuItems = ( onClickMenuItem ) => {
return taxonomiesMenuItems;
};

function useAuthorNeedsUniqueIndentifier() {
const authors = useSelect(
( select ) =>
select( coreStore ).getUsers( { who: 'authors', per_page: -1 } ),
[]
);
const authorsCountByName = useMemo( () => {
return ( authors || [] ).reduce( ( authorsCount, { name } ) => {
authorsCount[ name ] = ( authorsCount[ name ] || 0 ) + 1;
return authorsCount;
}, {} );
}, [ authors ] );
return useCallback(
( name ) => {
return authorsCountByName[ name ] > 1;
},
[ authorsCountByName ]
);
}

const USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX = { user: 'author' };
const USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS = { user: { who: 'authors' } };
export function useAuthorMenuItem( onClickMenuItem ) {
Expand All @@ -510,7 +452,6 @@ export function useAuthorMenuItem( onClickMenuItem ) {
USE_AUTHOR_MENU_ITEM_TEMPLATE_PREFIX,
USE_AUTHOR_MENU_ITEM_QUERY_PARAMETERS
);
const authorNeedsUniqueId = useAuthorNeedsUniqueIndentifier();
let authorMenuItem = defaultTemplateTypes?.find(
( { slug } ) => slug === 'author'
);
Expand Down Expand Up @@ -542,30 +483,10 @@ export function useAuthorMenuItem( onClickMenuItem ) {
};
},
getSpecificTemplate: ( suggestion ) => {
const needsUniqueId = authorNeedsUniqueId(
suggestion.name
);
const title = needsUniqueId
? sprintf(
// translators: %1$s: Represents the name of an author e.g: "Jorge", %2$s: Represents the slug of an author e.g: "author-jorge-slug".
__( 'Author: %1$s (%2$s)' ),
suggestion.name,
suggestion.slug
)
: sprintf(
// translators: %s: Represents the name of an author e.g: "Jorge".
__( 'Author: %s' ),
suggestion.name
);
const description = sprintf(
// translators: %s: Represents the name of an author e.g: "Jorge".
__( 'Template for Author: %s' ),
suggestion.name
);
const templateSlug = `author-${ suggestion.slug }`;
return {
title,
description,
slug: `author-${ suggestion.slug }`,
title: templateSlug,
slug: templateSlug,
templatePrefix: 'author',
};
},
Expand Down

0 comments on commit 4b5cf9c

Please sign in to comment.