Automattic\WooCommerce\Blocks
BlockTemplatesController::add_block_templates()
Add the block template objects to be used.
Method of the class: BlockTemplatesController{}
No Hooks.
Return
Array
.
Usage
$BlockTemplatesController = new BlockTemplatesController(); $BlockTemplatesController->add_block_templates( $query_result, $query, $template_type );
- $query_result(array) (required)
- Array of template objects.
- $query(array) (required)
- Arguments to retrieve templates.
- $template_type(string) (required)
- wp_template or wp_template_part.
BlockTemplatesController::add_block_templates() BlockTemplatesController::add block templates code WC 9.7.1
public function add_block_templates( $query_result, $query, $template_type ) { $slugs = isset( $query['slug__in'] ) ? $query['slug__in'] : array(); if ( ! BlockTemplateUtils::supports_block_templates( $template_type ) && ! in_array( ComingSoonTemplate::SLUG, $slugs, true ) ) { return $query_result; } $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $template_files = $this->get_block_templates( $slugs, $template_type ); $theme_slug = wp_get_theme()->get_stylesheet(); // @todo: Add apply_filters to _gutenberg_get_template_files() in Gutenberg to prevent duplication of logic. foreach ( $template_files as $template_file ) { // If we have a template which is eligible for a fallback, we need to explicitly tell Gutenberg that // it has a theme file (because it is using the fallback template file). And then `continue` to avoid // adding duplicates. if ( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $query_result, $template_file ) ) { 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; } // 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 ) { $query_result[] = $template_file; continue; } $possible_template_ids = [ $theme_slug . '//' . $template_file->slug, $theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/' . $template_file->slug, $theme_slug . '//' . BlockTemplateUtils::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'] . '/' . $template_file->slug, ]; $is_custom = false; $query_result_template_ids = array_column( $query_result, 'id' ); foreach ( $possible_template_ids as $template_id ) { if ( in_array( $template_id, $query_result_template_ids, true ) ) { $is_custom = true; break; } } $fits_slug_query = ! isset( $query['slug__in'] ) || in_array( $template_file->slug, $query['slug__in'], true ); $fits_area_query = ! isset( $query['area'] ) || ( property_exists( $template_file, 'area' ) && $template_file->area === $query['area'] ); $should_include = ! $is_custom && $fits_slug_query && $fits_area_query; if ( $should_include ) { $template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type ); $query_result[] = $template; } } // We need to remove theme (i.e. filesystem) templates that have the same slug as a customised one. // This only affects saved templates that were saved BEFORE a theme template with the same slug was added. $query_result = BlockTemplateUtils::remove_theme_templates_with_custom_alternative( $query_result ); // There is the chance that the user customized the default template, installed a theme with a custom template // and customized that one as well. When that happens, duplicates might appear in the list. // See: https://github.com/woocommerce/woocommerce/issues/42220. $query_result = BlockTemplateUtils::remove_duplicate_customized_templates( $query_result, $theme_slug ); /** * WC templates from theme aren't included in `$this->get_block_templates()` but are handled by Gutenberg. * We need to do additional search through all templates file to update title and description for WC * templates that aren't listed in theme.json. */ $query_result = array_map( function ( $template ) use ( $template_type ) { return BlockTemplateUtils::update_template_data( $template, $template_type ); }, $query_result ); return $query_result; }