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