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 7.7.0
public function add_block_templates( $query_result, $query, $template_type ) { if ( ! BlockTemplateUtils::supports_block_templates() ) { return $query_result; } $post_type = isset( $query['post_type'] ) ? $query['post_type'] : ''; $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 ) { // 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 ) { $template = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type ); } else { $template_file->title = BlockTemplateUtils::get_block_template_title( $template_file->slug ); $template_file->description = BlockTemplateUtils::get_block_template_description( $template_file->slug ); $query_result[] = $template_file; continue; } $is_not_custom = false === array_search( wp_get_theme()->get_stylesheet() . '//' . $template_file->slug, array_column( $query_result, 'id' ), true ); $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_not_custom && $fits_slug_query && $fits_area_query; if ( $should_include ) { $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 ); /** * 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 ) { if ( 'theme' === $template->origin && BlockTemplateUtils::template_has_title( $template ) ) { return $template; } if ( $template->title === $template->slug ) { $template->title = BlockTemplateUtils::get_block_template_title( $template->slug ); } if ( ! $template->description ) { $template->description = BlockTemplateUtils::get_block_template_description( $template->slug ); } if ( str_contains( $template->slug, 'single-product' ) ) { if ( ! is_admin() && ! BlockTemplateUtils::template_has_legacy_template_block( $template ) ) { $new_content = SingleProductTemplateCompatibility::add_compatibility_layer( $template->content ); $template->content = $new_content; } return $template; } return $template; }, $query_result ); return $query_result; }