woocommerce/plugins/woocommerce-blocks/src/BlockTemplatesController.php

448 lines
17 KiB
PHP
Raw Normal View History

<?php
namespace Automattic\WooCommerce\Blocks;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
/**
* BlockTypesController class.
*
* @internal
*/
class BlockTemplatesController {
/**
* Holds the path for the directory where the block templates will be kept.
*
* @var string
*/
private $templates_directory;
/**
* Holds the path for the directory where the block template parts will be kept.
*
* @var string
*/
private $template_parts_directory;
/**
* Directory name of the block template directory.
*
* @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';
/**
* 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;
$this->init();
}
}
/**
* Initialization method.
*/
protected function init() {
add_action( 'template_redirect', array( $this, 'render_block_template' ) );
add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
}
/**
* This function checks if there's a blocks template (ultimately it resolves either a saved blocks template from the
* database or a template file in `woo-gutenberg-products-block/templates/block-templates/`)
* to return to pre_get_posts short-circuiting the query in Gutenberg.
*
* @param \WP_Block_Template|null $template Return a block template object to short-circuit the default query,
* or null to allow WP to run its normal queries.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*
* @return mixed|\WP_Block_Template|\WP_Error
*/
public function maybe_return_blocks_template( $template, $id, $template_type ) {
if ( ! function_exists( 'gutenberg_get_block_template' ) ) {
return $template;
}
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$template_name_parts = explode( '//', $id );
if ( count( $template_name_parts ) < 2 ) {
return $template;
}
list( , $slug ) = $template_name_parts;
// Remove the filter at this point because if we don't then this function will infinite loop.
remove_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
// Check if the theme has a saved version of this template before falling back to the woo one. Please note how
// the slug has not been modified at this point, we're still using the default one passed to this hook.
$maybe_template = gutenberg_get_block_template( $id, $template_type );
if ( null !== $maybe_template ) {
add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
return $maybe_template;
}
// Theme-based template didn't exist, try switching the theme to woocommerce and try again. This function has
// been unhooked so won't run again.
add_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$maybe_template = gutenberg_get_block_template( 'woocommerce//' . $slug, $template_type );
// Re-hook this function, it was only unhooked to stop recursion.
add_filter( 'pre_get_block_file_template', array( $this, 'maybe_return_blocks_template' ), 10, 3 );
remove_filter( 'get_block_file_template', array( $this, 'get_single_block_template' ), 10, 3 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
if ( null !== $maybe_template ) {
return $maybe_template;
}
// At this point we haven't had any luck finding a template. Give up and let Gutenberg take control again.
return $template;
}
/**
* Runs on the get_block_template hook. If a template is already found and passed to this function, then return it
* and don't run.
* If a template is *not* passed, try to look for one that matches the ID in the database, if that's not found defer
* to Blocks templates files. Priority goes: DB-Theme, DB-Blocks, Filesystem-Theme, Filesystem-Blocks.
*
* @param \WP_Block_Template $template The found block template.
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param array $template_type wp_template or wp_template_part.
*
* @return mixed|null
*/
public function get_single_block_template( $template, $id, $template_type ) {
// The template was already found before the filter runs, just return it immediately.
if ( null !== $template ) {
return $template;
}
$template_name_parts = explode( '//', $id );
if ( count( $template_name_parts ) < 2 ) {
return $template;
}
list( , $slug ) = $template_name_parts;
// If this blocks template doesn't exist then we should just skip the function and let Gutenberg handle it.
if ( ! $this->block_template_is_available( $slug, $template_type ) ) {
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
return $template;
}
$available_templates = $this->get_block_templates( array( $slug ), $template_type );
return ( is_array( $available_templates ) && count( $available_templates ) > 0 )
? BlockTemplateUtils::gutenberg_build_template_result_from_file( $available_templates[0], $available_templates[0]->type )
: $template;
}
/**
* Add the block template objects to be used.
*
* @param array $query_result Array of template objects.
* @param array $query Optional. Arguments to retrieve templates.
* @param array $template_type wp_template or wp_template_part.
* @return array
*/
public function add_block_templates( $query_result, $query, $template_type ) {
if (
( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) &&
( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() )
) {
return $query_result;
}
$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array();
$template_files = $this->get_block_templates( $slugs, $template_type );
// @todo: Add apply_filters to _gutenberg_get_template_files() in Gutenberg to prevent duplication of logic.
foreach ( $template_files as $template_file ) {
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
// Avoid adding the same template if it's already in the array of $query_result.
if (
array_filter(
$query_result,
function( $query_result_template ) use ( $template_file ) {
return $query_result_template->slug === $template_file->slug &&
$query_result_template->theme === $template_file->theme;
}
)
) {
continue;
}
// If the current $post_type is set (e.g. on an Edit Post screen), and isn't included in the available post_types
// on the template file, then lets skip it so that it doesn't get added. This is typically used to hide templates
// in the template dropdown on the Edit Post page.
if ( $post_type &&
isset( $template_file->post_types ) &&
! in_array( $post_type, $template_file->post_types, true )
) {
continue;
}
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
// It would be custom if the template was modified in the editor, so if it's not custom we can load it from
// the filesystem.
if ( 'custom' !== $template_file->source ) {
$template = BlockTemplateUtils::gutenberg_build_template_result_from_file( $template_file, $template_type );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
} else {
$template_file->title = BlockTemplateUtils::convert_slug_to_title( $template_file->slug );
$query_result[] = $template_file;
continue;
}
$is_not_custom = false === array_search(
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
wp_get_theme()->get_stylesheet() . '//' . $template_file->slug,
array_column( $query_result, 'id' ),
true
);
$fits_slug_query =
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
! isset( $query['slug__in'] ) || in_array( $template_file->slug, $query['slug__in'], true );
$fits_area_query =
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
! isset( $query['area'] ) || $template_file->area === $query['area'];
$should_include = $is_not_custom && $fits_slug_query && $fits_area_query;
if ( $should_include ) {
$query_result[] = $template;
}
}
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$query_result = $this->remove_theme_templates_with_custom_alternative( $query_result );
return $query_result;
}
/**
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
* Removes templates that were added to a theme's block-templates directory, but already had a customised version saved in the database.
*
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
* @param \WP_Block_Template[]|\stdClass[] $templates List of templates to run the filter on.
*
* @return array List of templates with duplicates removed. The customised alternative is preferred over the theme default.
*/
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
public function remove_theme_templates_with_custom_alternative( $templates ) {
// Get the slugs of all templates that have been customised and saved in the database.
$customised_template_slugs = array_map(
function( $template ) {
return $template->slug;
},
array_values(
array_filter(
$templates,
function( $template ) {
// This template has been customised and saved as a post.
return 'custom' === $template->source;
}
)
)
);
// Remove theme (i.e. filesystem) templates that have the same slug as a customised one. We don't need to check
// for `woocommerce` in $template->source here because woocommerce templates won't have been added to $templates
// if a saved version was found in the db. This only affects saved templates that were saved BEFORE a theme
// template with the same slug was added.
return array_values(
array_filter(
$templates,
function( $template ) use ( $customised_template_slugs ) {
// This template has been customised and saved as a post, so return it.
return ! ( 'theme' === $template->source && in_array( $template->slug, $customised_template_slugs, true ) );
}
)
);
}
/**
* Gets the templates saved in the database.
*
* @param array $slugs An array of slugs to retrieve templates for.
* @param array $template_type wp_template or wp_template_part.
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
*
* @return int[]|\WP_Post[] An array of found templates.
*/
public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) {
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$check_query_args = array(
'post_type' => $template_type,
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
'posts_per_page' => -1,
'no_found_rows' => true,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => array( 'woocommerce', get_stylesheet() ),
),
),
);
if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
$check_query_args['post_name__in'] = $slugs;
}
$check_query = new \WP_Query( $check_query_args );
$saved_woo_templates = $check_query->posts;
return array_map(
function( $saved_woo_template ) {
return BlockTemplateUtils::gutenberg_build_template_result_from_post( $saved_woo_template );
},
$saved_woo_templates
);
}
/**
* Gets the templates from the WooCommerce blocks directory, skipping those for which a template already exists
* in the theme directory.
*
* @param string[] $slugs An array of slugs to filter templates by. Templates whose slug does not match will not be returned.
* @param array $already_found_templates Templates that have already been found, these are customised templates that are loaded from the database.
* @param string $template_type wp_template or wp_template_part.
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
*
* @return array Templates from the WooCommerce blocks plugin directory.
*/
public function get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type = 'wp_template' ) {
$directory = $this->get_templates_directory( $template_type );
$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 );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
// 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 ) ) {
continue;
}
// If the theme already has a template, or the template is already in the list (i.e. it came from the
// database) then we should not overwrite it with the one from the filesystem.
if (
BlockTemplateUtils::theme_has_template( $template_slug ) ||
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
count(
array_filter(
$already_found_templates,
function ( $template ) use ( $template_slug ) {
$template_obj = (object) $template; //phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found
return $template_obj->slug === $template_slug;
}
)
) > 0 ) {
continue;
}
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
// At this point the template only exists in the Blocks filesystem and has not been saved in the DB,
// or superseded by the theme.
$new_template_item = array(
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
'slug' => $template_slug,
'id' => 'woocommerce//' . $template_slug,
'path' => $template_file,
'type' => $template_type,
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
'theme' => 'woocommerce',
'source' => 'plugin',
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
'title' => BlockTemplateUtils::convert_slug_to_title( $template_slug ),
'description' => '',
'post_types' => array(), // Don't appear in any Edit Post template selector dropdown.
);
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$templates[] = (object) $new_template_item;
}
return $templates;
}
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
/**
* Get and build the block template objects from the block template files.
*
* @param array $slugs An array of slugs to retrieve templates for.
* @param array $template_type wp_template or wp_template_part.
*
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
* @return array
*/
public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) {
$templates_from_db = $this->get_block_templates_from_db( $slugs, $template_type );
$templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type );
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
return array_merge( $templates_from_db, $templates_from_woo );
}
/**
* Gets the directory where templates of a specific template type can be found.
*
* @param array $template_type wp_template or wp_template_part.
*
* @return string
*/
protected function get_templates_directory( $template_type = 'wp_template' ) {
if ( 'wp_template_part' === $template_type ) {
return $this->template_parts_directory;
}
return $this->templates_directory;
}
/**
* Checks whether a block template with that name exists in Woo Blocks
*
* @param string $template_name Template to check.
* @param array $template_type wp_template or wp_template_part.
*
* @return boolean
*/
public function block_template_is_available( $template_name, $template_type = 'wp_template' ) {
if ( ! $template_name ) {
return false;
}
$directory = $this->get_templates_directory( $template_type ) . '/' . $template_name . '.html';
return is_readable(
$directory
) || $this->get_block_templates( array( $template_name ), $template_type );
}
/**
* Renders the default block template from Woo Blocks if no theme templates exist.
*/
public function render_block_template() {
if (
is_embed() ||
( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) &&
( ! function_exists( 'gutenberg_supports_block_templates' ) || ! gutenberg_supports_block_templates() )
) {
return;
}
if (
is_singular( 'product' ) &&
! BlockTemplateUtils::theme_has_template( 'single-product' ) &&
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$this->block_template_is_available( 'single-product' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_product_taxonomy() && is_tax( 'product_cat' ) ) &&
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) &&
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$this->block_template_is_available( 'taxonomy-product_cat' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_product_taxonomy() && is_tax( 'product_tag' ) ) &&
! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) &&
$this->block_template_is_available( 'taxonomy-product_tag' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
} elseif (
( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
! BlockTemplateUtils::theme_has_template( 'archive-product' ) &&
Allow block templates to be customised and saved (https://github.com/woocommerce/woocommerce-blocks/pull/5062) * Ensure $template is object before accessing properties This is necessary because the gutenberg helper functions sometimes turn it into a WP_Block_Template object, and other times it's an array. Because of this it's safer to normalise them both as objects. * Add Gutenberg utils for processing templates based on a post from the db When a template is saved it gets saved to the database, we need to handle processing these WooCommerce templates that have been saved in the db and we need to use the gutenberg utils that are private, this is why they've been copied over. * Force theme to always be WooCommerce This is because the templates we're dealing with here should always belong to WooCommerce, not the currently selected theme * Add maybe_return_blocks_template and get_single_block_template funcs These are needed to get the template from either the DB or the filesystem when saving/retrieving the template. * Set theme to always be woocommerce when making templates from files This will ensure the correct slug is used in the gutenberg editor. * Check if template has been customised and saved in the database first * Prevent filesystem templates being used if a custom database one exists * Fix syntax error from rebase * Remove unnecessary code from BlockTemplateUtils * Ensure template item is an object containing correct properties * Prevent warnings from appearing * Ensure title is added to the template when saving * Filter templates that don't match the queried slug. * Remove unused code * Check if a saved version of the template exists when trying to render * Rename default_block_template_is_available to block_template_is_available * Re-hook pre_get_block_template before returning from maybe_return_blocks_template * Make comment easier to read * Look for template in woocommerce theme or real theme taxonomy * Remove duplicated title assignment * Prevent template being added twice when loading from the db * Filter templates before returning if slugs are supplied * Simplify `get_block_templates` function into two functions * Add function to stop theme templates that are added after db ones showing * Fix typographical errors
2021-11-05 19:07:34 +00:00
$this->block_template_is_available( 'archive-product' )
) {
add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 );
}
}
}