1: <?php
2: /**
3: * SHORTCODE :: Listing Auction [listing_auction]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingAuction
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: // phpcs:disable WordPress.DB.SlowDBQuery
18:
19: /**
20: * Listing Auction Shortcode [listing_auction]
21: *
22: * This shortcode allows for you to specify the property type(s) using
23: * [listing_auction post_type="property,rental" status="current,sold,leased" template="default"] option. You can also
24: * limit the number of entries that display. using [listing_auction limit="5"]
25: *
26: * @param array $atts Shortcode attributes.
27: *
28: * @return false|string
29: * @since 1.0
30: * @since 3.4.44 Fix: Missing commercial auction listings in shortcode.
31: * @since 3.4.48 Fixed class name. Passing shortcode results message shortcode type.
32: * @since 3.5.5 Sorting not working.
33: */
34: function epl_shortcode_listing_auction_callback( $atts ) {
35: $property_types = epl_get_active_post_types();
36: if ( ! empty( $property_types ) ) {
37: $property_types = array_keys( $property_types );
38: }
39:
40: $attributes = shortcode_atts(
41: array(
42: 'post_type' => $property_types, // Post Type.
43: 'status' => array( 'current', 'sold', 'leased' ),
44: 'limit' => '10', // Number of maximum posts to show.
45: 'template' => false, // Template can be set to "slim" for home open style template.
46: 'location' => '', // Location slug. Should be a name like sorrento.
47: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
48: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
49: 'sortby' => '', // Options: price, date, status : Default date.
50: 'sort_order' => 'DESC', // Sort order ASC or DESC.
51: 'query_object' => '', // only for internal use . if provided use it instead of custom query.
52: 'pagination' => 'on', // Pagination on or off.
53: 'instance_id' => '1', // Set instance ID when using multiple shortcodes on the same page.
54: 'class' => '', // Custom class.
55: ),
56: $atts
57: );
58:
59: if ( is_string( $attributes['post_type'] ) && 'rental' === $attributes['post_type'] ) {
60: $meta_key_price = 'property_rent';
61: } else {
62: $meta_key_price = 'property_price';
63: }
64:
65: $sort_options = array(
66: 'price' => $meta_key_price,
67: 'date' => 'post_date',
68: );
69:
70: $post_type = $attributes['post_type'];
71:
72: if ( ! is_array( $attributes['post_type'] ) ) {
73: $post_type = array_map( 'trim', explode( ',', $attributes['post_type'] ) );
74: }
75: ob_start();
76: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
77: $args = array(
78: 'post_type' => $post_type,
79: 'posts_per_page' => $attributes['limit'],
80: 'paged' => $paged,
81: );
82:
83: // Only properties which are not under offer should be allowed.
84: $args['meta_query'][] = array(
85: 'key' => 'property_under_offer',
86: 'value' => 'yes',
87: 'compare' => '!=',
88: );
89:
90: // Only properties which are not auction should be allowed.
91: $args['meta_query'][] = array(
92: 'relation' => 'OR',
93: array(
94: 'key' => 'property_authority',
95: 'value' => 'auction',
96: ),
97: array(
98: 'key' => 'property_com_authority',
99: 'value' => 'auction',
100: ),
101: );
102:
103: if ( ! empty( $attributes['location'] ) ) {
104: if ( ! is_array( $attributes['location'] ) ) {
105: $location = explode( ',', $attributes['location'] );
106: $location = array_map( 'trim', $location );
107:
108: $args['tax_query'][] = array(
109: 'taxonomy' => 'location',
110: 'field' => 'slug',
111: 'terms' => $location,
112: );
113: }
114: }
115:
116: if ( ! empty( $attributes['status'] ) ) {
117: if ( ! is_array( $attributes['status'] ) ) {
118: $status = explode( ',', $attributes['status'] );
119: $status = array_map( 'trim', $status );
120:
121: $args['meta_query'][] = array(
122: 'key' => 'property_status',
123: 'value' => $status,
124: 'compare' => 'IN',
125: );
126:
127: add_filter( 'epl_sorting_options', 'epl_sorting_options_callback' );
128: }
129: }
130:
131: if ( ! empty( $attributes['sortby'] ) ) {
132:
133: if ( 'price' === $attributes['sortby'] ) {
134: $args['orderby'] = 'meta_value_num';
135: $args['meta_key'] = $meta_key_price;
136: } elseif ( 'status' === $attributes['sortby'] ) {
137: $args['orderby'] = 'meta_value';
138: $args['meta_key'] = 'property_status';
139: } else {
140: $args['orderby'] = 'post_date';
141: }
142: $args['order'] = $attributes['sort_order'];
143: }
144:
145: $args['instance_id'] = $attributes['instance_id'];
146: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
147: $args = epl_add_orderby_args( $args, 'shortcode', 'listing_auction' );
148:
149: // Option to filter args.
150: $args = apply_filters( 'epl_shortcode_listing_auction_args', $args, $attributes );
151:
152: $query_open = new WP_Query( $args );
153:
154: if ( is_object( $attributes['query_object'] ) ) {
155: $query_open = $attributes['query_object'];
156: }
157:
158: if ( $query_open->have_posts() ) { ?>
159: <div class="loop epl-shortcode">
160: <div class="loop-content epl-shortcode-listing-auction <?php echo ' ' . esc_attr( epl_template_class( $attributes['template'], 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>">
161: <?php
162: if ( 'on' === $attributes['tools_top'] ) {
163: do_action( 'epl_property_loop_start', $attributes );
164: }
165: while ( $query_open->have_posts() ) {
166: $query_open->the_post();
167: $template = str_replace( '_', '-', $attributes['template'] );
168: epl_property_blog( $template );
169: }
170: if ( 'on' === $attributes['tools_bottom'] ) {
171: do_action( 'epl_property_loop_end' );
172: }
173: ?>
174: </div>
175: <div class="loop-footer">
176: <?php
177: if ( 'on' === $attributes['pagination'] ) {
178: do_action( 'epl_pagination', array( 'query' => $query_open ) );
179: }
180: ?>
181: </div>
182: </div>
183: <?php
184: } else {
185: do_action( 'epl_shortcode_results_message', 'auction' );
186: }
187: wp_reset_postdata();
188: return ob_get_clean();
189: }
190: add_shortcode( 'listing_auction', 'epl_shortcode_listing_auction_callback' );
191: