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

184 lines
5.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() {
2017-11-08 18:09:39 +00:00
add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
add_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
2017-11-08 16:46:15 +00:00
}
/**
* Filter the content and insert WooCommerce content. @todo
*
* @since 3.3.0
2017-11-08 18:09:39 +00:00
* @param string $content Existing post content.
2017-11-08 16:46:15 +00:00
* @return string
*/
public static function the_content_filter( $content ) {
2017-11-08 18:09:39 +00:00
if ( ! current_theme_supports( 'woocommerce' ) && is_page( wc_get_page_id( 'shop' ) ) && is_main_query() ) {
2017-11-08 16:46:15 +00:00
$page = max( 1, absint( get_query_var( 'paged' ) ) );
$columns = 3;
2017-11-08 18:09:39 +00:00
$rows = 3;
$shortcode = new WC_Shortcode_Products(
array_merge(
wc()->query->get_catalog_ordering_args(),
array(
'page' => $page,
'columns' => $columns,
'rows' => $rows,
'paginate' => true,
)
),
'products' );
2017-11-08 16:46:15 +00:00
$content .= $shortcode->get_content();
2017-11-08 18:09:39 +00:00
// Remove self to avoid nested calls.
remove_filter( 'the_content', array( __CLASS__, 'the_content_filter' ) );
2017-11-08 16:46:15 +00:00
}
return $content;
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 ) {
2017-11-08 18:09:39 +00:00
if ( is_embed() || ! current_theme_supports( 'woocommerce' ) ) {
2016-04-25 09:27:53 +00:00
return $template;
}
2016-12-06 11:52:15 +00:00
if ( $default_file = self::get_template_loader_default_file() ) {
/**
* Filter hook to choose which files to find before WooCommerce does it's own logic.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-12-06 11:52:15 +00:00
* @var array
*/
$search_files = self::get_template_loader_files( $default_file );
$template = locate_template( $search_files );
2013-06-06 12:27:04 +00:00
2016-12-06 11:52:15 +00:00
if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
$template = WC()->plugin_path() . '/templates/' . $default_file;
}
}
2013-06-06 12:27:04 +00:00
2016-12-06 11:52:15 +00:00
return $template;
}
2013-06-06 12:27:04 +00:00
2016-12-06 11:52:15 +00:00
/**
* Get the default filename for a template.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-12-06 11:52:15 +00:00
* @return string
*/
private static function get_template_loader_default_file() {
if ( is_singular( 'product' ) ) {
$default_file = 'single-product.php';
} elseif ( is_product_taxonomy() ) {
$term = get_queried_object();
2013-06-06 12:27:04 +00:00
if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
2016-12-06 11:52:15 +00:00
$default_file = 'taxonomy-' . $term->taxonomy . '.php';
} else {
2016-12-06 11:52:15 +00:00
$default_file = 'archive-product.php';
}
2016-12-06 11:52:15 +00:00
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
$default_file = 'archive-product.php';
} else {
$default_file = '';
}
return $default_file;
}
2016-12-06 11:52:15 +00:00
/**
* Get an array of filenames to search for a given template.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
* @param string $default_file The default file name.
2016-12-06 11:52:15 +00:00
* @return string[]
*/
private static function get_template_loader_files( $default_file ) {
$search_files = apply_filters( 'woocommerce_template_loader_files', array(), $default_file );
$search_files[] = 'woocommerce.php';
if ( is_page_template() ) {
$search_files[] = get_page_template_slug();
}
2016-12-06 11:52:15 +00:00
if ( is_product_taxonomy() ) {
$term = get_queried_object();
$search_files[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$search_files[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php';
$search_files[] = 'taxonomy-' . $term->taxonomy . '.php';
$search_files[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '.php';
2013-06-06 12:27:04 +00:00
}
2016-12-06 11:52:15 +00:00
$search_files[] = $default_file;
$search_files[] = WC()->template_path() . $default_file;
2013-06-06 12:27:04 +00:00
2016-12-06 11:52:15 +00:00
return array_unique( $search_files );
2013-06-06 12:27:04 +00:00
}
/**
* Load comments template.
2013-06-06 12:27:04 +00:00
*
* @param mixed $template
* @return string
*/
public static function comments_template_loader( $template ) {
2017-11-08 18:09:39 +00:00
if ( get_post_type() !== 'product' || ! current_theme_supports( 'woocommerce' ) ) {
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();