/classes/class-wc-query.php

https://github.com/CammoKing/woocommerce · PHP · 394 lines · 197 code · 79 blank · 118 comment · 32 complexity · 18e90ba38efbd3eff109d0b9f87b83b9 MD5 · raw file

  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. * @author WooThemes
  9. */
  10. class WC_Query {
  11. /** @var array Unfiltered product ids (before layered nav etc) */
  12. var $unfiltered_product_ids = array();
  13. /** @var array Filtered product ids (after layered nav) */
  14. var $filtered_product_ids = array();
  15. /** @var array Product IDs that match the layered nav + price filter */
  16. var $post__in = array();
  17. /** @var array The meta query for the page */
  18. var $meta_query = '';
  19. /** @var array Post IDs matching layered nav only */
  20. var $layered_nav_post__in = array();
  21. /** @var array Stores post IDs matching layered nav, so price filter can find max price in view */
  22. var $layered_nav_product_ids = array();
  23. /**
  24. * Constructor for the query class. Hooks in methods.
  25. *
  26. * @access public
  27. * @return void
  28. */
  29. function __construct() {
  30. add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts') );
  31. add_filter( 'the_posts', array( &$this, 'the_posts'), 11, 2 );
  32. add_filter( 'wp', array( &$this, 'remove_product_query') );
  33. }
  34. /**
  35. * Hook into pre_get_posts to do the main product query
  36. *
  37. * @access public
  38. * @param mixed $q query object
  39. * @return void
  40. */
  41. function pre_get_posts( $q ) {
  42. global $woocommerce;
  43. // We only want to affect the main query
  44. if ( ! $q->is_main_query() )
  45. return;
  46. // Special check for shops with the product archive on front
  47. if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == woocommerce_get_page_id('shop') ) {
  48. // This is a front-page shop
  49. $q->set( 'post_type', 'product' );
  50. $q->set( 'page_id', '' );
  51. if ( isset( $q->query['paged'] ) )
  52. $q->set( 'paged', $q->query['paged'] );
  53. // Define a variable so we know this is the front page shop later on
  54. define( 'SHOP_IS_ON_FRONT', true );
  55. // Get the actual WP page to avoid errors and let us use is_front_page()
  56. // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
  57. global $wp_post_types;
  58. $shop_page = get_post( woocommerce_get_page_id('shop') );
  59. $q->is_page = true;
  60. $wp_post_types['product']->ID = $shop_page->ID;
  61. $wp_post_types['product']->post_title = $shop_page->post_title;
  62. $wp_post_types['product']->post_name = $shop_page->post_name;
  63. // Fix conditional Functions like is_front_page
  64. $q->is_singular = false;
  65. $q->is_post_type_archive = true;
  66. $q->is_archive = true;
  67. // Fix WP SEO
  68. if ( function_exists( 'wpseo_get_value' ) ) {
  69. add_filter( 'wpseo_metadesc', array( &$this, 'wpseo_metadesc' ) );
  70. add_filter( 'wpseo_metakey', array( &$this, 'wpseo_metakey' ) );
  71. }
  72. } else {
  73. // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
  74. if ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( array_merge( array('product_cat', 'product_tag'), $woocommerce->get_attribute_taxonomy_names() ) ) )
  75. return;
  76. }
  77. $this->product_query( $q );
  78. // We're on a shop page so queue the woocommerce_get_products_in_view function
  79. add_action( 'wp', array( &$this, 'get_products_in_view' ), 2);
  80. // And remove the pre_get_posts hook
  81. $this->remove_product_query();
  82. }
  83. /**
  84. * wpseo_metadesc function.
  85. *
  86. * @access public
  87. * @param mixed $meta
  88. * @return void
  89. */
  90. function wpseo_metadesc() {
  91. return wpseo_get_value( 'metadesc', woocommerce_get_page_id('shop') );
  92. }
  93. /**
  94. * wpseo_metakey function.
  95. *
  96. * @access public
  97. * @return void
  98. */
  99. function wpseo_metakey() {
  100. return wpseo_get_value( 'metakey', woocommerce_get_page_id('shop') );
  101. }
  102. /**
  103. * Hook into the_posts to do the main product query if needed - relevanssi compatibility
  104. *
  105. * @access public
  106. * @param mixed $posts
  107. * @param bool $query (default: false)
  108. * @return void
  109. */
  110. function the_posts( $posts, $query = false ) {
  111. global $woocommerce;
  112. // Abort if theres no query
  113. if ( ! $query )
  114. return $posts;
  115. // Abort if we're not filtering posts
  116. if ( empty( $this->post__in ) )
  117. return $posts;
  118. // Abort if this query has already been done
  119. if ( ! empty( $query->wc_query ) )
  120. return $posts;
  121. // Abort if this isn't a search query
  122. if ( empty( $query->query_vars["s"] ) )
  123. return $posts;
  124. // Abort if we're not on a post type archive/product taxonomy
  125. if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( array_merge( array('product_cat', 'product_tag'), $woocommerce->get_attribute_taxonomy_names() ) ) )
  126. return $posts;
  127. $filtered_posts = array();
  128. $queried_post_ids = array();
  129. foreach ( $posts as $post ) {
  130. if ( in_array( $post->ID, $this->post__in ) ) {
  131. $filtered_posts[] = $post;
  132. $queried_post_ids[] = $post->ID;
  133. }
  134. }
  135. $query->posts = $filtered_posts;
  136. $query->post_count = count( $filtered_posts );
  137. // Ensure filters are set
  138. $this->unfiltered_product_ids = $queried_post_ids;
  139. $this->filtered_product_ids = $queried_post_ids;
  140. if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
  141. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  142. } else {
  143. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  144. }
  145. return $filtered_posts;
  146. }
  147. /**
  148. * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
  149. *
  150. * @access public
  151. * @param mixed $q
  152. * @return void
  153. */
  154. function product_query( $q ) {
  155. // Meta query
  156. $meta_query = (array) $q->get( 'meta_query' );
  157. $meta_query[] = $this->visibility_meta_query();
  158. $meta_query[] = $this->stock_status_meta_query();
  159. // Ordering
  160. $ordering = $this->get_catalog_ordering_args();
  161. // Get a list of post id's which match the current filters set (in the layered nav and price filter)
  162. $post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
  163. // Ordering query vars
  164. $q->set( 'orderby', $ordering['orderby'] );
  165. $q->set( 'order', $ordering['order'] );
  166. if ( isset( $ordering['meta_key'] ) )
  167. $q->set( 'meta_key', $ordering['meta_key'] );
  168. // Query vars that affect posts shown
  169. if ( ! $q->is_tax( 'product_cat' ) && ! $q->is_tax( 'product_tag' ) )
  170. $q->set( 'post_type', 'product' );
  171. $q->set( 'meta_query', $meta_query );
  172. $q->set( 'post__in', $post__in );
  173. $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' ) ) );
  174. // Set a special variable
  175. $q->set( 'wc_query', true );
  176. // Store variables
  177. $this->post__in = $post__in;
  178. $this->meta_query = $meta_query;
  179. do_action( 'woocommerce_product_query', $q, $this );
  180. }
  181. /**
  182. * Remove the query
  183. *
  184. * @access public
  185. * @return void
  186. */
  187. function remove_product_query() {
  188. remove_filter( 'pre_get_posts', array( &$this, 'pre_get_posts') );
  189. }
  190. /**
  191. * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
  192. *
  193. * @access public
  194. * @return void
  195. */
  196. function get_products_in_view() {
  197. global $wp_the_query;
  198. $unfiltered_product_ids = array();
  199. // Get main query
  200. $current_wp_query = $wp_the_query->query;
  201. // Get WP Query for current page (without 'paged')
  202. unset( $current_wp_query['paged'] );
  203. // Generate a transient name based on current query
  204. $transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
  205. $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
  206. if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
  207. // Get all visible posts, regardless of filters
  208. $unfiltered_product_ids = get_posts(
  209. array_merge(
  210. $current_wp_query,
  211. array(
  212. 'post_type' => 'product',
  213. 'numberposts' => -1,
  214. 'post_status' => 'publish',
  215. 'meta_query' => $this->meta_query,
  216. 'fields' => 'ids',
  217. 'no_found_rows' => true,
  218. 'update_post_meta_cache' => false,
  219. 'update_post_term_cache' => false
  220. )
  221. )
  222. );
  223. set_transient( $transient_name, $unfiltered_product_ids );
  224. }
  225. // Store the variable
  226. $this->unfiltered_product_ids = $unfiltered_product_ids;
  227. // Also store filtered posts ids...
  228. if ( sizeof( $this->post__in ) > 0 )
  229. $this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
  230. else
  231. $this->filtered_product_ids = $this->unfiltered_product_ids;
  232. // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
  233. if ( sizeof( $this->layered_nav_post__in ) > 0 )
  234. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  235. else
  236. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  237. }
  238. /**
  239. * Returns an array of arguments for ordering products based on the selected values
  240. *
  241. * @access public
  242. * @return array
  243. */
  244. function get_catalog_ordering_args() {
  245. global $woocommerce;
  246. $current_order = ( isset( $woocommerce->session->orderby ) ) ? $woocommerce->session->orderby : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  247. switch ( $current_order ) {
  248. case 'date' :
  249. $orderby = 'date';
  250. $order = 'desc';
  251. $meta_key = '';
  252. break;
  253. case 'price' :
  254. $orderby = 'meta_value_num';
  255. $order = 'asc';
  256. $meta_key = '_price';
  257. break;
  258. case 'high_price' :
  259. $orderby = 'meta_value_num';
  260. $order = 'desc';
  261. $meta_key = '_price';
  262. break;
  263. case 'title' :
  264. $orderby = 'title';
  265. $order = 'asc';
  266. $meta_key = '';
  267. break;
  268. default :
  269. $orderby = 'menu_order title';
  270. $order = 'asc';
  271. $meta_key = '';
  272. break;
  273. }
  274. $args = array();
  275. $args['orderby'] = $orderby;
  276. $args['order'] = $order;
  277. if ($meta_key)
  278. $args['meta_key'] = $meta_key;
  279. return apply_filters('woocommerce_get_catalog_ordering_args', $args );
  280. }
  281. /**
  282. * Returns a meta query to handle product visibility
  283. *
  284. * @access public
  285. * @param string $compare (default: 'IN')
  286. * @return array
  287. */
  288. function visibility_meta_query( $compare = 'IN' ) {
  289. if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
  290. $meta_query = array(
  291. 'key' => '_visibility',
  292. 'value' => $in,
  293. 'compare' => $compare
  294. );
  295. return $meta_query;
  296. }
  297. /**
  298. * Returns a meta query to handle product stock status
  299. *
  300. * @access public
  301. * @param string $status (default: 'instock')
  302. * @return array
  303. */
  304. function stock_status_meta_query( $status = 'instock' ) {
  305. $meta_query = array();
  306. if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
  307. $meta_query = array(
  308. 'key' => '_stock_status',
  309. 'value' => $status,
  310. 'compare' => '='
  311. );
  312. }
  313. return $meta_query;
  314. }
  315. }