PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/theme WP a cương/test_wp/wp-content/plugins/woocommerce/classes/class-wc-query.php

https://gitlab.com/hop23typhu/list-theme
PHP | 496 lines | 251 code | 95 blank | 150 comment | 47 complexity | 62e559a7ae80f33364b4a37dec5d1bed 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. * @category Class
  9. * @author WooThemes
  10. */
  11. class WC_Query {
  12. /** @public array Unfiltered product ids (before layered nav etc) */
  13. public $unfiltered_product_ids = array();
  14. /** @public array Filtered product ids (after layered nav) */
  15. public $filtered_product_ids = array();
  16. /** @public array Product IDs that match the layered nav + price filter */
  17. public $post__in = array();
  18. /** @public array The meta query for the page */
  19. public $meta_query = '';
  20. /** @public array Post IDs matching layered nav only */
  21. public $layered_nav_post__in = array();
  22. /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
  23. public $layered_nav_product_ids = array();
  24. /**
  25. * Constructor for the query class. Hooks in methods.
  26. *
  27. * @access public
  28. * @return void
  29. */
  30. public function __construct() {
  31. add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  32. add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
  33. add_filter( 'wp', array( $this, 'remove_product_query' ) );
  34. }
  35. /**
  36. * Hook into pre_get_posts to do the main product query
  37. *
  38. * @access public
  39. * @param mixed $q query object
  40. * @return void
  41. */
  42. public function pre_get_posts( $q ) {
  43. global $woocommerce;
  44. // We only want to affect the main query
  45. if ( ! $q->is_main_query() )
  46. return;
  47. // When orderby is set, WordPress shows posts. Get around that here.
  48. if ( $q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == woocommerce_get_page_id('shop') ) {
  49. $_query = wp_parse_args( $q->query );
  50. if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
  51. $q->is_page = true;
  52. $q->is_home = false;
  53. $q->set( 'page_id', get_option('page_on_front') );
  54. $q->set( 'post_type', 'product' );
  55. }
  56. }
  57. // Special check for shops with the product archive on front
  58. if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == woocommerce_get_page_id('shop') ) {
  59. // This is a front-page shop
  60. $q->set( 'post_type', 'product' );
  61. $q->set( 'page_id', '' );
  62. if ( isset( $q->query['paged'] ) )
  63. $q->set( 'paged', $q->query['paged'] );
  64. // Define a variable so we know this is the front page shop later on
  65. define( 'SHOP_IS_ON_FRONT', true );
  66. // Get the actual WP page to avoid errors and let us use is_front_page()
  67. // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
  68. global $wp_post_types;
  69. $shop_page = get_post( woocommerce_get_page_id('shop') );
  70. $q->is_page = true;
  71. $wp_post_types['product']->ID = $shop_page->ID;
  72. $wp_post_types['product']->post_title = $shop_page->post_title;
  73. $wp_post_types['product']->post_name = $shop_page->post_name;
  74. // Fix conditional Functions like is_front_page
  75. $q->is_singular = false;
  76. $q->is_post_type_archive = true;
  77. $q->is_archive = true;
  78. // Fix WP SEO
  79. if ( function_exists( 'wpseo_get_value' ) ) {
  80. add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
  81. add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
  82. }
  83. } else {
  84. // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
  85. if ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) )
  86. return;
  87. }
  88. $this->product_query( $q );
  89. if ( is_search() ) {
  90. add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
  91. add_filter( 'wp', array( $this, 'remove_posts_where' ) );
  92. }
  93. // We're on a shop page so queue the woocommerce_get_products_in_view function
  94. add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
  95. // And remove the pre_get_posts hook
  96. $this->remove_product_query();
  97. }
  98. /**
  99. * search_post_excerpt function.
  100. *
  101. * @access public
  102. * @param string $where (default: '')
  103. * @return string (modified where clause)
  104. */
  105. public function search_post_excerpt( $where = '' ) {
  106. global $wp_the_query;
  107. // If this is not a WC Query, do not modify the query
  108. if ( empty( $wp_the_query->query_vars['wc_query'] ) )
  109. return $where;
  110. $where = preg_replace(
  111. "/post_title\s+LIKE\s*(\'[^\']+\')/",
  112. "post_title LIKE $1) OR (post_excerpt LIKE $1", $where );
  113. return $where;
  114. }
  115. /**
  116. * wpseo_metadesc function.
  117. *
  118. * @access public
  119. * @param mixed $meta
  120. * @return void
  121. */
  122. public function wpseo_metadesc() {
  123. return wpseo_get_value( 'metadesc', woocommerce_get_page_id('shop') );
  124. }
  125. /**
  126. * wpseo_metakey function.
  127. *
  128. * @access public
  129. * @return void
  130. */
  131. public function wpseo_metakey() {
  132. return wpseo_get_value( 'metakey', woocommerce_get_page_id('shop') );
  133. }
  134. /**
  135. * Hook into the_posts to do the main product query if needed - relevanssi compatibility
  136. *
  137. * @access public
  138. * @param mixed $posts
  139. * @param bool $query (default: false)
  140. * @return void
  141. */
  142. public function the_posts( $posts, $query = false ) {
  143. global $woocommerce;
  144. // Abort if there's no query
  145. if ( ! $query )
  146. return $posts;
  147. // Abort if we're not filtering posts
  148. if ( empty( $this->post__in ) )
  149. return $posts;
  150. // Abort if this query has already been done
  151. if ( ! empty( $query->wc_query ) )
  152. return $posts;
  153. // Abort if this isn't a search query
  154. if ( empty( $query->query_vars["s"] ) )
  155. return $posts;
  156. // Abort if we're not on a post type archive/product taxonomy
  157. if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) )
  158. return $posts;
  159. $filtered_posts = array();
  160. $queried_post_ids = array();
  161. foreach ( $posts as $post ) {
  162. if ( in_array( $post->ID, $this->post__in ) ) {
  163. $filtered_posts[] = $post;
  164. $queried_post_ids[] = $post->ID;
  165. }
  166. }
  167. $query->posts = $filtered_posts;
  168. $query->post_count = count( $filtered_posts );
  169. // Ensure filters are set
  170. $this->unfiltered_product_ids = $queried_post_ids;
  171. $this->filtered_product_ids = $queried_post_ids;
  172. if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
  173. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  174. } else {
  175. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  176. }
  177. return $filtered_posts;
  178. }
  179. /**
  180. * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
  181. *
  182. * @access public
  183. * @param mixed $q
  184. * @return void
  185. */
  186. public function product_query( $q ) {
  187. // Meta query
  188. $meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
  189. // Ordering
  190. $ordering = $this->get_catalog_ordering_args();
  191. // Get a list of post id's which match the current filters set (in the layered nav and price filter)
  192. $post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
  193. // Ordering query vars
  194. $q->set( 'orderby', $ordering['orderby'] );
  195. $q->set( 'order', $ordering['order'] );
  196. if ( isset( $ordering['meta_key'] ) )
  197. $q->set( 'meta_key', $ordering['meta_key'] );
  198. // Query vars that affect posts shown
  199. if ( ! $q->is_tax( 'product_cat' ) && ! $q->is_tax( 'product_tag' ) )
  200. $q->set( 'post_type', 'product' );
  201. $q->set( 'meta_query', $meta_query );
  202. $q->set( 'post__in', $post__in );
  203. $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' ) ) );
  204. // Set a special variable
  205. $q->set( 'wc_query', true );
  206. // Store variables
  207. $this->post__in = $post__in;
  208. $this->meta_query = $meta_query;
  209. do_action( 'woocommerce_product_query', $q, $this );
  210. }
  211. /**
  212. * Remove the query
  213. *
  214. * @access public
  215. * @return void
  216. */
  217. public function remove_product_query() {
  218. remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  219. }
  220. /**
  221. * Remove the posts_where filter
  222. *
  223. * @access public
  224. * @return void
  225. */
  226. public function remove_posts_where() {
  227. remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
  228. }
  229. /**
  230. * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
  231. *
  232. * @access public
  233. * @return void
  234. */
  235. public function get_products_in_view() {
  236. global $wp_the_query;
  237. $unfiltered_product_ids = array();
  238. // Get main query
  239. $current_wp_query = $wp_the_query->query;
  240. // Get WP Query for current page (without 'paged')
  241. unset( $current_wp_query['paged'] );
  242. // Generate a transient name based on current query
  243. $transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
  244. $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
  245. if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
  246. // Get all visible posts, regardless of filters
  247. $unfiltered_product_ids = get_posts(
  248. array_merge(
  249. $current_wp_query,
  250. array(
  251. 'post_type' => 'product',
  252. 'numberposts' => -1,
  253. 'post_status' => 'publish',
  254. 'meta_query' => $this->meta_query,
  255. 'fields' => 'ids',
  256. 'no_found_rows' => true,
  257. 'update_post_meta_cache' => false,
  258. 'update_post_term_cache' => false
  259. )
  260. )
  261. );
  262. set_transient( $transient_name, $unfiltered_product_ids );
  263. }
  264. // Store the variable
  265. $this->unfiltered_product_ids = $unfiltered_product_ids;
  266. // Also store filtered posts ids...
  267. if ( sizeof( $this->post__in ) > 0 )
  268. $this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
  269. else
  270. $this->filtered_product_ids = $this->unfiltered_product_ids;
  271. // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
  272. if ( sizeof( $this->layered_nav_post__in ) > 0 )
  273. $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
  274. else
  275. $this->layered_nav_product_ids = $this->unfiltered_product_ids;
  276. }
  277. /**
  278. * Returns an array of arguments for ordering products based on the selected values
  279. *
  280. * @access public
  281. * @return array
  282. */
  283. public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
  284. global $woocommerce;
  285. // Get ordering from query string unless defined
  286. if ( ! $orderby ) {
  287. $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
  288. // Get order + orderby args from string
  289. $orderby_value = explode( '-', $orderby_value );
  290. $orderby = esc_attr( $orderby_value[0] );
  291. $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
  292. }
  293. $orderby = strtolower( $orderby );
  294. $order = strtoupper( $order );
  295. $args = array();
  296. switch ( $orderby ) {
  297. case 'date' :
  298. $args['orderby'] = 'date';
  299. $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
  300. $args['meta_key'] = '';
  301. break;
  302. case 'price' :
  303. $args['orderby'] = 'meta_value_num';
  304. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  305. $args['meta_key'] = '_price';
  306. break;
  307. case 'popularity' :
  308. $args['orderby'] = 'meta_value_num';
  309. $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC';
  310. $args['meta_key'] = 'total_sales';
  311. break;
  312. case 'rating' :
  313. $args['orderby'] = 'menu_order title';
  314. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  315. $args['meta_key'] = '';
  316. add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
  317. break;
  318. case 'title' :
  319. $args['orderby'] = 'title';
  320. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  321. $args['meta_key'] = '';
  322. break;
  323. // default - menu_order
  324. default :
  325. $args['orderby'] = 'menu_order title';
  326. $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC';
  327. $args['meta_key'] = '';
  328. break;
  329. }
  330. return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
  331. }
  332. /**
  333. * order_by_rating_post_clauses function.
  334. *
  335. * @access public
  336. * @param array $args
  337. * @return array
  338. */
  339. public function order_by_rating_post_clauses( $args ) {
  340. global $wpdb;
  341. $args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
  342. $args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
  343. $args['join'] .= "
  344. LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
  345. LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
  346. ";
  347. $args['orderby'] = "average_rating DESC";
  348. $args['groupby'] = "$wpdb->posts.ID";
  349. return $args;
  350. }
  351. /**
  352. * Appends meta queries to an array.
  353. *
  354. * @access public
  355. * @return void
  356. */
  357. public function get_meta_query( $meta_query = array() ) {
  358. if ( ! is_array( $meta_query ) )
  359. $meta_query = array();
  360. $meta_query[] = $this->visibility_meta_query();
  361. $meta_query[] = $this->stock_status_meta_query();
  362. return array_filter( $meta_query );
  363. }
  364. /**
  365. * Returns a meta query to handle product visibility
  366. *
  367. * @access public
  368. * @param string $compare (default: 'IN')
  369. * @return array
  370. */
  371. public function visibility_meta_query( $compare = 'IN' ) {
  372. if ( is_search() )
  373. $in = array( 'visible', 'search' );
  374. else
  375. $in = array( 'visible', 'catalog' );
  376. $meta_query = array(
  377. 'key' => '_visibility',
  378. 'value' => $in,
  379. 'compare' => $compare
  380. );
  381. return $meta_query;
  382. }
  383. /**
  384. * Returns a meta query to handle product stock status
  385. *
  386. * @access public
  387. * @param string $status (default: 'instock')
  388. * @return array
  389. */
  390. public function stock_status_meta_query( $status = 'instock' ) {
  391. $meta_query = array();
  392. if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
  393. $meta_query = array(
  394. 'key' => '_stock_status',
  395. 'value' => $status,
  396. 'compare' => '='
  397. );
  398. }
  399. return $meta_query;
  400. }
  401. }