woocommerce/classes/class-wc-query.php

391 lines
11 KiB
PHP
Raw Normal View History

2011-09-16 08:10:12 +00:00
<?php
/**
* Contains the query functions for WooCommerce which alter the front-end post queries and loops.
*
2012-01-27 16:38:39 +00:00
* @class WC_Query
2012-08-14 19:42:38 +00:00
* @version 1.6.4
2012-08-14 22:43:48 +00:00
* @package WooCommerce/Classes
2012-08-14 19:42:38 +00:00
* @author WooThemes
2011-09-16 08:10:12 +00:00
*/
2012-01-27 16:38:39 +00:00
class WC_Query {
2012-08-02 21:54:26 +00:00
/** @public array Unfiltered product ids (before layered nav etc) */
public $unfiltered_product_ids = array();
2012-08-14 20:19:41 +00:00
/** @public array Filtered product ids (after layered nav) */
public $filtered_product_ids = array();
2012-08-14 20:19:41 +00:00
/** @public array Product IDs that match the layered nav + price filter */
public $post__in = array();
2012-08-14 20:19:41 +00:00
/** @public array The meta query for the page */
public $meta_query = '';
2012-08-14 20:19:41 +00:00
/** @public array Post IDs matching layered nav only */
public $layered_nav_post__in = array();
2012-08-14 20:19:41 +00:00
/** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
public $layered_nav_product_ids = array();
2012-08-02 21:54:26 +00:00
2012-08-14 19:42:38 +00:00
/**
2012-08-15 17:08:42 +00:00
* Constructor for the query class. Hooks in methods.
2012-08-14 19:42:38 +00:00
*
* @access public
* @return void
*/
public function __construct() {
add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
add_filter( 'wp', array( $this, 'remove_product_query' ) );
2011-09-16 08:10:12 +00:00
}
2012-08-14 20:19:41 +00:00
2011-09-16 08:10:12 +00:00
/**
* Hook into pre_get_posts to do the main product query
2012-08-14 20:19:41 +00:00
*
* @access public
* @param mixed $q query object
* @return void
2011-09-16 08:10:12 +00:00
*/
public function pre_get_posts( $q ) {
2012-07-19 21:52:28 +00:00
global $woocommerce;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// We only want to affect the main query
if ( ! $q->is_main_query() )
return;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Special check for shops with the product archive on front
if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == woocommerce_get_page_id('shop') ) {
2012-08-02 21:54:26 +00:00
// This is a front-page shop
2012-06-13 23:52:21 +00:00
$q->set( 'post_type', 'product' );
$q->set( 'page_id', '' );
if ( isset( $q->query['paged'] ) )
$q->set( 'paged', $q->query['paged'] );
2012-08-02 21:54:26 +00:00
// Define a variable so we know this is the front page shop later on
define( 'SHOP_IS_ON_FRONT', true );
2012-08-02 21:54:26 +00:00
// Get the actual WP page to avoid errors and let us use is_front_page()
// This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
global $wp_post_types;
2012-11-27 16:22:47 +00:00
$shop_page = get_post( woocommerce_get_page_id('shop') );
2012-08-22 19:44:27 +00:00
$q->is_page = true;
2012-08-02 21:54:26 +00:00
$wp_post_types['product']->ID = $shop_page->ID;
$wp_post_types['product']->post_title = $shop_page->post_title;
$wp_post_types['product']->post_name = $shop_page->post_name;
2012-08-02 21:54:26 +00:00
// Fix conditional Functions like is_front_page
2012-06-13 23:52:21 +00:00
$q->is_singular = false;
$q->is_post_type_archive = true;
$q->is_archive = true;
2012-11-27 16:22:47 +00:00
2012-08-22 19:44:27 +00:00
// Fix WP SEO
if ( function_exists( 'wpseo_get_value' ) ) {
add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
2012-08-22 19:44:27 +00:00
}
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
} else {
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
if ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) )
2012-06-13 23:52:21 +00:00
return;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
}
2012-08-02 21:54:26 +00:00
$this->product_query( $q );
2012-08-02 21:54:26 +00:00
// We're on a shop page so queue the woocommerce_get_products_in_view function
add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
2012-08-02 21:54:26 +00:00
// And remove the pre_get_posts hook
2012-06-13 23:52:21 +00:00
$this->remove_product_query();
}
2012-11-27 16:22:47 +00:00
2012-08-22 19:44:27 +00:00
/**
* wpseo_metadesc function.
2012-11-27 16:22:47 +00:00
*
2012-08-22 19:44:27 +00:00
* @access public
* @param mixed $meta
* @return void
*/
public function wpseo_metadesc() {
2012-08-22 19:44:27 +00:00
return wpseo_get_value( 'metadesc', woocommerce_get_page_id('shop') );
}
2012-11-27 16:22:47 +00:00
2012-08-22 19:44:27 +00:00
/**
* wpseo_metakey function.
2012-11-27 16:22:47 +00:00
*
2012-08-22 19:44:27 +00:00
* @access public
* @return void
*/
public function wpseo_metakey() {
2012-08-22 19:44:27 +00:00
return wpseo_get_value( 'metakey', woocommerce_get_page_id('shop') );
}
2012-03-26 14:34:23 +00:00
2012-08-14 20:19:41 +00:00
2012-03-26 14:34:23 +00:00
/**
* Hook into the_posts to do the main product query if needed - relevanssi compatibility
2012-08-14 20:19:41 +00:00
*
* @access public
* @param mixed $posts
* @param bool $query (default: false)
* @return void
2012-03-26 14:34:23 +00:00
*/
public function the_posts( $posts, $query = false ) {
2012-03-26 14:34:23 +00:00
global $woocommerce;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Abort if theres no query
if ( ! $query )
return $posts;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Abort if we're not filtering posts
if ( empty( $this->post__in ) )
return $posts;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Abort if this query has already been done
if ( ! empty( $query->wc_query ) )
return $posts;
2012-08-02 21:54:26 +00:00
// Abort if this isn't a search query
2012-06-13 23:52:21 +00:00
if ( empty( $query->query_vars["s"] ) )
return $posts;
2012-08-02 21:54:26 +00:00
2012-06-13 23:52:21 +00:00
// Abort if we're not on a post type archive/product taxonomy
if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) )
2012-06-13 23:52:21 +00:00
return $posts;
2012-08-02 21:54:26 +00:00
2012-03-26 14:34:23 +00:00
$filtered_posts = array();
$queried_post_ids = array();
2012-08-02 21:54:26 +00:00
2012-03-26 14:34:23 +00:00
foreach ( $posts as $post ) {
if ( in_array( $post->ID, $this->post__in ) ) {
$filtered_posts[] = $post;
$queried_post_ids[] = $post->ID;
2012-08-02 21:54:26 +00:00
}
2012-03-26 14:34:23 +00:00
}
$query->posts = $filtered_posts;
$query->post_count = count( $filtered_posts );
2012-08-02 21:54:26 +00:00
2012-03-26 14:34:23 +00:00
// Ensure filters are set
$this->unfiltered_product_ids = $queried_post_ids;
$this->filtered_product_ids = $queried_post_ids;
if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
$this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
} else {
$this->layered_nav_product_ids = $this->unfiltered_product_ids;
}
return $filtered_posts;
}
2012-08-02 21:54:26 +00:00
2012-08-14 20:19:41 +00:00
/**
* Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
2012-08-14 20:19:41 +00:00
*
* @access public
* @param mixed $q
* @return void
*/
public function product_query( $q ) {
2012-08-02 21:54:26 +00:00
// Meta query
2011-09-16 08:10:12 +00:00
$meta_query = (array) $q->get( 'meta_query' );
$meta_query[] = $this->visibility_meta_query();
$meta_query[] = $this->stock_status_meta_query();
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Ordering
$ordering = $this->get_catalog_ordering_args();
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Get a list of post id's which match the current filters set (in the layered nav and price filter)
2012-06-13 23:52:21 +00:00
$post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Ordering query vars
$q->set( 'orderby', $ordering['orderby'] );
$q->set( 'order', $ordering['order'] );
2012-08-02 21:54:26 +00:00
if ( isset( $ordering['meta_key'] ) )
2012-05-17 12:56:18 +00:00
$q->set( 'meta_key', $ordering['meta_key'] );
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Query vars that affect posts shown
2012-08-02 21:54:26 +00:00
if ( ! $q->is_tax( 'product_cat' ) && ! $q->is_tax( 'product_tag' ) )
2012-05-17 12:56:18 +00:00
$q->set( 'post_type', 'product' );
2011-09-16 08:10:12 +00:00
$q->set( 'meta_query', $meta_query );
$q->set( 'post__in', $post__in );
2012-06-13 23:52:21 +00:00
$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' ) ) );
2012-08-02 21:54:26 +00:00
2012-03-26 14:34:23 +00:00
// Set a special variable
$q->set( 'wc_query', true );
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Store variables
$this->post__in = $post__in;
$this->meta_query = $meta_query;
2012-08-02 21:54:26 +00:00
do_action( 'woocommerce_product_query', $q, $this );
2011-09-16 08:10:12 +00:00
}
2012-08-14 20:19:41 +00:00
2011-09-16 08:10:12 +00:00
/**
* Remove the query
2012-08-14 20:19:41 +00:00
*
* @access public
* @return void
2011-09-16 08:10:12 +00:00
*/
public function remove_product_query() {
remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
2011-09-16 08:10:12 +00:00
}
2012-08-02 21:54:26 +00:00
2012-08-14 20:19:41 +00:00
2011-09-16 08:10:12 +00:00
/**
2011-11-13 02:15:00 +00:00
* Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
2012-08-14 20:19:41 +00:00
*
* @access public
* @return void
2011-09-16 08:10:12 +00:00
*/
public function get_products_in_view() {
2012-06-13 23:52:21 +00:00
global $wp_the_query;
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
$unfiltered_product_ids = array();
2012-06-13 23:52:21 +00:00
// Get main query
$current_wp_query = $wp_the_query->query;
2012-08-02 21:54:26 +00:00
2011-11-13 02:15:00 +00:00
// Get WP Query for current page (without 'paged')
2012-05-17 12:56:18 +00:00
unset( $current_wp_query['paged'] );
2012-08-02 21:54:26 +00:00
2011-11-13 02:15:00 +00:00
// Generate a transient name based on current query
2012-06-13 23:52:21 +00:00
$transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
2012-05-17 12:56:18 +00:00
$transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
2012-08-02 21:54:26 +00:00
if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
2012-08-02 21:54:26 +00:00
2011-11-13 02:15:00 +00:00
// Get all visible posts, regardless of filters
$unfiltered_product_ids = get_posts(
2012-08-02 21:54:26 +00:00
array_merge(
2011-11-13 02:15:00 +00:00
$current_wp_query,
array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'meta_query' => $this->meta_query,
'fields' => 'ids',
2012-06-13 23:52:21 +00:00
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
2011-11-13 02:15:00 +00:00
)
2011-09-16 08:10:12 +00:00
)
2011-11-13 02:15:00 +00:00
);
2012-08-02 21:54:26 +00:00
set_transient( $transient_name, $unfiltered_product_ids );
}
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Store the variable
$this->unfiltered_product_ids = $unfiltered_product_ids;
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// Also store filtered posts ids...
2012-06-13 23:52:21 +00:00
if ( sizeof( $this->post__in ) > 0 )
2012-03-26 14:34:23 +00:00
$this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
2012-06-13 23:52:21 +00:00
else
2011-09-16 08:10:12 +00:00
$this->filtered_product_ids = $this->unfiltered_product_ids;
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
// And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
2012-06-13 23:52:21 +00:00
if ( sizeof( $this->layered_nav_post__in ) > 0 )
2012-03-26 14:34:23 +00:00
$this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
2012-06-13 23:52:21 +00:00
else
2011-09-16 08:10:12 +00:00
$this->layered_nav_product_ids = $this->unfiltered_product_ids;
}
2012-08-02 21:54:26 +00:00
2012-08-14 20:19:41 +00:00
/**
* Returns an array of arguments for ordering products based on the selected values
2012-08-14 20:19:41 +00:00
*
* @access public
* @return array
*/
public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
2012-09-07 17:26:13 +00:00
global $woocommerce;
2012-11-27 16:22:47 +00:00
// Get ordering from session unless defined
if ( ! $orderby )
$orderby = isset( $woocommerce->session->orderby ) ? $woocommerce->session->orderby : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
2012-08-02 21:54:26 +00:00
$args = array();
switch ( $orderby ) {
case 'date' :
$args['orderby'] = 'date';
$args['order'] = $order ? $order : 'desc';
$args['meta_key'] = '';
break;
case 'price' :
$args['orderby'] = 'meta_value_num';
$args['order'] = $order ? $order : 'asc';
$args['meta_key'] = '_price';
break;
case 'high_price' :
$args['orderby'] = 'meta_value_num';
$args['order'] = $order ? $order : 'desc';
$args['meta_key'] = '_price';
break;
case 'title' :
$args['orderby'] = 'title';
$args['order'] = $order ? $order : 'asc';
$args['meta_key'] = '';
break;
default :
$args['orderby'] = 'menu_order title';
$args['order'] = $order ? $order : 'asc';
$args['meta_key'] = '';
break;
2012-06-13 23:52:21 +00:00
}
2012-08-02 21:54:26 +00:00
return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
}
2012-08-02 21:54:26 +00:00
2012-08-14 20:19:41 +00:00
/**
* Returns a meta query to handle product visibility
2012-08-14 20:19:41 +00:00
*
* @access public
* @param string $compare (default: 'IN')
* @return array
*/
public function visibility_meta_query( $compare = 'IN' ) {
if ( is_search() ) $in = array( 'visible', 'search' ); else $in = array( 'visible', 'catalog' );
2012-08-02 21:54:26 +00:00
$meta_query = array(
2011-12-24 17:05:51 +00:00
'key' => '_visibility',
'value' => $in,
'compare' => $compare
);
2012-08-02 21:54:26 +00:00
return $meta_query;
}
2012-08-02 21:54:26 +00:00
2012-08-14 20:19:41 +00:00
/**
* Returns a meta query to handle product stock status
2012-08-14 20:19:41 +00:00
*
* @access public
* @param string $status (default: 'instock')
* @return array
*/
public function stock_status_meta_query( $status = 'instock' ) {
$meta_query = array();
2012-06-13 23:52:21 +00:00
if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
$meta_query = array(
2011-12-24 17:05:51 +00:00
'key' => '_stock_status',
'value' => $status,
'compare' => '='
);
2012-06-13 23:52:21 +00:00
}
return $meta_query;
}
2012-08-02 21:54:26 +00:00
2011-09-16 08:10:12 +00:00
}