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

105 lines
3.2 KiB
PHP
Raw Normal View History

2013-06-06 12:27:04 +00:00
<?php
/**
* 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
/**
* 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.
*
* Templates are in the 'templates' folder. woocommerce looks for theme
* overrides in /theme/woocommerce/ by default
*
* 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
* 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 = '';
if ( is_single() && get_post_type() == 'product' ) {
$file = 'single-product.php';
$find[] = $file;
2013-10-24 18:36:22 +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';
$find[] = WC_TEMPLATE_PATH . 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$find[] = 'taxonomy-' . $term->taxonomy . '.php';
$find[] = WC_TEMPLATE_PATH . 'taxonomy-' . $term->taxonomy . '.php';
$find[] = $file;
$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;
2013-10-24 18:36:22 +00:00
$find[] = WC_TEMPLATE_PATH . $file;
2013-06-06 12:27:04 +00:00
}
if ( $file ) {
$template = locate_template( array_unique( $find ) );
$status_options = get_option( 'woocommerce_status_options', array() );
if ( ! $template || ( ! empty( $status_options['template_debug_mode'] ) && current_user_can( 'manage_options' ) ) ) {
$template = WC()->plugin_path() . '/templates/' . $file;
}
2013-06-06 12:27:04 +00:00
}
return $template;
}
/**
* comments_template_loader function.
*
* @param mixed $template
* @return string
*/
public static function comments_template_loader( $template ) {
2013-06-06 12:27:04 +00:00
if ( get_post_type() !== 'product' )
return $template;
2013-10-24 18:36:22 +00:00
if ( file_exists( STYLESHEETPATH . '/' . WC_TEMPLATE_PATH . 'single-product-reviews.php' ))
return STYLESHEETPATH . '/' . WC_TEMPLATE_PATH . 'single-product-reviews.php';
elseif ( file_exists( TEMPLATEPATH . '/' . WC_TEMPLATE_PATH . 'single-product-reviews.php' ))
return TEMPLATEPATH . '/' . WC_TEMPLATE_PATH . 'single-product-reviews.php';
2013-06-14 15:15:26 +00:00
elseif ( file_exists( STYLESHEETPATH . '/' . 'single-product-reviews.php' ))
return STYLESHEETPATH . '/' . 'single-product-reviews.php';
elseif ( file_exists( TEMPLATEPATH . '/' . 'single-product-reviews.php' ))
return TEMPLATEPATH . '/' . 'single-product-reviews.php';
2013-06-06 12:27:04 +00:00
else
return WC()->plugin_path() . '/templates/single-product-reviews.php';
2013-06-06 12:27:04 +00:00
}
}
WC_Template_Loader::init();