/includes/class-wc-query.php

  1. <?php 
  2. /** 
  3. * Contains the query functions for WooCommerce which alter the front-end post queries and loops 
  4. * 
  5. * @class WC_Query 
  6. * @version 2.6.0 
  7. * @package WooCommerce/Classes 
  8. * @category Class 
  9. * @author WooThemes 
  10. */ 
  11.  
  12. if ( ! defined( 'ABSPATH' ) ) { 
  13. exit; 
  14.  
  15. /** 
  16. * WC_Query Class. 
  17. */ 
  18. class WC_Query { 
  19.  
  20. /** @public array Query vars to add to wp */ 
  21. public $query_vars = array(); 
  22.  
  23. /** 
  24. * Stores chosen attributes 
  25. * @var array 
  26. */ 
  27. private static $_chosen_attributes; 
  28.  
  29. /** 
  30. * Constructor for the query class. Hooks in methods. 
  31. * 
  32. * @access public 
  33. */ 
  34. public function __construct() { 
  35. add_action( 'init', array( $this, 'add_endpoints' ) ); 
  36. if ( ! is_admin() ) { 
  37. add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 ); 
  38. add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 ); 
  39. add_action( 'parse_request', array( $this, 'parse_request' ), 0 ); 
  40. add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); 
  41. add_action( 'wp', array( $this, 'remove_product_query' ) ); 
  42. add_action( 'wp', array( $this, 'remove_ordering_args' ) ); 
  43. $this->init_query_vars(); 
  44.  
  45. /** 
  46. * Get any errors from querystring. 
  47. */ 
  48. public function get_errors() { 
  49. if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) ) { 
  50. wc_add_notice( $error, 'error' ); 
  51.  
  52. /** 
  53. * Init query vars by loading options. 
  54. */ 
  55. public function init_query_vars() { 
  56. // Query vars to add to WP. 
  57. $this->query_vars = array( 
  58. // Checkout actions. 
  59. 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),  
  60. 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),  
  61. // My account actions. 
  62. 'orders' => get_option( 'woocommerce_myaccount_orders_endpoint', 'orders' ),  
  63. 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),  
  64. 'downloads' => get_option( 'woocommerce_myaccount_downloads_endpoint', 'downloads' ),  
  65. 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),  
  66. 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),  
  67. 'payment-methods' => get_option( 'woocommerce_myaccount_payment_methods_endpoint', 'payment-methods' ),  
  68. 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),  
  69. 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),  
  70. 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),  
  71. 'delete-payment-method' => get_option( 'woocommerce_myaccount_delete_payment_method_endpoint', 'delete-payment-method' ),  
  72. 'set-default-payment-method' => get_option( 'woocommerce_myaccount_set_default_payment_method_endpoint', 'set-default-payment-method' ),  
  73. ); 
  74.  
  75. /** 
  76. * Get page title for an endpoint. 
  77. * @param string 
  78. * @return string 
  79. */ 
  80. public function get_endpoint_title( $endpoint ) { 
  81. global $wp; 
  82.  
  83. switch ( $endpoint ) { 
  84. case 'order-pay' : 
  85. $title = __( 'Pay for order', 'woocommerce' ); 
  86. break; 
  87. case 'order-received' : 
  88. $title = __( 'Order received', 'woocommerce' ); 
  89. break; 
  90. case 'orders' : 
  91. if ( ! empty( $wp->query_vars['orders'] ) ) { 
  92. /** translators: %s: page */ 
  93. $title = sprintf( __( 'Orders (page %d)', 'woocommerce' ), intval( $wp->query_vars['orders'] ) ); 
  94. } else { 
  95. $title = __( 'Orders', 'woocommerce' ); 
  96. break; 
  97. case 'view-order' : 
  98. $order = wc_get_order( $wp->query_vars['view-order'] ); 
  99. /** translators: %s: order number */ 
  100. $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : ''; 
  101. break; 
  102. case 'downloads' : 
  103. $title = __( 'Downloads', 'woocommerce' ); 
  104. break; 
  105. case 'edit-account' : 
  106. $title = __( 'Account details', 'woocommerce' ); 
  107. break; 
  108. case 'edit-address' : 
  109. $title = __( 'Addresses', 'woocommerce' ); 
  110. break; 
  111. case 'payment-methods' : 
  112. $title = __( 'Payment methods', 'woocommerce' ); 
  113. break; 
  114. case 'add-payment-method' : 
  115. $title = __( 'Add payment method', 'woocommerce' ); 
  116. break; 
  117. case 'lost-password' : 
  118. $title = __( 'Lost password', 'woocommerce' ); 
  119. break; 
  120. default : 
  121. $title = ''; 
  122. break; 
  123.  
  124. return apply_filters( 'woocommerce_endpoint_' . $endpoint . '_title', $title, $endpoint ); 
  125.  
  126. /** 
  127. * Endpoint mask describing the places the endpoint should be added. 
  128. * 
  129. * @since 2.6.2 
  130. * @return int 
  131. */ 
  132. public function get_endpoints_mask() { 
  133. if ( 'page' === get_option( 'show_on_front' ) ) { 
  134. $page_on_front = get_option( 'page_on_front' ); 
  135. $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' ); 
  136. $checkout_page_id = get_option( 'woocommerce_checkout_page_id' ); 
  137.  
  138. if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ) ) ) { 
  139. return EP_ROOT | EP_PAGES; 
  140.  
  141. return EP_PAGES; 
  142.  
  143. /** 
  144. * Add endpoints for query vars. 
  145. */ 
  146. public function add_endpoints() { 
  147. $mask = $this->get_endpoints_mask(); 
  148.  
  149. foreach ( $this->query_vars as $key => $var ) { 
  150. if ( ! empty( $var ) ) { 
  151. add_rewrite_endpoint( $var, $mask ); 
  152.  
  153. /** 
  154. * Add query vars. 
  155. * 
  156. * @access public 
  157. * @param array $vars 
  158. * @return array 
  159. */ 
  160. public function add_query_vars( $vars ) { 
  161. foreach ( $this->get_query_vars() as $key => $var ) { 
  162. $vars[] = $key; 
  163. return $vars; 
  164.  
  165. /** 
  166. * Get query vars. 
  167. * 
  168. * @return array 
  169. */ 
  170. public function get_query_vars() { 
  171. return apply_filters( 'woocommerce_get_query_vars', $this->query_vars ); 
  172.  
  173. /** 
  174. * Get query current active query var. 
  175. * 
  176. * @return string 
  177. */ 
  178. public function get_current_endpoint() { 
  179. global $wp; 
  180. foreach ( $this->get_query_vars() as $key => $value ) { 
  181. if ( isset( $wp->query_vars[ $key ] ) ) { 
  182. return $key; 
  183. return ''; 
  184.  
  185. /** 
  186. * Parse the request and look for query vars - endpoints may not be supported. 
  187. */ 
  188. public function parse_request() { 
  189. global $wp; 
  190.  
  191. // Map query vars to their keys, or get them if endpoints are not supported 
  192. foreach ( $this->get_query_vars() as $key => $var ) { 
  193. if ( isset( $_GET[ $var ] ) ) { 
  194. $wp->query_vars[ $key ] = $_GET[ $var ]; 
  195. } elseif ( isset( $wp->query_vars[ $var ] ) ) { 
  196. $wp->query_vars[ $key ] = $wp->query_vars[ $var ]; 
  197.  
  198. /** 
  199. * Are we currently on the front page? 
  200. * @return boolean 
  201. */ 
  202. private function is_showing_page_on_front( $q ) { 
  203. return $q->is_home() && 'page' === get_option( 'show_on_front' ); 
  204.  
  205. /** 
  206. * Is the front page a page we define? 
  207. * @return boolean 
  208. */ 
  209. private function page_on_front_is( $page_id ) { 
  210. return absint( get_option( 'page_on_front' ) ) === absint( $page_id ); 
  211.  
  212. /** 
  213. * Hook into pre_get_posts to do the main product query. 
  214. * 
  215. * @param mixed $q query object 
  216. */ 
  217. public function pre_get_posts( $q ) { 
  218. // We only want to affect the main query 
  219. if ( ! $q->is_main_query() ) { 
  220. return; 
  221.  
  222. // Fix for endpoints on the homepage 
  223. if ( $this->is_showing_page_on_front( $q ) && ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) { 
  224. $_query = wp_parse_args( $q->query ); 
  225. if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) { 
  226. $q->is_page = true; 
  227. $q->is_home = false; 
  228. $q->is_singular = true; 
  229. $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); 
  230. add_filter( 'redirect_canonical', '__return_false' ); 
  231.  
  232. // When orderby is set, WordPress shows posts. Get around that here. 
  233. if ( $this->is_showing_page_on_front( $q ) && $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) { 
  234. $_query = wp_parse_args( $q->query ); 
  235. if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) { 
  236. $q->is_page = true; 
  237. $q->is_home = false; 
  238. $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); 
  239. $q->set( 'post_type', 'product' ); 
  240.  
  241. // Fix product feeds 
  242. if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) { 
  243. $q->is_comment_feed = false; 
  244.  
  245. // Special check for shops with the product archive on front 
  246. if ( $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) { 
  247. // This is a front-page shop 
  248. $q->set( 'post_type', 'product' ); 
  249. $q->set( 'page_id', '' ); 
  250.  
  251. if ( isset( $q->query['paged'] ) ) { 
  252. $q->set( 'paged', $q->query['paged'] ); 
  253.  
  254. // Define a variable so we know this is the front page shop later on 
  255. if ( ! defined( 'SHOP_IS_ON_FRONT' ) ) { 
  256. define( 'SHOP_IS_ON_FRONT', true ); 
  257.  
  258. // Get the actual WP page to avoid errors and let us use is_front_page() 
  259. // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096 
  260. global $wp_post_types; 
  261.  
  262. $shop_page = get_post( wc_get_page_id( 'shop' ) ); 
  263.  
  264. $wp_post_types['product']->ID = $shop_page->ID; 
  265. $wp_post_types['product']->post_title = $shop_page->post_title; 
  266. $wp_post_types['product']->post_name = $shop_page->post_name; 
  267. $wp_post_types['product']->post_type = $shop_page->post_type; 
  268. $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type ); 
  269.  
  270. // Fix conditional Functions like is_front_page 
  271. $q->is_singular = false; 
  272. $q->is_post_type_archive = true; 
  273. $q->is_archive = true; 
  274. $q->is_page = true; 
  275.  
  276. // Remove post type archive name from front page title tag 
  277. add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); 
  278.  
  279. // Fix WP SEO 
  280. if ( class_exists( 'WPSEO_Meta' ) ) { 
  281. add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) ); 
  282. add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) ); 
  283.  
  284. // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies 
  285. } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) { 
  286. return; 
  287.  
  288. $this->product_query( $q ); 
  289.  
  290. if ( is_search() ) { 
  291. add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) ); 
  292. add_filter( 'wp', array( $this, 'remove_posts_where' ) ); 
  293.  
  294. // And remove the pre_get_posts hook 
  295. $this->remove_product_query(); 
  296.  
  297. /** 
  298. * Search post excerpt. 
  299. * 
  300. * @access public 
  301. * @param string $where (default: '') 
  302. * @return string (modified where clause) 
  303. */ 
  304. public function search_post_excerpt( $where = '' ) { 
  305. global $wp_the_query; 
  306.  
  307. // If this is not a WC Query, do not modify the query 
  308. if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) ) 
  309. return $where; 
  310.  
  311. $where = preg_replace( 
  312. "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",  
  313. "post_title LIKE $1) OR (post_excerpt LIKE $1", $where ); 
  314.  
  315. return $where; 
  316.  
  317. /** 
  318. * WP SEO meta description. 
  319. * 
  320. * Hooked into wpseo_ hook already, so no need for function_exist. 
  321. * 
  322. * @access public 
  323. * @return string 
  324. */ 
  325. public function wpseo_metadesc() { 
  326. return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) ); 
  327.  
  328. /** 
  329. * WP SEO meta key. 
  330. * 
  331. * Hooked into wpseo_ hook already, so no need for function_exist. 
  332. * 
  333. * @access public 
  334. * @return string 
  335. */ 
  336. public function wpseo_metakey() { 
  337. return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) ); 
  338.  
  339. /** 
  340. * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop. 
  341. * 
  342. * @param mixed $q 
  343. */ 
  344. public function product_query( $q ) { 
  345. // Ordering query vars 
  346. $ordering = $this->get_catalog_ordering_args(); 
  347. $q->set( 'orderby', $ordering['orderby'] ); 
  348. $q->set( 'order', $ordering['order'] ); 
  349. if ( isset( $ordering['meta_key'] ) ) { 
  350. $q->set( 'meta_key', $ordering['meta_key'] ); 
  351.  
  352. // Query vars that affect posts shown 
  353. $q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) ); 
  354. $q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) ); 
  355. $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) ); 
  356. $q->set( 'wc_query', 'product_query' ); 
  357. $q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) ); 
  358.  
  359. do_action( 'woocommerce_product_query', $q, $this ); 
  360.  
  361.  
  362. /** 
  363. * Remove the query. 
  364. */ 
  365. public function remove_product_query() { 
  366. remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); 
  367.  
  368. /** 
  369. * Remove ordering queries. 
  370. */ 
  371. public function remove_ordering_args() { 
  372. remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); 
  373. remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); 
  374.  
  375. /** 
  376. * Remove the posts_where filter. 
  377. */ 
  378. public function remove_posts_where() { 
  379. remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) ); 
  380.  
  381. /** 
  382. * Returns an array of arguments for ordering products based on the selected values. 
  383. * 
  384. * @access public 
  385. * @return array 
  386. */ 
  387. public function get_catalog_ordering_args( $orderby = '', $order = '' ) { 
  388. // Get ordering from query string unless defined 
  389. if ( ! $orderby ) { 
  390. $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); 
  391.  
  392. // Get order + orderby args from string 
  393. $orderby_value = explode( '-', $orderby_value ); 
  394. $orderby = esc_attr( $orderby_value[0] ); 
  395. $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order; 
  396.  
  397. $orderby = strtolower( $orderby ); 
  398. $order = strtoupper( $order ); 
  399. $args = array(); 
  400.  
  401. // default - menu_order 
  402. $args['orderby'] = 'menu_order title'; 
  403. $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; 
  404. $args['meta_key'] = ''; 
  405.  
  406. switch ( $orderby ) { 
  407. case 'rand' : 
  408. $args['orderby'] = 'rand'; 
  409. break; 
  410. case 'date' : 
  411. $args['orderby'] = 'date ID'; 
  412. $args['order'] = ( 'ASC' === $order ) ? 'ASC' : 'DESC'; 
  413. break; 
  414. case 'price' : 
  415. $args['orderby'] = "meta_value_num ID"; 
  416. $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; 
  417. $args['meta_key'] = '_price'; 
  418. break; 
  419. case 'popularity' : 
  420. $args['meta_key'] = 'total_sales'; 
  421.  
  422. // Sorting handled later though a hook 
  423. add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); 
  424. break; 
  425. case 'rating' : 
  426. $args['meta_key'] = '_wc_average_rating'; 
  427. $args['orderby'] = array( 
  428. 'meta_value_num' => 'DESC',  
  429. 'ID' => 'ASC',  
  430. ); 
  431. break; 
  432. case 'title' : 
  433. $args['orderby'] = 'title'; 
  434. $args['order'] = ( 'DESC' === $order ) ? 'DESC' : 'ASC'; 
  435. break; 
  436.  
  437. return apply_filters( 'woocommerce_get_catalog_ordering_args', $args ); 
  438.  
  439. /** 
  440. * WP Core doens't let us change the sort direction for invidual orderby params - https://core.trac.wordpress.org/ticket/17065. 
  441. * 
  442. * This lets us sort by meta value desc, and have a second orderby param. 
  443. * 
  444. * @access public 
  445. * @param array $args 
  446. * @return array 
  447. */ 
  448. public function order_by_popularity_post_clauses( $args ) { 
  449. global $wpdb; 
  450. $args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC"; 
  451. return $args; 
  452.  
  453. /** 
  454. * Order by rating post clauses. 
  455. * 
  456. * @deprecated 3.0.0 
  457. * @param array $args 
  458. * @return array 
  459. */ 
  460. public function order_by_rating_post_clauses( $args ) { 
  461. global $wpdb; 
  462.  
  463. wc_deprecated_function( 'order_by_rating_post_clauses', '3.0' ); 
  464.  
  465. $args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating "; 
  466. $args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) "; 
  467. $args['join'] .= " 
  468. LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID) 
  469. LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id) 
  470. "; 
  471. $args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC"; 
  472. $args['groupby'] = "$wpdb->posts.ID"; 
  473.  
  474. return $args; 
  475.  
  476. /** 
  477. * Appends meta queries to an array. 
  478. * 
  479. * @param array $meta_query 
  480. * @param bool $main_query 
  481. * @return array 
  482. */ 
  483. public function get_meta_query( $meta_query = array(), $main_query = false ) { 
  484. if ( ! is_array( $meta_query ) ) { 
  485. $meta_query = array(); 
  486. $meta_query['price_filter'] = $this->price_filter_meta_query(); 
  487. return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) ); 
  488.  
  489. /** 
  490. * Appends tax queries to an array. 
  491. * @param array $tax_query 
  492. * @param bool $main_query 
  493. * @return array 
  494. */ 
  495. public function get_tax_query( $tax_query = array(), $main_query = false ) { 
  496. if ( ! is_array( $tax_query ) ) { 
  497. $tax_query = array( 'relation' => 'AND' ); 
  498.  
  499. // Layered nav filters on terms. 
  500. if ( $main_query && ( $_chosen_attributes = $this->get_layered_nav_chosen_attributes() ) ) { 
  501. foreach ( $_chosen_attributes as $taxonomy => $data ) { 
  502. $tax_query[] = array( 
  503. 'taxonomy' => $taxonomy,  
  504. 'field' => 'slug',  
  505. 'terms' => $data['terms'],  
  506. 'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',  
  507. 'include_children' => false,  
  508. ); 
  509.  
  510. $product_visibility_terms = wc_get_product_visibility_term_ids(); 
  511. $product_visibility_not_in = array( is_search() && $main_query ? $product_visibility_terms['exclude-from-search'] : $product_visibility_terms['exclude-from-catalog'] ); 
  512.  
  513. // Hide out of stock products. 
  514. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 
  515. $product_visibility_not_in[] = $product_visibility_terms['outofstock']; 
  516.  
  517. // Filter by rating. 
  518. if ( isset( $_GET['rating_filter'] ) ) { 
  519. $rating_filter = array_filter( array_map( 'absint', explode( ', ', $_GET['rating_filter'] ) ) ); 
  520. $rating_terms = array(); 
  521. for ( $i = 1; $i <= 5; $i ++ ) { 
  522. if ( in_array( $i, $rating_filter ) && isset( $product_visibility_terms[ 'rated-' . $i ] ) ) { 
  523. $rating_terms[] = $product_visibility_terms[ 'rated-' . $i ]; 
  524. if ( ! empty( $rating_terms ) ) { 
  525. $tax_query[] = array( 
  526. 'taxonomy' => 'product_visibility',  
  527. 'field' => 'term_taxonomy_id',  
  528. 'terms' => $rating_terms,  
  529. 'operator' => 'IN',  
  530. 'rating_filter' => true,  
  531. ); 
  532.  
  533. if ( ! empty( $product_visibility_not_in ) ) { 
  534. $tax_query[] = array( 
  535. 'taxonomy' => 'product_visibility',  
  536. 'field' => 'term_taxonomy_id',  
  537. 'terms' => $product_visibility_not_in,  
  538. 'operator' => 'NOT IN',  
  539. ); 
  540.  
  541. return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) ); 
  542.  
  543. /** 
  544. * Return a meta query for filtering by price. 
  545. * @return array 
  546. */ 
  547. private function price_filter_meta_query() { 
  548. if ( isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) { 
  549. $meta_query = wc_get_min_max_price_meta_query( $_GET ); 
  550. $meta_query['price_filter'] = true; 
  551.  
  552. return $meta_query; 
  553.  
  554. return array(); 
  555.  
  556. /** 
  557. * Return a meta query for filtering by rating. 
  558. * 
  559. * @deprecated 3.0.0 Replaced with taxonomy. 
  560. * @return array 
  561. */ 
  562. public function rating_filter_meta_query() { 
  563. return array(); 
  564.  
  565. /** 
  566. * Returns a meta query to handle product visibility. 
  567. * 
  568. * @deprecated 3.0.0 Replaced with taxonomy. 
  569. * @param string $compare (default: 'IN') 
  570. * @return array 
  571. */ 
  572. public function visibility_meta_query( $compare = 'IN' ) { 
  573. return array(); 
  574.  
  575. /** 
  576. * Returns a meta query to handle product stock status. 
  577. * 
  578. * @deprecated 3.0.0 Replaced with taxonomy. 
  579. * @param string $status (default: 'instock') 
  580. * @return array 
  581. */ 
  582. public function stock_status_meta_query( $status = 'instock' ) { 
  583. return array(); 
  584.  
  585. /** 
  586. * Get the tax query which was used by the main query. 
  587. * @return array 
  588. */ 
  589. public static function get_main_tax_query() { 
  590. global $wp_the_query; 
  591.  
  592. $tax_query = isset( $wp_the_query->tax_query, $wp_the_query->tax_query->queries ) ? $wp_the_query->tax_query->queries : array(); 
  593.  
  594. return $tax_query; 
  595.  
  596. /** 
  597. * Get the meta query which was used by the main query. 
  598. * @return array 
  599. */ 
  600. public static function get_main_meta_query() { 
  601. global $wp_the_query; 
  602.  
  603. $args = $wp_the_query->query_vars; 
  604. $meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array(); 
  605.  
  606. return $meta_query; 
  607.  
  608. /** 
  609. * Based on WP_Query::parse_search 
  610. */ 
  611. public static function get_main_search_query_sql() { 
  612. global $wp_the_query, $wpdb; 
  613.  
  614. $args = $wp_the_query->query_vars; 
  615. $search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array(); 
  616. $sql = array(); 
  617.  
  618. foreach ( $search_terms as $term ) { 
  619. // Terms prefixed with '-' should be excluded. 
  620. $include = '-' !== substr( $term, 0, 1 ); 
  621.  
  622. if ( $include ) { 
  623. $like_op = 'LIKE'; 
  624. $andor_op = 'OR'; 
  625. } else { 
  626. $like_op = 'NOT LIKE'; 
  627. $andor_op = 'AND'; 
  628. $term = substr( $term, 1 ); 
  629.  
  630. $like = '%' . $wpdb->esc_like( $term ) . '%'; 
  631. $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 ); 
  632.  
  633. if ( ! empty( $sql ) && ! is_user_logged_in() ) { 
  634. $sql[] = "($wpdb->posts.post_password = '')"; 
  635.  
  636. return implode( ' AND ', $sql ); 
  637.  
  638. /** 
  639. * Layered Nav Init. 
  640. */ 
  641. public static function get_layered_nav_chosen_attributes() { 
  642. if ( ! is_array( self::$_chosen_attributes ) ) { 
  643. self::$_chosen_attributes = array(); 
  644.  
  645. if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) { 
  646. foreach ( $attribute_taxonomies as $tax ) { 
  647. $attribute = wc_sanitize_taxonomy_name( $tax->attribute_name ); 
  648. $taxonomy = wc_attribute_taxonomy_name( $attribute ); 
  649. $filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ', ', wc_clean( $_GET[ 'filter_' . $attribute ] ) ) : array(); 
  650.  
  651. if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) ) { 
  652. continue; 
  653.  
  654. $query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ) ) ? wc_clean( $_GET[ 'query_type_' . $attribute ] ) : ''; 
  655. self::$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding 
  656. self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); 
  657. return self::$_chosen_attributes; 
  658.  
  659. /** 
  660. * @deprecated 2.6.0 
  661. */ 
  662. public function layered_nav_init() { 
  663. wc_deprecated_function( 'layered_nav_init', '2.6' ); 
  664.  
  665. /** 
  666. * Get an unpaginated list all product IDs (both filtered and unfiltered). Makes use of transients. 
  667. * @deprecated 2.6.0 due to performance concerns 
  668. */ 
  669. public function get_products_in_view() { 
  670. wc_deprecated_function( 'get_products_in_view', '2.6' ); 
  671.  
  672. /** 
  673. * Layered Nav post filter. 
  674. * @deprecated 2.6.0 due to performance concerns 
  675. */ 
  676. public function layered_nav_query( $filtered_posts ) { 
  677. wc_deprecated_function( 'layered_nav_query', '2.6' ); 
.