* * @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 ); } } /** * Add template part areas for our blocks. * * @param array $area_definitions An array of supported area objects. */ public function add_template_part_areas( $area_definitions ) { return array_merge( $area_definitions, array( array( 'area' => 'mini-cart', 'label' => __( 'Mini Cart', 'woocommerce' ), 'description' => __( 'The Mini Cart template defines a page area that contains the content of the Mini Cart block.', 'woocommerce' ), 'icon' => 'sidebar', 'area_tag' => 'div', ), ) ); } /** * Add mini cart content block to new template part for Mini Cart area. * * @param int $post_id Post ID. * @param \WP_Post $post Post object. * @param bool $update Whether this is an existing post being updated. */ public function add_mini_cart_content_to_template_part( $post_id, $post, $update ) { // We only inject the mini cart content when the template part is created. if ( $update ) { return; } // If by somehow, the template part was created with content, bail. if ( ! empty( $post->content ) ) { return; } if ( ! function_exists( 'get_block_file_template' ) ) { return; } if ( 'wp_template_part' !== $post->post_type ) { return; } $type_terms = get_the_terms( $post, 'wp_template_part_area' ); if ( is_wp_error( $type_terms ) || false === $type_terms ) { return; } if ( 'mini-cart' !== $type_terms[0]->name ) { return; } // Remove the filter temporarily for wp_update_post below. remove_filter( 'wp_insert_post', array( $this, 'add_mini_cart_content_to_template_part' ), 10, 3 ); $block_template = null; /** * We only use the mini cart content from file. */ if ( BlockTemplateUtils::theme_has_template_part( 'mini-cart' ) ) { $template_id = sprintf( '%s//mini-cart', wp_get_theme()->get_stylesheet() ); $block_template = get_block_file_template( $template_id, 'wp_template_part' ); } else { $available_templates = $this->get_block_templates_from_woocommerce( array( 'mini-cart' ), array(), 'wp_template_part' ); if ( is_array( $available_templates ) && count( $available_templates ) > 0 ) { $block_template = BlockTemplateUtils::gutenberg_build_template_result_from_file( $available_templates[0], $available_templates[0]->type ); } } if ( is_a( $block_template, 'WP_Block_Template' ) ) { $post->post_content = $block_template->content; } else { // Just for extra safety. $post->post_content = ''; } wp_update_post( $post ); add_filter( 'wp_insert_post', array( $this, 'add_mini_cart_content_to_template_part' ), 10, 3 ); } }