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