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

Move Woo Blocks template directories to latest Gutenberg convention #5464

Merged
merged 7 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not replace self::TEMPLATES_ROOT_DIR with BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES'] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we can because they have the same value. However, the reason why I added TEMPLATES_ROOT_DIR in the first place is because they represent two different things:

  1. BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES'] represents the block-templates directory name in themes.
  2. BlockTemplatesController::TEMPLATES_ROOT_DIR represents the root directory of all templates in our own repo. For example, we have email templates.

The just happen to be both named templates now, so we have the awkward path templates/templates (which makes me sad :'(). But technically they are separate entities that could also be changed independently at any point.

Does that make any sense?


$this->templates_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATES'];
$this->template_parts_directory = $root_path . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'];
$this->init();
}
}
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
11 changes: 4 additions & 7 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 );
Comment on lines +259 to +262
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better. 👏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It was also necessary, by the way. I didn't simplify it just for the sake of it. But because our dir structure became templates/templates, the original substr code would not work anymore.

}

/**
Expand Down