abstracts
6 years ago
admin
6 years ago
cli
7 years ago
customizer
8 years ago
data-stores
6 years ago
emails
6 years ago
export
6 years ago
gateways
6 years ago
import
6 years ago
integrations
6 years ago
interfaces
6 years ago
legacy
6 years ago
libraries
6 years ago
log-handlers
7 years ago
payment-tokens
8 years ago
queue
7 years ago
shipping
6 years ago
shortcodes
6 years ago
theme-support
6 years ago
tracks
6 years ago
traits
6 years ago
walkers
8 years ago
wccom-site
6 years ago
widgets
6 years ago
class-wc-ajax.php
6 years ago
class-wc-api.php
6 years ago
class-wc-auth.php
7 years ago
class-wc-autoloader.php
6 years ago
class-wc-background-emailer.php
8 years ago
class-wc-background-updater.php
7 years ago
class-wc-breadcrumb.php
7 years ago
class-wc-cache-helper.php
6 years ago
class-wc-cart-fees.php
6 years ago
class-wc-cart-session.php
6 years ago
class-wc-cart-totals.php
6 years ago
class-wc-cart.php
6 years ago
class-wc-checkout.php
6 years ago
class-wc-cli.php
8 years ago
class-wc-comments.php
6 years ago
class-wc-countries.php
6 years ago
class-wc-coupon.php
6 years ago
class-wc-customer-download-log.php
8 years ago
class-wc-customer-download.php
6 years ago
class-wc-customer.php
7 years ago
class-wc-data-exception.php
8 years ago
class-wc-data-store.php
6 years ago
class-wc-datetime.php
7 years ago
class-wc-deprecated-action-hooks.php
8 years ago
class-wc-deprecated-filter-hooks.php
7 years ago
class-wc-discounts.php
6 years ago
class-wc-download-handler.php
6 years ago
class-wc-emails.php
6 years ago
class-wc-embed.php
8 years ago
class-wc-form-handler.php
6 years ago
class-wc-frontend-scripts.php
6 years ago
class-wc-geo-ip.php
8 years ago
class-wc-geolite-integration.php
6 years ago
class-wc-geolocation.php
6 years ago
class-wc-https.php
8 years ago
class-wc-install.php
6 years ago
class-wc-integrations.php
6 years ago
class-wc-log-levels.php
7 years ago
class-wc-logger.php
7 years ago
class-wc-meta-data.php
7 years ago
class-wc-order-factory.php
6 years ago
class-wc-order-item-coupon.php
7 years ago
class-wc-order-item-fee.php
7 years ago
class-wc-order-item-meta.php
7 years ago
class-wc-order-item-product.php
6 years ago
class-wc-order-item-shipping.php
7 years ago
class-wc-order-item-tax.php
6 years ago
class-wc-order-item.php
6 years ago
class-wc-order-query.php
7 years ago
class-wc-order-refund.php
8 years ago
class-wc-order.php
6 years ago
class-wc-payment-gateways.php
7 years ago
class-wc-payment-tokens.php
6 years ago
class-wc-post-data.php
6 years ago
class-wc-post-types.php
7 years ago
class-wc-privacy-background-process.php
8 years ago
class-wc-privacy-erasers.php
8 years ago
class-wc-privacy-exporters.php
7 years ago
class-wc-privacy.php
7 years ago
class-wc-product-attribute.php
7 years ago
class-wc-product-download.php
7 years ago
class-wc-product-external.php
8 years ago
class-wc-product-factory.php
7 years ago
class-wc-product-grouped.php
8 years ago
class-wc-product-query.php
7 years ago
class-wc-product-simple.php
6 years ago
class-wc-product-variable.php
6 years ago
class-wc-product-variation.php
7 years ago
class-wc-query.php
6 years ago
class-wc-rate-limiter.php
6 years ago
class-wc-regenerate-images-request.php
7 years ago
class-wc-regenerate-images.php
7 years ago
class-wc-register-wp-admin-settings.php
8 years ago
class-wc-rest-authentication.php
6 years ago
class-wc-rest-exception.php
6 years ago
class-wc-session-handler.php
6 years ago
class-wc-shipping-rate.php
8 years ago
class-wc-shipping-zone.php
6 years ago
class-wc-shipping-zones.php
8 years ago
class-wc-shipping.php
6 years ago
class-wc-shortcodes.php
6 years ago
class-wc-structured-data.php
6 years ago
class-wc-tax.php
6 years ago
class-wc-template-loader.php
7 years ago
class-wc-tracker.php
6 years ago
class-wc-validation.php
6 years ago
class-wc-webhook.php
6 years ago
class-woocommerce.php
6 years ago
wc-account-functions.php
6 years ago
wc-attribute-functions.php
6 years ago
wc-cart-functions.php
6 years ago
wc-conditional-functions.php
6 years ago
wc-core-functions.php
6 years ago
wc-coupon-functions.php
7 years ago
wc-deprecated-functions.php
7 years ago
wc-formatting-functions.php
6 years ago
wc-notice-functions.php
6 years ago
wc-order-functions.php
6 years ago
wc-order-item-functions.php
6 years ago
wc-page-functions.php
6 years ago
wc-product-functions.php
6 years ago
wc-rest-functions.php
7 years ago
wc-stock-functions.php
6 years ago
wc-template-functions.php
6 years ago
wc-template-hooks.php
6 years ago
wc-term-functions.php
6 years ago
wc-update-functions.php
6 years ago
wc-user-functions.php
6 years ago
wc-webhook-functions.php
7 years ago
wc-widget-functions.php
8 years ago
class-wc-query.php
888 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 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * WC_Query Class. |
| 13 | */ |
| 14 | class WC_Query { |
| 15 | |
| 16 | /** |
| 17 | * Query vars to add to wp. |
| 18 | * |
| 19 | * @var array |
| 20 | */ |
| 21 | public $query_vars = array(); |
| 22 | |
| 23 | /** |
| 24 | * Reference to the main product query on the page. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | private static $product_query; |
| 29 | |
| 30 | /** |
| 31 | * Stores chosen attributes. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private static $_chosen_attributes; |
| 36 | |
| 37 | /** |
| 38 | * Constructor for the query class. Hooks in methods. |
| 39 | */ |
| 40 | public function __construct() { |
| 41 | add_action( 'init', array( $this, 'add_endpoints' ) ); |
| 42 | if ( ! is_admin() ) { |
| 43 | add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 ); |
| 44 | add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); |
| 45 | add_action( 'parse_request', array( $this, 'parse_request' ), 0 ); |
| 46 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 47 | add_filter( 'the_posts', array( $this, 'remove_product_query_filters' ) ); |
| 48 | add_filter( 'get_pagenum_link', array( $this, 'remove_add_to_cart_pagination' ), 10, 1 ); |
| 49 | } |
| 50 | $this->init_query_vars(); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get any errors from querystring. |
| 55 | */ |
| 56 | public function get_errors() { |
| 57 | $error = ! empty( $_GET['wc_error'] ) ? sanitize_text_field( wp_unslash( $_GET['wc_error'] ) ) : ''; // WPCS: input var ok, CSRF ok. |
| 58 | |
| 59 | if ( $error && ! wc_has_notice( $error, 'error' ) ) { |
| 60 | wc_add_notice( $error, 'error' ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Init query vars by loading options. |
| 66 | */ |
| 67 | public function init_query_vars() { |
| 68 | // Query vars to add to WP. |
| 69 | $this->query_vars = array( |
| 70 | // Checkout actions. |
| 71 | 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ), |
| 72 | 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ), |
| 73 | // My account actions. |
| 74 | 'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ), |
| 75 | 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ), |
| 76 | 'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ), |
| 77 | 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ), |
| 78 | 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ), |
| 79 | 'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ), |
| 80 | 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ), |
| 81 | 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ), |
| 82 | 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ), |
| 83 | 'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ), |
| 84 | 'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ), |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get page title for an endpoint. |
| 90 | * |
| 91 | * @param string $endpoint Endpoint key. |
| 92 | * @return string |
| 93 | */ |
| 94 | public function get_endpoint_title( $endpoint ) { |
| 95 | global $wp; |
| 96 | |
| 97 | switch ( $endpoint ) { |
| 98 | case 'order-pay': |
| 99 | $title = __( 'Pay for order', 'woocommerce' ); |
| 100 | break; |
| 101 | case 'order-received': |
| 102 | $title = __( 'Order received', 'woocommerce' ); |
| 103 | break; |
| 104 | case 'orders': |
| 105 | if ( ! empty( $wp->query_vars['orders'] ) ) { |
| 106 | /* translators: %s: page */ |
| 107 | $title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) ); |
| 108 | } else { |
| 109 | $title = __( 'Orders', 'woocommerce' ); |
| 110 | } |
| 111 | break; |
| 112 | case 'view-order': |
| 113 | $order = wc_get_order( $wp->query_vars['view-order'] ); |
| 114 | /* translators: %s: order number */ |
| 115 | $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : ''; |
| 116 | break; |
| 117 | case 'downloads': |
| 118 | $title = __( 'Downloads', 'woocommerce' ); |
| 119 | break; |
| 120 | case 'edit-account': |
| 121 | $title = __( 'Account details', 'woocommerce' ); |
| 122 | break; |
| 123 | case 'edit-address': |
| 124 | $title = __( 'Addresses', 'woocommerce' ); |
| 125 | break; |
| 126 | case 'payment-methods': |
| 127 | $title = __( 'Payment methods', 'woocommerce' ); |
| 128 | break; |
| 129 | case 'add-payment-method': |
| 130 | $title = __( 'Add payment method', 'woocommerce' ); |
| 131 | break; |
| 132 | case 'lost-password': |
| 133 | $title = __( 'Lost password', 'woocommerce' ); |
| 134 | break; |
| 135 | default: |
| 136 | $title = ''; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Endpoint mask describing the places the endpoint should be added. |
| 145 | * |
| 146 | * @since 2.6.2 |
| 147 | * @return int |
| 148 | */ |
| 149 | public function get_endpoints_mask() { |
| 150 | if ( 'page' === get_option( 'show_on_front' ) ) { |
| 151 | $page_on_front = get_option( 'page_on_front' ); |
| 152 | $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' ); |
| 153 | $checkout_page_id = get_option( 'woocommerce_checkout_page_id' ); |
| 154 | |
| 155 | if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ), true ) ) { |
| 156 | return EP_ROOT | EP_PAGES; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | return EP_PAGES; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Add endpoints for query vars. |
| 165 | */ |
| 166 | public function add_endpoints() { |
| 167 | $mask = $this->get_endpoints_mask(); |
| 168 | |
| 169 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 170 | if ( ! empty( $var ) ) { |
| 171 | add_rewrite_endpoint( $var, $mask ); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Add query vars. |
| 178 | * |
| 179 | * @param array $vars Query vars. |
| 180 | * @return array |
| 181 | */ |
| 182 | public function add_query_vars( $vars ) { |
| 183 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 184 | $vars[] = $key; |
| 185 | } |
| 186 | return $vars; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get query vars. |
| 191 | * |
| 192 | * @return array |
| 193 | */ |
| 194 | public function get_query_vars() { |
| 195 | return apply_filters( 'woocommerce_get_query_vars', $this->query_vars ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get query current active query var. |
| 200 | * |
| 201 | * @return string |
| 202 | */ |
| 203 | public function get_current_endpoint() { |
| 204 | global $wp; |
| 205 | |
| 206 | foreach ( $this->get_query_vars() as $key => $value ) { |
| 207 | if ( isset( $wp->query_vars[ $key ] ) ) { |
| 208 | return $key; |
| 209 | } |
| 210 | } |
| 211 | return ''; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Parse the request and look for query vars - endpoints may not be supported. |
| 216 | */ |
| 217 | public function parse_request() { |
| 218 | global $wp; |
| 219 | |
| 220 | // Map query vars to their keys, or get them if endpoints are not supported. |
| 221 | foreach ( $this->get_query_vars() as $key => $var ) { |
| 222 | if ( isset( $_GET[ $var ] ) ) { // WPCS: input var ok, CSRF ok. |
| 223 | $wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) ); // WPCS: input var ok, CSRF ok. |
| 224 | } elseif ( isset( $wp->query_vars[ $var ] ) ) { |
| 225 | $wp->query_vars[ $key ] = $wp->query_vars[ $var ]; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Are we currently on the front page? |
| 232 | * |
| 233 | * @param WP_Query $q Query instance. |
| 234 | * @return bool |
| 235 | */ |
| 236 | private function is_showing_page_on_front( $q ) { |
| 237 | return ( $q->is_home() && ! $q->is_posts_page ) && 'page' === get_option( 'show_on_front' ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Is the front page a page we define? |
| 242 | * |
| 243 | * @param int $page_id Page ID. |
| 244 | * @return bool |
| 245 | */ |
| 246 | private function page_on_front_is( $page_id ) { |
| 247 | return absint( get_option( 'page_on_front' ) ) === absint( $page_id ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Hook into pre_get_posts to do the main product query. |
| 252 | * |
| 253 | * @param WP_Query $q Query instance. |
| 254 | */ |
| 255 | public function pre_get_posts( $q ) { |
| 256 | // We only want to affect the main query. |
| 257 | if ( ! $q->is_main_query() ) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // Fixes for queries on static homepages. |
| 262 | if ( $this->is_showing_page_on_front( $q ) ) { |
| 263 | |
| 264 | // Fix for endpoints on the homepage. |
| 265 | if ( ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) { |
| 266 | $_query = wp_parse_args( $q->query ); |
| 267 | if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) { |
| 268 | $q->is_page = true; |
| 269 | $q->is_home = false; |
| 270 | $q->is_singular = true; |
| 271 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 272 | add_filter( 'redirect_canonical', '__return_false' ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // When orderby is set, WordPress shows posts on the front-page. Get around that here. |
| 277 | if ( $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) { |
| 278 | $_query = wp_parse_args( $q->query ); |
| 279 | if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) { |
| 280 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 281 | $q->is_page = true; |
| 282 | $q->is_home = false; |
| 283 | |
| 284 | // WP supporting themes show post type archive. |
| 285 | if ( current_theme_supports( 'woocommerce' ) ) { |
| 286 | $q->set( 'post_type', 'product' ); |
| 287 | } else { |
| 288 | $q->is_singular = true; |
| 289 | } |
| 290 | } |
| 291 | } elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification |
| 292 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
| 293 | $q->is_page = true; |
| 294 | $q->is_home = false; |
| 295 | $q->is_singular = true; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Fix product feeds. |
| 300 | if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) { |
| 301 | $q->is_comment_feed = false; |
| 302 | } |
| 303 | |
| 304 | // Special check for shops with the PRODUCT POST TYPE ARCHIVE on front. |
| 305 | if ( current_theme_supports( 'woocommerce' ) && $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) { |
| 306 | // This is a front-page shop. |
| 307 | $q->set( 'post_type', 'product' ); |
| 308 | $q->set( 'page_id', '' ); |
| 309 | |
| 310 | if ( isset( $q->query['paged'] ) ) { |
| 311 | $q->set( 'paged', $q->query['paged'] ); |
| 312 | } |
| 313 | |
| 314 | // Define a variable so we know this is the front page shop later on. |
| 315 | wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true ); |
| 316 | |
| 317 | // Get the actual WP page to avoid errors and let us use is_front_page(). |
| 318 | // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096. |
| 319 | global $wp_post_types; |
| 320 | |
| 321 | $shop_page = get_post( wc_get_page_id( 'shop' ) ); |
| 322 | |
| 323 | $wp_post_types['product']->ID = $shop_page->ID; |
| 324 | $wp_post_types['product']->post_title = $shop_page->post_title; |
| 325 | $wp_post_types['product']->post_name = $shop_page->post_name; |
| 326 | $wp_post_types['product']->post_type = $shop_page->post_type; |
| 327 | $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type ); |
| 328 | |
| 329 | // Fix conditional Functions like is_front_page. |
| 330 | $q->is_singular = false; |
| 331 | $q->is_post_type_archive = true; |
| 332 | $q->is_archive = true; |
| 333 | $q->is_page = true; |
| 334 | |
| 335 | // Remove post type archive name from front page title tag. |
| 336 | add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); |
| 337 | |
| 338 | // Fix WP SEO. |
| 339 | if ( class_exists( 'WPSEO_Meta' ) ) { |
| 340 | add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) ); |
| 341 | add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) ); |
| 342 | } |
| 343 | } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) { |
| 344 | // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies. |
| 345 | return; |
| 346 | } |
| 347 | |
| 348 | $this->product_query( $q ); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Pre_get_posts above may adjust the main query to add WooCommerce logic. When this query is done, we need to ensure |
| 353 | * all custom filters are removed. |
| 354 | * |
| 355 | * This is done here during the_posts filter. The input is not changed. |
| 356 | * |
| 357 | * @param array $posts Posts from WP Query. |
| 358 | * @return array |
| 359 | */ |
| 360 | public function remove_product_query_filters( $posts ) { |
| 361 | $this->remove_ordering_args(); |
| 362 | remove_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 ); |
| 363 | return $posts; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * WP SEO meta description. |
| 368 | * |
| 369 | * Hooked into wpseo_ hook already, so no need for function_exist. |
| 370 | * |
| 371 | * @return string |
| 372 | */ |
| 373 | public function wpseo_metadesc() { |
| 374 | return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) ); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * WP SEO meta key. |
| 379 | * |
| 380 | * Hooked into wpseo_ hook already, so no need for function_exist. |
| 381 | * |
| 382 | * @return string |
| 383 | */ |
| 384 | public function wpseo_metakey() { |
| 385 | return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) ); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Query the products, applying sorting/ordering etc. |
| 390 | * This applies to the main WordPress loop. |
| 391 | * |
| 392 | * @param WP_Query $q Query instance. |
| 393 | */ |
| 394 | public function product_query( $q ) { |
| 395 | if ( ! is_feed() ) { |
| 396 | $ordering = $this->get_catalog_ordering_args(); |
| 397 | $q->set( 'orderby', $ordering['orderby'] ); |
| 398 | $q->set( 'order', $ordering['order'] ); |
| 399 | |
| 400 | if ( isset( $ordering['meta_key'] ) ) { |
| 401 | $q->set( 'meta_key', $ordering['meta_key'] ); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | // Query vars that affect posts shown. |
| 406 | $q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) ); |
| 407 | $q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) ); |
| 408 | $q->set( 'wc_query', 'product_query' ); |
| 409 | $q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) ); |
| 410 | |
| 411 | // Work out how many products to query. |
| 412 | $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() ) ); |
| 413 | |
| 414 | // Store reference to this query. |
| 415 | self::$product_query = $q; |
| 416 | |
| 417 | // Additonal hooks to change WP Query. |
| 418 | add_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 ); |
| 419 | |
| 420 | do_action( 'woocommerce_product_query', $q, $this ); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Remove the query. |
| 425 | */ |
| 426 | public function remove_product_query() { |
| 427 | remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Remove ordering queries. |
| 432 | */ |
| 433 | public function remove_ordering_args() { |
| 434 | remove_filter( 'posts_clauses', array( $this, 'order_by_price_asc_post_clauses' ) ); |
| 435 | remove_filter( 'posts_clauses', array( $this, 'order_by_price_desc_post_clauses' ) ); |
| 436 | remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); |
| 437 | remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Returns an array of arguments for ordering products based on the selected values. |
| 442 | * |
| 443 | * @param string $orderby Order by param. |
| 444 | * @param string $order Order param. |
| 445 | * @return array |
| 446 | */ |
| 447 | public function get_catalog_ordering_args( $orderby = '', $order = '' ) { |
| 448 | // Get ordering from query string unless defined. |
| 449 | if ( ! $orderby ) { |
| 450 | $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) wp_unslash( $_GET['orderby'] ) ) : wc_clean( get_query_var( 'orderby' ) ); // WPCS: sanitization ok, input var ok, CSRF ok. |
| 451 | |
| 452 | if ( ! $orderby_value ) { |
| 453 | if ( is_search() ) { |
| 454 | $orderby_value = 'relevance'; |
| 455 | } else { |
| 456 | $orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) ); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // Get order + orderby args from string. |
| 461 | $orderby_value = is_array( $orderby_value ) ? $orderby_value : explode( '-', $orderby_value ); |
| 462 | $orderby = esc_attr( $orderby_value[0] ); |
| 463 | $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order; |
| 464 | } |
| 465 | |
| 466 | // Convert to correct format. |
| 467 | $orderby = strtolower( is_array( $orderby ) ? (string) current( $orderby ) : (string) $orderby ); |
| 468 | $order = strtoupper( is_array( $order ) ? (string) current( $order ) : (string) $order ); |
| 469 | $args = array( |
| 470 | 'orderby' => $orderby, |
| 471 | 'order' => ( 'DESC' === $order ) ? 'DESC' : 'ASC', |
| 472 | 'meta_key' => '', // @codingStandardsIgnoreLine |
| 473 | ); |
| 474 | |
| 475 | switch ( $orderby ) { |
| 476 | case 'id': |
| 477 | $args['orderby'] = 'ID'; |
| 478 | break; |
| 479 | case 'menu_order': |
| 480 | $args['orderby'] = 'menu_order title'; |
| 481 | break; |
| 482 | case 'title': |
| 483 | $args['orderby'] = 'title'; |
| 484 | $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; |
| 485 | break; |
| 486 | case 'relevance': |
| 487 | $args['orderby'] = 'relevance'; |
| 488 | $args['order'] = 'DESC'; |
| 489 | break; |
| 490 | case 'rand': |
| 491 | $args['orderby'] = 'rand'; // @codingStandardsIgnoreLine |
| 492 | break; |
| 493 | case 'date': |
| 494 | $args['orderby'] = 'date ID'; |
| 495 | $args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC'; |
| 496 | break; |
| 497 | case 'price': |
| 498 | $callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses'; |
| 499 | add_filter( 'posts_clauses', array( $this, $callback ) ); |
| 500 | break; |
| 501 | case 'popularity': |
| 502 | add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); |
| 503 | break; |
| 504 | case 'rating': |
| 505 | add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); |
| 506 | break; |
| 507 | } |
| 508 | |
| 509 | return apply_filters( 'woocommerce_get_catalog_ordering_args', $args, $orderby, $order ); |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Custom query used to filter products by price. |
| 514 | * |
| 515 | * @since 3.6.0 |
| 516 | * |
| 517 | * @param array $args Query args. |
| 518 | * @param WC_Query $wp_query WC_Query object. |
| 519 | * |
| 520 | * @return array |
| 521 | */ |
| 522 | public function price_filter_post_clauses( $args, $wp_query ) { |
| 523 | global $wpdb; |
| 524 | |
| 525 | if ( ! $wp_query->is_main_query() || ( ! isset( $_GET['max_price'] ) && ! isset( $_GET['min_price'] ) ) ) { |
| 526 | return $args; |
| 527 | } |
| 528 | |
| 529 | $current_min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0; // WPCS: input var ok, CSRF ok. |
| 530 | $current_max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX; // WPCS: input var ok, CSRF ok. |
| 531 | |
| 532 | /** |
| 533 | * Adjust if the store taxes are not displayed how they are stored. |
| 534 | * Kicks in when prices excluding tax are displayed including tax. |
| 535 | */ |
| 536 | if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) { |
| 537 | $tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class. |
| 538 | $tax_rates = WC_Tax::get_rates( $tax_class ); |
| 539 | |
| 540 | if ( $tax_rates ) { |
| 541 | $current_min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_min_price, $tax_rates ) ); |
| 542 | $current_max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $current_max_price, $tax_rates ) ); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 547 | $args['where'] .= $wpdb->prepare( |
| 548 | ' AND wc_product_meta_lookup.min_price >= %f AND wc_product_meta_lookup.max_price <= %f ', |
| 549 | $current_min_price, |
| 550 | $current_max_price |
| 551 | ); |
| 552 | return $args; |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Handle numeric price sorting. |
| 557 | * |
| 558 | * @param array $args Query args. |
| 559 | * @return array |
| 560 | */ |
| 561 | public function order_by_price_asc_post_clauses( $args ) { |
| 562 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 563 | $args['orderby'] = ' wc_product_meta_lookup.min_price ASC, wc_product_meta_lookup.product_id ASC '; |
| 564 | return $args; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Handle numeric price sorting. |
| 569 | * |
| 570 | * @param array $args Query args. |
| 571 | * @return array |
| 572 | */ |
| 573 | public function order_by_price_desc_post_clauses( $args ) { |
| 574 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 575 | $args['orderby'] = ' wc_product_meta_lookup.max_price DESC, wc_product_meta_lookup.product_id DESC '; |
| 576 | return $args; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * WP Core does not let us change the sort direction for individual orderby params - https://core.trac.wordpress.org/ticket/17065. |
| 581 | * |
| 582 | * This lets us sort by meta value desc, and have a second orderby param. |
| 583 | * |
| 584 | * @param array $args Query args. |
| 585 | * @return array |
| 586 | */ |
| 587 | public function order_by_popularity_post_clauses( $args ) { |
| 588 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 589 | $args['orderby'] = ' wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC '; |
| 590 | return $args; |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Order by rating post clauses. |
| 595 | * |
| 596 | * @param array $args Query args. |
| 597 | * @return array |
| 598 | */ |
| 599 | public function order_by_rating_post_clauses( $args ) { |
| 600 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
| 601 | $args['orderby'] = ' wc_product_meta_lookup.average_rating DESC, wc_product_meta_lookup.product_id DESC '; |
| 602 | return $args; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Join wc_product_meta_lookup to posts if not already joined. |
| 607 | * |
| 608 | * @param string $sql SQL join. |
| 609 | * @return string |
| 610 | */ |
| 611 | private function append_product_sorting_table_join( $sql ) { |
| 612 | global $wpdb; |
| 613 | |
| 614 | if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) { |
| 615 | $sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id "; |
| 616 | } |
| 617 | return $sql; |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Appends meta queries to an array. |
| 622 | * |
| 623 | * @param array $meta_query Meta query. |
| 624 | * @param bool $main_query If is main query. |
| 625 | * @return array |
| 626 | */ |
| 627 | public function get_meta_query( $meta_query = array(), $main_query = false ) { |
| 628 | if ( ! is_array( $meta_query ) ) { |
| 629 | $meta_query = array(); |
| 630 | } |
| 631 | return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) ); |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * Appends tax queries to an array. |
| 636 | * |
| 637 | * @param array $tax_query Tax query. |
| 638 | * @param bool $main_query If is main query. |
| 639 | * @return array |
| 640 | */ |
| 641 | public function get_tax_query( $tax_query = array(), $main_query = false ) { |
| 642 | if ( ! is_array( $tax_query ) ) { |
| 643 | $tax_query = array( |
| 644 | 'relation' => 'AND', |
| 645 | ); |
| 646 | } |
| 647 | |
| 648 | // Layered nav filters on terms. |
| 649 | if ( $main_query ) { |
| 650 | foreach ( $this->get_layered_nav_chosen_attributes() as $taxonomy => $data ) { |
| 651 | $tax_query[] = array( |
| 652 | 'taxonomy' => $taxonomy, |
| 653 | 'field' => 'slug', |
| 654 | 'terms' => $data['terms'], |
| 655 | 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN', |
| 656 | 'include_children' => false, |
| 657 | ); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | $product_visibility_terms = wc_get_product_visibility_term_ids(); |
| 662 | $product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] ); |
| 663 | |
| 664 | // Hide out of stock products. |
| 665 | if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { |
| 666 | $product_visibility_not_in[] = $product_visibility_terms['outofstock']; |
| 667 | } |
| 668 | |
| 669 | // Filter by rating. |
| 670 | if ( isset( $_GET['rating_filter'] ) ) { // WPCS: input var ok, CSRF ok. |
| 671 | $rating_filter = array_filter( array_map( 'absint', explode( ',', $_GET['rating_filter'] ) ) ); // WPCS: input var ok, CSRF ok, Sanitization ok. |
| 672 | $rating_terms = array(); |
| 673 | for ( $i = 1; $i <= 5; $i ++ ) { |
| 674 | if ( in_array( $i, $rating_filter, true ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) { |
| 675 | $rating_terms[] = $product_visibility_terms[ 'rated-' . $i ]; |
| 676 | } |
| 677 | } |
| 678 | if ( ! empty( $rating_terms ) ) { |
| 679 | $tax_query[] = array( |
| 680 | 'taxonomy' => 'product_visibility', |
| 681 | 'field' => 'term_taxonomy_id', |
| 682 | 'terms' => $rating_terms, |
| 683 | 'operator' => 'IN', |
| 684 | 'rating_filter' => true, |
| 685 | ); |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if ( ! empty( $product_visibility_not_in ) ) { |
| 690 | $tax_query[] = array( |
| 691 | 'taxonomy' => 'product_visibility', |
| 692 | 'field' => 'term_taxonomy_id', |
| 693 | 'terms' => $product_visibility_not_in, |
| 694 | 'operator' => 'NOT IN', |
| 695 | ); |
| 696 | } |
| 697 | |
| 698 | return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) ); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Get the main query which product queries ran against. |
| 703 | * |
| 704 | * @return array |
| 705 | */ |
| 706 | public static function get_main_query() { |
| 707 | return self::$product_query; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * Get the tax query which was used by the main query. |
| 712 | * |
| 713 | * @return array |
| 714 | */ |
| 715 | public static function get_main_tax_query() { |
| 716 | $tax_query = isset( self::$product_query->tax_query, self::$product_query->tax_query->queries ) ? self::$product_query->tax_query->queries : array(); |
| 717 | |
| 718 | return $tax_query; |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Get the meta query which was used by the main query. |
| 723 | * |
| 724 | * @return array |
| 725 | */ |
| 726 | public static function get_main_meta_query() { |
| 727 | $args = self::$product_query->query_vars; |
| 728 | $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); |
| 729 | |
| 730 | return $meta_query; |
| 731 | } |
| 732 | |
| 733 | /** |
| 734 | * Based on WP_Query::parse_search |
| 735 | */ |
| 736 | public static function get_main_search_query_sql() { |
| 737 | global $wpdb; |
| 738 | |
| 739 | $args = self::$product_query->query_vars; |
| 740 | $search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array(); |
| 741 | $sql = array(); |
| 742 | |
| 743 | foreach ( $search_terms as $term ) { |
| 744 | // Terms prefixed with '-' should be excluded. |
| 745 | $include = '-' !== substr( $term, 0, 1 ); |
| 746 | |
| 747 | if ( $include ) { |
| 748 | $like_op = 'LIKE'; |
| 749 | $andor_op = 'OR'; |
| 750 | } else { |
| 751 | $like_op = 'NOT LIKE'; |
| 752 | $andor_op = 'AND'; |
| 753 | $term = substr( $term, 1 ); |
| 754 | } |
| 755 | |
| 756 | $like = '%' . $wpdb->esc_like( $term ) . '%'; |
| 757 | $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 ); // unprepared SQL ok. |
| 758 | } |
| 759 | |
| 760 | if ( ! empty( $sql ) && ! is_user_logged_in() ) { |
| 761 | $sql[] = "($wpdb->posts.post_password = '')"; |
| 762 | } |
| 763 | |
| 764 | return implode( ' AND ', $sql ); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Get an array of attributes and terms selected with the layered nav widget. |
| 769 | * |
| 770 | * @return array |
| 771 | */ |
| 772 | public static function get_layered_nav_chosen_attributes() { |
| 773 | if ( ! is_array( self::$_chosen_attributes ) ) { |
| 774 | self::$_chosen_attributes = array(); |
| 775 | |
| 776 | if ( ! empty( $_GET ) ) { // WPCS: input var ok, CSRF ok. |
| 777 | foreach ( $_GET as $key => $value ) { // WPCS: input var ok, CSRF ok. |
| 778 | if ( 0 === strpos( $key, 'filter_' ) ) { |
| 779 | $attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) ); |
| 780 | $taxonomy = wc_attribute_taxonomy_name( $attribute ); |
| 781 | $filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array(); |
| 782 | |
| 783 | if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) { |
| 784 | continue; |
| 785 | } |
| 786 | |
| 787 | $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 ] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. |
| 788 | self::$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding. |
| 789 | self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | return self::$_chosen_attributes; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Remove the add-to-cart param from pagination urls. |
| 799 | * |
| 800 | * @param string $url URL. |
| 801 | * @return string |
| 802 | */ |
| 803 | public function remove_add_to_cart_pagination( $url ) { |
| 804 | return remove_query_arg( 'add-to-cart', $url ); |
| 805 | } |
| 806 | |
| 807 | // @codingStandardsIgnoreStart |
| 808 | /** |
| 809 | * Return a meta query for filtering by rating. |
| 810 | * |
| 811 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 812 | * @return array |
| 813 | */ |
| 814 | public function rating_filter_meta_query() { |
| 815 | return array(); |
| 816 | } |
| 817 | |
| 818 | /** |
| 819 | * Returns a meta query to handle product visibility. |
| 820 | * |
| 821 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 822 | * @param string $compare (default: 'IN') |
| 823 | * @return array |
| 824 | */ |
| 825 | public function visibility_meta_query( $compare = 'IN' ) { |
| 826 | return array(); |
| 827 | } |
| 828 | |
| 829 | /** |
| 830 | * Returns a meta query to handle product stock status. |
| 831 | * |
| 832 | * @deprecated 3.0.0 Replaced with taxonomy. |
| 833 | * @param string $status (default: 'instock') |
| 834 | * @return array |
| 835 | */ |
| 836 | public function stock_status_meta_query( $status = 'instock' ) { |
| 837 | return array(); |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Layered nav init. |
| 842 | * |
| 843 | * @deprecated 2.6.0 |
| 844 | */ |
| 845 | public function layered_nav_init() { |
| 846 | wc_deprecated_function( 'layered_nav_init', '2.6' ); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients. |
| 851 | * |
| 852 | * @deprecated 2.6.0 due to performance concerns |
| 853 | */ |
| 854 | public function get_products_in_view() { |
| 855 | wc_deprecated_function( 'get_products_in_view', '2.6' ); |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * Layered Nav post filter. |
| 860 | * |
| 861 | * @deprecated 2.6.0 due to performance concerns |
| 862 | * |
| 863 | * @param mixed $deprecated Deprecated. |
| 864 | */ |
| 865 | public function layered_nav_query( $deprecated ) { |
| 866 | wc_deprecated_function( 'layered_nav_query', '2.6' ); |
| 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Search post excerpt. |
| 871 | * |
| 872 | * @deprecated 3.2.0 - Not needed anymore since WordPress 4.5. |
| 873 | */ |
| 874 | public function search_post_excerpt( $where = '' ) { |
| 875 | wc_deprecated_function( 'WC_Query::search_post_excerpt', '3.2.0', 'Excerpt added to search query by default since WordPress 4.5.' ); |
| 876 | return $where; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * Remove the posts_where filter. |
| 881 | * @deprecated 3.2.0 - Nothing to remove anymore because search_post_excerpt() is deprecated. |
| 882 | */ |
| 883 | public function remove_posts_where() { |
| 884 | wc_deprecated_function( 'WC_Query::remove_posts_where', '3.2.0', 'Nothing to remove anymore because search_post_excerpt() is deprecated.' ); |
| 885 | } |
| 886 | // @codingStandardsIgnoreEnd |
| 887 | } |
| 888 |