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