class-wc-template-loader.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. /**
  3. * Template Loader
  4. *
  5. * @package WooCommerce/Classes
  6. */
  7. defined( 'ABSPATH' ) || exit;
  8. /**
  9. * Template loader class.
  10. */
  11. class WC_Template_Loader {
  12. /**
  13. * Store the shop page ID.
  14. *
  15. * @var integer
  16. */
  17. private static $shop_page_id = 0;
  18. /**
  19. * Store whether we're processing a product inside the_content filter.
  20. *
  21. * @var boolean
  22. */
  23. private static $in_content_filter = false;
  24. /**
  25. * Is WooCommerce support defined?
  26. *
  27. * @var boolean
  28. */
  29. private static $theme_support = false;
  30. /**
  31. * Hook in methods.
  32. */
  33. public static function init() {
  34. self::$theme_support = current_theme_supports( 'woocommerce' );
  35. self::$shop_page_id = wc_get_page_id( 'shop' );
  36. // Supported themes.
  37. if ( self::$theme_support ) {
  38. add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
  39. add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
  40. } else {
  41. // Unsupported themes.
  42. add_action( 'template_redirect', array( __CLASS__, 'unsupported_theme_init' ) );
  43. }
  44. }
  45. /**
  46. * Load a template.
  47. *
  48. * Handles template usage so that we can use our own templates instead of the themes.
  49. *
  50. * Templates are in the 'templates' folder. woocommerce looks for theme.
  51. * overrides in /theme/woocommerce/ by default.
  52. *
  53. * For beginners, it also looks for a woocommerce.php template first. If the user adds.
  54. * this to the theme (containing a woocommerce() inside) this will be used for all.
  55. * woocommerce templates.
  56. *
  57. * @param string $template Template to load.
  58. * @return string
  59. */
  60. public static function template_loader( $template ) {
  61. if ( is_embed() ) {
  62. return $template;
  63. }
  64. $default_file = self::get_template_loader_default_file();
  65. if ( $default_file ) {
  66. /**
  67. * Filter hook to choose which files to find before WooCommerce does it's own logic.
  68. *
  69. * @since 3.0.0
  70. * @var array
  71. */
  72. $search_files = self::get_template_loader_files( $default_file );
  73. $template = locate_template( $search_files );
  74. if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
  75. $template = WC()->plugin_path() . '/templates/' . $default_file;
  76. }
  77. }
  78. return $template;
  79. }
  80. /**
  81. * Get the default filename for a template.
  82. *
  83. * @since 3.0.0
  84. * @return string
  85. */
  86. private static function get_template_loader_default_file() {
  87. if ( is_singular( 'product' ) ) {
  88. $default_file = 'single-product.php';
  89. } elseif ( is_product_taxonomy() ) {
  90. $object = get_queried_object();
  91. if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
  92. $default_file = 'taxonomy-' . $object->taxonomy . '.php';
  93. } else {
  94. $default_file = 'archive-product.php';
  95. }
  96. } elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
  97. $default_file = self::$theme_support ? 'archive-product.php' : '';
  98. } else {
  99. $default_file = '';
  100. }
  101. return $default_file;
  102. }
  103. /**
  104. * Get an array of filenames to search for a given template.
  105. *
  106. * @since 3.0.0
  107. * @param string $default_file The default file name.
  108. * @return string[]
  109. */
  110. private static function get_template_loader_files( $default_file ) {
  111. $templates = apply_filters( 'woocommerce_template_loader_files', array(), $default_file );
  112. $templates[] = 'woocommerce.php';
  113. if ( is_page_template() ) {
  114. $templates[] = get_page_template_slug();
  115. }
  116. if ( is_singular( 'product' ) ) {
  117. $object = get_queried_object();
  118. $name_decoded = urldecode( $object->post_name );
  119. if ( $name_decoded !== $object->post_name ) {
  120. $templates[] = "single-product-{$name_decoded}.php";
  121. }
  122. $templates[] = "single-product-{$object->post_name}.php";
  123. }
  124. if ( is_product_taxonomy() ) {
  125. $object = get_queried_object();
  126. $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
  127. $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
  128. $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
  129. $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '.php';
  130. }
  131. $templates[] = $default_file;
  132. $templates[] = WC()->template_path() . $default_file;
  133. return array_unique( $templates );
  134. }
  135. /**
  136. * Load comments template.
  137. *
  138. * @param string $template template to load.
  139. * @return string
  140. */
  141. public static function comments_template_loader( $template ) {
  142. if ( get_post_type() !== 'product' ) {
  143. return $template;
  144. }
  145. $check_dirs = array(
  146. trailingslashit( get_stylesheet_directory() ) . WC()->template_path(),
  147. trailingslashit( get_template_directory() ) . WC()->template_path(),
  148. trailingslashit( get_stylesheet_directory() ),
  149. trailingslashit( get_template_directory() ),
  150. trailingslashit( WC()->plugin_path() ) . 'templates/',
  151. );
  152. if ( WC_TEMPLATE_DEBUG_MODE ) {
  153. $check_dirs = array( array_pop( $check_dirs ) );
  154. }
  155. foreach ( $check_dirs as $dir ) {
  156. if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) {
  157. return trailingslashit( $dir ) . 'single-product-reviews.php';
  158. }
  159. }
  160. }
  161. /**
  162. * Unsupported theme compatibility methods.
  163. */
  164. /**
  165. * Hook in methods to enhance the unsupported theme experience on pages.
  166. *
  167. * @since 3.3.0
  168. */
  169. public static function unsupported_theme_init() {
  170. if ( 0 < self::$shop_page_id ) {
  171. if ( is_product_taxonomy() ) {
  172. self::unsupported_theme_tax_archive_init();
  173. } elseif ( is_product() ) {
  174. self::unsupported_theme_product_page_init();
  175. } else {
  176. self::unsupported_theme_shop_page_init();
  177. }
  178. }
  179. }
  180. /**
  181. * Hook in methods to enhance the unsupported theme experience on the Shop page.
  182. *
  183. * @since 3.3.0
  184. */
  185. private static function unsupported_theme_shop_page_init() {
  186. add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 );
  187. add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 );
  188. add_filter( 'comments_number', array( __CLASS__, 'unsupported_theme_comments_number_filter' ) );
  189. }
  190. /**
  191. * Hook in methods to enhance the unsupported theme experience on Product pages.
  192. *
  193. * @since 3.3.0
  194. */
  195. private static function unsupported_theme_product_page_init() {
  196. add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 );
  197. add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) );
  198. add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) );
  199. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  200. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  201. add_theme_support( 'wc-product-gallery-zoom' );
  202. add_theme_support( 'wc-product-gallery-lightbox' );
  203. add_theme_support( 'wc-product-gallery-slider' );
  204. }
  205. /**
  206. * Enhance the unsupported theme experience on Product Category and Attribute pages by rendering
  207. * those pages using the single template and shortcode-based content. To do this we make a dummy
  208. * post and set a shortcode as the post content. This approach is adapted from bbPress.
  209. *
  210. * @since 3.3.0
  211. */
  212. private static function unsupported_theme_tax_archive_init() {
  213. global $wp_query, $post;
  214. $queried_object = get_queried_object();
  215. $args = self::get_current_shop_view_args();
  216. $shortcode_args = array(
  217. 'page' => $args->page,
  218. 'columns' => $args->columns,
  219. 'rows' => $args->rows,
  220. 'orderby' => '',
  221. 'order' => '',
  222. 'paginate' => true,
  223. 'cache' => false,
  224. );
  225. if ( is_product_category() ) {
  226. $shortcode_args['category'] = sanitize_title( $queried_object->slug );
  227. } elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
  228. $shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
  229. $shortcode_args['terms'] = sanitize_title( $queried_object->slug );
  230. } elseif ( is_product_tag() ) {
  231. $shortcode_args['tag'] = sanitize_title( $queried_object->slug );
  232. } else {
  233. // Default theme archive for all other taxonomies.
  234. return;
  235. }
  236. // Description handling.
  237. if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // WPCS: input var ok, CSRF ok.
  238. $prefix = '<div class="term-description">' . wc_format_content( $queried_object->description ) . '</div>'; // WPCS: XSS ok.
  239. } else {
  240. $prefix = '';
  241. }
  242. add_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
  243. $shortcode = new WC_Shortcode_Products( $shortcode_args );
  244. remove_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
  245. $shop_page = get_post( self::$shop_page_id );
  246. $dummy_post_properties = array(
  247. 'ID' => 0,
  248. 'post_status' => 'publish',
  249. 'post_author' => $shop_page->post_author,
  250. 'post_parent' => 0,
  251. 'post_type' => 'page',
  252. 'post_date' => $shop_page->post_date,
  253. 'post_date_gmt' => $shop_page->post_date_gmt,
  254. 'post_modified' => $shop_page->post_modified,
  255. 'post_modified_gmt' => $shop_page->post_modified_gmt,
  256. 'post_content' => $prefix . $shortcode->get_content(),
  257. 'post_title' => wc_clean( $queried_object->name ),
  258. 'post_excerpt' => '',
  259. 'post_content_filtered' => '',
  260. 'post_mime_type' => '',
  261. 'post_password' => '',
  262. 'post_name' => $queried_object->slug,
  263. 'guid' => '',
  264. 'menu_order' => 0,
  265. 'pinged' => '',
  266. 'to_ping' => '',
  267. 'ping_status' => '',
  268. 'comment_status' => 'closed',
  269. 'comment_count' => 0,
  270. 'filter' => 'raw',
  271. );
  272. // Set the $post global.
  273. $post = new WP_Post( (object) $dummy_post_properties ); // @codingStandardsIgnoreLine.
  274. // Copy the new post global into the main $wp_query.
  275. $wp_query->post = $post;
  276. $wp_query->posts = array( $post );
  277. // Prevent comments form from appearing.
  278. $wp_query->post_count = 1;
  279. $wp_query->is_404 = false;
  280. $wp_query->is_page = true;
  281. $wp_query->is_single = true;
  282. $wp_query->is_archive = false;
  283. $wp_query->is_tax = true;
  284. $wp_query->max_num_pages = 0;
  285. // Prepare everything for rendering.
  286. setup_postdata( $post );
  287. remove_all_filters( 'the_content' );
  288. remove_all_filters( 'the_excerpt' );
  289. add_filter( 'template_include', array( __CLASS__, 'force_single_template_filter' ) );
  290. }
  291. /**
  292. * Add layered nav args to WP_Query args generated by the 'products' shortcode.
  293. *
  294. * @since 3.3.4
  295. * @param array $query WP_Query args.
  296. * @return array
  297. */
  298. public static function unsupported_archive_layered_nav_compatibility( $query ) {
  299. foreach ( WC()->query->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
  300. $query['tax_query'][] = array(
  301. 'taxonomy' => $taxonomy,
  302. 'field' => 'slug',
  303. 'terms' => $data['terms'],
  304. 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
  305. 'include_children' => false,
  306. );
  307. }
  308. return $query;
  309. }
  310. /**
  311. * Force the loading of one of the single templates instead of whatever template was about to be loaded.
  312. *
  313. * @since 3.3.0
  314. * @param string $template Path to template.
  315. * @return string
  316. */
  317. public static function force_single_template_filter( $template ) {
  318. $possible_templates = array(
  319. 'page',
  320. 'single',
  321. 'singular',
  322. 'index',
  323. );
  324. foreach ( $possible_templates as $possible_template ) {
  325. $path = get_query_template( $possible_template );
  326. if ( $path ) {
  327. return $path;
  328. }
  329. }
  330. return $template;
  331. }
  332. /**
  333. * Get information about the current shop page view.
  334. *
  335. * @since 3.3.0
  336. * @return array
  337. */
  338. private static function get_current_shop_view_args() {
  339. return (object) array(
  340. 'page' => absint( max( 1, absint( get_query_var( 'paged' ) ) ) ),
  341. 'columns' => wc_get_default_products_per_row(),
  342. 'rows' => wc_get_default_product_rows_per_page(),
  343. );
  344. }
  345. /**
  346. * Filter the title and insert WooCommerce content on the shop page.
  347. *
  348. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  349. *
  350. * @since 3.3.0
  351. * @param string $title Existing title.
  352. * @param int $id ID of the post being filtered.
  353. * @return string
  354. */
  355. public static function unsupported_theme_title_filter( $title, $id ) {
  356. if ( self::$theme_support || ! $id !== self::$shop_page_id ) {
  357. return $title;
  358. }
  359. if ( is_page( self::$shop_page_id ) || ( is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) === self::$shop_page_id ) ) {
  360. $args = self::get_current_shop_view_args();
  361. $title_suffix = array();
  362. if ( $args->page > 1 ) {
  363. /* translators: %d: Page number. */
  364. $title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
  365. }
  366. if ( $title_suffix ) {
  367. $title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
  368. }
  369. }
  370. return $title;
  371. }
  372. /**
  373. * Filter the content and insert WooCommerce content on the shop page.
  374. *
  375. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  376. *
  377. * @since 3.3.0
  378. * @param string $content Existing post content.
  379. * @return string
  380. */
  381. public static function unsupported_theme_shop_content_filter( $content ) {
  382. global $wp_query;
  383. if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
  384. return $content;
  385. }
  386. self::$in_content_filter = true;
  387. // Remove the filter we're in to avoid nested calls.
  388. remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ) );
  389. // Unsupported theme shop page.
  390. if ( is_page( self::$shop_page_id ) ) {
  391. $args = self::get_current_shop_view_args();
  392. $shortcode = new WC_Shortcode_Products(
  393. array_merge(
  394. wc()->query->get_catalog_ordering_args(),
  395. array(
  396. 'page' => $args->page,
  397. 'columns' => $args->columns,
  398. 'rows' => $args->rows,
  399. 'orderby' => '',
  400. 'order' => '',
  401. 'paginate' => true,
  402. 'cache' => false,
  403. )
  404. ),
  405. 'products' );
  406. // Allow queries to run e.g. layered nav.
  407. add_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
  408. $content = $content . $shortcode->get_content();
  409. // Remove actions and self to avoid nested calls.
  410. remove_action( 'pre_get_posts', array( wc()->query, 'product_query' ) );
  411. WC()->query->remove_ordering_args();
  412. }
  413. self::$in_content_filter = false;
  414. return $content;
  415. }
  416. /**
  417. * Filter the content and insert WooCommerce content on the shop page.
  418. *
  419. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  420. *
  421. * @since 3.3.0
  422. * @param string $content Existing post content.
  423. * @return string
  424. */
  425. public static function unsupported_theme_product_content_filter( $content ) {
  426. global $wp_query;
  427. if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
  428. return $content;
  429. }
  430. self::$in_content_filter = true;
  431. // Remove the filter we're in to avoid nested calls.
  432. remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ) );
  433. if ( is_product() ) {
  434. $content = do_shortcode( '[product_page id="' . get_the_ID() . '" show_title=0]' );
  435. }
  436. self::$in_content_filter = false;
  437. return $content;
  438. }
  439. /**
  440. * Suppress the comments number on the Shop page for unsupported themes since there is no commenting on the Shop page.
  441. *
  442. * @since 3.4.5
  443. * @param string $comments_number The comments number text.
  444. * @return string
  445. */
  446. public static function unsupported_theme_comments_number_filter( $comments_number ) {
  447. if ( is_page( self::$shop_page_id ) ) {
  448. return '';
  449. }
  450. return $comments_number;
  451. }
  452. /**
  453. * Are we filtering content for unsupported themes?
  454. *
  455. * @since 3.3.2
  456. * @return bool
  457. */
  458. public static function in_content_filter() {
  459. return (bool) self::$in_content_filter;
  460. }
  461. /**
  462. * Prevent the main featured image on product pages because there will be another featured image
  463. * in the gallery.
  464. *
  465. * @since 3.3.0
  466. * @param string $html Img element HTML.
  467. * @return string
  468. */
  469. public static function unsupported_theme_single_featured_image_filter( $html ) {
  470. if ( self::in_content_filter() || ! is_product() || ! is_main_query() ) {
  471. return $html;
  472. }
  473. return '';
  474. }
  475. /**
  476. * Remove the Review tab and just use the regular comment form.
  477. *
  478. * @param array $tabs Tab info.
  479. * @return array
  480. */
  481. public static function unsupported_theme_remove_review_tab( $tabs ) {
  482. unset( $tabs['reviews'] );
  483. return $tabs;
  484. }
  485. }
  486. add_action( 'init', array( 'WC_Template_Loader', 'init' ) );