Add Render Logic to BlockTemplateController (https://github.com/woocommerce/woocommerce-blocks/pull/4984)
* 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 <dinhtungdu@gmail.com> Co-authored-by: Tung Du <dinhtungdu@gmail.com>
This commit is contained in:
parent
e347634205
commit
66cc0178eb
|
@ -37,6 +37,7 @@ class BlockTemplatesController {
|
|||
*/
|
||||
protected function init() {
|
||||
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
|
||||
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,4 +106,37 @@ class BlockTemplatesController {
|
|||
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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ class BlockTemplateUtils {
|
|||
* @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();
|
||||
|
|
Loading…
Reference in New Issue