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

Add render logic to BlockTemplateController #4984

Merged
merged 6 commits into from
Oct 29, 2021
Merged
Changes from 3 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
34 changes: 34 additions & 0 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function __construct() {
*/
protected function init() {
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_action( 'wp', array( $this, 'render_block_template' ) );
}

/**
Expand Down Expand Up @@ -103,4 +104,37 @@ public function theme_has_template( $template_name ) {
return is_readable( get_template_directory() . '/block-templates/' . $template_name . '.html' ) ||
is_readable( get_stylesheet_directory() . '/block-templates/' . $template_name . '.html' );
}

/**
* Checks whether a block template with that name exists in Woo Blocks
*
* @param string $template_name Template to check.
* @return boolean
*/
public function default_block_template_is_available( $template_name ) {
if ( ! $template_name ) {
return false;
}

return is_readable(
$this->templates_directory . '/' . $template_name . '.html'
);
}

/**
* Renders the default block template from Woo Blocks if no theme templates exist.
*/
public function render_block_template() {
if ( is_embed() || ! gutenberg_supports_block_templates() ) {
tjcafferkey marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (
is_singular( 'product' ) &&
! $this->theme_has_template( 'single-product' ) &&
$this->default_block_template_is_available( 'single-product' )
) {
add_filter( 'wc_has_block_template', '__return_true', 10, 0 );
}
}
}