| 1 |
<?php |
| 2 |
/** |
| 3 |
* Build query for archive listings page. |
| 4 |
* |
| 5 |
* @package Auto Listings. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace AutoListings; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Query |
| 12 |
*/ |
| 13 |
class Query { |
| 14 |
|
| 15 |
/** |
| 16 |
* Add hooks when module is loaded. |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
if ( is_admin() ) { |
| 20 |
return; |
| 21 |
} |
| 22 |
add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Hook into pre_get_posts to do the main listing query. |
| 27 |
* |
| 28 |
* @param mixed $query Query object. |
| 29 |
*/ |
| 30 |
public function pre_get_posts( $query ) { |
| 31 |
$condition = apply_filters( 'auto_listings_ordering_condtion', ( $query->is_post_type_archive( 'auto-listing' ) || $query->is_tax( 'body-type' ) ) && $query->is_main_query() ); |
| 32 |
if ( ! $condition ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$this->listings_query( $query ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Query the listings, applying sorting/ordering etc. This applies to the main WordPress loop. |
| 41 |
* |
| 42 |
* @param mixed $query Query object. |
| 43 |
*/ |
| 44 |
public function listings_query( $query ) { |
| 45 |
$query->set( 'post_status', 'publish' ); |
| 46 |
|
| 47 |
// Ordering query vars. |
| 48 |
$ordering = $this->get_ordering_args(); |
| 49 |
$query->set( 'orderby', $ordering['orderby'] ); |
| 50 |
$query->set( 'order', $ordering['order'] ); |
| 51 |
if ( isset( $ordering['meta_key'] ) ) { |
| 52 |
$query->set( 'meta_key', $ordering['meta_key'] ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Query the listings, applying sorting/ordering etc. This applies to the main WordPress loop. |
| 58 |
* |
| 59 |
* @param string $orderby Sort listings by parameter. |
| 60 |
* @param string $order Ascending or descending order. |
| 61 |
*/ |
| 62 |
public function get_ordering_args( $orderby = '', $order = '' ) { |
| 63 |
// Get ordering from query string unless defined. |
| 64 |
if ( ! $orderby ) { |
| 65 |
$orderby_value = filter_input( INPUT_GET, 'orderby' ); |
| 66 |
$orderby_value = isset( $orderby_value ) ? esc_html( $orderby_value ) : 'date'; |
| 67 |
|
| 68 |
// Get order + orderby args from string. |
| 69 |
$orderby_value = explode( '-', $orderby_value ); |
| 70 |
$orderby = esc_attr( $orderby_value[0] ); |
| 71 |
$order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order; |
| 72 |
} |
| 73 |
|
| 74 |
$orderby = strtolower( $orderby ); |
| 75 |
$order = strtoupper( $order ); |
| 76 |
$args = []; |
| 77 |
|
| 78 |
// Default - menu_order. |
| 79 |
$args['orderby'] = 'date ID'; |
| 80 |
$args['order'] = 'OLD' === $order ? 'ASC' : 'DESC'; |
| 81 |
$args['meta_key'] = ''; |
| 82 |
|
| 83 |
switch ( $orderby ) { |
| 84 |
case 'date': |
| 85 |
$args['orderby'] = 'date ID'; |
| 86 |
$args['order'] = 'OLD' === $order ? 'ASC' : 'DESC'; |
| 87 |
break; |
| 88 |
case 'price': |
| 89 |
$args['orderby'] = 'meta_value_num ID'; |
| 90 |
$args['order'] = 'HIGH' === $order ? 'DESC' : 'ASC'; |
| 91 |
$args['meta_key'] = '_al_listing_price'; |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
return apply_filters( 'auto_listings_get_ordering_args', $args ); |
| 96 |
} |
| 97 |
} |
| 98 |
|