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