block_templates_from_db( $slugs = array(), $template_type = 'wp_template' ) { $check_query_args = array( 'post_type' => $template_type, '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( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG, BlockTemplateUtils::PLUGIN_SLUG, 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::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. * * @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::get_template_paths( $directory ); $templates = array(); foreach ( $template_files as $template_file ) { $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 the theme has an archive-product.html template, but not a taxonomy-product_cat.html template let's use the themes archive-product.html template. if ( BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $template_slug ) ) { $template_file = BlockTemplateUtils::get_theme_template_path( 'archive-product' ); $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 array $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 = $this->get_block_templates_from_db( $slugs, $template_type ); $templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type ); $templates = array_merge( $templates_from_db, $templates_from_woo ); return BlockTemplateUtils::filter_block_templates_by_feature_flag( $templates ); } /** * 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() || ! BlockTemplateUtils::supports_block_templates() ) { return; } if ( is_singular( 'product' ) && ! BlockTemplateUtils::theme_has_template( 'single-product' ) && $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' ) && $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' ) && $this->block_template_is_available( 'archive-product' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } /** * Remove the template panel from the Sidebar of the Shop page because * the Site Editor handles it. * * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/6278 * * @param bool $is_support Whether the active theme supports block templates. * * @return bool */ public function remove_block_template_support_for_shop_page( $is_support ) { global $pagenow, $post; if ( is_admin() && 'post.php' === $pagenow && function_exists( 'wc_get_page_id' ) && is_a( $post, 'WP_Post' ) && wc_get_page_id( 'shop' ) === $post->ID ) { return false; } return $is_support; } }