late' ) { 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' ) && $this->block_template_is_available( 'single-product' ) ) { $templates = get_block_templates( array( 'slug__in' => array( 'single-product' ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( 'single-product' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } elseif ( ( is_product_taxonomy() && is_tax( 'product_cat' ) ) && $this->block_template_is_available( 'taxonomy-product_cat' ) ) { $templates = get_block_templates( array( 'slug__in' => array( 'taxonomy-product_cat' ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_cat' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } elseif ( ( is_product_taxonomy() && is_tax( 'product_tag' ) ) && $this->block_template_is_available( 'taxonomy-product_tag' ) ) { $templates = get_block_templates( array( 'slug__in' => array( 'taxonomy-product_tag' ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( 'taxonomy-product_tag' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } elseif ( is_post_type_archive( 'product' ) && is_search() ) { $templates = get_block_templates( array( 'slug__in' => array( ProductSearchResultsTemplate::SLUG ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( ProductSearchResultsTemplate::SLUG ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } elseif ( ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) && $this->block_template_is_available( 'archive-product' ) ) { $templates = get_block_templates( array( 'slug__in' => array( 'archive-product' ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( 'archive-product' ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } } elseif ( is_cart() && ! BlockTemplateUtils::theme_has_template( CartTemplate::get_slug() ) && $this->block_template_is_available( CartTemplate::get_slug() ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } elseif ( is_checkout() && ! BlockTemplateUtils::theme_has_template( CheckoutTemplate::get_slug() ) && $this->block_template_is_available( CheckoutTemplate::get_slug() ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } elseif ( is_wc_endpoint_url( 'order-received' ) && ! BlockTemplateUtils::theme_has_template( OrderConfirmationTemplate::get_slug() ) && $this->block_template_is_available( OrderConfirmationTemplate::get_slug() ) ) { add_filter( 'woocommerce_has_block_template', '__return_true', 10, 0 ); } else { $queried_object = get_queried_object(); if ( is_null( $queried_object ) ) { return; } if ( isset( $queried_object->taxonomy ) && taxonomy_is_product_attribute( $queried_object->taxonomy ) && $this->block_template_is_available( ProductAttributeTemplate::SLUG ) ) { $templates = get_block_templates( array( 'slug__in' => array( ProductAttributeTemplate::SLUG ) ) ); if ( isset( $templates[0] ) && BlockTemplateUtils::template_has_legacy_template_block( $templates[0] ) ) { add_filter( 'woocommerce_disable_compatibility_layer', '__return_true' ); } if ( ! BlockTemplateUtils::theme_has_template( ProductAttributeTemplate::SLUG ) ) { 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; } /** * Update the product archive title to "Shop". * * @param string $post_type_name Post type 'name' label. * @param string $post_type Post type. * * @return string */ public function update_product_archive_title( $post_type_name, $post_type ) { if ( function_exists( 'is_shop' ) && is_shop() && 'product' === $post_type ) { return __( 'Shop', 'woocommerce' ); } return $post_type_name; } /** * Migrates page content to templates if needed. */ public function maybe_migrate_content() { if ( ! $this->has_migrated_page( 'cart' ) ) { $this->migrate_page( 'cart', CartTemplate::get_placeholder_page() ); } if ( ! $this->has_migrated_page( 'checkout' ) ) { $this->migrate_page( 'checkout', CheckoutTemplate::get_placeholder_page() ); } } /** * Check if a page has been migrated to a template. * * @param string $page_id Page ID. * @return boolean */ protected function has_migrated_page( $page_id ) { return (bool) get_option( 'has_migrated_' . $page_id, false ); } /** * Prepare default page template. * * @param \WP_Post $page Page object. * @return string */ protected function get_default_migrate_page_template( $page ) { $default_template_content = $this->get_block_template_part( 'header' ); $default_template_content .= '