1: <?php
2: /**
3: * SHORTCODE :: Listing Category [listing_category]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingCategory
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.1.1
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 Category [listing_category]
21: *
22: * This shortcode allows for you to specify the property type(s) using
23: * [listing_category post_type="property" status="current,sold,leased" category_key="property_rural_category" category_key="farm"] option. You can also
24: * limit the number of entries that display. using [listing_category limit="5"]
25: * Added Commercial Category Support
26: *
27: * @param array $atts Shortcode attributes.
28: *
29: * @return false|string|void
30: * @since 1.1.1
31: * @since 3.5 support for third and fourth agents.
32: * @since 3.4.48 Fixed class name.
33: * @since 3.5.5 Sorting not working.
34: */
35: function epl_shortcode_listing_category_callback( $atts ) {
36: $property_types = epl_get_active_post_types();
37: if ( ! empty( $property_types ) ) {
38: $property_types = array_keys( $property_types );
39: }
40:
41: $attributes = shortcode_atts(
42: array(
43: 'post_type' => $property_types,
44: 'status' => array( 'current', 'sold', 'leased' ),
45: 'commercial_listing_type' => '', // Listing Type, 'sale' , 'lease', 'both'.
46: 'category_key' => '', // Meta field key.
47: 'category_value' => '', // Meta field value.
48: 'category_compare' => 'IN', // Compare using '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'EXISTS', 'NOT EXISTS', 'IN','NOT IN','BETWEEN','NOT BETWEEN'.
49: 'limit' => '10', // Number of maximum posts to show.
50: 'offset' => '', // Offset posts. When used, pagination is disabled.
51: 'author' => '', // Author of listings.
52: 'agent' => '', // Agent of listings.
53: 'template' => false, // Template can be set to "slim" for home open style template.
54: 'location' => '', // Location slug. Should be a name like sorrento.
55: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
56: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
57: 'sortby' => '', // Options: price, date, status, rand (for random) : Default date.
58: 'sort_order' => 'DESC', // Sort by ASC or DESC.
59: 'pagination' => 'on', // Enable or disable pagination.
60: 'instance_id' => '1', // Set instance ID when using multiple shortcodes on the same page.
61: 'class' => '', // Custom class.
62: ),
63: $atts
64: );
65:
66: $post_type = $attributes['post_type'];
67: $status = $attributes['status'];
68: $commercial_listing_type = $attributes['commercial_listing_type'];
69: $category_key = $attributes['category_key'];
70: $category_value = $attributes['category_value'];
71: $category_compare = $attributes['category_compare'];
72: $limit = $attributes['limit'];
73: $offset = $attributes['offset'];
74: $author = $attributes['author'];
75: $agent = $attributes['agent'];
76: $template = $attributes['template'];
77: $location = $attributes['location'];
78: $tools_top = $attributes['tools_top'];
79: $tools_bottom = $attributes['tools_bottom'];
80: $sortby = $attributes['sortby'];
81: $sort_order = $attributes['sort_order'];
82: $pagination = $attributes['pagination'];
83: $instance_id = $attributes['instance_id'];
84: $class = $attributes['class'];
85:
86: if ( empty( $post_type ) ) {
87: return;
88: }
89:
90: if ( is_string( $post_type ) && 'rental' === $post_type ) {
91: $meta_key_price = 'property_rent';
92: } else {
93: $meta_key_price = 'property_price';
94: }
95:
96: $sort_options = array(
97: 'price' => $meta_key_price,
98: 'date' => 'post_date',
99: );
100: if ( ! is_array( $post_type ) ) {
101: $post_type = array_map( 'trim', explode( ',', $post_type ) );
102: }
103: ob_start();
104: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
105: $args = array(
106: 'post_type' => $post_type,
107: 'posts_per_page' => $limit,
108: 'paged' => $paged,
109: );
110:
111: // Offset query does not work with pagination.
112: if ( ! empty( $offset ) ) {
113: $args['offset'] = $offset;
114: $pagination = 'off'; // Disable pagination when offset is used.
115: }
116:
117: if ( ! empty( $location ) ) {
118: if ( ! is_array( $location ) ) {
119: $location = explode( ',', $location );
120: $location = array_map( 'trim', $location );
121:
122: $args['tax_query'][] = array(
123: 'taxonomy' => 'location',
124: 'field' => 'slug',
125: 'terms' => $location,
126: );
127: }
128: }
129:
130: if ( ! empty( $status ) ) {
131: if ( ! is_array( $status ) ) {
132: $status = explode( ',', $status );
133: $status = array_map( 'trim', $status );
134:
135: $args['meta_query'][] = array(
136: 'key' => 'property_status',
137: 'value' => $status,
138: 'compare' => 'IN',
139: );
140: }
141: }
142:
143: // Listings of specified author.
144: if ( ! empty( $author ) ) {
145: $author = (array) $author;
146: if ( is_array( $author ) ) {
147: $author_ids = array_map( 'epl_get_author_id_from_name', $author );
148: $author = implode( ',', $author_ids );
149: }
150: $args['author'] = trim( $author );
151: }
152:
153: // Listings by specified agent.
154: if ( ! empty( $agent ) ) {
155:
156: $agent = array_map( 'trim', explode( ',', $agent ) );
157: $agent = array_filter( $agent );
158: $agent_meta_query = array(
159: 'relation' => 'OR',
160: );
161:
162: foreach ( $agent as $single_agent ) {
163: $agent_meta_query[] = array(
164: 'key' => 'property_agent',
165: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
166: 'compare' => 'IN',
167: );
168: $agent_meta_query[] = array(
169: 'key' => 'property_second_agent',
170: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
171: 'compare' => 'IN',
172: );
173:
174: if ( in_array( 'commercial', $post_type, true ) ||
175: in_array( 'commercial_land', $post_type, true ) ||
176: in_array( 'business', $post_type, true )
177: ) {
178:
179: $agent_meta_query[] = array(
180: 'key' => 'property_third_agent',
181: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
182: 'compare' => 'IN',
183: );
184: $agent_meta_query[] = array(
185: 'key' => 'property_fourth_agent',
186: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
187: 'compare' => 'IN',
188: );
189:
190: }
191: }
192:
193: $args['meta_query'][] = $agent_meta_query;
194: }
195:
196: if ( ! empty( $commercial_listing_type ) ) {
197: if ( ! is_array( $commercial_listing_type ) ) {
198: $commercial_listing_type = explode( ',', $commercial_listing_type );
199: $commercial_listing_type = array_map( 'trim', $commercial_listing_type );
200:
201: $args['meta_query'][] = array(
202: 'key' => 'property_com_listing_type',
203: 'value' => $commercial_listing_type,
204: 'compare' => 'IN',
205: );
206: }
207: }
208:
209: if ( ! empty( $category_key ) && ! empty( $category_value ) ) {
210:
211: if ( ! is_array( $category_value ) && in_array( $category_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true ) ) {
212: $category_value = explode( ',', $category_value );
213: $category_value = array_map( 'trim', $category_value );
214: }
215:
216: $this_meta_query = array(
217: 'key' => $category_key,
218: 'value' => $category_value,
219: 'compare' => $category_compare,
220: );
221:
222: if ( in_array( $category_compare, array( 'BETWEEN', 'NOT BETWEEN' ), true ) ) {
223:
224: if ( is_numeric( $category_value[0] ) ) {
225: $this_meta_query['type'] = 'numeric';
226: }
227: }
228: $args['meta_query'][] = $this_meta_query;
229: }
230:
231: if ( ! empty( $sortby ) ) {
232:
233: if ( 'price' === $sortby ) {
234: $args['orderby'] = 'meta_value_num';
235: $args['meta_key'] = $meta_key_price;
236: } elseif ( 'rand' === $sortby ) {
237: $args['orderby'] = 'rand';
238: } elseif ( 'status' === $sortby ) {
239: $args['orderby'] = 'meta_value';
240: $args['meta_key'] = 'property_status';
241: } else {
242: $args['orderby'] = 'post_date';
243: }
244: $args['order'] = $sort_order;
245: }
246:
247: $args['instance_id'] = $attributes['instance_id'];
248: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
249: $args = epl_add_orderby_args( $args, 'shortcode', 'listing_category' );
250:
251: // Option to filter args.
252: $args = apply_filters( 'epl_shortcode_listing_category_args', $args, $attributes );
253:
254: $query_open = new WP_Query( $args );
255: if ( $query_open->have_posts() ) { ?>
256: <div class="loop epl-shortcode">
257: <div class="loop-content epl-shortcode-listing-category <?php echo ' ' . esc_attr( epl_template_class( $template, 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>">
258: <?php
259: if ( 'on' === $tools_top ) {
260: do_action( 'epl_property_loop_start', $attributes );
261: }
262: while ( $query_open->have_posts() ) {
263: $query_open->the_post();
264:
265: $template = str_replace( '_', '-', $template );
266: epl_property_blog( $template );
267: }
268: if ( 'on' === $tools_bottom ) {
269: do_action( 'epl_property_loop_end' );
270: }
271: ?>
272: </div>
273: <div class="loop-footer">
274: <?php
275: if ( 'on' === $pagination ) {
276: do_action( 'epl_pagination', array( 'query' => $query_open ) );
277: }
278: ?>
279: </div>
280: </div>
281: <?php
282: } else {
283: do_action( 'epl_shortcode_results_message', 'category' );
284: }
285: wp_reset_postdata();
286: return ob_get_clean();
287: }
288: add_shortcode( 'listing_category', 'epl_shortcode_listing_category_callback' );
289: