11<?php
22namespace Automattic \WooCommerce \Blocks \Utils ;
33
4+ use Automattic \WooCommerce \Blocks \Templates \ProductSearchResultsTemplate ;
45use Automattic \WooCommerce \Blocks \Domain \Services \FeatureGating ;
56
67/**
@@ -197,7 +198,8 @@ public static function build_template_result_from_file( $template_file, $templat
197198 $ template ->source = $ template_file ->source ? $ template_file ->source : 'plugin ' ;
198199 $ template ->slug = $ template_file ->slug ;
199200 $ template ->type = $ template_type ;
200- $ template ->title = ! empty ( $ template_file ->title ) ? $ template_file ->title : self ::convert_slug_to_title ( $ template_file ->slug );
201+ $ template ->title = ! empty ( $ template_file ->title ) ? $ template_file ->title : self ::get_block_template_title ( $ template_file ->slug );
202+ $ template ->description = ! empty ( $ template_file ->description ) ? $ template_file ->description : self ::get_block_template_description ( $ template_file ->slug );
201203 $ template ->status = 'publish ' ;
202204 $ template ->has_theme_file = true ;
203205 $ template ->origin = $ template_file ->source ;
@@ -228,8 +230,8 @@ public static function create_new_block_template_object( $template_file, $templa
228230 'theme ' => $ template_is_from_theme ? $ theme_name : self ::PLUGIN_SLUG ,
229231 // Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
230232 'source ' => $ template_is_from_theme ? 'theme ' : 'plugin ' ,
231- 'title ' => self ::convert_slug_to_title ( $ template_slug ),
232- 'description ' => '' ,
233+ 'title ' => self ::get_block_template_title ( $ template_slug ),
234+ 'description ' => self :: get_block_template_description ( $ template_slug ) ,
233235 'post_types ' => array (), // Don't appear in any Edit Post template selector dropdown.
234236 );
235237
@@ -255,27 +257,67 @@ public static function get_template_paths( $base_directory ) {
255257 }
256258
257259 /**
258- * Converts template slugs into readable titles.
260+ * Returns template titles.
259261 *
260262 * @param string $template_slug The templates slug (e.g. single-product).
261- * @return string Human friendly title converted from the slug .
263+ * @return string Human friendly title.
262264 */
263- public static function convert_slug_to_title ( $ template_slug ) {
264- switch ( $ template_slug ) {
265- case 'single-product ' :
266- return __ ( 'Single Product ' , 'woo-gutenberg-products-block ' );
267- case 'archive-product ' :
268- return __ ( 'Product Catalog ' , 'woo-gutenberg-products-block ' );
269- case 'taxonomy-product_cat ' :
270- return __ ( 'Products by Category ' , 'woo-gutenberg-products-block ' );
271- case 'taxonomy-product_tag ' :
272- return __ ( 'Products by Tag ' , 'woo-gutenberg-products-block ' );
273- default :
274- // Replace all hyphens and underscores with spaces.
275- return ucwords ( preg_replace ( '/[\-_]/ ' , ' ' , $ template_slug ) );
265+ public static function get_block_template_title ( $ template_slug ) {
266+ $ plugin_template_types = self ::get_plugin_block_template_types ();
267+ if ( isset ( $ plugin_template_types [ $ template_slug ] ) ) {
268+ return $ plugin_template_types [ $ template_slug ]['title ' ];
269+ } else {
270+ // Human friendly title converted from the slug.
271+ return ucwords ( preg_replace ( '/[\-_]/ ' , ' ' , $ template_slug ) );
276272 }
277273 }
278274
275+ /**
276+ * Returns template descriptions.
277+ *
278+ * @param string $template_slug The templates slug (e.g. single-product).
279+ * @return string Template description.
280+ */
281+ public static function get_block_template_description ( $ template_slug ) {
282+ $ plugin_template_types = self ::get_plugin_block_template_types ();
283+ if ( isset ( $ plugin_template_types [ $ template_slug ] ) ) {
284+ return $ plugin_template_types [ $ template_slug ]['description ' ];
285+ }
286+ }
287+
288+ /**
289+ * Returns a filtered list of plugin template types, containing their
290+ * localized titles and descriptions.
291+ *
292+ * @return array The plugin template types.
293+ */
294+ public static function get_plugin_block_template_types () {
295+ $ plugin_template_types = array (
296+ 'single-product ' => array (
297+ 'title ' => _x ( 'Single Product ' , 'Template name ' , 'woo-gutenberg-products-block ' ),
298+ 'description ' => __ ( 'Template used to display the single product. ' , 'woo-gutenberg-products-block ' ),
299+ ),
300+ 'archive-product ' => array (
301+ 'title ' => _x ( 'Product Catalog ' , 'Template name ' , 'woo-gutenberg-products-block ' ),
302+ 'description ' => __ ( 'Template used to display products. ' , 'woo-gutenberg-products-block ' ),
303+ ),
304+ 'taxonomy-product_cat ' => array (
305+ 'title ' => _x ( 'Products by Category ' , 'Template name ' , 'woo-gutenberg-products-block ' ),
306+ 'description ' => __ ( 'Template used to display products by category. ' , 'woo-gutenberg-products-block ' ),
307+ ),
308+ 'taxonomy-product_tag ' => array (
309+ 'title ' => _x ( 'Products by Tag ' , 'Template name ' , 'woo-gutenberg-products-block ' ),
310+ 'description ' => __ ( 'Template used to display products by tag. ' , 'woo-gutenberg-products-block ' ),
311+ ),
312+ ProductSearchResultsTemplate::SLUG => array (
313+ 'title ' => _x ( 'Product Search Results ' , 'Template name ' , 'woo-gutenberg-products-block ' ),
314+ 'description ' => __ ( 'Template used to display search results for products. ' , 'woo-gutenberg-products-block ' ),
315+ ),
316+ );
317+
318+ return $ plugin_template_types ;
319+ }
320+
279321 /**
280322 * Converts template paths into a slug
281323 *
0 commit comments