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