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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 1 year ago admin 1 year ago blocks 1 year ago cli 1 year ago customizer 2 years ago data-stores 1 year ago emails 1 year ago export 1 year ago gateways 1 year ago import 1 year ago integrations 2 years ago interfaces 1 year ago legacy 2 years ago libraries 3 years ago log-handlers 2 years ago payment-tokens 5 years ago product-usage 1 year ago queue 4 years ago react-admin 1 year ago rest-api 1 year ago shipping 1 year ago shortcodes 1 year ago theme-support 2 years ago tracks 1 year ago traits 5 years ago walkers 5 years ago wccom-site 1 year ago widgets 1 year ago class-wc-ajax.php 2 years ago class-wc-auth.php 1 year ago class-wc-autoloader.php 1 year 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 1 year ago class-wc-breadcrumb.php 5 years ago class-wc-cache-helper.php 1 year ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 2 years ago class-wc-cart-totals.php 2 years ago class-wc-cart.php 1 year ago class-wc-checkout.php 1 year ago class-wc-cli.php 1 year ago class-wc-comments.php 3 years ago class-wc-countries.php 1 year ago class-wc-coupon.php 1 year ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 2 years 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 1 year ago class-wc-download-handler.php 1 year ago class-wc-emails.php 1 year ago class-wc-embed.php 5 years ago class-wc-form-handler.php 1 year ago class-wc-frontend-scripts.php 1 year ago class-wc-geo-ip.php 1 year ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 3 years ago class-wc-https.php 2 years ago class-wc-install.php 1 year ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 1 year ago class-wc-meta-data.php 4 years ago class-wc-order-factory.php 2 years ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 2 years ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 2 years ago class-wc-order-item-shipping.php 2 years ago class-wc-order-item-tax.php 4 years ago class-wc-order-item.php 2 years ago class-wc-order-query.php 4 years ago class-wc-order-refund.php 2 years ago class-wc-order.php 2 years ago class-wc-payment-gateways.php 2 years ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 1 year ago class-wc-post-types.php 1 year ago class-wc-privacy-background-process.php 5 years ago class-wc-privacy-erasers.php 1 year ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 years ago class-wc-product-download.php 2 years ago class-wc-product-external.php 5 years ago class-wc-product-factory.php 2 years ago class-wc-product-grouped.php 8 years ago class-wc-product-query.php 5 years ago class-wc-product-simple.php 1 year ago class-wc-product-variable.php 1 year ago class-wc-product-variation.php 1 year ago class-wc-query.php 2 years 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 2 years ago class-wc-shipping-rate.php 3 years ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 5 years ago class-wc-shipping.php 4 years ago class-wc-shortcodes.php 2 years ago class-wc-structured-data.php 1 year ago class-wc-tax.php 2 years ago class-wc-template-loader.php 2 years ago class-wc-tracker.php 1 year ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 year ago class-woocommerce.php 1 year ago wc-account-functions.php 1 year ago wc-attribute-functions.php 1 year ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 year ago wc-conditional-functions.php 2 years ago wc-core-functions.php 1 year ago wc-coupon-functions.php 3 years ago wc-deprecated-functions.php 2 years ago wc-formatting-functions.php 2 years ago wc-notice-functions.php 2 years ago wc-order-functions.php 1 year ago wc-order-item-functions.php 3 years ago wc-page-functions.php 1 year ago wc-product-functions.php 1 year ago wc-rest-functions.php 1 year ago wc-stock-functions.php 1 year ago wc-template-functions.php 1 year ago wc-template-hooks.php 1 year ago wc-term-functions.php 3 years ago wc-update-functions.php 1 year ago wc-user-functions.php 1 year ago wc-webhook-functions.php 1 year ago wc-widget-functions.php 5 years ago
class-wc-template-loader.php
696 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 ( wc_current_theme_is_fse_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 = false;
138 $template_filename = $template_name . '.html';
139 // Since Gutenberg 12.1.0, the conventions for block templates directories have changed,
140 // we should check both these possible directories for backwards-compatibility.
141 $possible_templates_dirs = array( 'templates', 'block-templates' );
142
143 // Combine the possible root directory names with either the template directory
144 // or the stylesheet directory for child themes, getting all possible block templates
145 // locations combinations.
146 $filepath = DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $template_filename;
147 $legacy_filepath = DIRECTORY_SEPARATOR . 'block-templates' . DIRECTORY_SEPARATOR . $template_filename;
148 $possible_paths = array(
149 get_stylesheet_directory() . $filepath,
150 get_stylesheet_directory() . $legacy_filepath,
151 get_template_directory() . $filepath,
152 get_template_directory() . $legacy_filepath,
153 );
154
155 // Check the first matching one.
156 foreach ( $possible_paths as $path ) {
157 if ( is_readable( $path ) ) {
158 $has_template = true;
159 break;
160 }
161 }
162
163 /**
164 * Filters the value of the result of the block template check.
165 *
166 * @since x.x.x
167 *
168 * @param boolean $has_template value to be filtered.
169 * @param string $template_name The name of the template.
170 */
171 return (bool) apply_filters( 'woocommerce_has_block_template', $has_template, $template_name );
172 }
173
174 /**
175 * Get the default filename for a template except if a block template with
176 * the same name exists.
177 *
178 * @since 3.0.0
179 * @since 5.5.0 If a block template with the same name exists, return an
180 * empty string.
181 * @since 6.3.0 It checks custom product taxonomies
182 * @return string
183 */
184 private static function get_template_loader_default_file() {
185 if (
186 is_singular( 'product' ) &&
187 ! self::has_block_template( 'single-product' )
188 ) {
189 $default_file = 'single-product.php';
190 } elseif ( is_product_taxonomy() ) {
191 $object = get_queried_object();
192
193 if ( self::taxonomy_has_block_template( $object ) ) {
194 $default_file = '';
195 } else {
196 if ( taxonomy_is_product_attribute( $object->taxonomy ) ) {
197 $default_file = 'taxonomy-product-attribute.php';
198 } elseif ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
199 $default_file = 'taxonomy-' . $object->taxonomy . '.php';
200 } elseif ( ! self::has_block_template( 'archive-product' ) ) {
201 $default_file = 'archive-product.php';
202 } else {
203 $default_file = '';
204 }
205 }
206 } elseif (
207 ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) &&
208 ! self::has_block_template( 'archive-product' )
209 ) {
210 $default_file = self::$theme_support ? 'archive-product.php' : '';
211 } else {
212 $default_file = '';
213 }
214 return $default_file;
215 }
216
217 /**
218 * Get an array of filenames to search for a given template.
219 *
220 * @since 3.0.0
221 * @param string $default_file The default file name.
222 * @return string[]
223 */
224 private static function get_template_loader_files( $default_file ) {
225 $templates = apply_filters( 'woocommerce_template_loader_files', array(), $default_file );
226 $templates[] = 'woocommerce.php';
227
228 if ( is_page_template() ) {
229 $page_template = get_page_template_slug();
230
231 if ( $page_template ) {
232 $validated_file = validate_file( $page_template );
233 if ( 0 === $validated_file ) {
234 $templates[] = $page_template;
235 } else {
236 error_log( "WooCommerce: Unable to validate template path: \"$page_template\". Error Code: $validated_file." ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
237 }
238 }
239 }
240
241 if ( is_singular( 'product' ) ) {
242 $object = get_queried_object();
243 $name_decoded = urldecode( $object->post_name );
244 if ( $name_decoded !== $object->post_name ) {
245 $templates[] = "single-product-{$name_decoded}.php";
246 }
247 $templates[] = "single-product-{$object->post_name}.php";
248 }
249
250 if ( is_product_taxonomy() ) {
251 $object = get_queried_object();
252
253 $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
254 $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
255 $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
256 $templates[] = WC()->template_path() . 'taxonomy-' . $object->taxonomy . '.php';
257
258 if ( taxonomy_is_product_attribute( $object->taxonomy ) ) {
259 $templates[] = 'taxonomy-product_attribute.php';
260 $templates[] = WC()->template_path() . 'taxonomy-product_attribute.php';
261 $templates[] = $default_file;
262 }
263
264 if ( is_tax( 'product_cat' ) || is_tax( 'product_tag' ) ) {
265 $cs_taxonomy = str_replace( '_', '-', $object->taxonomy );
266 $cs_default = str_replace( '_', '-', $default_file );
267 $templates[] = 'taxonomy-' . $object->taxonomy . '-' . $object->slug . '.php';
268 $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '-' . $object->slug . '.php';
269 $templates[] = 'taxonomy-' . $object->taxonomy . '.php';
270 $templates[] = WC()->template_path() . 'taxonomy-' . $cs_taxonomy . '.php';
271 $templates[] = $cs_default;
272 }
273 }
274
275 $templates[] = $default_file;
276 if ( isset( $cs_default ) ) {
277 $templates[] = WC()->template_path() . $cs_default;
278 }
279 $templates[] = WC()->template_path() . $default_file;
280
281 return array_unique( $templates );
282 }
283
284 /**
285 * Load comments template.
286 *
287 * @param string $template template to load.
288 * @return string
289 */
290 public static function comments_template_loader( $template ) {
291 if ( get_post_type() !== 'product' ) {
292 return $template;
293 }
294
295 $check_dirs = array(
296 trailingslashit( get_stylesheet_directory() ) . WC()->template_path(),
297 trailingslashit( get_template_directory() ) . WC()->template_path(),
298 trailingslashit( get_stylesheet_directory() ),
299 trailingslashit( get_template_directory() ),
300 trailingslashit( WC()->plugin_path() ) . 'templates/',
301 );
302
303 if ( WC_TEMPLATE_DEBUG_MODE ) {
304 $check_dirs = array( array_pop( $check_dirs ) );
305 }
306
307 foreach ( $check_dirs as $dir ) {
308 if ( file_exists( trailingslashit( $dir ) . 'single-product-reviews.php' ) ) {
309 return trailingslashit( $dir ) . 'single-product-reviews.php';
310 }
311 }
312 }
313
314 /**
315 * Unsupported theme compatibility methods.
316 */
317
318 /**
319 * Hook in methods to enhance the unsupported theme experience on pages.
320 *
321 * @since 3.3.0
322 */
323 public static function unsupported_theme_init() {
324 if ( 0 < self::$shop_page_id ) {
325 if ( is_product_taxonomy() ) {
326 self::unsupported_theme_tax_archive_init();
327 } elseif ( is_product() ) {
328 self::unsupported_theme_product_page_init();
329 } else {
330 self::unsupported_theme_shop_page_init();
331 }
332 }
333 }
334
335 /**
336 * Hook in methods to enhance the unsupported theme experience on the Shop page.
337 *
338 * @since 3.3.0
339 */
340 private static function unsupported_theme_shop_page_init() {
341 add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ), 10 );
342 add_filter( 'the_title', array( __CLASS__, 'unsupported_theme_title_filter' ), 10, 2 );
343 add_filter( 'comments_number', array( __CLASS__, 'unsupported_theme_comments_number_filter' ) );
344 }
345
346 /**
347 * Hook in methods to enhance the unsupported theme experience on Product pages.
348 *
349 * @since 3.3.0
350 */
351 private static function unsupported_theme_product_page_init() {
352 add_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ), 10 );
353 add_filter( 'post_thumbnail_html', array( __CLASS__, 'unsupported_theme_single_featured_image_filter' ) );
354 add_filter( 'woocommerce_product_tabs', array( __CLASS__, 'unsupported_theme_remove_review_tab' ) );
355 remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
356 remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10 );
357 self::add_support_for_product_page_gallery();
358 }
359
360 /**
361 * Add theme support for Product page gallery.
362 *
363 * @since x.x.x
364 */
365 private static function add_support_for_product_page_gallery() {
366 add_theme_support( 'wc-product-gallery-zoom' );
367 add_theme_support( 'wc-product-gallery-lightbox' );
368 add_theme_support( 'wc-product-gallery-slider' );
369 }
370
371 /**
372 * Enhance the unsupported theme experience on Product Category and Attribute pages by rendering
373 * those pages using the single template and shortcode-based content. To do this we make a dummy
374 * post and set a shortcode as the post content. This approach is adapted from bbPress.
375 *
376 * @since 3.3.0
377 */
378 private static function unsupported_theme_tax_archive_init() {
379 global $wp_query, $post;
380
381 $queried_object = get_queried_object();
382 $args = self::get_current_shop_view_args();
383 $shortcode_args = array(
384 'page' => $args->page,
385 'columns' => $args->columns,
386 'rows' => $args->rows,
387 'orderby' => '',
388 'order' => '',
389 'paginate' => true,
390 'cache' => false,
391 );
392
393 if ( is_product_category() ) {
394 $shortcode_args['category'] = sanitize_title( $queried_object->slug );
395 } elseif ( taxonomy_is_product_attribute( $queried_object->taxonomy ) ) {
396 $shortcode_args['attribute'] = sanitize_title( $queried_object->taxonomy );
397 $shortcode_args['terms'] = sanitize_title( $queried_object->slug );
398 } elseif ( is_product_tag() ) {
399 $shortcode_args['tag'] = sanitize_title( $queried_object->slug );
400 } else {
401 // Default theme archive for all other taxonomies.
402 return;
403 }
404
405 // Description handling.
406 if ( ! empty( $queried_object->description ) && ( empty( $_GET['product-page'] ) || 1 === absint( $_GET['product-page'] ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
407 $prefix = '<div class="term-description">' . wc_format_content( wp_kses_post( $queried_object->description ) ) . '</div>';
408 } else {
409 $prefix = '';
410 }
411
412 add_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
413 $shortcode = new WC_Shortcode_Products( $shortcode_args );
414 remove_filter( 'woocommerce_shortcode_products_query', array( __CLASS__, 'unsupported_archive_layered_nav_compatibility' ) );
415 $shop_page = get_post( self::$shop_page_id );
416
417 $dummy_post_properties = array(
418 'ID' => 0,
419 'post_status' => 'publish',
420 'post_author' => $shop_page->post_author,
421 'post_parent' => 0,
422 'post_type' => 'page',
423 'post_date' => $shop_page->post_date,
424 'post_date_gmt' => $shop_page->post_date_gmt,
425 'post_modified' => $shop_page->post_modified,
426 'post_modified_gmt' => $shop_page->post_modified_gmt,
427 'post_content' => $prefix . $shortcode->get_content(),
428 'post_title' => wc_clean( $queried_object->name ),
429 'post_excerpt' => '',
430 'post_content_filtered' => '',
431 'post_mime_type' => '',
432 'post_password' => '',
433 'post_name' => $queried_object->slug,
434 'guid' => '',
435 'menu_order' => 0,
436 'pinged' => '',
437 'to_ping' => '',
438 'ping_status' => '',
439 'comment_status' => 'closed',
440 'comment_count' => 0,
441 'filter' => 'raw',
442 );
443
444 // Set the $post global.
445 $post = new WP_Post( (object) $dummy_post_properties ); // @codingStandardsIgnoreLine.
446
447 // Copy the new post global into the main $wp_query.
448 $wp_query->post = $post;
449 $wp_query->posts = array( $post );
450
451 // Prevent comments form from appearing.
452 $wp_query->post_count = 1;
453 $wp_query->is_404 = false;
454 $wp_query->is_page = true;
455 $wp_query->is_single = true;
456 $wp_query->is_archive = false;
457 $wp_query->is_tax = true;
458 $wp_query->max_num_pages = 0;
459
460 // Prepare everything for rendering.
461 setup_postdata( $post );
462 remove_all_filters( 'the_content' );
463 remove_all_filters( 'the_excerpt' );
464 add_filter( 'template_include', array( __CLASS__, 'force_single_template_filter' ) );
465 }
466
467 /**
468 * Add layered nav args to WP_Query args generated by the 'products' shortcode.
469 *
470 * @since 3.3.4
471 * @param array $query WP_Query args.
472 * @return array
473 */
474 public static function unsupported_archive_layered_nav_compatibility( $query ) {
475 foreach ( WC()->query->get_layered_nav_chosen_attributes() as $taxonomy => $data ) {
476 $query['tax_query'][] = array(
477 'taxonomy' => $taxonomy,
478 'field' => 'slug',
479 'terms' => $data['terms'],
480 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
481 'include_children' => false,
482 );
483 }
484 return $query;
485 }
486
487 /**
488 * Force the loading of one of the single templates instead of whatever template was about to be loaded.
489 *
490 * @since 3.3.0
491 * @param string $template Path to template.
492 * @return string
493 */
494 public static function force_single_template_filter( $template ) {
495 $possible_templates = array(
496 'page',
497 'single',
498 'singular',
499 'index',
500 );
501
502 foreach ( $possible_templates as $possible_template ) {
503 $path = get_query_template( $possible_template );
504 if ( $path ) {
505 return $path;
506 }
507 }
508
509 return $template;
510 }
511
512 /**
513 * Get information about the current shop page view.
514 *
515 * @since 3.3.0
516 * @return array
517 */
518 private static function get_current_shop_view_args() {
519 return (object) array(
520 'page' => absint( max( 1, absint( get_query_var( 'paged' ) ) ) ),
521 'columns' => wc_get_default_products_per_row(),
522 'rows' => wc_get_default_product_rows_per_page(),
523 );
524 }
525
526 /**
527 * Filter the title and insert WooCommerce content on the shop page.
528 *
529 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
530 *
531 * @since 3.3.0
532 * @param string $title Existing title.
533 * @param int $id ID of the post being filtered.
534 * @return string
535 */
536 public static function unsupported_theme_title_filter( $title, $id ) {
537 if ( self::$theme_support || ! $id !== self::$shop_page_id ) {
538 return $title;
539 }
540
541 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 ) ) {
542 $args = self::get_current_shop_view_args();
543 $title_suffix = array();
544
545 if ( $args->page > 1 ) {
546 /* translators: %d: Page number. */
547 $title_suffix[] = sprintf( esc_html__( 'Page %d', 'woocommerce' ), $args->page );
548 }
549
550 if ( $title_suffix ) {
551 $title = $title . ' &ndash; ' . implode( ', ', $title_suffix );
552 }
553 }
554 return $title;
555 }
556
557 /**
558 * Filter the content and insert WooCommerce content on the shop page.
559 *
560 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
561 *
562 * @since 3.3.0
563 * @param string $content Existing post content.
564 * @return string
565 */
566 public static function unsupported_theme_shop_content_filter( $content ) {
567 global $wp_query;
568
569 if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
570 return $content;
571 }
572
573 self::$in_content_filter = true;
574
575 // Remove the filter we're in to avoid nested calls.
576 remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_shop_content_filter' ) );
577
578 // Unsupported theme shop page.
579 if ( is_page( self::$shop_page_id ) ) {
580 $args = self::get_current_shop_view_args();
581 $shortcode = new WC_Shortcode_Products(
582 array_merge(
583 WC()->query->get_catalog_ordering_args(),
584 array(
585 'page' => $args->page,
586 'columns' => $args->columns,
587 'rows' => $args->rows,
588 'orderby' => '',
589 'order' => '',
590 'paginate' => true,
591 'cache' => false,
592 )
593 ),
594 'products'
595 );
596
597 // Allow queries to run e.g. layered nav.
598 add_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
599
600 $content = $content . $shortcode->get_content();
601
602 // Remove actions and self to avoid nested calls.
603 remove_action( 'pre_get_posts', array( WC()->query, 'product_query' ) );
604 WC()->query->remove_ordering_args();
605 }
606
607 self::$in_content_filter = false;
608
609 return $content;
610 }
611
612 /**
613 * Filter the content and insert WooCommerce content on the shop page.
614 *
615 * For non-WC themes, this will setup the main shop page to be shortcode based to improve default appearance.
616 *
617 * @since 3.3.0
618 * @param string $content Existing post content.
619 * @return string
620 */
621 public static function unsupported_theme_product_content_filter( $content ) {
622 global $wp_query;
623
624 if ( self::$theme_support || ! is_main_query() || ! in_the_loop() ) {
625 return $content;
626 }
627
628 self::$in_content_filter = true;
629
630 // Remove the filter we're in to avoid nested calls.
631 remove_filter( 'the_content', array( __CLASS__, 'unsupported_theme_product_content_filter' ) );
632
633 if ( is_product() ) {
634 $content = do_shortcode( '[product_page id="' . get_the_ID() . '" show_title=0 status="any"]' );
635 }
636
637 self::$in_content_filter = false;
638
639 return $content;
640 }
641
642 /**
643 * Suppress the comments number on the Shop page for unsupported themes since there is no commenting on the Shop page.
644 *
645 * @since 3.4.5
646 * @param string $comments_number The comments number text.
647 * @return string
648 */
649 public static function unsupported_theme_comments_number_filter( $comments_number ) {
650 if ( is_page( self::$shop_page_id ) ) {
651 return '';
652 }
653
654 return $comments_number;
655 }
656
657 /**
658 * Are we filtering content for unsupported themes?
659 *
660 * @since 3.3.2
661 * @return bool
662 */
663 public static function in_content_filter() {
664 return (bool) self::$in_content_filter;
665 }
666
667 /**
668 * Prevent the main featured image on product pages because there will be another featured image
669 * in the gallery.
670 *
671 * @since 3.3.0
672 * @param string $html Img element HTML.
673 * @return string
674 */
675 public static function unsupported_theme_single_featured_image_filter( $html ) {
676 if ( self::in_content_filter() || ! is_product() || ! is_main_query() ) {
677 return $html;
678 }
679
680 return '';
681 }
682
683 /**
684 * Remove the Review tab and just use the regular comment form.
685 *
686 * @param array $tabs Tab info.
687 * @return array
688 */
689 public static function unsupported_theme_remove_review_tab( $tabs ) {
690 unset( $tabs['reviews'] );
691 return $tabs;
692 }
693 }
694
695 add_action( 'init', array( 'WC_Template_Loader', 'init' ) );
696