woocommerce/includes/class-wc-template-loader.php

122 lines
3.1 KiB
PHP
Raw Normal View History

2013-06-06 12:27:04 +00:00
<?php
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
2015-11-03 13:53:50 +00:00
* Template Loader
*
* @class WC_Template
* @version 2.2.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
*/
class WC_Template_Loader {
2013-06-06 12:27:04 +00:00
/**
2015-11-03 13:31:20 +00:00
* Hook in methods.
*/
public static function init() {
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
2013-06-06 12:27:04 +00:00
}
/**
* Load a template.
*
* Handles template usage so that we can use our own templates instead of the themes.
*
2015-11-03 13:31:20 +00:00
* Templates are in the 'templates' folder. woocommerce looks for theme.
* overrides in /theme/woocommerce/ by default.
2013-06-06 12:27:04 +00:00
*
2015-11-03 13:31:20 +00:00
* For beginners, it also looks for a woocommerce.php template first. If the user adds.
* this to the theme (containing a woocommerce() inside) this will be used for all.
2013-06-06 12:27:04 +00:00
* woocommerce templates.
*
* @param mixed $template
* @return string
*/
public static function template_loader( $template ) {
2013-06-06 12:27:04 +00:00
$find = array( 'woocommerce.php' );
$file = '';
2016-04-25 09:27:53 +00:00
if ( is_embed() ) {
return $template;
}
2013-06-06 12:27:04 +00:00
if ( is_single() && get_post_type() == 'product' ) {
$file = 'single-product.php';
$find[] = $file;
2014-06-19 12:45:09 +00:00
$find[] = WC()->template_path() . $file;
2013-06-06 12:27:04 +00:00
} elseif ( is_product_taxonomy() ) {
2013-06-06 12:27:04 +00:00
$term = get_queried_object();
2013-06-06 12:27:04 +00:00
if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
$file = 'taxonomy-' . $term->taxonomy . '.php';
} else {
$file = 'archive-product.php';
}
$find[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
2014-06-19 12:45:09 +00:00
$find[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$find[] = 'taxonomy-' . $term->taxonomy . '.php';
2014-06-19 12:45:09 +00:00
$find[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '.php';
$find[] = $file;
2014-06-19 12:45:09 +00:00
$find[] = WC()->template_path() . $file;
2013-06-06 12:27:04 +00:00
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
2013-06-06 12:27:04 +00:00
$file = 'archive-product.php';
$find[] = $file;
2014-06-19 12:45:09 +00:00
$find[] = WC()->template_path() . $file;
2013-06-06 12:27:04 +00:00
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
$template = WC()->plugin_path() . '/templates/' . $file;
}
2013-06-06 12:27:04 +00:00
}
return $template;
}
/**
* Load comments template.
2013-06-06 12:27:04 +00:00
*
* @param mixed $template
* @return string
*/
public static function comments_template_loader( $template ) {
if ( get_post_type() !== 'product' ) {
2013-06-06 12:27:04 +00:00
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 ) );
}
2013-06-06 12:27:04 +00:00
foreach ( $check_dirs as $dir ) {
if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) {
return trailingslashit( $dir ) . 'single-product-reviews.php';
}
}
2013-06-06 12:27:04 +00:00
}
}
2014-06-19 12:45:09 +00:00
WC_Template_Loader::init();