Skip to content

Commit

Permalink
Add Render Logic to BlockTemplateController (woocommerce#4984)
Browse files Browse the repository at this point in the history
* Add render logic to BlockTemplatesController

* Comment out action to test e2e tests

* Add add_action back into initialise render method

* Check function exists before using it

* Change hook from wp to template_redirect

* Update src/BlockTemplatesController.php

Co-authored-by: Tung Du <[email protected]>

Co-authored-by: Tung Du <[email protected]>
  • Loading branch information
2 people authored and jonny-bull committed Dec 14, 2021
1 parent bdcb130 commit 6ae40c4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
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( 'template_redirect', array( $this, 'render_block_template' ) );
}

/**
Expand Down Expand Up @@ -105,4 +106,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() || ! function_exists( 'gutenberg_supports_block_templates' ) || ! 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 );
}
}
}
2 changes: 1 addition & 1 deletion src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static function gutenberg_inject_theme_attribute_in_content( $template_co
* @return WP_Block_Template Template.
*/
public static function gutenberg_build_template_result_from_file( $template_file, $template_type ) {
$default_template_types = gutenberg_get_default_template_types();
$default_template_types = function_exists( 'gutenberg_get_default_template_types' ) ? gutenberg_get_default_template_types() : array();
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$template_content = file_get_contents( $template_file['path'] );
$theme = wp_get_theme()->get_stylesheet();
Expand Down

0 comments on commit 6ae40c4

Please sign in to comment.