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3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-template-loader.php
woocommerce / includes Last commit date
abstracts 3 months ago admin 3 months ago blocks 10 months ago cli 8 months ago customizer 3 months ago data-stores 3 months ago emails 3 months ago export 1 year ago gateways 3 months ago import 3 months ago integrations 2 years ago interfaces 3 months ago legacy 3 months ago libraries 1 year ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 3 months ago react-admin 3 months ago rest-api 3 months ago shipping 3 months ago shortcodes 3 months ago theme-support 2 years ago tracks 3 months ago traits 5 years ago walkers 5 years ago wccom-site 9 months ago widgets 6 months ago class-wc-ajax.php 3 months ago class-wc-auth.php 1 year ago class-wc-autoloader.php 8 months ago class-wc-background-emailer.php 5 years ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 4 months ago class-wc-breadcrumb.php 3 months ago class-wc-cache-helper.php 8 months ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 4 months ago class-wc-cart-totals.php 10 months ago class-wc-cart.php 3 months ago class-wc-checkout.php 3 months ago class-wc-cli.php 9 months ago class-wc-comments.php 3 months ago class-wc-countries.php 3 months ago class-wc-coupon.php 3 months ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 3 months ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 years ago class-wc-discounts.php 10 months ago class-wc-download-handler.php 1 year ago class-wc-emails.php 3 months ago class-wc-embed.php 1 year ago class-wc-form-handler.php 4 months ago class-wc-frontend-scripts.php 3 months ago class-wc-geo-ip.php 8 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 3 months ago class-wc-https.php 2 years ago class-wc-install.php 3 months ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 3 months ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 3 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 4 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 4 months ago class-wc-order-item-shipping.php 4 months ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 4 months ago class-wc-order-query.php 3 months ago class-wc-order-refund.php 1 year ago class-wc-order.php 3 months ago class-wc-payment-gateways.php 4 months ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 3 months ago class-wc-post-types.php 4 months ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 9 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 11 months ago class-wc-product-attribute.php 3 months ago class-wc-product-download.php 3 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 3 months ago class-wc-product-grouped.php 3 months ago class-wc-product-query.php 3 months ago class-wc-product-simple.php 10 months ago class-wc-product-variable.php 3 months ago class-wc-product-variation.php 1 year ago class-wc-query.php 3 months ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 1 year ago class-wc-register-wp-admin-settings.php 4 years ago class-wc-rest-authentication.php 1 year ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 8 months ago class-wc-shipping-rate.php 11 months ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 6 months ago class-wc-shipping.php 1 year ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 3 months ago class-wc-tax.php 3 months ago class-wc-template-loader.php 6 months ago class-wc-tracker.php 8 months ago class-wc-validation.php 2 years ago class-wc-webhook.php 8 months ago class-woocommerce.php 3 months ago wc-account-functions.php 6 months ago wc-attribute-functions.php 9 months ago wc-brands-functions.php 1 year ago wc-cart-functions.php 4 months ago wc-conditional-functions.php 10 months ago wc-core-functions.php 3 months ago wc-coupon-functions.php 4 months ago wc-deprecated-functions.php 3 months ago wc-formatting-functions.php 6 months ago wc-interactivity-api-functions.php 3 months ago wc-notice-functions.php 4 months ago wc-order-functions.php 3 months ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 3 months ago wc-page-functions.php 1 year ago wc-product-functions.php 3 months ago wc-rest-functions.php 6 months ago wc-stock-functions.php 6 months ago wc-template-functions.php 3 months ago wc-template-hooks.php 9 months ago wc-term-functions.php 6 months ago wc-update-functions.php 3 months ago wc-user-functions.php 6 months ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
class-wc-template-loader.php
670 lines
1 <?php
2 /**
3 * Template Loader
4 *
5 * @package WooCommerce\Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Template loader class.
12 */
13 class WC_Template_Loader {
14
15 /**
16 * Store the shop page ID.
17 *
18 * @var integer
19 */
20 private static $shop_page_id = 0;
21
22 /**
23 * Store whether we're processing a product inside the_content filter.
24 *
25 * @var boolean
26 */
27 private static $in_content_filter = false;
28
29 /**
30 * Is WooCommerce support defined?
31 *
32 * @var boolean
33 */
34 private static $theme_support = false;
35
36 /**
37 * Hook in methods.
38 */
39 public static function init() {
40 self::$theme_support = wc_current_theme_supports_woocommerce_or_fse();
41 self::$shop_page_id = wc_get_page_id( 'shop' );
42
43 // Supported themes.
44 if ( self::$theme_support ) {
45 add_filter( 'template_include', array( __CLASS__, 'template_loader' ) );
46 add_filter( 'comments_template', array( __CLASS__, 'comments_template_loader' ) );
47
48 // Loads gallery scripts on Product page for FSE themes.
49 if ( wp_is_block_theme() ) {
50 self::add_support_for_product_page_gallery();
51 }
52 } else {
53 // Unsupported themes.
54 add_action( 'template_redirect', array( __CLASS__, 'unsupported_theme_init' ) );
55 }
56 }
57
58 /**
59 * Load a template.
60 *
61 * Handles template usage so that we can use our own templates instead of the theme's.
62 *
63 * Templates are in the 'templates' folder. WooCommerce looks for theme
64 * overrides in /theme/woocommerce/ by default.
65 *
66 * For beginners, it also looks for a woocommerce.php template first. If the user adds
67 * this to the theme (containing a woocommerce() inside) this will be used for all
68 * WooCommerce templates.
69 *
70 * @param string $template Template to load.
71 * @return string
72 */
73 public static function template_loader( $template ) {
74 if ( is_embed() ) {
75 return $template;
76 }
77
78 $default_file = self::get_template_loader_default_file();
79
80 if ( $default_file ) {
81 /**
82 * Filter hook to choose which files to find before WooCommerce does it's own logic.
83 *
84 * @since 3.0.0
85 * @var array
86 */
87 $search_files = self::get_template_loader_files( $default_file );
88 $template = locate_template( $search_files );
89
90 if ( ! $template || WC_TEMPLATE_DEBUG_MODE ) {
91 if ( false !== strpos( $default_file, 'product_cat' ) || false !== strpos( $default_file, 'product_tag' ) ) {
92 $cs_template = str_replace( '_', '-', $default_file );
93 $template = WC()->plugin_path() . '/templates/' . $cs_template;
94 } else {
95 $template = WC()->plugin_path() . '/templates/' . $default_file;
96 }
97 }
98 }
99
100 return $template;
101 }
102
103 /**
104 * Checks whether a block template for a given taxonomy exists.
105 *
106 * **Note:** This checks both the `templates` and `block-templates` directories
107 * as both conventions should be supported.
108 *
109 * @param object $taxonomy Object taxonomy to check.
110 * @return boolean
111 */
112 private static function taxonomy_has_block_template( $taxonomy ): bool {
113 if ( taxonomy_is_product_attribute( $taxonomy->taxonomy ) ) {
114 $template_name = 'taxonomy-product_attribute';
115 } else {
116 $template_name = 'taxonomy-' . $taxonomy->taxonomy;
117 }
118
119 return self::has_block_template( $template_name );
120 }
121
122 /**
123 * Checks whether a block template with that name exists.
124 *
125 * **Note: ** This checks both the `templates` and `block-templates` directories
126 * as both conventions should be supported.
127 *
128 * @since 5.5.0
129 * @param string $template_name Template to check.
130 * @return boolean
131 */
132 private static function has_block_template( $template_name ) {
133 if ( ! $template_name ) {
134 return false;
135 }
136
137 $has_template = WP_Block_Templates_Registry::get_instance()->is_registered( 'woocommerce//' . $template_name );
138
139 /**
140 * Filters the value of the result of the block template check.
141 *
142 * @since 10.2.0
143 *
144 * @param boolean $has_template value to be filtered.
145 * @param string $template_name The name of the template.
146 */
147 return (bool) apply_filters( 'woocommerce_has_block_template', $has_template, $template_name );
148 }
149
150 /**
151 * Get the default filename for a template except if a block template with
152 * the same name exists.
153 *
154 * @since 3.0.0
155 * @since 5.5.0 If a block template with the same name exists, return an
156 * empty string.
157 * @since 6.3.0 It checks custom product taxonomies
158 * @return string
159 */
160 private static function get_template_loader_default_file() {
161 if (
162 is_singular( 'product' ) &&
163 ! self::has_block_template( 'single-product' )
164 ) {
165 $default_file = 'single-product.php';
166 } elseif ( is_product_taxonomy() ) {
167 $object = get_queried_object();
168
169 if ( self::taxonomy_has_block_template( $object ) ) {
170 $default_file = '';
171 } elseif ( taxonomy_is_product_attribute( $object->taxonomy ) ) {
172 $default_file = 'taxonomy-product-attribute.php';
173 } elseif ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
174 $default_file = 'taxonomy-' . $object->taxonomy . '.php';
175 } elseif ( ! self::has_block_template( 'archive-product' ) ) {
176 $default_file = 'archive-product.php';
177 } else {
178 $default_file = '';
179 }
180 } elseif (
181 ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
182 ! self::has_block_template( 'archive-product' )
183 ) {
184 $default_file = self::$theme_support ? 'archive-product.php' : '';
185 } else {
186 $default_file = '';
187 }
188 return $default_file;
189 }
190
191 /**
192 * Get an array of filenames to search for a given template.
193 *
194 * @since 3.0.0
195 * @param string $default_file The default file name.
196 * @return string[]
197 */
198 private static function get_template_loader_files( $default_file ) {
199 $templates = apply_filters( 'woocommerce_template_loader_files', array(), $default_file );
200 $templates[] = 'woocommerce.php';
201
202 if ( is_page_template() ) {
203 $page_template = get_page_template_slug();
204
205 if ( $page_template ) {
206 $validated_file = validate_file( $page_template );
207 if ( 0 === $validated_file ) {
208 $templates[] = $page_template;
209 } else {
210 error_log( "WooCommerce: Unable to validate template path: \"$page_template\". Error Code: $validated_file." ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
211 }
212 }
213 }
214
215 if ( is_singular( 'product' ) ) {
216 $object = get_queried_object();
217 $name_decoded = urldecode( $object->post_name );
218 if ( $name_decoded !== $object->post_name ) {
219 $templates[] = "single-product-{$name_decoded}.php";
220 }
221 $templates[] = "single-product-{$object->post_name}.php";
222 }
223
224 if ( is_product_taxonomy() ) {
225 $object = get_queried_object();
226
227 $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
228 $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
229 $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
230 $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '.php';
231
232 if ( taxonomy_is_product_attribute( $object->taxonomy ) ) {
233 $templates[] = 'taxonomy-product_attribute.php';
234 $templates[] = WC()->template_path() . 'taxonomy-product_attribute.php';
235 $templates[] = $default_file;
236 }
237
238 if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
239 $cs_taxonomy = str_replace( '_', '-', $object->taxonomy );
240 $cs_default = str_replace( '_', '-', $default_file );
241 $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
242 $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '-' . $object->slug . '.php';
243 $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
244 $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '.php';
245 $templates[] = $cs_default;
246 }
247 }
248
249 $templates[] = $default_file;
250 if ( isset( $cs_default ) ) {
251 $templates[] = WC()->template_path() . $cs_default;
252 }
253 $templates[] = WC()->template_path() . $default_file;
254
255 return array_unique( $templates );
256 }
257
258 /**
259 * Load comments template.
260 *
261 * @param string $template template to load.
262 * @return string
263 */
264 public static function comments_template_loader( $template ) {
265 if ( get_post_type() !== 'product' ) {
266 return $template;
267 }
268
269 $check_dirs = array(
270 trailingslashit( get_stylesheet_directory() ) . WC()->template_path(),
271 trailingslashit( get_template_directory() ) . WC()->template_path(),
272 trailingslashit( get_stylesheet_directory() ),
273 trailingslashit( get_template_directory() ),
274 trailingslashit( WC()->plugin_path() ) . 'templates/',
275 );
276
277 if ( WC_TEMPLATE_DEBUG_MODE ) {
278 $check_dirs = array( array_pop( $check_dirs ) );
279 }
280
281 foreach ( $check_dirs as $dir ) {
282 if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) {
283 return trailingslashit( $dir ) . 'single-product-reviews.php';
284 }
285 }
286 }
287
288 /**
289 * Unsupported theme compatibility methods.
290 */
291
292 /**
293 * Hook in methods to enhance the unsupported theme experience on pages.
294 *
295 * @since 3.3.0
296 */
297 public static function unsupported_theme_init() {
298 if ( 0 < self::$shop_page_id ) {
299 if ( is_product_taxonomy() ) {
300 self::unsupported_theme_tax_archive_init();
301 } elseif ( is_product() ) {
302 self::unsupported_theme_product_page_init();
303 } else {
304 self::unsupported_theme_shop_page_init();
305 }
306 }
307 }
308
309 /**
310 * Hook in methods to enhance the unsupported theme experience on the Shop page.
311 *
312 * @since 3.3.0
313 */
314 private static function unsupported_theme_shop_page_init() {
315 add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 );
316 add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 );
317 add_filter( 'comments_number', array( __CLASS__, 'unsupported_theme_comments_number_filter' ) );
318 }
319
320 /**
321 * Hook in methods to enhance the unsupported theme experience on Product pages.
322 *
323 * @since 3.3.0
324 */
325 private static function unsupported_theme_product_page_init() {
326 add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 );
327 add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) );
328 add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) );
329 remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
330 remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
331 self::add_support_for_product_page_gallery();
332 }
333
334 /**
335 * Add theme support for Product page gallery.
336 *
337 * @since x.x.x
338 */
339 private static function add_support_for_product_page_gallery() {
340 add_theme_support( 'wc-product-gallery-zoom' );
341 add_theme_support( 'wc-product-gallery-lightbox' );
342 add_theme_support( 'wc-product-gallery-slider' );
343 }
344
345 /**
346 * Enhance the unsupported theme experience on Product Category and Attribute pages by rendering
347 * those pages using the single template and shortcode-based content. To do this we make a dummy
348 * post and set a shortcode as the post content. This approach is adapted from bbPress.
349 *
350 * @since 3.3.0
351 */
352 private static function unsupported_theme_tax_archive_init() {
353 global $wp_query, $post;
354
355 $queried_object = get_queried_object();
356 $args = self::get_current_shop_view_args();
357 $shortcode_args = array(
358 'page' => $args->page,
359 'columns' => $args->columns,
360 'rows' => $args->rows,
361 'orderby' => '',
362 'order' => '',
363 'paginate' => true,
364 'cache' => false,
365 );
366
367 if ( is_product_category() ) {
368 $shortcode_args['category'] = sanitize_title( $queried_object->slug );
369 } elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
370 $shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
371 $shortcode_args['terms'] = sanitize_title( $queried_object->slug );
372 } elseif ( is_product_tag() ) {
373 $shortcode_args['tag'] = sanitize_title( $queried_object->slug );
374 } else {
375 // Default theme archive for all other taxonomies.
376 return;
377 }
378
379 // Description handling.
380 if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
381 $prefix = '<div class="term-description">' . wc_format_content( wp_kses_post( $queried_object->description ) ) . '</div>';
382 } else {
383 $prefix = '';
384 }
385
386 add_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
387 $shortcode = new WC_Shortcode_Products( $shortcode_args );
388 remove_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
389 $shop_page = get_post( self::$shop_page_id );
390
391 $dummy_post_properties = array(
392 'ID' => 0,
393 'post_status' => 'publish',
394 'post_author' => $shop_page->post_author,
395 'post_parent' => 0,
396 'post_type' => 'page',
397 'post_date' => $shop_page->post_date,
398 'post_date_gmt' => $shop_page->post_date_gmt,
399 'post_modified' => $shop_page->post_modified,
400 'post_modified_gmt' => $shop_page->post_modified_gmt,
401 'post_content' => $prefix . $shortcode->get_content(),
402 'post_title' => wc_clean( $queried_object->name ),
403 'post_excerpt' => '',
404 'post_content_filtered' => '',
405 'post_mime_type' => '',
406 'post_password' => '',
407 'post_name' => $queried_object->slug,
408 'guid' => '',
409 'menu_order' => 0,
410 'pinged' => '',
411 'to_ping' => '',
412 'ping_status' => '',
413 'comment_status' => 'closed',
414 'comment_count' => 0,
415 'filter' => 'raw',
416 );
417
418 // Set the $post global.
419 $post = new WP_Post( (object) $dummy_post_properties ); // @codingStandardsIgnoreLine.
420
421 // Copy the new post global into the main $wp_query.
422 $wp_query->post = $post;
423 $wp_query->posts = array( $post );
424
425 // Prevent comments form from appearing.
426 $wp_query->post_count = 1;
427 $wp_query->is_404 = false;
428 $wp_query->is_page = true;
429 $wp_query->is_single = true;
430 $wp_query->is_archive = false;
431 $wp_query->is_tax = true;
432 $wp_query->max_num_pages = 0;
433
434 // Prepare everything for rendering.
435 setup_postdata( $post );
436 remove_all_filters( 'the_content' );
437 remove_all_filters( 'the_excerpt' );
438 add_filter( 'template_include', array( __CLASS__, 'force_single_template_filter' ) );
439 }
440
441 /**
442 * Add layered nav args to WP_Query args generated by the 'products' shortcode.
443 *
444 * @since 3.3.4
445 * @param array $query WP_Query args.
446 * @return array
447 */
448 public static function unsupported_archive_layered_nav_compatibility( $query ) {
449 foreach ( WC()->query->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
450 $query['tax_query'][] = array(
451 'taxonomy' => $taxonomy,
452 'field' => 'slug',
453 'terms' => $data['terms'],
454 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
455 'include_children' => false,
456 );
457 }
458 return $query;
459 }
460
461 /**
462 * Force the loading of one of the single templates instead of whatever template was about to be loaded.
463 *
464 * @since 3.3.0
465 * @param string $template Path to template.
466 * @return string
467 */
468 public static function force_single_template_filter( $template ) {
469 $possible_templates = array(
470 'page',
471 'single',
472 'singular',
473 'index',
474 );
475
476 foreach ( $possible_templates as $possible_template ) {
477 $path = get_query_template( $possible_template );
478 if ( $path ) {
479 return $path;
480 }
481 }
482
483 return $template;
484 }
485
486 /**
487 * Get information about the current shop page view.
488 *
489 * @since 3.3.0
490 * @return array
491 */
492 private static function get_current_shop_view_args() {
493 return (object) array(
494 'page' => absint( max( 1, absint( get_query_var( 'paged' ) ) ) ),
495 'columns' => wc_get_default_products_per_row(),
496 'rows' => wc_get_default_product_rows_per_page(),
497 );
498 }
499
500 /**
501 * Filter the title and insert WooCommerce content on the shop page.
502 *
503 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
504 *
505 * @since 3.3.0
506 * @param string $title Existing title.
507 * @param int|null $id ID of the post being filtered.
508 * @return string
509 */
510 public static function unsupported_theme_title_filter( $title, $id = null ) {
511 if ( is_null( $id ) || self::$theme_support || ! $id !== self::$shop_page_id ) {
512 return $title;
513 }
514
515 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 ) ) {
516 $args = self::get_current_shop_view_args();
517 $title_suffix = array();
518
519 if ( $args->page > 1 ) {
520 /* translators: %d: Page number. */
521 $title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
522 }
523
524 if ( $title_suffix ) {
525 $title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
526 }
527 }
528 return $title;
529 }
530
531 /**
532 * Filter the content and insert WooCommerce content on the shop page.
533 *
534 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
535 *
536 * @since 3.3.0
537 * @param string $content Existing post content.
538 * @return string
539 */
540 public static function unsupported_theme_shop_content_filter( $content ) {
541 global $wp_query;
542
543 if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
544 return $content;
545 }
546
547 self::$in_content_filter = true;
548
549 // Remove the filter we're in to avoid nested calls.
550 remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ) );
551
552 // Unsupported theme shop page.
553 if ( is_page( self::$shop_page_id ) ) {
554 $args = self::get_current_shop_view_args();
555 $shortcode = new WC_Shortcode_Products(
556 array_merge(
557 WC()->query->get_catalog_ordering_args(),
558 array(
559 'page' => $args->page,
560 'columns' => $args->columns,
561 'rows' => $args->rows,
562 'orderby' => '',
563 'order' => '',
564 'paginate' => true,
565 'cache' => false,
566 )
567 ),
568 'products'
569 );
570
571 // Allow queries to run e.g. layered nav.
572 add_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
573
574 $content = $content . $shortcode->get_content();
575
576 // Remove actions and self to avoid nested calls.
577 remove_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
578 WC()->query->remove_ordering_args();
579 }
580
581 self::$in_content_filter = false;
582
583 return $content;
584 }
585
586 /**
587 * Filter the content and insert WooCommerce content on the shop page.
588 *
589 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
590 *
591 * @since 3.3.0
592 * @param string $content Existing post content.
593 * @return string
594 */
595 public static function unsupported_theme_product_content_filter( $content ) {
596 global $wp_query;
597
598 if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
599 return $content;
600 }
601
602 self::$in_content_filter = true;
603
604 // Remove the filter we're in to avoid nested calls.
605 remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ) );
606
607 if ( is_product() ) {
608 $content = do_shortcode( '[product_page id="' . get_the_ID() . '" show_title=0 status="any"]' );
609 }
610
611 self::$in_content_filter = false;
612
613 return $content;
614 }
615
616 /**
617 * Suppress the comments number on the Shop page for unsupported themes since there is no commenting on the Shop page.
618 *
619 * @since 3.4.5
620 * @param string $comments_number The comments number text.
621 * @return string
622 */
623 public static function unsupported_theme_comments_number_filter( $comments_number ) {
624 if ( is_page( self::$shop_page_id ) ) {
625 return '';
626 }
627
628 return $comments_number;
629 }
630
631 /**
632 * Are we filtering content for unsupported themes?
633 *
634 * @since 3.3.2
635 * @return bool
636 */
637 public static function in_content_filter() {
638 return (bool) self::$in_content_filter;
639 }
640
641 /**
642 * Prevent the main featured image on product pages because there will be another featured image
643 * in the gallery.
644 *
645 * @since 3.3.0
646 * @param string $html Img element HTML.
647 * @return string
648 */
649 public static function unsupported_theme_single_featured_image_filter( $html ) {
650 if ( self::in_content_filter() || ! is_product() || ! is_main_query() ) {
651 return $html;
652 }
653
654 return '';
655 }
656
657 /**
658 * Remove the Review tab and just use the regular comment form.
659 *
660 * @param array $tabs Tab info.
661 * @return array
662 */
663 public static function unsupported_theme_remove_review_tab( $tabs ) {
664 unset( $tabs['reviews'] );
665 return $tabs;
666 }
667 }
668
669 add_action( 'init', array( 'WC_Template_Loader', 'init' ) );
670