暫無描述

class-wc-template-loader.php 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 theme's.
  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. if ( false !== strpos( $default_file, 'product_cat' ) || false !== strpos( $default_file, 'product_tag' ) ) {
  76. $cs_template = str_replace( '_', '-', $default_file );
  77. $template = WC()->plugin_path() . '/templates/' . $cs_template;
  78. } else {
  79. $template = WC()->plugin_path() . '/templates/' . $default_file;
  80. }
  81. }
  82. }
  83. return $template;
  84. }
  85. /**
  86. * Checks whether a block template with that name exists.
  87. *
  88. * @since 5.5.0
  89. * @param string $template_name Template to check.
  90. * @return boolean
  91. */
  92. private static function has_block_template( $template_name ) {
  93. if ( ! $template_name ) {
  94. return false;
  95. }
  96. return is_readable(
  97. get_stylesheet_directory() . '/block-templates/' . $template_name . '.html'
  98. );
  99. }
  100. /**
  101. * Get the default filename for a template except if a block template with
  102. * the same name exists.
  103. *
  104. * @since 3.0.0
  105. * @since 5.5.0 If a block template with the same name exists, return an
  106. * empty string.
  107. * @return string
  108. */
  109. private static function get_template_loader_default_file() {
  110. if (
  111. is_singular( 'product' ) &&
  112. ! self::has_block_template( 'single-product' )
  113. ) {
  114. $default_file = 'single-product.php';
  115. } elseif ( is_product_taxonomy() ) {
  116. $object = get_queried_object();
  117. if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
  118. if ( self::has_block_template( 'taxonomy-' . $object->taxonomy ) ) {
  119. $default_file = '';
  120. } else {
  121. $default_file = 'taxonomy-' . $object->taxonomy . '.php';
  122. }
  123. } elseif ( ! self::has_block_template( 'archive-product' ) ) {
  124. $default_file = 'archive-product.php';
  125. }
  126. } elseif (
  127. ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
  128. ! self::has_block_template( 'archive-product' )
  129. ) {
  130. $default_file = self::$theme_support ? 'archive-product.php' : '';
  131. } else {
  132. $default_file = '';
  133. }
  134. return $default_file;
  135. }
  136. /**
  137. * Get an array of filenames to search for a given template.
  138. *
  139. * @since 3.0.0
  140. * @param string $default_file The default file name.
  141. * @return string[]
  142. */
  143. private static function get_template_loader_files( $default_file ) {
  144. $templates = apply_filters( 'woocommerce_template_loader_files', array(), $default_file );
  145. $templates[] = 'woocommerce.php';
  146. if ( is_page_template() ) {
  147. $page_template = get_page_template_slug();
  148. if ( $page_template ) {
  149. $validated_file = validate_file( $page_template );
  150. if ( 0 === $validated_file ) {
  151. $templates[] = $page_template;
  152. } else {
  153. error_log( "WooCommerce: Unable to validate template path: \"$page_template\". Error Code: $validated_file." ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
  154. }
  155. }
  156. }
  157. if ( is_singular( 'product' ) ) {
  158. $object = get_queried_object();
  159. $name_decoded = urldecode( $object->post_name );
  160. if ( $name_decoded !== $object->post_name ) {
  161. $templates[] = "single-product-{$name_decoded}.php";
  162. }
  163. $templates[] = "single-product-{$object->post_name}.php";
  164. }
  165. if ( is_product_taxonomy() ) {
  166. $object = get_queried_object();
  167. $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
  168. $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
  169. $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
  170. $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '.php';
  171. if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
  172. $cs_taxonomy = str_replace( '_', '-', $object->taxonomy );
  173. $cs_default = str_replace( '_', '-', $default_file );
  174. $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
  175. $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '-' . $object->slug . '.php';
  176. $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
  177. $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '.php';
  178. $templates[] = $cs_default;
  179. }
  180. }
  181. $templates[] = $default_file;
  182. if ( isset( $cs_default ) ) {
  183. $templates[] = WC()->template_path() . $cs_default;
  184. }
  185. $templates[] = WC()->template_path() . $default_file;
  186. return array_unique( $templates );
  187. }
  188. /**
  189. * Load comments template.
  190. *
  191. * @param string $template template to load.
  192. * @return string
  193. */
  194. public static function comments_template_loader( $template ) {
  195. if ( get_post_type() !== 'product' ) {
  196. return $template;
  197. }
  198. $check_dirs = array(
  199. trailingslashit( get_stylesheet_directory() ) . WC()->template_path(),
  200. trailingslashit( get_template_directory() ) . WC()->template_path(),
  201. trailingslashit( get_stylesheet_directory() ),
  202. trailingslashit( get_template_directory() ),
  203. trailingslashit( WC()->plugin_path() ) . 'templates/',
  204. );
  205. if ( WC_TEMPLATE_DEBUG_MODE ) {
  206. $check_dirs = array( array_pop( $check_dirs ) );
  207. }
  208. foreach ( $check_dirs as $dir ) {
  209. if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) {
  210. return trailingslashit( $dir ) . 'single-product-reviews.php';
  211. }
  212. }
  213. }
  214. /**
  215. * Unsupported theme compatibility methods.
  216. */
  217. /**
  218. * Hook in methods to enhance the unsupported theme experience on pages.
  219. *
  220. * @since 3.3.0
  221. */
  222. public static function unsupported_theme_init() {
  223. if ( 0 < self::$shop_page_id ) {
  224. if ( is_product_taxonomy() ) {
  225. self::unsupported_theme_tax_archive_init();
  226. } elseif ( is_product() ) {
  227. self::unsupported_theme_product_page_init();
  228. } else {
  229. self::unsupported_theme_shop_page_init();
  230. }
  231. }
  232. }
  233. /**
  234. * Hook in methods to enhance the unsupported theme experience on the Shop page.
  235. *
  236. * @since 3.3.0
  237. */
  238. private static function unsupported_theme_shop_page_init() {
  239. add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 );
  240. add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 );
  241. add_filter( 'comments_number', array( __CLASS__, 'unsupported_theme_comments_number_filter' ) );
  242. }
  243. /**
  244. * Hook in methods to enhance the unsupported theme experience on Product pages.
  245. *
  246. * @since 3.3.0
  247. */
  248. private static function unsupported_theme_product_page_init() {
  249. add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 );
  250. add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) );
  251. add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) );
  252. remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  253. remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
  254. add_theme_support( 'wc-product-gallery-zoom' );
  255. add_theme_support( 'wc-product-gallery-lightbox' );
  256. add_theme_support( 'wc-product-gallery-slider' );
  257. }
  258. /**
  259. * Enhance the unsupported theme experience on Product Category and Attribute pages by rendering
  260. * those pages using the single template and shortcode-based content. To do this we make a dummy
  261. * post and set a shortcode as the post content. This approach is adapted from bbPress.
  262. *
  263. * @since 3.3.0
  264. */
  265. private static function unsupported_theme_tax_archive_init() {
  266. global $wp_query, $post;
  267. $queried_object = get_queried_object();
  268. $args = self::get_current_shop_view_args();
  269. $shortcode_args = array(
  270. 'page' => $args->page,
  271. 'columns' => $args->columns,
  272. 'rows' => $args->rows,
  273. 'orderby' => '',
  274. 'order' => '',
  275. 'paginate' => true,
  276. 'cache' => false,
  277. );
  278. if ( is_product_category() ) {
  279. $shortcode_args['category'] = sanitize_title( $queried_object->slug );
  280. } elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
  281. $shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
  282. $shortcode_args['terms'] = sanitize_title( $queried_object->slug );
  283. } elseif ( is_product_tag() ) {
  284. $shortcode_args['tag'] = sanitize_title( $queried_object->slug );
  285. } else {
  286. // Default theme archive for all other taxonomies.
  287. return;
  288. }
  289. // Description handling.
  290. if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  291. $prefix = '<div class="term-description">' . wc_format_content( wp_kses_post( $queried_object->description ) ) . '</div>';
  292. } else {
  293. $prefix = '';
  294. }
  295. add_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
  296. $shortcode = new WC_Shortcode_Products( $shortcode_args );
  297. remove_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
  298. $shop_page = get_post( self::$shop_page_id );
  299. $dummy_post_properties = array(
  300. 'ID' => 0,
  301. 'post_status' => 'publish',
  302. 'post_author' => $shop_page->post_author,
  303. 'post_parent' => 0,
  304. 'post_type' => 'page',
  305. 'post_date' => $shop_page->post_date,
  306. 'post_date_gmt' => $shop_page->post_date_gmt,
  307. 'post_modified' => $shop_page->post_modified,
  308. 'post_modified_gmt' => $shop_page->post_modified_gmt,
  309. 'post_content' => $prefix . $shortcode->get_content(),
  310. 'post_title' => wc_clean( $queried_object->name ),
  311. 'post_excerpt' => '',
  312. 'post_content_filtered' => '',
  313. 'post_mime_type' => '',
  314. 'post_password' => '',
  315. 'post_name' => $queried_object->slug,
  316. 'guid' => '',
  317. 'menu_order' => 0,
  318. 'pinged' => '',
  319. 'to_ping' => '',
  320. 'ping_status' => '',
  321. 'comment_status' => 'closed',
  322. 'comment_count' => 0,
  323. 'filter' => 'raw',
  324. );
  325. // Set the $post global.
  326. $post = new WP_Post( (object) $dummy_post_properties ); // @codingStandardsIgnoreLine.
  327. // Copy the new post global into the main $wp_query.
  328. $wp_query->post = $post;
  329. $wp_query->posts = array( $post );
  330. // Prevent comments form from appearing.
  331. $wp_query->post_count = 1;
  332. $wp_query->is_404 = false;
  333. $wp_query->is_page = true;
  334. $wp_query->is_single = true;
  335. $wp_query->is_archive = false;
  336. $wp_query->is_tax = true;
  337. $wp_query->max_num_pages = 0;
  338. // Prepare everything for rendering.
  339. setup_postdata( $post );
  340. remove_all_filters( 'the_content' );
  341. remove_all_filters( 'the_excerpt' );
  342. add_filter( 'template_include', array( __CLASS__, 'force_single_template_filter' ) );
  343. }
  344. /**
  345. * Add layered nav args to WP_Query args generated by the 'products' shortcode.
  346. *
  347. * @since 3.3.4
  348. * @param array $query WP_Query args.
  349. * @return array
  350. */
  351. public static function unsupported_archive_layered_nav_compatibility( $query ) {
  352. foreach ( WC()->query->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
  353. $query['tax_query'][] = array(
  354. 'taxonomy' => $taxonomy,
  355. 'field' => 'slug',
  356. 'terms' => $data['terms'],
  357. 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
  358. 'include_children' => false,
  359. );
  360. }
  361. return $query;
  362. }
  363. /**
  364. * Force the loading of one of the single templates instead of whatever template was about to be loaded.
  365. *
  366. * @since 3.3.0
  367. * @param string $template Path to template.
  368. * @return string
  369. */
  370. public static function force_single_template_filter( $template ) {
  371. $possible_templates = array(
  372. 'page',
  373. 'single',
  374. 'singular',
  375. 'index',
  376. );
  377. foreach ( $possible_templates as $possible_template ) {
  378. $path = get_query_template( $possible_template );
  379. if ( $path ) {
  380. return $path;
  381. }
  382. }
  383. return $template;
  384. }
  385. /**
  386. * Get information about the current shop page view.
  387. *
  388. * @since 3.3.0
  389. * @return array
  390. */
  391. private static function get_current_shop_view_args() {
  392. return (object) array(
  393. 'page' => absint( max( 1, absint( get_query_var( 'paged' ) ) ) ),
  394. 'columns' => wc_get_default_products_per_row(),
  395. 'rows' => wc_get_default_product_rows_per_page(),
  396. );
  397. }
  398. /**
  399. * Filter the title and insert WooCommerce content on the shop page.
  400. *
  401. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  402. *
  403. * @since 3.3.0
  404. * @param string $title Existing title.
  405. * @param int $id ID of the post being filtered.
  406. * @return string
  407. */
  408. public static function unsupported_theme_title_filter( $title, $id ) {
  409. if ( self::$theme_support || ! $id !== self::$shop_page_id ) {
  410. return $title;
  411. }
  412. 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 ) ) {
  413. $args = self::get_current_shop_view_args();
  414. $title_suffix = array();
  415. if ( $args->page > 1 ) {
  416. /* translators: %d: Page number. */
  417. $title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
  418. }
  419. if ( $title_suffix ) {
  420. $title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
  421. }
  422. }
  423. return $title;
  424. }
  425. /**
  426. * Filter the content and insert WooCommerce content on the shop page.
  427. *
  428. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  429. *
  430. * @since 3.3.0
  431. * @param string $content Existing post content.
  432. * @return string
  433. */
  434. public static function unsupported_theme_shop_content_filter( $content ) {
  435. global $wp_query;
  436. if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
  437. return $content;
  438. }
  439. self::$in_content_filter = true;
  440. // Remove the filter we're in to avoid nested calls.
  441. remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ) );
  442. // Unsupported theme shop page.
  443. if ( is_page( self::$shop_page_id ) ) {
  444. $args = self::get_current_shop_view_args();
  445. $shortcode = new WC_Shortcode_Products(
  446. array_merge(
  447. WC()->query->get_catalog_ordering_args(),
  448. array(
  449. 'page' => $args->page,
  450. 'columns' => $args->columns,
  451. 'rows' => $args->rows,
  452. 'orderby' => '',
  453. 'order' => '',
  454. 'paginate' => true,
  455. 'cache' => false,
  456. )
  457. ),
  458. 'products'
  459. );
  460. // Allow queries to run e.g. layered nav.
  461. add_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
  462. $content = $content . $shortcode->get_content();
  463. // Remove actions and self to avoid nested calls.
  464. remove_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
  465. WC()->query->remove_ordering_args();
  466. }
  467. self::$in_content_filter = false;
  468. return $content;
  469. }
  470. /**
  471. * Filter the content and insert WooCommerce content on the shop page.
  472. *
  473. * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
  474. *
  475. * @since 3.3.0
  476. * @param string $content Existing post content.
  477. * @return string
  478. */
  479. public static function unsupported_theme_product_content_filter( $content ) {
  480. global $wp_query;
  481. if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
  482. return $content;
  483. }
  484. self::$in_content_filter = true;
  485. // Remove the filter we're in to avoid nested calls.
  486. remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ) );
  487. if ( is_product() ) {
  488. $content = do_shortcode( '[product_page id="' . get_the_ID() . '" show_title=0 status="any"]' );
  489. }
  490. self::$in_content_filter = false;
  491. return $content;
  492. }
  493. /**
  494. * Suppress the comments number on the Shop page for unsupported themes since there is no commenting on the Shop page.
  495. *
  496. * @since 3.4.5
  497. * @param string $comments_number The comments number text.
  498. * @return string
  499. */
  500. public static function unsupported_theme_comments_number_filter( $comments_number ) {
  501. if ( is_page( self::$shop_page_id ) ) {
  502. return '';
  503. }
  504. return $comments_number;
  505. }
  506. /**
  507. * Are we filtering content for unsupported themes?
  508. *
  509. * @since 3.3.2
  510. * @return bool
  511. */
  512. public static function in_content_filter() {
  513. return (bool) self::$in_content_filter;
  514. }
  515. /**
  516. * Prevent the main featured image on product pages because there will be another featured image
  517. * in the gallery.
  518. *
  519. * @since 3.3.0
  520. * @param string $html Img element HTML.
  521. * @return string
  522. */
  523. public static function unsupported_theme_single_featured_image_filter( $html ) {
  524. if ( self::in_content_filter() || ! is_product() || ! is_main_query() ) {
  525. return $html;
  526. }
  527. return '';
  528. }
  529. /**
  530. * Remove the Review tab and just use the regular comment form.
  531. *
  532. * @param array $tabs Tab info.
  533. * @return array
  534. */
  535. public static function unsupported_theme_remove_review_tab( $tabs ) {
  536. unset( $tabs['reviews'] );
  537. return $tabs;
  538. }
  539. }
  540. add_action( 'init', array( 'WC_Template_Loader', 'init' ) );