Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 957a589

Browse files
Add descriptions for all WC templates and refactor Search template hierarchy loading logic (#6345)
* Refactor template title and description loading and search template hierarchy updating * Remove an unnecessary return * Fix template descriptions for custom templates * Fix template description typos * Move the template description loading logic to mirror the title's * Remove the descriptions filter and move the search hierarchy code back to a separate file * Restore the original order of the container registrations * Restore the product search template slug * Restore the Product Search Results slug as a constant Co-authored-by: Luigi <[email protected]>
1 parent 3e94906 commit 957a589

File tree

3 files changed

+65
-47
lines changed

3 files changed

+65
-47
lines changed

src/BlockTemplatesController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ public function add_block_templates( $query_result, $query, $template_type ) {
160160
if ( 'custom' !== $template_file->source ) {
161161
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
162162
} else {
163-
$template_file->title = BlockTemplateUtils::convert_slug_to_title( $template_file->slug );
164-
$query_result[] = $template_file;
163+
$template_file->title = BlockTemplateUtils::get_block_template_title( $template_file->slug );
164+
$template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug );
165+
$query_result[] = $template_file;
165166
continue;
166167
}
167168

@@ -386,5 +387,4 @@ function_exists( 'wc_get_page_id' ) &&
386387

387388
return $is_support;
388389
}
389-
390390
}

src/Templates/ProductSearchResultsTemplate.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
*/
99
class ProductSearchResultsTemplate {
1010

11-
const SLUG = 'product-search-results';
12-
const TITLE = 'Product Search Results';
13-
const DESCRIPTION = 'Template used to display search results for products.';
11+
const SLUG = 'product-search-results';
1412

1513
/**
1614
* Constructor.
@@ -24,7 +22,6 @@ public function __construct() {
2422
*/
2523
protected function init() {
2624
add_filter( 'search_template_hierarchy', array( $this, 'update_search_template_hierarchy' ), 10, 3 );
27-
add_filter( 'get_block_templates', array( $this, 'set_template_info' ) );
2825
}
2926

3027
/**
@@ -34,29 +31,8 @@ protected function init() {
3431
*/
3532
public function update_search_template_hierarchy( $templates ) {
3633
if ( ( is_search() && is_post_type_archive( 'product' ) ) && wc_current_theme_is_fse_theme() ) {
37-
return [ self::SLUG ];
34+
array_unshift( $templates, self::SLUG );
3835
}
3936
return $templates;
4037
}
41-
42-
/**
43-
* Update Product Search Template info.
44-
*
45-
* @param array $templates List of templates.
46-
*/
47-
public function set_template_info( $templates ) {
48-
return array_map(
49-
function ( $template ) {
50-
if ( self::SLUG !== $template->slug ) {
51-
return $template;
52-
}
53-
54-
$template->title = self::TITLE;
55-
$template->description = self::DESCRIPTION;
56-
57-
return $template;
58-
},
59-
$templates
60-
);
61-
}
6238
}

src/Utils/BlockTemplateUtils.php

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Automattic\WooCommerce\Blocks\Utils;
33

4+
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
45
use 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

Comments
 (0)