plugin_path() . '/templates/' . $cs_template; } else { $template = WC()->plugin_path() . '/templates/' . $default_file; } } } return $template; } /** * Checks whether a block template for a given taxonomy exists. * * **Note:** This checks both the `templates` and `block-templates` directories * as both conventions should be supported. * * @param object $taxonomy Object taxonomy to check. * @return boolean */ private static function taxonomy_has_block_template( $taxonomy ) : bool { if ( taxonomy_is_product_attribute( $taxonomy->taxonomy ) ) { $template_name = 'taxonomy-product_attribute'; } else { $template_name = 'taxonomy-' . $taxonomy->taxonomy; } return self::has_block_template( $template_name ); } /** * Checks whether a block template with that name exists. * * **Note: ** This checks both the `templates` and `block-templates` directories * as both conventions should be supported. * * @since 5.5.0 * @param string $template_name Template to check. * @return boolean */ private static function has_block_template( $template_name ) { if ( ! $template_name ) { return false; } $has_template = false; $template_filename = $template_name . '.html'; // Since Gutenberg 12.1.0, the conventions for block templates directories have changed, // we should check both these possible directories for backwards-compatibility. $possible_templates_dirs = array( 'templates', 'block-templates' ); // Combine the possible root directory names with either the template directory // or the stylesheet directory for child themes, getting all possible block templates // locations combinations. $filepath = DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template_filename; $legacy_filepath = DIRECTORY_SEPARATOR . 'block-templates' . DIRECTORY_SEPARATOR . $template_filename; $possible_paths = array( get_stylesheet_directory() . $filepath, get_stylesheet_directory() . $legacy_filepath, get_template_directory() . $filepath, get_template_directory() . $legacy_filepath, ); // Check the first matching one. foreach ( $possible_paths as $path ) { if ( is_readable( $path ) ) { $has_template = true; break; } } /** * Filters the value of the result of the block template check. * * @since x.x.x * * @param boolean $has_template value to be filtered. * @param string $template_name The name of the template. */ return (bool) apply_filters( 'woocommerce_has_block_template', $has_template, $template_name ); } /** * Get the default filename for a template except if a block template with * the same name exists. * * @since 3.0.0 * @since 5.5.0 If a block template with the same name exists, return an * empty string. * @since 6.3.0 It checks custom product taxonomies * @return string */ private static function get_template_loader_default_file() { if ( is_singular( 'product' ) && ! self::has_block_template( 'single-product' ) ) { $default_file = 'single-product.php'; } elseif ( is_product_taxonomy() ) { $object = get_queried_object(); if ( self::taxonomy_has_block_template( $object ) ) { $default_file = ''; } else { if ( taxonomy_is_product_attribute( $object->taxonomy ) ) { $default_file = 'taxonomy-product-attribute.php'; } elseif ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) { $default_file = 'taxonomy-' . $object->taxonomy . '.php'; } elseif ( ! self::has_block_template( 'archive-product' ) ) { $default_file = 'archive-product.php'; } else { $default_file = ''; } } } elseif ( ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) && ! self::has_block_template( 'archive-product' ) ) { $default_file = self::$theme_support ? 'archive-product.php' : ''; } else { $default_file = ''; } return $default_file; } /** * Get an array of filenames to search for a given template. * * @since 3.0.0 * @param string $default_file The default file name. * @return string[] */ private static function get_template_loader_files( $default_file ) { $templates = apply_filters( 'woocommerce_template_loader_files', array(), $default_file ); $templates[] = 'woocommerce.php'; if ( is_page_template() ) { $page_template = get_page_template_slug(); if ( $page_template ) { $validated_file = validate_file( $page_template ); if ( 0 === $validated_file ) { $templates[] = $page_template; } else { error_log( "WooCommerce: Unable to validate template path: \"$page_template\". Error Code: $validated_file." ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } } } if ( is_singular( 'product' ) ) { $object = get_queried_object(); $name_decoded = urldecode( $object->post_name ); if ( $name_decoded !== $object->post_name ) { $templates[] = "single-product-{$name_decoded}.php"; } $templates[] = "single-product-{$object->post_name}.php"; } if ( is_product_taxonomy() ) { $object = get_queried_object(); $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php'; $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php'; $templates[] = 'taxonomy-' . $object->taxonomy . '.php'; $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '.php'; if ( taxonomy_is_product_attribute( $object->taxonomy ) ) { $templates[] = 'taxonomy-product_attribute.php'; $templates[] = WC()->template_path() . 'taxonomy-product_attribute.php'; $templates[] = $default_file; } if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) { $cs_taxonomy = str_replace( '_', '-', $object->taxonomy ); $cs_default = str_replace( '_', '-', $default_file ); $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php'; $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '-' . $object->slug . '.php'; $templates[] = 'taxonomy-' . $object->taxonomy . '.php'; $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '.php'; $templates[] = $cs_default; } } $templates[] = $default_file; if ( isset( $cs_default ) ) { $templates[] = WC()->template_path() . $cs_default; } $templates[] = WC()->template_path() . $default_file; return array_unique( $templates ); } /** * Load comments template. * * @param string $template template to load. * @return string */ public static function comments_template_loader( $template ) { if ( get_post_type() !== 'product' ) { return $template; } $check_dirs = array( trailingslashit( get_stylesheet_directory() ) . WC()->template_path(), trailingslashit( get_template_directory() ) . WC()->template_path(), trailingslashit( get_stylesheet_directory() ), trailingslashit( get_template_directory() ), trailingslashit( WC()->plugin_path() ) . 'templates/', ); if ( WC_TEMPLATE_DEBUG_MODE ) { $check_dirs = array( array_pop( $check_dirs ) ); } foreach ( $check_dirs as $dir ) { if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) { return trailingslashit( $dir ) . 'single-product-reviews.php'; } } } /** * Unsupported theme compatibility methods. */ /** * Hook in methods to enhance the unsupported theme experience on pages. * * @since 3.3.0 */ public static function unsupported_theme_init() { if ( 0 < self::$shop_page_id ) { if ( is_product_taxonomy() ) { self::unsupported_theme_tax_archive_init(); } elseif ( is_product() ) { self::unsupported_theme_product_page_init(); } else { self::unsupported_theme_shop_page_init(); } } } /** * Hook in methods to enhance the unsupported theme experience on the Shop page. * * @since 3.3.0 */ private static function unsupported_theme_shop_page_init() { add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 ); add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 ); add_filter( 'comments_number', array( __CLASS__, 'unsupported_theme_comments_number_filter' ) ); } /** * Hook in methods to enhance the unsupported theme experience on Product pages. * * @since 3.3.0 */ private static function unsupported_theme_product_page_init() { add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 ); add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) ); add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) ); remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 ); remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 ); self::add_support_for_product_page_gallery(); } /** * Add theme support for Product page gallery. * * @since x.x.x */ private static function add_support_for_product_page_gallery() { add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-slider' ); } /** * Enhance the unsupported theme experience on Product Category and Attribute pages by rendering * those pages using the single template and shortcode-based content. To do this we make a dummy * post and set a shortcode as the post content. This approach is adapted from bbPress. * * @since 3.3.0 */ private static function unsupported_theme_tax_archive_init() { global $wp_query, $post; $queried_object = get_queried_object(); $args = self::get_current_shop_view_args(); $shortcode_args = array( 'page' => $args->page, 'columns' => $args->columns, 'rows' => $args->rows, 'orderby' => '', 'order' => '', 'paginate' => true, 'cache' => false, ); if ( is_product_category() ) { $shortcode_args['category'] = sanitize_title( $queried_object->slug ); } elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) { $shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy ); $shortcode_args['terms'] = sanitize_title( $queried_object->slug ); } elseif ( is_product_tag() ) { $shortcode_args['tag'] = sanitize_title( $queried_object->slug ); } else { // Default theme archive for all other taxonomies. return; } // Description handling. if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $prefix = '