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

Commit

Permalink
Move Woo Blocks template directories to latest Gutenberg convention (#…
Browse files Browse the repository at this point in the history
…5464)

* Align Woo Block template locations with the newest convention

While we now support both the old and new conventions for the templates
paths, our own repo should be aligned with the latest convention.

See: #5455
Fixes: #5343

* Simplify `generate_template_slug_from_path` function
* Change `BlockTemplatesController` constructor to get correct dir names
* Update Mini Cart template path
  • Loading branch information
sunyatasattva authored Jan 5, 2022
1 parent 95c9ead commit 512cc04
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 28 deletions.
27 changes: 8 additions & 19 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,22 @@ class BlockTemplatesController {
private $template_parts_directory;

/**
* Directory name of the block template directory.
* Directory which contains all templates
*
* @var string
*/
const TEMPLATES_DIR_NAME = 'block-templates';

/**
* Directory name of the block template parts directory.
*
* @var string
*/
const TEMPLATE_PARTS_DIR_NAME = 'block-template-parts';
const TEMPLATES_ROOT_DIR = 'templates';

/**
* Constructor.
*/
public function __construct() {
// This feature is gated for WooCommerce versions 6.0.0 and above.
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '6.0.0', '>=' ) ) {
$this->templates_directory = plugin_dir_path( __DIR__ ) . 'templates/' . self::TEMPLATES_DIR_NAME;
$this->template_parts_directory = plugin_dir_path( __DIR__ ) . 'templates/' . self::TEMPLATE_PARTS_DIR_NAME;
$root_path = plugin_dir_path( __DIR__ ) . self::TEMPLATES_ROOT_DIR . DIRECTORY_SEPARATOR;

$this->templates_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES'];
$this->template_parts_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'];
$this->init();
}
}
Expand All @@ -61,7 +56,7 @@ protected function init() {

/**
* This function checks if there's a blocks template (ultimately it resolves either a saved blocks template from the
* database or a template file in `woo-gutenberg-products-block/templates/block-templates/`)
* database or a template file in `woo-gutenberg-products-block/templates/templates/`)
* to return to pre_get_posts short-circuiting the query in Gutenberg.
*
* @param \WP_Block_Template|null $template Return a block template object to short-circuit the default query,
Expand Down Expand Up @@ -311,14 +306,8 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem
$template_files = BlockTemplateUtils::gutenberg_get_template_paths( $directory );
$templates = array();

if ( 'wp_template_part' === $template_type ) {
$dir_name = self::TEMPLATE_PARTS_DIR_NAME;
} else {
$dir_name = self::TEMPLATES_DIR_NAME;
}

foreach ( $template_files as $template_file ) {
$template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file, $dir_name );
$template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file );

// This template does not have a slug we're looking for. Skip it.
if ( is_array( $slugs ) && count( $slugs ) > 0 && ! in_array( $template_slug, $slugs, true ) ) {
Expand Down
2 changes: 1 addition & 1 deletion src/BlockTypes/MiniCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ protected function get_markup( $attributes ) {
if ( '' === $template_part_contents ) {
$template_part_contents = do_blocks(
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
file_get_contents( Package::get_path() . 'templates/block-template-parts/mini-cart.html' )
file_get_contents( Package::get_path() . 'templates/' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/mini-cart.html' )
);
}

Expand Down
13 changes: 5 additions & 8 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,12 @@ public static function convert_slug_to_title( $template_slug ) {
* Converts template paths into a slug
*
* @param string $path The template's path.
* @param string $directory_name The template's directory name.
* @return string slug
*/
public static function generate_template_slug_from_path( $path, $directory_name = 'block-templates' ) {
return substr(
$path,
strpos( $path, $directory_name . DIRECTORY_SEPARATOR ) + 1 + strlen( $directory_name ),
-5
);
public static function generate_template_slug_from_path( $path ) {
$template_extension = '.html';

return basename( $path, $template_extension );
}

/**
Expand Down Expand Up @@ -298,8 +295,8 @@ public static function get_theme_template_path( $template_slug, $template_type =
function( $carry, $item ) use ( $template_filename ) {
$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;

$carry[] = get_template_directory() . $filepath;
$carry[] = get_stylesheet_directory() . $filepath;
$carry[] = get_template_directory() . $filepath;

return $carry;
},
Expand Down

0 comments on commit 512cc04

Please sign in to comment.