1: <?php
2: /**
3: * SHORTCODE :: Listing Location Taxonomy [listing_location]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingLocation
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.1.2
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 Shortcode [listing_location]
21: *
22: * This shortcode allows for you to specify feature property type(s) using
23: * [listing_location post_type="property" location="sorrento" location_id="6" status="current,sold,leased" template="default"] option. You can also
24: * limit the number of entries that display. using [listing_location limit="5"]
25: *
26: * @param array $atts Shortcode attributes.
27: *
28: * @return false|string|void
29: * @since 1.1.2
30: * @since 3.4.48 Fixed class name.
31: * @since 3.5.5 Sorting not working.
32: */
33: function epl_shortcode_listing_tax_location_callback( $atts ) {
34: $property_types = epl_get_active_post_types();
35: if ( ! empty( $property_types ) ) {
36: $property_types = array_keys( $property_types );
37: }
38:
39: $attributes = shortcode_atts(
40: array(
41: 'post_type' => $property_types, // Post Type.
42: 'status' => array( 'current', 'sold', 'leased' ),
43: 'location' => '', // Location slug.
44: 'location_id' => '', // Location ID.
45: 'limit' => '10', // Number of maximum posts to show.
46: 'offset' => '', // Offset posts. When used, pagination is disabled.
47: 'template' => false, // Template can be set to "slim" for home open style template.
48: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
49: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
50: 'sortby' => '', // Options: price, date, status : Default date.
51: 'sort_order' => 'DESC', // Sort by ASC or DESC.
52: 'pagination' => 'on', // Enable or disable pagination.
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: $post_type = $attributes['post_type'];
60: $status = $attributes['status'];
61: $limit = $attributes['limit'];
62: $location = $attributes['location'];
63: $location_id = $attributes['location_id'];
64: $offset = $attributes['offset'];
65: $template = $attributes['template'];
66: $tools_top = $attributes['tools_top'];
67: $tools_bottom = $attributes['tools_bottom'];
68: $sortby = $attributes['sortby'];
69: $sort_order = $attributes['sort_order'];
70: $pagination = $attributes['pagination'];
71: $instance_id = $attributes['instance_id'];
72: $class = $attributes['class'];
73:
74: if ( empty( $post_type ) ) {
75: return;
76: }
77: if ( is_string( $post_type ) && 'rental' === $post_type ) {
78: $meta_key_price = 'property_rent';
79: } else {
80: $meta_key_price = 'property_price';
81: }
82: $sort_options = array(
83: 'price' => $meta_key_price,
84: 'date' => 'post_date',
85: );
86: if ( ! is_array( $post_type ) ) {
87: $post_type = array_map( 'trim', explode( ',', $post_type ) );
88: }
89: ob_start();
90: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
91: $args = array(
92: 'post_type' => $post_type,
93: 'posts_per_page' => $limit,
94: 'paged' => $paged,
95: );
96:
97: // Offset query does not work with pagination.
98: if ( ! empty( $offset ) ) {
99: $args['offset'] = $offset;
100: $pagination = 'off'; // Disable pagination when offset is used.
101: }
102:
103: if ( ! empty( $location ) ) {
104: if ( ! is_array( $location ) ) {
105: $location = explode( ',', $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( $location_id ) ) {
117: if ( ! is_array( $location_id ) ) {
118: $location_id = explode( ',', $location_id );
119: $location_id = array_map( 'trim', $location_id );
120:
121: $args['tax_query'][] = array(
122: 'taxonomy' => 'location',
123: 'field' => 'id',
124: 'terms' => $location_id,
125: );
126: }
127: }
128:
129: if ( ! empty( $status ) ) {
130: if ( ! is_array( $status ) ) {
131: $status = explode( ',', $status );
132: $status = array_map( 'trim', $status );
133:
134: $args['meta_query'][] = array(
135: 'key' => 'property_status',
136: 'value' => $status,
137: 'compare' => 'IN',
138: );
139: }
140: }
141:
142: if ( ! empty( $sortby ) ) {
143:
144: if ( 'price' === $sortby ) {
145: $args['orderby'] = 'meta_value_num';
146: $args['meta_key'] = $meta_key_price;
147: } elseif ( 'status' === $sortby ) {
148: $args['orderby'] = 'meta_value';
149: $args['meta_key'] = 'property_status';
150: } else {
151: $args['orderby'] = 'post_date';
152: }
153: $args['order'] = $sort_order;
154: }
155:
156: $args['instance_id'] = $attributes['instance_id'];
157: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
158: $args = epl_add_orderby_args( $args, 'shortcode', 'listing_location' );
159:
160: // Option to filter args.
161: $args = apply_filters( 'epl_shortcode_listing_location_args', $args, $attributes );
162:
163: $query_open = new WP_Query( $args );
164: if ( $query_open->have_posts() ) { ?>
165: <div class="loop epl-shortcode">
166: <div class="loop-content epl-shortcode-listing-location <?php echo ' ' . esc_attr( epl_template_class( $template, 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>">
167: <?php
168: if ( 'on' === $tools_top ) {
169: do_action( 'epl_property_loop_start', $attributes );
170: }
171: while ( $query_open->have_posts() ) {
172: $query_open->the_post();
173:
174: $template = str_replace( '_', '-', $template );
175: epl_property_blog( $template );
176: }
177: if ( 'on' === $tools_bottom ) {
178: do_action( 'epl_property_loop_end' );
179: }
180:
181: ?>
182: </div>
183: <div class="loop-footer">
184: <?php
185: if ( 'on' === $pagination ) {
186: do_action( 'epl_pagination', array( 'query' => $query_open ) );
187: }
188: ?>
189: </div>
190: </div>
191: <?php
192: } else {
193: do_action( 'epl_shortcode_results_message', 'location' );
194: }
195: wp_reset_postdata();
196: return ob_get_clean();
197: }
198: add_shortcode( 'listing_location', 'epl_shortcode_listing_tax_location_callback' );
199: