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