and description to WooCommerce templates. * * @param WP_Block_Template|null $block_template The found block template, or null if there isn't one. * @param string $id Template unique identifier (example: 'theme_slug//template_slug'). * @param array $template_type Template type: 'wp_template' or 'wp_template_part'. * @return WP_Block_Template|null */ public function add_block_template_details( $block_template, $id, $template_type ) { return BlockTemplateUtils::update_template_data( $block_template, $template_type ); } /** * 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 string $template_type wp_template or wp_template_part. * @return array */ 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; } /** * Gets the templates saved in the database. * * @param array $slugs An array of slugs to retrieve templates for. * @param string $template_type wp_template or wp_template_part. * * @return int[]|\WP_Post[] An array of found templates. */ public function get_block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { wc_deprecated_function( 'BlockTemplatesController::get_block_templates_from_db()', '7.8', '\Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils::get_block_templates_from_db()' ); return BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ); } /** * 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. * * @return array Templates from the WooCommerce blocks plugin directory. */ public function get_block_templates_from_woocommerce( $slugs, $already_found_templates, $template_type = 'wp_template' ) { $template_files = BlockTemplateUtils::get_template_paths( $template_type ); $templates = array(); foreach ( $template_files as $template_file ) { // Skip the template if it's blockified, and we should only use classic ones. if ( ! BlockTemplateUtils::should_use_blockified_product_grid_templates() && strpos( $template_file, 'blockified' ) !== false ) { continue; } $template_slug = BlockTemplateUtils::generate_template_slug_from_path( $template_file ); // 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 ) || 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; } if ( BlockTemplateUtils::template_is_eligible_for_fallback_from_db( $template_slug, $already_found_templates ) ) { $template = clone BlockTemplateUtils::get_fallback_template_from_db( $template_slug, $already_found_templates ); $template_id = explode( '//', $template->id ); $template->id = $template_id[0] . '//' . $template_slug; $template->slug = $template_slug; $template->title = BlockTemplateUtils::get_block_template_title( $template_slug ); $template->description = BlockTemplateUtils::get_block_template_description( $template_slug ); $templates[] = $template; continue; } // If the template is not present in the theme but its fallback template is, // let's use the theme's fallback template. if ( BlockTemplateUtils::template_is_eligible_for_fallback_from_theme( $template_slug ) ) { $registered_template = BlockTemplateUtils::get_template( $template_slug ); $template_file = BlockTemplateUtils::get_theme_template_path( $registered_template->fallback_template ); $templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug, true ); continue; } // At this point the template only exists in the Blocks filesystem and has not been saved in the DB, // or superseded by the theme. $templates[] = BlockTemplateUtils::create_new_block_template_object( $template_file, $template_type, $template_slug ); } return $templates; } /** * Get and build the block template objects from the block template files. * * @param array $slugs An array of slugs to retrieve templates for. * @param string $template_type wp_template or wp_template_part. * * @return array WP_Block_Template[] An array of block template objects. */ public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) { $templates_from_db = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type ); $templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type ); return array_merge( $templates_from_db, $templates_from_woo ); } /** * 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 = BlockTemplateUtils::get_templates_directory( $template_type ) . '/' . $template_name . '.html'; return is_readable( $directory ) || $this->get_block_templates( array( $template_name ), $template_type ); } }