abstracts
1 month ago
admin
1 month ago
blocks
1 month ago
cli
2 months ago
customizer
6 months ago
data-stores
1 month ago
emails
1 month ago
export
1 month ago
gateways
2 months ago
import
1 month ago
integrations
1 month ago
interfaces
6 months ago
legacy
1 month ago
libraries
2 months ago
log-handlers
1 year ago
payment-tokens
6 years ago
product-usage
1 year ago
queue
6 months ago
react-admin
1 month ago
rest-api
1 month ago
shipping
5 months ago
shortcodes
1 month ago
theme-support
2 years ago
tracks
2 months ago
traits
5 years ago
walkers
5 years ago
wccom-site
2 months ago
widgets
1 month ago
class-wc-ajax.php
1 month ago
class-wc-auth.php
2 years ago
class-wc-autoloader.php
10 months ago
class-wc-background-emailer.php
3 months ago
class-wc-background-updater.php
6 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 month ago
class-wc-breadcrumb.php
6 months ago
class-wc-cache-helper.php
2 months ago
class-wc-cart-fees.php
2 years ago
class-wc-cart-session.php
5 months ago
class-wc-cart-totals.php
1 year ago
class-wc-cart.php
1 month ago
class-wc-checkout.php
1 month ago
class-wc-cli.php
1 year ago
class-wc-comments.php
6 months ago
class-wc-countries.php
3 months ago
class-wc-coupon.php
3 months ago
class-wc-customer-download-log.php
6 years ago
class-wc-customer-download.php
2 years ago
class-wc-customer.php
1 month 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
5 months ago
class-wc-discounts.php
1 year ago
class-wc-download-handler.php
1 month ago
class-wc-emails.php
1 month ago
class-wc-embed.php
1 year ago
class-wc-form-handler.php
1 month ago
class-wc-frontend-scripts.php
4 weeks ago
class-wc-geo-ip.php
10 months ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
1 month ago
class-wc-https.php
3 years ago
class-wc-install.php
1 month ago
class-wc-integrations.php
6 years ago
class-wc-log-levels.php
2 years ago
class-wc-logger.php
6 months ago
class-wc-meta-data.php
2 months ago
class-wc-order-factory.php
3 months ago
class-wc-order-item-coupon.php
4 years ago
class-wc-order-item-fee.php
1 month ago
class-wc-order-item-meta.php
4 years ago
class-wc-order-item-product.php
2 months ago
class-wc-order-item-shipping.php
7 months ago
class-wc-order-item-tax.php
2 months ago
class-wc-order-item.php
1 month ago
class-wc-order-query.php
2 months ago
class-wc-order-refund.php
1 year ago
class-wc-order.php
1 month ago
class-wc-payment-gateways.php
1 month ago
class-wc-payment-tokens.php
3 years ago
class-wc-post-data.php
1 month ago
class-wc-post-types.php
1 month ago
class-wc-privacy-background-process.php
1 year ago
class-wc-privacy-erasers.php
1 year ago
class-wc-privacy-exporters.php
5 years ago
class-wc-privacy.php
1 year ago
class-wc-product-attribute.php
6 months ago
class-wc-product-download.php
1 month ago
class-wc-product-external.php
1 year ago
class-wc-product-factory.php
5 months ago
class-wc-product-grouped.php
1 month ago
class-wc-product-query.php
6 months ago
class-wc-product-simple.php
1 year ago
class-wc-product-variable.php
2 months ago
class-wc-product-variation.php
2 months ago
class-wc-query.php
1 month ago
class-wc-rate-limiter.php
4 years ago
class-wc-regenerate-images-request.php
3 years ago
class-wc-regenerate-images.php
1 month ago
class-wc-register-wp-admin-settings.php
5 years ago
class-wc-rest-authentication.php
2 years ago
class-wc-rest-exception.php
5 years ago
class-wc-session-handler.php
5 months ago
class-wc-shipping-rate.php
1 year ago
class-wc-shipping-zone.php
5 years ago
class-wc-shipping-zones.php
9 months ago
class-wc-shipping.php
1 month ago
class-wc-shortcodes.php
1 year ago
class-wc-structured-data.php
1 month ago
class-wc-tax.php
1 month ago
class-wc-template-loader.php
9 months ago
class-wc-tracker.php
1 month ago
class-wc-validation.php
1 month ago
class-wc-webhook.php
2 months ago
class-woocommerce.php
4 weeks ago
wc-account-functions.php
2 months ago
wc-attribute-functions.php
1 month ago
wc-brands-functions.php
1 year ago
wc-cart-functions.php
2 months ago
wc-conditional-functions.php
1 year ago
wc-core-functions.php
3 months ago
wc-coupon-functions.php
7 months ago
wc-deprecated-functions.php
2 months ago
wc-formatting-functions.php
1 month ago
wc-interactivity-api-functions.php
3 months ago
wc-notice-functions.php
7 months ago
wc-order-functions.php
3 months ago
wc-order-item-functions.php
3 years ago
wc-order-step-logger-functions.php
6 months ago
wc-page-functions.php
1 month ago
wc-product-functions.php
1 month ago
wc-rest-functions.php
9 months ago
wc-stock-functions.php
1 month ago
wc-template-functions.php
2 months ago
wc-template-hooks.php
1 year ago
wc-term-functions.php
1 month ago
wc-update-functions.php
1 month ago
wc-user-functions.php
1 month ago
wc-webhook-functions.php
2 months ago
wc-widget-functions.php
6 years ago
class-wc-query.php
1176 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Contains the query functions for WooCommerce which alter the front-end post queries and loops |
| 4 | * |
| 5 | * @version 3.2.0 |
| 6 | * @package WooCommerce\Classes |
| 7 | */ |
| 8 | |
| 9 | use Automattic\WooCommerce\Internal\ProductAttributesLookup\Filterer; |
| 10 | use Automattic\WooCommerce\Enums\ProductStockStatus; |
| 11 | use Automattic\WooCommerce\Enums\TaxDisplayMode; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * WC_Query Class. |
| 17 | */ |
| 18 | class WC_Query { |
| 19 | |
| 20 | /** |
| 21 | * Query vars to add to wp. |
| 22 | * |
| 23 | * @var array |
| 24 | */ |
| 25 | public $query_vars = array(); |
| 26 | |
| 27 | /** |
| 28 | * Reference to the main product query on the page. |
| 29 | * |
| 30 | * @var WP_Query |
| 31 | */ |
| 32 | private static $product_query; |
| 33 | |
| 34 | /** |
| 35 | * Stores chosen attributes. |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | private static $chosen_attributes; |
| 40 | |
| 41 | /** |
| 42 | * The instance of the class that helps filtering with the product attributes lookup table. |
| 43 | * |
| 44 | * @var Filterer |
| 45 | */ |
| 46 | private $filterer; |
| 47 | |
| 48 | /** |
| 49 | * Constructor for the query class. Hooks in methods. |
| 50 | */ |
| 51 | public function __construct() { |
| 52 | $this->filterer = wc_get_container()->get( Filterer::class ); |
| 53 | |
| 54 | add_action( 'init', array( $this, 'add_endpoints' ) ); |
| 55 | if ( ! is_admin() ) { |
| 56 | add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 ); |
| 57 | add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); |
| 58 | add_action( 'parse_request', array( $this, 'parse_request' ), 0 ); |
| 59 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 60 | add_filter( 'get_pagenum_link', array( $this, 'remove_add_to_cart_pagination' ), 10, 1 ); |
| 61 | } |
| 62 | $this->init_query_vars(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Reset the chosen attributes so that get_layered_nav_chosen_attributes will get them from the query again. |
| 67 | */ |
| 68 | public static function reset_chosen_attributes() { |
| 69 | self::$chosen_attributes = null; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get any errors from querystring. |
| 74 | */ |
| 75 | public function get_errors() { |
| 76 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 77 | $error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : ''; |
| 78 | |
| 79 | if ( $error && ! wc_has_notice( $error, 'error' ) ) { |
| 80 | wc_add_notice( $error, 'error' ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Init query vars by loading options. |
| 86 | */ |
| 87 | public function init_query_vars() { |
| 88 | // Query vars to add to WP. |
| 89 | $this->query_vars = array( |
| 90 | // Checkout actions. |
| 91 | 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ), |
| 92 | 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ), |
| 93 | // My account actions. |
| 94 | 'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ), |
| 95 | 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ), |
| 96 | 'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ), |
| 97 | 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ), |
| 98 | 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ), |
| 99 | 'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ), |
| 100 | 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ), |
| 101 | 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ), |
| 102 | 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ), |
| 103 | 'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ), |
| 104 | 'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ), |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get page title for an endpoint. |
| 110 | * |
| 111 | * @param string $endpoint Endpoint key. |
| 112 | * @param string $action Optional action or variation within the endpoint. |
| 113 | * |
| 114 | * @since 2.3.0 |
| 115 | * @since 4.6.0 Added $action parameter. |
| 116 | * @return string The page title. |
| 117 | */ |
| 118 | public function get_endpoint_title( $endpoint, $action = '' ) { |
| 119 | global $wp; |
| 120 | |
| 121 | switch ( $endpoint ) { |
| 122 | case 'order-pay': |
| 123 | $title = __( 'Pay for order', 'woocommerce' ); |
| 124 | break; |
| 125 | case 'order-received': |
| 126 | $title = __( 'Order received', 'woocommerce' ); |
| 127 | break; |
| 128 | case 'orders': |
| 129 | if ( ! empty( $wp->query_vars['orders'] ) ) { |
| 130 | /* translators: %s: page */ |
| 131 | $title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) ); |
| 132 | } else { |
| 133 | $title = __( 'Orders', 'woocommerce' ); |
| 134 | } |
| 135 | break; |
| 136 | case 'view-order': |
| 137 | $order = wc_get_order( $wp->query_vars['view-order'] ); |
| 138 | /* translators: %s: order number */ |
| 139 | $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : ''; |
| 140 | break; |
| 141 | case 'downloads': |
| 142 | $title = __( 'Downloads', 'woocommerce' ); |
| 143 | break; |
| 144 | case 'edit-account': |
| 145 | $title = __( 'Account details', 'woocommerce' ); |
| 146 | break; |
| 147 | case 'edit-address': |
| 148 | $title = __( 'Addresses', 'woocommerce' ); |
| 149 | break; |
| 150 | case 'payment-methods': |
| 151 | $title = __( 'Payment methods', 'woocommerce' ); |
| 152 | break; |
| 153 | case 'add-payment-method': |
| 154 | $title = __( 'Add payment method', 'woocommerce' ); |
| 155 | break; |
| 156 | case 'lost-password': |
| 157 | if ( in_array( $action, array( 'rp', 'resetpass', 'newaccount' ), true ) ) { |
| 158 | $title = __( 'Set password', 'woocommerce' ); |
| 159 | } else { |
| 160 | $title = __( 'Lost password', 'woocommerce' ); |
| 161 | } |
| 162 | break; |
| 163 | default: |
| 164 | $title = ''; |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Filters the page title used for my-account endpoints. |
| 170 | * |
| 171 | * @since 2.6.0 |
| 172 | * @since 4.6.0 Added $action parameter. |
| 173 | * |
| 174 | * @see get_endpoint_title() |
| 175 | * |
| 176 | * @param string $title Default title. |
| 177 | * @param string $endpoint Endpoint key. |
| 178 | * @param string $action Optional action or variation within the endpoint. |
| 179 | */ |
| 180 | return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint, $action ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Endpoint mask describing the places the endpoint should be added. |
| 185 | * |
| 186 | * @since 2.6.2 |
| 187 | * @return int |
| 188 | */ |
| 189 | public function get_endpoints_mask() { |
| 190 | if ( 'page' === get_option( 'show_on_front' ) ) { |
| 191 | $page_on_front = get_option( 'page_on_front' ); |
| 192 | $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' ); |
| 193 | $checkout_page_id = get_option( 'woocommerce_checkout_page_id' ); |
| 194 | |
| 195 | if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) { |
| 196 | return EP_ROOT | EP_PAGES; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return EP_PAGES; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Add endpoints for query vars. |
| 205 | */ |
| 206 | public function add_endpoints() { |
| 207 | $mask = $this->get_endpoints_mask(); |
| 208 | |
| 209 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 210 | if ( ! empty( $var ) ) { |
| 211 | add_rewrite_endpoint( $var, $mask ); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Add query vars. |
| 218 | * |
| 219 | * @param array $vars Query vars. |
| 220 | * @return array |
| 221 | */ |
| 222 | public function add_query_vars( $vars ) { |
| 223 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 224 | $vars[] = $key; |
| 225 | } |
| 226 | return $vars; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Get query vars. |
| 231 | * |
| 232 | * @return array |
| 233 | */ |
| 234 | public function get_query_vars() { |
| 235 | return apply_filters( 'woocommerce_get_query_vars', $this->query_vars ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Get query current active query var. |
| 240 | * |
| 241 | * @return string |
| 242 | */ |
| 243 | public function get_current_endpoint() { |
| 244 | global $wp; |
| 245 | |
| 246 | foreach ( $this->get_query_vars() as $key => $value ) { |
| 247 | if ( isset( $wp->query_vars[ $key ] ) ) { |
| 248 | return $key; |
| 249 | } |
| 250 | } |
| 251 | return ''; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Parse the request and look for query vars - endpoints may not be supported. |
| 256 | */ |
| 257 | public function parse_request() { |
| 258 | global $wp; |
| 259 | |
| 260 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 261 | // Map query vars to their keys, or get them if endpoints are not supported. |
| 262 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 263 | if ( isset( $_GET[ $var ] ) ) { |
| 264 | $wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) ); |
| 265 | } elseif ( isset( $wp->query_vars[ $var ] ) ) { |
| 266 | $wp->query_vars[ $key ] = $wp->query_vars[ $var ]; |
| 267 | } |
| 268 | } |
| 269 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Are we currently on the front page? |
| 274 | * |
| 275 | * @param WP_Query $q Query instance. |
| 276 | * @return bool |
| 277 | */ |
| 278 | private function is_showing_page_on_front( $q ) { |
| 279 | return ( $q->is_home() && ! $q->is_posts_page ) && 'page' === get_option( 'show_on_front' ); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Is the front page a page we define? |
| 284 | * |
| 285 | * @param int $page_id Page ID. |
| 286 | * @return bool |
| 287 | */ |
| 288 | private function page_on_front_is( $page_id ) { |
| 289 | return absint( get_option( 'page_on_front' ) ) === absint( $page_id ); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Returns a copy of `$query` with all query vars that are allowed on the front page stripped. |
| 294 | * Used when the shop page is also the front page. |
| 295 | * |
| 296 | * @param array $query The unfiltered array. |
| 297 | * @return array The filtered query vars. |
| 298 | */ |
| 299 | private function filter_out_valid_front_page_query_vars( $query ) { |
| 300 | return array_filter( |
| 301 | $query, |
| 302 | function ( $key ) { |
| 303 | return ! $this->is_query_var_valid_on_front_page( $key ); |
| 304 | }, |
| 305 | ARRAY_FILTER_USE_KEY |
| 306 | ); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Checks whether a query var is allowed on the front page or not. |
| 311 | * |
| 312 | * @param string $query_var Query var name. |
| 313 | * @return boolean TRUE when query var is allowed on the front page. FALSE otherwise. |
| 314 | */ |
| 315 | private function is_query_var_valid_on_front_page( $query_var ) { |
| 316 | return in_array( $query_var, array( 'preview', 'page', 'paged', 'cpage', 'orderby' ), true ) |
| 317 | || in_array( $query_var, array( 'min_price', 'max_price', 'rating_filter' ), true ) |
| 318 | || 0 === strpos( $query_var, 'filter_' ) |
| 319 | || 0 === strpos( $query_var, 'query_type_' ); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Hook into pre_get_posts to do the main product query. |
| 324 | * |
| 325 | * @param WP_Query $q Query instance. |
| 326 | */ |
| 327 | public function pre_get_posts( $q ) { |
| 328 | // We only want to affect the main query. |
| 329 | if ( ! $q->is_main_query() ) { |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | // Fixes for queries on static homepages. |
| 334 | if ( $this->is_showing_page_on_front( $q ) ) { |
| 335 | |
| 336 | // Fix for endpoints on the homepage. |
| 337 | if ( ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) { |
| 338 | $_query = wp_parse_args( $q->query ); |
| 339 | if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) { |
| 340 | $q->is_page = true; |
| 341 | $q->is_home = false; |
| 342 | $q->is_singular = true; |
| 343 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 344 | add_filter( 'redirect_canonical', '__return_false' ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // When orderby is set, WordPress shows posts on the front-page. Get around that here. |
| 349 | if ( $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) { |
| 350 | $_query = $this->filter_out_valid_front_page_query_vars( wp_parse_args( $q->query ) ); |
| 351 | |
| 352 | if ( empty( $_query ) ) { |
| 353 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 354 | $q->is_page = true; |
| 355 | $q->is_home = false; |
| 356 | |
| 357 | // WP supporting themes show post type archive. |
| 358 | if ( wc_current_theme_supports_woocommerce_or_fse() ) { |
| 359 | $q->set( 'post_type', 'product' ); |
| 360 | } else { |
| 361 | $q->is_singular = true; |
| 362 | } |
| 363 | } |
| 364 | } elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 365 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 366 | $q->is_page = true; |
| 367 | $q->is_home = false; |
| 368 | $q->is_singular = true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | // Fix product feeds. |
| 373 | if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) { |
| 374 | $q->is_comment_feed = false; |
| 375 | } |
| 376 | |
| 377 | $shop_page = null; |
| 378 | $shop_page_id = wc_get_page_id( 'shop' ); |
| 379 | |
| 380 | // Special check for shops with the PRODUCT POST TYPE ARCHIVE on front. |
| 381 | if ( wc_current_theme_supports_woocommerce_or_fse() && $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === $shop_page_id ) { |
| 382 | // This is a front-page shop. |
| 383 | $q->set( 'post_type', 'product' ); |
| 384 | $q->set( 'page_id', '' ); |
| 385 | |
| 386 | if ( isset( $q->query['paged'] ) ) { |
| 387 | $q->set( 'paged', $q->query['paged'] ); |
| 388 | } |
| 389 | |
| 390 | // Define a variable so we know this is the front page shop later on. |
| 391 | wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true ); |
| 392 | |
| 393 | // Get the actual WP page to avoid errors and let us use is_front_page(). |
| 394 | // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096. |
| 395 | global $wp_post_types; |
| 396 | |
| 397 | $shop_page = get_post( $shop_page_id ); |
| 398 | |
| 399 | $wp_post_types['product']->ID = $shop_page->ID; |
| 400 | $wp_post_types['product']->post_title = $shop_page->post_title; |
| 401 | $wp_post_types['product']->post_name = $shop_page->post_name; |
| 402 | $wp_post_types['product']->post_type = $shop_page->post_type; |
| 403 | $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type ); |
| 404 | |
| 405 | // Fix conditional Functions like is_front_page. |
| 406 | $q->is_singular = false; |
| 407 | $q->is_post_type_archive = true; |
| 408 | $q->is_archive = true; |
| 409 | $q->is_page = true; |
| 410 | |
| 411 | // Remove post type archive name from front page title tag. |
| 412 | add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); |
| 413 | |
| 414 | // Fix WP SEO. |
| 415 | if ( class_exists( 'WPSEO_Meta' ) ) { |
| 416 | add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) ); |
| 417 | add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) ); |
| 418 | } |
| 419 | } elseif ( $q->is_post_type_archive( 'product' ) && ! $q->is_tax() && $shop_page_id > 0 ) { |
| 420 | // This is a regular shop page (product archive). |
| 421 | $shop_page = get_post( $shop_page_id ); |
| 422 | } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) { |
| 423 | // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies. |
| 424 | if ( $q->is_search() ) { |
| 425 | // Exclude products flagged as hidden from search (catalog_visibility = hidden or catalog). |
| 426 | // Applied inline rather than via get_tax_query() to avoid pulling in layered nav filters |
| 427 | // and the woocommerce_product_query_tax_query hook, which are scoped to product archives. |
| 428 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 429 | $exclude_term_id = isset( $product_visibility_terms['exclude-from-search'] ) ? (int) $product_visibility_terms['exclude-from-search'] : 0; |
| 430 | |
| 431 | if ( $exclude_term_id > 0 ) { |
| 432 | $existing_tax_query = $q->get( 'tax_query' ); |
| 433 | $existing_tax_query = is_array( $existing_tax_query ) ? $existing_tax_query : array(); |
| 434 | $existing_tax_query[] = array( |
| 435 | 'taxonomy' => 'product_visibility', |
| 436 | 'field' => 'term_taxonomy_id', |
| 437 | 'terms' => array( $exclude_term_id ), |
| 438 | 'operator' => 'NOT IN', |
| 439 | ); |
| 440 | $q->set( 'tax_query', $existing_tax_query ); |
| 441 | } |
| 442 | } |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | // Set queried object for any shop page scenario. |
| 447 | if ( $shop_page ) { |
| 448 | $q->queried_object = $shop_page; |
| 449 | $q->queried_object_id = $shop_page->ID; |
| 450 | } |
| 451 | |
| 452 | $this->product_query( $q ); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Handler for the 'the_posts' WP filter. |
| 457 | * |
| 458 | * @param array $posts Posts from WP Query. |
| 459 | * @param WP_Query $query Current query. |
| 460 | * |
| 461 | * @return array |
| 462 | */ |
| 463 | public function handle_get_posts( $posts, $query ) { |
| 464 | if ( 'product_query' !== $query->get( 'wc_query' ) ) { |
| 465 | return $posts; |
| 466 | } |
| 467 | $this->remove_product_query_filters( $posts ); |
| 468 | return $posts; |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Prime featured image caches for product queries to avoid individual |
| 473 | * queries during rendering. |
| 474 | * |
| 475 | * @since 10.8.0 |
| 476 | * |
| 477 | * @param array $posts Posts from WP Query. |
| 478 | * @param WP_Query $query Current query. |
| 479 | * @return array |
| 480 | */ |
| 481 | public function prime_thumbnail_caches( $posts, $query ) { |
| 482 | if ( 'product_query' === $query->get( 'wc_query' ) ) { |
| 483 | update_post_thumbnail_cache( $query ); |
| 484 | } |
| 485 | return $posts; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure |
| 490 | * all custom filters are removed. |
| 491 | * |
| 492 | * This is done here during the_posts filter. The input is not changed. |
| 493 | * |
| 494 | * @param array $posts Posts from WP Query. |
| 495 | * @return array |
| 496 | */ |
| 497 | public function remove_product_query_filters( $posts ) { |
| 498 | $this->remove_ordering_args(); |
| 499 | remove_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 ); |
| 500 | return $posts; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * This function used to be hooked to found_posts and adjust the posts count when the filtering by attribute |
| 505 | * widget was used and variable products were present. Now it isn't hooked anymore and does nothing but return |
| 506 | * the input unchanged, since the pull request in which it was introduced has been reverted. |
| 507 | * |
| 508 | * @since 4.4.0 |
| 509 | * @param int $count Original posts count, as supplied by the found_posts filter. |
| 510 | * @param WP_Query $query The current WP_Query object. |
| 511 | * |
| 512 | * @return int Adjusted posts count. |
| 513 | */ |
| 514 | public function adjust_posts_count( $count, $query ) { |
| 515 | return $count; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Instance version of get_layered_nav_chosen_attributes, needed for unit tests. |
| 520 | * |
| 521 | * @return array |
| 522 | */ |
| 523 | protected function get_layered_nav_chosen_attributes_inst() { |
| 524 | return self::get_layered_nav_chosen_attributes(); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Get the posts (or the ids of the posts) found in the current WP loop. |
| 529 | * |
| 530 | * @return array Array of posts or post ids. |
| 531 | */ |
| 532 | protected function get_current_posts() { |
| 533 | return $GLOBALS['wp_query']->posts; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * WP SEO meta description. |
| 538 | * |
| 539 | * Hooked into wpseo_ hook already, so no need for function_exist. |
| 540 | * |
| 541 | * @return string |
| 542 | */ |
| 543 | public function wpseo_metadesc() { |
| 544 | return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) ); |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * WP SEO meta key. |
| 549 | * |
| 550 | * Hooked into wpseo_ hook already, so no need for function_exist. |
| 551 | * |
| 552 | * @return string |
| 553 | */ |
| 554 | public function wpseo_metakey() { |
| 555 | return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) ); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Query the products, applying sorting/ordering etc. |
| 560 | * This applies to the main WordPress loop. |
| 561 | * |
| 562 | * @param WP_Query $q Query instance. |
| 563 | */ |
| 564 | public function product_query( $q ) { |
| 565 | if ( ! is_feed() ) { |
| 566 | $ordering = $this->get_catalog_ordering_args(); |
| 567 | $q->set( 'orderby', $ordering['orderby'] ); |
| 568 | $q->set( 'order', $ordering['order'] ); |
| 569 | |
| 570 | if ( isset( $ordering['meta_key'] ) ) { |
| 571 | $q->set( 'meta_key', $ordering['meta_key'] ); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Query vars that affect posts shown. |
| 576 | $q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) ); |
| 577 | $q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) ); |
| 578 | $q->set( 'wc_query', 'product_query' ); |
| 579 | $q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) ); |
| 580 | |
| 581 | // Work out how many products to query. |
| 582 | $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ) ); |
| 583 | |
| 584 | // Store reference to this query. |
| 585 | self::$product_query = $q; |
| 586 | |
| 587 | // Additional hooks to change WP Query. |
| 588 | add_filter( 'posts_clauses', array( $this, 'product_query_post_clauses' ), 10, 2 ); |
| 589 | add_filter( 'the_posts', array( $this, 'handle_get_posts' ), 10, 2 ); |
| 590 | add_filter( 'the_posts', array( $this, 'prime_thumbnail_caches' ), 10, 2 ); |
| 591 | |
| 592 | do_action( 'woocommerce_product_query', $q, $this ); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Add extra clauses to the product query. |
| 597 | * |
| 598 | * @param array $args Product query clauses. |
| 599 | * @param WP_Query $wp_query The current product query. |
| 600 | * @return array The updated product query clauses array. |
| 601 | * |
| 602 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 603 | */ |
| 604 | public function product_query_post_clauses( $args, $wp_query ) { |
| 605 | $args = $this->price_filter_post_clauses( $args, $wp_query ); |
| 606 | $args = $this->filterer->filter_by_attribute_post_clauses( $args, $wp_query, self::get_layered_nav_chosen_attributes() ); |
| 607 | |
| 608 | return $args; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Remove the query. |
| 613 | */ |
| 614 | public function remove_product_query() { |
| 615 | remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Remove ordering queries. |
| 620 | */ |
| 621 | public function remove_ordering_args() { |
| 622 | remove_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) ); |
| 623 | remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) ); |
| 624 | remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); |
| 625 | remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * Check whether the current search query contains at least one positive (non-exclusion) term. |
| 630 | * |
| 631 | * WordPress relevance ordering requires positive search terms to build a valid ORDER BY clause. |
| 632 | * Searches that are empty or contain only exclusion terms (e.g. "-condebug") produce no positive |
| 633 | * terms, which results in invalid SQL when relevance ordering is used. |
| 634 | * |
| 635 | * This method delegates tokenization to WP_Query so it correctly handles WordPress's search |
| 636 | * term parsing (splitting on spaces, commas, and +) and respects the |
| 637 | * wp_query_search_exclusion_prefix filter. |
| 638 | * |
| 639 | * @return bool |
| 640 | */ |
| 641 | private function has_positive_search_terms(): bool { |
| 642 | $search_string = get_query_var( 's' ); |
| 643 | $search_string = is_array( $search_string ) ? '' : trim( (string) $search_string ); |
| 644 | |
| 645 | if ( '' === $search_string ) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | // Use WP_Query to parse search terms using core's tokenization rules. |
| 650 | $search_query = new class( array( 's' => $search_string ) ) extends \WP_Query { |
| 651 | /** |
| 652 | * This constructor is overridden to avoid triggering a database query while allowing access to search term parsing routines. |
| 653 | * Using public query APIs such as the `parse_query` method leads to test regressions, so an anonymous class approach is used instead. |
| 654 | * |
| 655 | * @param string|array $query URL query string or array of vars. |
| 656 | */ |
| 657 | public function __construct( $query = '' ) { |
| 658 | $this->query_vars = (array) $query; |
| 659 | $this->parse_search( $this->query_vars ); |
| 660 | } |
| 661 | }; |
| 662 | $search_terms = $search_query->query_vars['search_terms'] ?? array(); |
| 663 | |
| 664 | if ( empty( $search_terms ) ) { |
| 665 | return false; |
| 666 | } |
| 667 | |
| 668 | /** This filter is documented in wp-includes/class-wp-query.php */ |
| 669 | $exclusion_prefix = (string) apply_filters( 'wp_query_search_exclusion_prefix', '-' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingSinceComment |
| 670 | |
| 671 | if ( '' !== $exclusion_prefix ) { |
| 672 | $search_terms = array_filter( |
| 673 | $search_terms, |
| 674 | static fn( $term ) => ! str_starts_with( $term, $exclusion_prefix ) |
| 675 | ); |
| 676 | } |
| 677 | |
| 678 | return ! empty( $search_terms ); |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Returns an array of arguments for ordering products based on the selected values. |
| 683 | * |
| 684 | * @param string $orderby Order by param. |
| 685 | * @param string $order Order param. |
| 686 | * @return array |
| 687 | */ |
| 688 | public function get_catalog_ordering_args( $orderby = '', $order = '' ) { |
| 689 | // Get ordering from query string unless defined. |
| 690 | if ( ! $orderby ) { |
| 691 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 692 | if ( isset( $_GET['orderby'] ) ) { |
| 693 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 694 | $orderby_value = wc_clean( wp_unslash( $_GET['orderby'] ) ); |
| 695 | if ( is_array( $orderby_value ) ) { |
| 696 | $orderby_value = $orderby_value[0]; |
| 697 | } |
| 698 | } else { |
| 699 | $orderby_value = wc_clean( get_query_var( 'orderby' ) ); |
| 700 | } |
| 701 | |
| 702 | if ( ! $orderby_value ) { |
| 703 | if ( is_search() && $this->has_positive_search_terms() ) { |
| 704 | $orderby_value = 'relevance'; |
| 705 | } else { |
| 706 | $orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) ); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | // Get order + orderby args from string. |
| 711 | $orderby_value = is_array( $orderby_value ) ? $orderby_value : explode( '-', $orderby_value ); |
| 712 | $orderby = esc_attr( $orderby_value[0] ); |
| 713 | $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order; |
| 714 | } |
| 715 | |
| 716 | // Convert to correct format. |
| 717 | $orderby = strtolower( is_array( $orderby ) ? (string) current( $orderby ) : (string) $orderby ); |
| 718 | // ID can already be added and requires to be capitalized. |
| 719 | $orderby = str_replace( ' id', ' ID', $orderby ); |
| 720 | $order = strtoupper( is_array( $order ) ? (string) current( $order ) : (string) $order ); |
| 721 | |
| 722 | $args = array( |
| 723 | 'orderby' => $orderby, |
| 724 | 'order' => ( 'DESC' === $order ) ? 'DESC' : 'ASC', |
| 725 | 'meta_key' => '', // @codingStandardsIgnoreLine |
| 726 | ); |
| 727 | |
| 728 | switch ( $orderby ) { |
| 729 | case 'id': |
| 730 | $args['orderby'] = 'ID'; |
| 731 | break; |
| 732 | case 'menu_order': |
| 733 | $args['orderby'] = 'menu_order title'; |
| 734 | break; |
| 735 | case 'title': |
| 736 | $args['orderby'] = 'title'; |
| 737 | $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; |
| 738 | break; |
| 739 | case 'relevance': |
| 740 | $args['orderby'] = 'relevance'; |
| 741 | $args['order'] = 'DESC'; |
| 742 | break; |
| 743 | case 'rand': |
| 744 | $args['orderby'] = 'rand'; // @codingStandardsIgnoreLine |
| 745 | break; |
| 746 | case 'modified': |
| 747 | case 'date': |
| 748 | $args['orderby'] = $orderby . ' ID'; |
| 749 | $args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC'; |
| 750 | break; |
| 751 | case 'price': |
| 752 | $callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses'; |
| 753 | add_filter( 'posts_clauses', array( $this, $callback ) ); |
| 754 | break; |
| 755 | case 'popularity': |
| 756 | add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); |
| 757 | break; |
| 758 | case 'rating': |
| 759 | add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); |
| 760 | break; |
| 761 | } |
| 762 | |
| 763 | return apply_filters( 'woocommerce_get_catalog_ordering_args', $args, $orderby, $order ); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * Custom query used to filter products by price. |
| 768 | * |
| 769 | * @since 3.6.0 |
| 770 | * |
| 771 | * @param array $args Query args. |
| 772 | * @param WP_Query $wp_query WP_Query object. |
| 773 | * |
| 774 | * @return array |
| 775 | */ |
| 776 | public function price_filter_post_clauses( $args, $wp_query ) { |
| 777 | global $wpdb; |
| 778 | |
| 779 | /** |
| 780 | * Filter whether to add the filter post clauses |
| 781 | * |
| 782 | * @param bool $is_main_query Whether the current query is 'is_main_query'. |
| 783 | * @param WP_Query $wp_query The current WP_Query object. |
| 784 | * |
| 785 | * @since 9.9.0 |
| 786 | */ |
| 787 | $enable_filtering = apply_filters( 'woocommerce_enable_post_clause_filtering', $wp_query->is_main_query(), $wp_query ); |
| 788 | |
| 789 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 790 | if ( ! $enable_filtering || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) { |
| 791 | return $args; |
| 792 | } |
| 793 | |
| 794 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 795 | $current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0; |
| 796 | $current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX; |
| 797 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 798 | |
| 799 | /** |
| 800 | * Adjust if the store taxes are not displayed how they are stored. |
| 801 | * Kicks in when prices excluding tax are displayed including tax. |
| 802 | */ |
| 803 | if ( wc_tax_enabled() && TaxDisplayMode::INCLUSIVE === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) { |
| 804 | $tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class. |
| 805 | $tax_rates = WC_Tax::get_rates( $tax_class ); |
| 806 | |
| 807 | if ( $tax_rates ) { |
| 808 | $current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) ); |
| 809 | $current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) ); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 814 | $args['where'] .= $wpdb->prepare( |
| 815 | ' AND NOT (%f<wc_product_meta_lookup.min_price OR %f>wc_product_meta_lookup.max_price ) ', |
| 816 | $current_max_price, |
| 817 | $current_min_price |
| 818 | ); |
| 819 | return $args; |
| 820 | } |
| 821 | |
| 822 | /** |
| 823 | * Handle numeric price sorting. |
| 824 | * |
| 825 | * @param array $args Query args. |
| 826 | * @return array |
| 827 | */ |
| 828 | public function order_by_price_asc_post_clauses( $args ) { |
| 829 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 830 | $args['orderby'] = ' wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC '; |
| 831 | return $args; |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * Handle numeric price sorting. |
| 836 | * |
| 837 | * @param array $args Query args. |
| 838 | * @return array |
| 839 | */ |
| 840 | public function order_by_price_desc_post_clauses( $args ) { |
| 841 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 842 | $args['orderby'] = ' wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC '; |
| 843 | return $args; |
| 844 | } |
| 845 | |
| 846 | /** |
| 847 | * WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065. |
| 848 | * |
| 849 | * This lets us sort by meta value desc, and have a second orderby param. |
| 850 | * |
| 851 | * @param array $args Query args. |
| 852 | * @return array |
| 853 | */ |
| 854 | public function order_by_popularity_post_clauses( $args ) { |
| 855 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 856 | $args['orderby'] = ' wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC '; |
| 857 | return $args; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Order by rating post clauses. |
| 862 | * |
| 863 | * @param array $args Query args. |
| 864 | * @return array |
| 865 | */ |
| 866 | public function order_by_rating_post_clauses( $args ) { |
| 867 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 868 | $args['orderby'] = ' wc_product_meta_lookup.average_rating DESC, wc_product_meta_lookup.rating_count DESC, wc_product_meta_lookup.product_id DESC '; |
| 869 | return $args; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Join wc_product_meta_lookup to posts if not already joined. |
| 874 | * |
| 875 | * @param string $sql SQL join. |
| 876 | * @return string |
| 877 | */ |
| 878 | private function append_product_sorting_table_join( $sql ) { |
| 879 | global $wpdb; |
| 880 | |
| 881 | if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) { |
| 882 | $sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id "; |
| 883 | } |
| 884 | return $sql; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Appends meta queries to an array. |
| 889 | * |
| 890 | * @param array $meta_query Meta query. |
| 891 | * @param bool $main_query If is main query. |
| 892 | * @return array |
| 893 | */ |
| 894 | public function get_meta_query( $meta_query = array(), $main_query = false ) { |
| 895 | if ( ! is_array( $meta_query ) ) { |
| 896 | $meta_query = array(); |
| 897 | } |
| 898 | return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) ); |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * Appends tax queries to an array. |
| 903 | * |
| 904 | * @param array $tax_query Tax query. |
| 905 | * @param bool $main_query If is main query. |
| 906 | * @return array |
| 907 | */ |
| 908 | public function get_tax_query( $tax_query = array(), $main_query = false ) { |
| 909 | if ( ! is_array( $tax_query ) ) { |
| 910 | $tax_query = array( |
| 911 | 'relation' => 'AND', |
| 912 | ); |
| 913 | } |
| 914 | |
| 915 | if ( $main_query && ! $this->filterer->filtering_via_lookup_table_is_active() ) { |
| 916 | // Layered nav filters on terms. |
| 917 | foreach ( self::get_layered_nav_chosen_attributes() as $taxonomy => $data ) { |
| 918 | $tax_query[] = array( |
| 919 | 'taxonomy' => $taxonomy, |
| 920 | 'field' => 'slug', |
| 921 | 'terms' => $data['terms'], |
| 922 | 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN', |
| 923 | 'include_children' => false, |
| 924 | ); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 929 | $product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] ); |
| 930 | |
| 931 | // Hide out of stock products. |
| 932 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 933 | $product_visibility_not_in[] = $product_visibility_terms[ ProductStockStatus::OUT_OF_STOCK ]; |
| 934 | } |
| 935 | |
| 936 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 937 | // Filter by rating. |
| 938 | if ( isset( $_GET['rating_filter'] ) ) { |
| 939 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 940 | $rating_filter = array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) ); |
| 941 | $rating_terms = array(); |
| 942 | for ( $i = 1; $i <= 5; $i++ ) { |
| 943 | if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) { |
| 944 | $rating_terms[] = $product_visibility_terms[ 'rated-' . $i ]; |
| 945 | } |
| 946 | } |
| 947 | if ( ! empty( $rating_terms ) ) { |
| 948 | $tax_query[] = array( |
| 949 | 'taxonomy' => 'product_visibility', |
| 950 | 'field' => 'term_taxonomy_id', |
| 951 | 'terms' => $rating_terms, |
| 952 | 'operator' => 'IN', |
| 953 | 'rating_filter' => true, |
| 954 | ); |
| 955 | } |
| 956 | } |
| 957 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 958 | |
| 959 | if ( ! empty( $product_visibility_not_in ) ) { |
| 960 | $tax_query[] = array( |
| 961 | 'taxonomy' => 'product_visibility', |
| 962 | 'field' => 'term_taxonomy_id', |
| 963 | 'terms' => $product_visibility_not_in, |
| 964 | 'operator' => 'NOT IN', |
| 965 | ); |
| 966 | } |
| 967 | |
| 968 | return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) ); |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * Get the main query which product queries ran against. |
| 973 | * |
| 974 | * @return WP_Query |
| 975 | */ |
| 976 | public static function get_main_query() { |
| 977 | return self::$product_query; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Get the tax query which was used by the main query. |
| 982 | * |
| 983 | * @return array |
| 984 | */ |
| 985 | public static function get_main_tax_query() { |
| 986 | $tax_query = isset( self::$product_query->tax_query, self::$product_query->tax_query->queries ) ? self::$product_query->tax_query->queries : array(); |
| 987 | |
| 988 | return $tax_query; |
| 989 | } |
| 990 | |
| 991 | /** |
| 992 | * Get the meta query which was used by the main query. |
| 993 | * |
| 994 | * @return array |
| 995 | */ |
| 996 | public static function get_main_meta_query() { |
| 997 | // PHPStan infers $product_query as non-nullable from other call sites. See https://github.com/woocommerce/woocommerce/pull/64360#issuecomment-4360066970. |
| 998 | // @phpstan-ignore-next-line isset.property See above. |
| 999 | if ( ! isset( self::$product_query ) ) { |
| 1000 | return array(); |
| 1001 | } |
| 1002 | $args = self::$product_query->query_vars; |
| 1003 | $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); |
| 1004 | |
| 1005 | return $meta_query; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Based on WP_Query::parse_search |
| 1010 | */ |
| 1011 | public static function get_main_search_query_sql() { |
| 1012 | global $wpdb; |
| 1013 | |
| 1014 | // PHPStan infers $product_query as non-nullable from other call sites. See https://github.com/woocommerce/woocommerce/pull/64360#issuecomment-4360066970. |
| 1015 | // @phpstan-ignore-next-line isset.property See above. |
| 1016 | if ( ! isset( self::$product_query ) ) { |
| 1017 | return ''; |
| 1018 | } |
| 1019 | $args = self::$product_query->query_vars; |
| 1020 | $search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array(); |
| 1021 | $sql = array(); |
| 1022 | |
| 1023 | foreach ( $search_terms as $term ) { |
| 1024 | // Terms prefixed with '-' should be excluded. |
| 1025 | $include = '-' !== substr( $term, 0, 1 ); |
| 1026 | |
| 1027 | if ( $include ) { |
| 1028 | $like_op = 'LIKE'; |
| 1029 | $andor_op = 'OR'; |
| 1030 | } else { |
| 1031 | $like_op = 'NOT LIKE'; |
| 1032 | $andor_op = 'AND'; |
| 1033 | $term = substr( $term, 1 ); |
| 1034 | } |
| 1035 | |
| 1036 | $like = '%' . $wpdb->esc_like( $term ) . '%'; |
| 1037 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 1038 | $sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like ); |
| 1039 | } |
| 1040 | |
| 1041 | if ( ! empty( $sql ) && ! is_user_logged_in() ) { |
| 1042 | $sql[] = "($wpdb->posts.post_password = '')"; |
| 1043 | } |
| 1044 | |
| 1045 | return implode( ' AND ', $sql ); |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * Get an array of attributes and terms selected with the layered nav widget. |
| 1050 | * |
| 1051 | * @return array |
| 1052 | */ |
| 1053 | public static function get_layered_nav_chosen_attributes() { |
| 1054 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 1055 | if ( ! is_array( self::$chosen_attributes ) ) { |
| 1056 | self::$chosen_attributes = array(); |
| 1057 | |
| 1058 | if ( ! empty( $_GET ) ) { |
| 1059 | foreach ( $_GET as $key => $value ) { |
| 1060 | if ( 0 === strpos( $key, 'filter_' ) ) { |
| 1061 | if ( ! is_string( $value ) ) { |
| 1062 | continue; |
| 1063 | } |
| 1064 | |
| 1065 | $attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) ); |
| 1066 | $taxonomy = wc_attribute_taxonomy_name( $attribute ); |
| 1067 | $filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array(); |
| 1068 | |
| 1069 | if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) { |
| 1070 | continue; |
| 1071 | } |
| 1072 | |
| 1073 | $query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) : ''; |
| 1074 | self::$chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding. |
| 1075 | self::$chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | return self::$chosen_attributes; |
| 1081 | // phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 1082 | } |
| 1083 | |
| 1084 | /** |
| 1085 | * Remove the add-to-cart param from pagination urls. |
| 1086 | * |
| 1087 | * @param string $url URL. |
| 1088 | * @return string |
| 1089 | */ |
| 1090 | public function remove_add_to_cart_pagination( $url ) { |
| 1091 | return remove_query_arg( 'add-to-cart', $url ); |
| 1092 | } |
| 1093 | |
| 1094 | /** |
| 1095 | * Return a meta query for filtering by rating. |
| 1096 | * |
| 1097 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 1098 | * @return array |
| 1099 | */ |
| 1100 | public function rating_filter_meta_query() { |
| 1101 | return array(); |
| 1102 | } |
| 1103 | |
| 1104 | /** |
| 1105 | * Returns a meta query to handle product visibility. |
| 1106 | * |
| 1107 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 1108 | * @param string $compare (default: 'IN'). |
| 1109 | * @return array |
| 1110 | */ |
| 1111 | public function visibility_meta_query( $compare = 'IN' ) { |
| 1112 | return array(); |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * Returns a meta query to handle product stock status. |
| 1117 | * |
| 1118 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 1119 | * @param string $status (default: 'instock'). |
| 1120 | * @return array |
| 1121 | */ |
| 1122 | public function stock_status_meta_query( $status = ProductStockStatus::IN_STOCK ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 1123 | return array(); |
| 1124 | } |
| 1125 | |
| 1126 | /** |
| 1127 | * Layered nav init. |
| 1128 | * |
| 1129 | * @deprecated 2.6.0 |
| 1130 | */ |
| 1131 | public function layered_nav_init() { |
| 1132 | wc_deprecated_function( 'layered_nav_init', '2.6' ); |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients. |
| 1137 | * |
| 1138 | * @deprecated 2.6.0 due to performance concerns |
| 1139 | */ |
| 1140 | public function get_products_in_view() { |
| 1141 | wc_deprecated_function( 'get_products_in_view', '2.6' ); |
| 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * Layered Nav post filter. |
| 1146 | * |
| 1147 | * @deprecated 2.6.0 due to performance concerns |
| 1148 | * |
| 1149 | * @param mixed $deprecated Deprecated. |
| 1150 | */ |
| 1151 | public function layered_nav_query( $deprecated ) { |
| 1152 | wc_deprecated_function( 'layered_nav_query', '2.6' ); |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * Search post excerpt. |
| 1157 | * |
| 1158 | * @param string $where Where clause. |
| 1159 | * |
| 1160 | * @deprecated 3.2.0 - Not needed anymore since WordPress 4.5. |
| 1161 | */ |
| 1162 | public function search_post_excerpt( $where = '' ) { |
| 1163 | wc_deprecated_function( 'WC_Query::search_post_excerpt', '3.2.0', 'Excerpt added to search query by default since WordPress 4.5.' ); |
| 1164 | return $where; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * Remove the posts_where filter. |
| 1169 | * |
| 1170 | * @deprecated 3.2.0 - Nothing to remove anymore because search_post_excerpt() is deprecated. |
| 1171 | */ |
| 1172 | public function remove_posts_where() { |
| 1173 | wc_deprecated_function( 'WC_Query::remove_posts_where', '3.2.0', 'Nothing to remove anymore because search_post_excerpt() is deprecated.' ); |
| 1174 | } |
| 1175 | } |
| 1176 |