WC_Template_Loader

Template Loader.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-template-loader.php  
  1. class WC_Template_Loader { 
  2.  
  3. /** 
  4. * Hook in methods. 
  5. */ 
  6. public static function init() { 
  7. add_filter( 'template_include', array( __CLASS__, 'template_loader' ) ); 
  8. add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) ); 
  9.  
  10. /** 
  11. * Load a template. 
  12. * Handles template usage so that we can use our own templates instead of the themes. 
  13. * Templates are in the 'templates' folder. woocommerce looks for theme. 
  14. * overrides in /theme/woocommerce/ by default. 
  15. * For beginners, it also looks for a woocommerce.php template first. If the user adds. 
  16. * this to the theme (containing a woocommerce() inside) this will be used for all. 
  17. * woocommerce templates. 
  18. * @param mixed $template 
  19. * @return string 
  20. */ 
  21. public static function template_loader( $template ) { 
  22. if ( is_embed() ) { 
  23. return $template; 
  24.  
  25. if ( $default_file = self::get_template_loader_default_file() ) { 
  26. /** 
  27. * Filter hook to choose which files to find before WooCommerce does it's own logic. 
  28. * @since 3.0.0 
  29. * @var array 
  30. */ 
  31. $search_files = self::get_template_loader_files( $default_file ); 
  32. $template = locate_template( $search_files ); 
  33.  
  34. if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) { 
  35. $template = WC()->plugin_path() . '/templates/' . $default_file; 
  36.  
  37. return $template; 
  38.  
  39. /** 
  40. * Get the default filename for a template. 
  41. * @since 3.0.0 
  42. * @return string 
  43. */ 
  44. private static function get_template_loader_default_file() { 
  45. if ( is_singular( 'product' ) ) { 
  46. $default_file = 'single-product.php'; 
  47. } elseif ( is_product_taxonomy() ) { 
  48. $term = get_queried_object(); 
  49.  
  50. if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) { 
  51. $default_file = 'taxonomy-' . $term->taxonomy . '.php'; 
  52. } else { 
  53. $default_file = 'archive-product.php'; 
  54. } elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) { 
  55. $default_file = 'archive-product.php'; 
  56. } else { 
  57. $default_file = ''; 
  58. return $default_file; 
  59.  
  60. /** 
  61. * Get an array of filenames to search for a given template. 
  62. * @since 3.0.0 
  63. * @param string $file The default file name. 
  64. * @return string[] 
  65. */ 
  66. private static function get_template_loader_files( $default_file ) { 
  67. $search_files = apply_filters( 'woocommerce_template_loader_files', array(), $default_file ); 
  68. $search_files[] = 'woocommerce.php'; 
  69.  
  70. if ( is_product_taxonomy() ) { 
  71. $term = get_queried_object(); 
  72. $search_files[] = 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php'; 
  73. $search_files[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '-' . $term->slug . '.php'; 
  74. $search_files[] = 'taxonomy-' . $term->taxonomy . '.php'; 
  75. $search_files[] = WC()->template_path() . 'taxonomy-' . $term->taxonomy . '.php'; 
  76.  
  77. $search_files[] = $default_file; 
  78. $search_files[] = WC()->template_path() . $default_file; 
  79.  
  80. return array_unique( $search_files ); 
  81.  
  82. /** 
  83. * Load comments template. 
  84. * @param mixed $template 
  85. * @return string 
  86. */ 
  87. public static function comments_template_loader( $template ) { 
  88. if ( get_post_type() !== 'product' ) { 
  89. return $template; 
  90.  
  91. $check_dirs = array( 
  92. trailingslashit( get_stylesheet_directory() ) . WC()->template_path(),  
  93. trailingslashit( get_template_directory() ) . WC()->template_path(),  
  94. trailingslashit( get_stylesheet_directory() ),  
  95. trailingslashit( get_template_directory() ),  
  96. trailingslashit( WC()->plugin_path() ) . 'templates/',  
  97. ); 
  98.  
  99. if ( WC_TEMPLATE_DEBUG_MODE ) { 
  100. $check_dirs = array( array_pop( $check_dirs ) ); 
  101.  
  102. foreach ( $check_dirs as $dir ) { 
  103. if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) { 
  104. return trailingslashit( $dir ) . 'single-product-reviews.php';