PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-query.php

https://github.com/dedavidd/piratenpartij.nl
PHP | 826 lines | 445 code | 159 blank | 222 comment | 87 complexity | 24b45f325f6f15a6727c441855445eb7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, GPL-3.0
  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 1.6.4
  7. * @package WooCommerce/Classes
  8. * @category Class
  9. * @author WooThemes
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  12. if ( ! class_exists( 'WC_Query' ) ) :
  13. /**
  14. * WC_Query Class
  15. */
  16. class WC_Query {
  17. /** @public array Query vars to add to wp */
  18. public $query_vars = array();
  19. /** @public array Unfiltered product ids (before layered nav etc) */
  20. public $unfiltered_product_ids = array();
  21. /** @public array Filtered product ids (after layered nav) */
  22. public $filtered_product_ids = array();
  23. /** @public array Filtered product ids (after layered nav, per taxonomy) */
  24. public $filtered_product_ids_for_taxonomy = array();
  25. /** @public array Product IDs that match the layered nav + price filter */
  26. public $post__in = array();
  27. /** @public array The meta query for the page */
  28. public $meta_query = '';
  29. /** @public array Post IDs matching layered nav only */
  30. public $layered_nav_post__in = array();
  31. /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
  32. public $layered_nav_product_ids = array();
  33. /**
  34. * Constructor for the query class. Hooks in methods.
  35. *
  36. * @access public
  37. */
  38. public function __construct() {
  39. add_action( 'init', array( $this, 'add_endpoints' ) );
  40. add_action( 'init', array( $this, 'layered_nav_init' ) );
  41. add_action( 'init', array( $this, 'price_filter_init' ) );
  42. if ( ! is_admin() ) {
  43. add_action( 'init', array( $this, 'get_errors' ) );
  44. add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
  45. add_action( 'parse_request', array( $this, 'parse_request'), 0 );
  46. add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  47. add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
  48. add_action( 'wp', array( $this, 'remove_product_query' ) );
  49. add_action( 'wp', array( $this, 'remove_ordering_args' ) );
  50. }
  51. $this->init_query_vars();
  52. }
  53. /**
  54. * Init query vars by loading options.
  55. */
  56. public function init_query_vars() {
  57. // Query vars to add to WP
  58. $this->query_vars = array(
  59. // Checkout actions
  60. 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
  61. 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
  62. // My account actions
  63. 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
  64. 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
  65. 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
  66. 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
  67. 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
  68. 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
  69. );
  70. }
  71. /**
  72. * Get any errors from querystring
  73. */
  74. public function get_errors() {
  75. if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) )
  76. wc_add_notice( $error, 'error' );
  77. }
  78. /**
  79. * Add endpoints for query vars
  80. */
  81. public function add_endpoints() {
  82. foreach ( $this->query_vars as $key => $var )
  83. add_rewrite_endpoint( $var, EP_PAGES );
  84. }
  85. /**
  86. * add_query_vars function.
  87. *
  88. * @access public
  89. * @param array $vars
  90. * @return array
  91. */
  92. public function add_query_vars( $vars ) {
  93. foreach ( $this->query_vars as $key => $var )
  94. $vars[] = $key;
  95. return $vars;
  96. }
  97. /**
  98. * Get query vars
  99. * @return array()
  100. */
  101. public function get_query_vars() {
  102. return $this->query_vars;
  103. }
  104. /**
  105. * Parse the request and look for query vars - endpoints may not be supported
  106. */
  107. public function parse_request() {
  108. global $wp;
  109. // Map query vars to their keys, or get them if endpoints are not supported
  110. foreach ( $this->query_vars as $key => $var ) {
  111. if ( isset( $_GET[ $var ] ) ) {
  112. $wp->query_vars[ $key ] = $_GET[ $var ];
  113. }
  114. elseif ( isset( $wp->query_vars[ $var ] ) ) {
  115. $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
  116. }
  117. }
  118. }
  119. /**
  120. * Hook into pre_get_posts to do the main product query
  121. *
  122. * @access public
  123. * @param mixed $q query object
  124. * @return void
  125. */
  126. public function pre_get_posts( $q ) {
  127. // We only want to affect the main query
  128. if ( ! $q->is_main_query() )
  129. return;
  130. // When orderby is set, WordPress shows posts. Get around that here.
  131. if ( $q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == wc_get_page_id('shop') ) {
  132. $_query = wp_parse_args( $q->query );
  133. if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
  134. $q->is_page = true;
  135. $q->is_home = false;
  136. $q->set( 'page_id', get_option('page_on_front') );
  137. $q->set( 'post_type', 'product' );
  138. }
  139. }
  140. // Special check for shops with the product archive on front
  141. if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == wc_get_page_id('shop') ) {
  142. // This is a front-page shop
  143. $q->set( 'post_type', 'product' );
  144. $q->set( 'page_id', '' );
  145. if ( isset( $q->query['paged'] ) )
  146. $q->set( 'paged', $q->query['paged'] );
  147. // Define a variable so we know this is the front page shop later on
  148. define( 'SHOP_IS_ON_FRONT', true );
  149. // Get the actual WP page to avoid errors and let us use is_front_page()
  150. // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
  151. global $wp_post_types;
  152. $shop_page = get_post( wc_get_page_id('shop') );
  153. $q->is_page = true;
  154. $wp_post_types['product']->ID = $shop_page->ID;
  155. $wp_post_types['product']->post_title = $shop_page->post_title;
  156. $wp_post_types['product']->post_name = $shop_page->post_name;
  157. $wp_post_types['product']->post_type = $shop_page->post_type;
  158. $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type );
  159. // Fix conditional Functions like is_front_page
  160. $q->is_singular = false;
  161. $q->is_post_type_archive = true;
  162. $q->is_archive = true;
  163. // Fix WP SEO
  164. if ( class_exists( 'WPSEO_Meta' ) ) {
  165. add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
  166. add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
  167. }
  168. } else {
  169. // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
  170. if ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) )
  171. return;
  172. }
  173. $this->product_query( $q );
  174. if ( is_search() ) {
  175. add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
  176. add_filter( 'wp', array( $this, 'remove_posts_where' ) );
  177. }
  178. add_filter( 'posts_where', array( $this, 'exclude_protected_products' ) );
  179. // We're on a shop page so queue the woocommerce_get_products_in_view function
  180. add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
  181. // And remove the pre_get_posts hook
  182. $this->remove_product_query();
  183. }
  184. /**
  185. * search_post_excerpt function.
  186. *
  187. * @access public
  188. * @param string $where (default: '')
  189. * @return string (modified where clause)
  190. */
  191. public function search_post_excerpt( $where = '' ) {
  192. global $wp_the_query;
  193. // If this is not a WC Query, do not modify the query
  194. if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
  195. return $where;
  196. $where = preg_replace(
  197. "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
  198. "post_title LIKE $1) OR (post_excerpt LIKE $1", $where );
  199. return $where;
  200. }
  201. /**
  202. * Prevent password protected products appearing in the loops
  203. *
  204. * @param string $where
  205. * @return string
  206. */
  207. public function exclude_protected_products( $where ) {
  208. global $wpdb;
  209. $where .= " AND {$wpdb->posts}.post_password = ''";
  210. return $where;
  211. }
  212. /**
  213. * wpseo_metadesc function.
  214. * Hooked into wpseo_ hook already, so no need for function_exist
  215. *
  216. * @access public
  217. * @return string
  218. */
  219. public function wpseo_metadesc() {
  220. return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id('shop') );
  221. }
  222. /**
  223. * wpseo_metakey function.
  224. * Hooked into wpseo_ hook already, so no need for function_exist
  225. *
  226. * @access public
  227. * @return string
  228. */
  229. public function wpseo_metakey() {
  230. return WPSEO_Meta::get_value( 'metakey', wc_get_page_id('shop') );
  231. }
  232. /**
  233. * Hook into the_posts to do the main product query if needed - relevanssi compatibility
  234. *
  235. * @access public
  236. * @param array $posts
  237. * @param WP_Query|bool $query (default: false)
  238. * @return array
  239. */
  240. public function the_posts( $posts, $query = false ) {
  241. // Abort if there's no query
  242. if ( ! $query )
  243. return $posts;
  244. // Abort if we're not filtering posts
  245. if ( empty( $this->post__in ) )
  246. return $posts;
  247. // Abort if this query has already been done
  248. if ( ! empty( $query->wc_query ) )
  249. return $posts;
  250. // Abort if this isn't a search query
  251. if ( empty( $query->query_vars["s"] ) )
  252. return $posts;
  253. // Abort if we're not on a post type archive/product taxonomy
  254. if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) )
  255. return $posts;
  256. $filtered_posts = array();
  257. $queried_post_ids = array();
  258. foreach ( $posts as $post ) {
  259. if ( in_array( $post->ID, $this->post__in ) ) {
  260. $filtered_posts[] = $post;
  261. $queried_post_ids[] = $post->ID;
  262. }
  263. }
  264. $query->posts = $filtered_posts;
  265. $query->post_count = count( $filtered_posts );
  266. // Ensure filters are set
  267. $this->unfiltered_product_ids = $queried_post_ids;
  268. $this->filtered_product_ids = $queried_post_ids;
  269. if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
  270. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  271. } else {
  272. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  273. }
  274. return $filtered_posts;
  275. }
  276. /**
  277. * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
  278. *
  279. * @access public
  280. * @param mixed $q
  281. * @return void
  282. */
  283. public function product_query( $q ) {
  284. // Meta query
  285. $meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
  286. // Ordering
  287. $ordering = $this->get_catalog_ordering_args();
  288. // Get a list of post id's which match the current filters set (in the layered nav and price filter)
  289. $post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
  290. // Ordering query vars
  291. $q->set( 'orderby', $ordering['orderby'] );
  292. $q->set( 'order', $ordering['order'] );
  293. if ( isset( $ordering['meta_key'] ) )
  294. $q->set( 'meta_key', $ordering['meta_key'] );
  295. // Query vars that affect posts shown
  296. $q->set( 'meta_query', $meta_query );
  297. $q->set( 'post__in', $post__in );
  298. $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' ) ) );
  299. // Set a special variable
  300. $q->set( 'wc_query', true );
  301. // Store variables
  302. $this->post__in = $post__in;
  303. $this->meta_query = $meta_query;
  304. do_action( 'woocommerce_product_query', $q, $this );
  305. }
  306. /**
  307. * Remove the query
  308. *
  309. * @access public
  310. * @return void
  311. */
  312. public function remove_product_query() {
  313. remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  314. }
  315. /**
  316. * Remove ordering queries
  317. */
  318. public function remove_ordering_args() {
  319. remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
  320. remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
  321. }
  322. /**
  323. * Remove the posts_where filter
  324. *
  325. * @access public
  326. * @return void
  327. */
  328. public function remove_posts_where() {
  329. remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
  330. }
  331. /**
  332. * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
  333. *
  334. * @access public
  335. * @return void
  336. */
  337. public function get_products_in_view() {
  338. global $wp_the_query;
  339. $unfiltered_product_ids = array();
  340. // Get main query
  341. $current_wp_query = $wp_the_query->query;
  342. // Get WP Query for current page (without 'paged')
  343. unset( $current_wp_query['paged'] );
  344. // Generate a transient name based on current query
  345. $transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
  346. $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
  347. if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
  348. // Get all visible posts, regardless of filters
  349. $unfiltered_product_ids = get_posts(
  350. array_merge(
  351. $current_wp_query,
  352. array(
  353. 'post_type' => 'product',
  354. 'numberposts' => -1,
  355. 'post_status' => 'publish',
  356. 'meta_query' => $this->meta_query,
  357. 'fields' => 'ids',
  358. 'no_found_rows' => true,
  359. 'update_post_meta_cache' => false,
  360. 'update_post_term_cache' => false
  361. )
  362. )
  363. );
  364. set_transient( $transient_name, $unfiltered_product_ids, YEAR_IN_SECONDS );
  365. }
  366. // Store the variable
  367. $this->unfiltered_product_ids = $unfiltered_product_ids;
  368. // Also store filtered posts ids...
  369. if ( sizeof( $this->post__in ) > 0 )
  370. $this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
  371. else
  372. $this->filtered_product_ids = $this->unfiltered_product_ids;
  373. // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
  374. if ( sizeof( $this->layered_nav_post__in ) > 0 )
  375. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  376. else
  377. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  378. }
  379. /**
  380. * Returns an array of arguments for ordering products based on the selected values
  381. *
  382. * @access public
  383. * @return array
  384. */
  385. public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
  386. // Get ordering from query string unless defined
  387. if ( ! $orderby ) {
  388. $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  389. // Get order + orderby args from string
  390. $orderby_value = explode( '-', $orderby_value );
  391. $orderby = esc_attr( $orderby_value[0] );
  392. $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
  393. }
  394. $orderby = strtolower( $orderby );
  395. $order = strtoupper( $order );
  396. $args = array();
  397. // default - menu_order
  398. $args['orderby'] = 'menu_order title';
  399. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  400. $args['meta_key'] = '';
  401. switch ( $orderby ) {
  402. case 'rand' :
  403. $args['orderby'] = 'rand';
  404. break;
  405. case 'date' :
  406. $args['orderby'] = 'date';
  407. $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
  408. break;
  409. case 'price' :
  410. $args['orderby'] = 'meta_value_num';
  411. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  412. $args['meta_key'] = '_price';
  413. break;
  414. case 'popularity' :
  415. $args['meta_key'] = 'total_sales';
  416. // Sorting handled later though a hook
  417. add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
  418. break;
  419. case 'rating' :
  420. // Sorting handled later though a hook
  421. add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
  422. break;
  423. case 'title' :
  424. $args['orderby'] = 'title';
  425. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  426. break;
  427. }
  428. return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
  429. }
  430. /**
  431. * WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065
  432. *
  433. * This lets us sort by meta value desc, and have a second orderby param.
  434. *
  435. * @access public
  436. * @param array $args
  437. * @return array
  438. */
  439. public function order_by_popularity_post_clauses( $args ) {
  440. global $wpdb;
  441. $args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
  442. return $args;
  443. }
  444. /**
  445. * order_by_rating_post_clauses function.
  446. *
  447. * @access public
  448. * @param array $args
  449. * @return array
  450. */
  451. public function order_by_rating_post_clauses( $args ) {
  452. global $wpdb;
  453. $args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
  454. $args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
  455. $args['join'] .= "
  456. LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
  457. LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
  458. ";
  459. $args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC";
  460. $args['groupby'] = "$wpdb->posts.ID";
  461. return $args;
  462. }
  463. /**
  464. * Appends meta queries to an array.
  465. * @access public
  466. * @param array $meta_query
  467. * @return array
  468. */
  469. public function get_meta_query( $meta_query = array() ) {
  470. if ( ! is_array( $meta_query ) )
  471. $meta_query = array();
  472. $meta_query[] = $this->visibility_meta_query();
  473. $meta_query[] = $this->stock_status_meta_query();
  474. return array_filter( $meta_query );
  475. }
  476. /**
  477. * Returns a meta query to handle product visibility
  478. *
  479. * @access public
  480. * @param string $compare (default: 'IN')
  481. * @return array
  482. */
  483. public function visibility_meta_query( $compare = 'IN' ) {
  484. if ( is_search() )
  485. $in = array( 'visible', 'search' );
  486. else
  487. $in = array( 'visible', 'catalog' );
  488. $meta_query = array(
  489. 'key' => '_visibility',
  490. 'value' => $in,
  491. 'compare' => $compare
  492. );
  493. return $meta_query;
  494. }
  495. /**
  496. * Returns a meta query to handle product stock status
  497. *
  498. * @access public
  499. * @param string $status (default: 'instock')
  500. * @return array
  501. */
  502. public function stock_status_meta_query( $status = 'instock' ) {
  503. $meta_query = array();
  504. if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
  505. $meta_query = array(
  506. 'key' => '_stock_status',
  507. 'value' => $status,
  508. 'compare' => '='
  509. );
  510. }
  511. return $meta_query;
  512. }
  513. /**
  514. * Layered Nav Init
  515. */
  516. public function layered_nav_init( ) {
  517. if ( is_active_widget( false, false, 'woocommerce_layered_nav', true ) && ! is_admin() ) {
  518. global $_chosen_attributes;
  519. $_chosen_attributes = array();
  520. $attribute_taxonomies = wc_get_attribute_taxonomies();
  521. if ( $attribute_taxonomies ) {
  522. foreach ( $attribute_taxonomies as $tax ) {
  523. $attribute = wc_sanitize_taxonomy_name( $tax->attribute_name );
  524. $taxonomy = wc_attribute_taxonomy_name( $attribute );
  525. $name = 'filter_' . $attribute;
  526. $query_type_name = 'query_type_' . $attribute;
  527. if ( ! empty( $_GET[ $name ] ) && taxonomy_exists( $taxonomy ) ) {
  528. $_chosen_attributes[ $taxonomy ]['terms'] = explode( ',', $_GET[ $name ] );
  529. if ( empty( $_GET[ $query_type_name ] ) || ! in_array( strtolower( $_GET[ $query_type_name ] ), array( 'and', 'or' ) ) )
  530. $_chosen_attributes[ $taxonomy ]['query_type'] = apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
  531. else
  532. $_chosen_attributes[ $taxonomy ]['query_type'] = strtolower( $_GET[ $query_type_name ] );
  533. }
  534. }
  535. }
  536. add_filter('loop_shop_post_in', array( $this, 'layered_nav_query' ) );
  537. }
  538. }
  539. /**
  540. * Layered Nav post filter
  541. *
  542. * @param array $filtered_posts
  543. * @return array
  544. */
  545. public function layered_nav_query( $filtered_posts ) {
  546. global $_chosen_attributes, $wp_query;
  547. if ( sizeof( $_chosen_attributes ) > 0 ) {
  548. $matched_products = array(
  549. 'and' => array(),
  550. 'or' => array()
  551. );
  552. $filtered_attribute = array(
  553. 'and' => false,
  554. 'or' => false
  555. );
  556. foreach ( $_chosen_attributes as $attribute => $data ) {
  557. $matched_products_from_attribute = array();
  558. $filtered = false;
  559. if ( sizeof( $data['terms'] ) > 0 ) {
  560. foreach ( $data['terms'] as $value ) {
  561. $posts = get_posts(
  562. array(
  563. 'post_type' => 'product',
  564. 'numberposts' => -1,
  565. 'post_status' => 'publish',
  566. 'fields' => 'ids',
  567. 'no_found_rows' => true,
  568. 'tax_query' => array(
  569. array(
  570. 'taxonomy' => $attribute,
  571. 'terms' => $value,
  572. 'field' => 'id'
  573. )
  574. )
  575. )
  576. );
  577. if ( ! is_wp_error( $posts ) ) {
  578. if ( sizeof( $matched_products_from_attribute ) > 0 || $filtered )
  579. $matched_products_from_attribute = $data['query_type'] == 'or' ? array_merge( $posts, $matched_products_from_attribute ) : array_intersect( $posts, $matched_products_from_attribute );
  580. else
  581. $matched_products_from_attribute = $posts;
  582. $filtered = true;
  583. }
  584. }
  585. }
  586. if ( sizeof( $matched_products[ $data['query_type'] ] ) > 0 || $filtered_attribute[ $data['query_type'] ] === true ) {
  587. $matched_products[ $data['query_type'] ] = ( $data['query_type'] == 'or' ) ? array_merge( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] ) : array_intersect( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] );
  588. } else {
  589. $matched_products[ $data['query_type'] ] = $matched_products_from_attribute;
  590. }
  591. $filtered_attribute[ $data['query_type'] ] = true;
  592. $this->filtered_product_ids_for_taxonomy[ $attribute ] = $matched_products_from_attribute;
  593. }
  594. // Combine our AND and OR result sets
  595. if ( $filtered_attribute['and'] && $filtered_attribute['or'] )
  596. $results = array_intersect( $matched_products[ 'and' ], $matched_products[ 'or' ] );
  597. else
  598. $results = array_merge( $matched_products[ 'and' ], $matched_products[ 'or' ] );
  599. if ( $filtered ) {
  600. WC()->query->layered_nav_post__in = $results;
  601. WC()->query->layered_nav_post__in[] = 0;
  602. if ( sizeof( $filtered_posts ) == 0 ) {
  603. $filtered_posts = $results;
  604. $filtered_posts[] = 0;
  605. } else {
  606. $filtered_posts = array_intersect( $filtered_posts, $results );
  607. $filtered_posts[] = 0;
  608. }
  609. }
  610. }
  611. return (array) $filtered_posts;
  612. }
  613. /**
  614. * Price filter Init
  615. */
  616. public function price_filter_init() {
  617. if ( is_active_widget( false, false, 'woocommerce_price_filter', true ) && ! is_admin() ) {
  618. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  619. wp_register_script( 'wc-price-slider', WC()->plugin_url() . '/assets/js/frontend/price-slider' . $suffix . '.js', array( 'jquery-ui-slider' ), WC_VERSION, true );
  620. wp_localize_script( 'wc-price-slider', 'woocommerce_price_slider_params', array(
  621. 'currency_symbol' => get_woocommerce_currency_symbol(),
  622. 'currency_pos' => get_option( 'woocommerce_currency_pos' ),
  623. 'min_price' => isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : '',
  624. 'max_price' => isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : ''
  625. ) );
  626. add_filter( 'loop_shop_post_in', array( $this, 'price_filter' ) );
  627. }
  628. }
  629. /**
  630. * Price Filter post filter
  631. *
  632. * @param array $filtered_posts
  633. * @return array
  634. */
  635. public function price_filter( $filtered_posts ) {
  636. global $wpdb;
  637. if ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) {
  638. $matched_products = array();
  639. $min = floatval( $_GET['min_price'] );
  640. $max = floatval( $_GET['max_price'] );
  641. $matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare("
  642. SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
  643. INNER JOIN $wpdb->postmeta ON ID = post_id
  644. WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d
  645. ", '_price', $min, $max ), OBJECT_K ), $min, $max );
  646. if ( $matched_products_query ) {
  647. foreach ( $matched_products_query as $product ) {
  648. if ( $product->post_type == 'product' )
  649. $matched_products[] = $product->ID;
  650. if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) )
  651. $matched_products[] = $product->post_parent;
  652. }
  653. }
  654. // Filter the id's
  655. if ( sizeof( $filtered_posts ) == 0) {
  656. $filtered_posts = $matched_products;
  657. $filtered_posts[] = 0;
  658. } else {
  659. $filtered_posts = array_intersect( $filtered_posts, $matched_products );
  660. $filtered_posts[] = 0;
  661. }
  662. }
  663. return (array) $filtered_posts;
  664. }
  665. }
  666. endif;
  667. return new WC_Query();