WC_Query

WC_Query Class.

Defined (1)

The class is defined in the following location(s).

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