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

Commit

Permalink
Add render logic to BlockTemplatesController
Browse files Browse the repository at this point in the history
  • Loading branch information
tjcafferkey committed Oct 25, 2021
1 parent c4a983c commit 84b718e
Showing 1 changed file with 34 additions and 0 deletions.
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() ) {
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 );
}
}
}

0 comments on commit 84b718e

Please sign in to comment.