Add descriptions for all WC templates and refactor Search template hierarchy loading logic (https://github.com/woocommerce/woocommerce-blocks/pull/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 <gigitux@gmail.com>
This commit is contained in:
Daniel Dudzic 2022-06-20 15:49:18 +02:00 committed by GitHub
parent 4d9780269f
commit 8f00447f2d
3 changed files with 65 additions and 47 deletions

View File

@ -160,8 +160,9 @@ class BlockTemplatesController {
if ( 'custom' !== $template_file->source ) {
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
} else {
$template_file->title = BlockTemplateUtils::convert_slug_to_title( $template_file->slug );
$query_result[] = $template_file;
$template_file->title = BlockTemplateUtils::get_block_template_title( $template_file->slug );
$template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug );
$query_result[] = $template_file;
continue;
}
@ -386,5 +387,4 @@ class BlockTemplatesController {
return $is_support;
}
}

View File

@ -8,9 +8,7 @@ namespace Automattic\WooCommerce\Blocks\Templates;
*/
class ProductSearchResultsTemplate {
const SLUG = 'product-search-results';
const TITLE = 'Product Search Results';
const DESCRIPTION = 'Template used to display search results for products.';
const SLUG = 'product-search-results';
/**
* Constructor.
@ -24,7 +22,6 @@ class ProductSearchResultsTemplate {
*/
protected function init() {
add_filter( 'search_template_hierarchy', array( $this, 'update_search_template_hierarchy' ), 10, 3 );
add_filter( 'get_block_templates', array( $this, 'set_template_info' ) );
}
/**
@ -34,29 +31,8 @@ class ProductSearchResultsTemplate {
*/
public function update_search_template_hierarchy( $templates ) {
if ( ( is_search() && is_post_type_archive( 'product' ) ) && wc_current_theme_is_fse_theme() ) {
return [ self::SLUG ];
array_unshift( $templates, self::SLUG );
}
return $templates;
}
/**
* Update Product Search Template info.
*
* @param array $templates List of templates.
*/
public function set_template_info( $templates ) {
return array_map(
function ( $template ) {
if ( self::SLUG !== $template->slug ) {
return $template;
}
$template->title = self::TITLE;
$template->description = self::DESCRIPTION;
return $template;
},
$templates
);
}
}

View File

@ -1,6 +1,7 @@
<?php
namespace Automattic\WooCommerce\Blocks\Utils;
use Automattic\WooCommerce\Blocks\Templates\ProductSearchResultsTemplate;
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
/**
@ -197,7 +198,8 @@ class BlockTemplateUtils {
$template->source = $template_file->source ? $template_file->source : 'plugin';
$template->slug = $template_file->slug;
$template->type = $template_type;
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::convert_slug_to_title( $template_file->slug );
$template->title = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
$template->description = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
$template->status = 'publish';
$template->has_theme_file = true;
$template->origin = $template_file->source;
@ -228,8 +230,8 @@ class BlockTemplateUtils {
'theme' => $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG,
// 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.
'source' => $template_is_from_theme ? 'theme' : 'plugin',
'title' => self::convert_slug_to_title( $template_slug ),
'description' => '',
'title' => self::get_block_template_title( $template_slug ),
'description' => self::get_block_template_description( $template_slug ),
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
);
@ -255,27 +257,67 @@ class BlockTemplateUtils {
}
/**
* Converts template slugs into readable titles.
* Returns template titles.
*
* @param string $template_slug The templates slug (e.g. single-product).
* @return string Human friendly title converted from the slug.
* @return string Human friendly title.
*/
public static function convert_slug_to_title( $template_slug ) {
switch ( $template_slug ) {
case 'single-product':
return __( 'Single Product', 'woo-gutenberg-products-block' );
case 'archive-product':
return __( 'Product Catalog', 'woo-gutenberg-products-block' );
case 'taxonomy-product_cat':
return __( 'Products by Category', 'woo-gutenberg-products-block' );
case 'taxonomy-product_tag':
return __( 'Products by Tag', 'woo-gutenberg-products-block' );
default:
// Replace all hyphens and underscores with spaces.
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
public static function get_block_template_title( $template_slug ) {
$plugin_template_types = self::get_plugin_block_template_types();
if ( isset( $plugin_template_types[ $template_slug ] ) ) {
return $plugin_template_types[ $template_slug ]['title'];
} else {
// Human friendly title converted from the slug.
return ucwords( preg_replace( '/[\-_]/', ' ', $template_slug ) );
}
}
/**
* Returns template descriptions.
*
* @param string $template_slug The templates slug (e.g. single-product).
* @return string Template description.
*/
public static function get_block_template_description( $template_slug ) {
$plugin_template_types = self::get_plugin_block_template_types();
if ( isset( $plugin_template_types[ $template_slug ] ) ) {
return $plugin_template_types[ $template_slug ]['description'];
}
}
/**
* Returns a filtered list of plugin template types, containing their
* localized titles and descriptions.
*
* @return array The plugin template types.
*/
public static function get_plugin_block_template_types() {
$plugin_template_types = array(
'single-product' => array(
'title' => _x( 'Single Product', 'Template name', 'woo-gutenberg-products-block' ),
'description' => __( 'Template used to display the single product.', 'woo-gutenberg-products-block' ),
),
'archive-product' => array(
'title' => _x( 'Product Catalog', 'Template name', 'woo-gutenberg-products-block' ),
'description' => __( 'Template used to display products.', 'woo-gutenberg-products-block' ),
),
'taxonomy-product_cat' => array(
'title' => _x( 'Products by Category', 'Template name', 'woo-gutenberg-products-block' ),
'description' => __( 'Template used to display products by category.', 'woo-gutenberg-products-block' ),
),
'taxonomy-product_tag' => array(
'title' => _x( 'Products by Tag', 'Template name', 'woo-gutenberg-products-block' ),
'description' => __( 'Template used to display products by tag.', 'woo-gutenberg-products-block' ),
),
ProductSearchResultsTemplate::SLUG => array(
'title' => _x( 'Product Search Results', 'Template name', 'woo-gutenberg-products-block' ),
'description' => __( 'Template used to display search results for products.', 'woo-gutenberg-products-block' ),
),
);
return $plugin_template_types;
}
/**
* Converts template paths into a slug
*