1: <?php
2: /**
3: * SHORTCODE :: Listing Feature [listing_feature]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingFeature
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 Feature [listing_feature]
21: *
22: * This shortcode allows for you to specify feature property type(s) using
23: * [listing_feature post_type="property" status="current,sold,leased" feature_id="" feature="" template="default"] option. You can also
24: * limit the number of entries that display. using [listing_category limit="5"]
25: *
26: * @param array $atts Shortcode attributes.
27: *
28: * @return false|string|void
29: * @since 1.1.2
30: * @since 3.4.27 Fixed the issue: no spaces between classes when both class & template attributs are set.
31: * @since 3.4.48 Fixed class name.
32: * @since 3.5.5 Sorting not working.
33: */
34: function epl_shortcode_listing_tax_feature_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: 'feature' => '', // Feature slug.
45: 'feature_id' => '', // Feature ID.
46: 'limit' => '10', // Number of maximum posts to show.
47: 'offset' => '', // Offset posts. When used, pagination is disabled.
48: 'template' => false, // Template can be set to "slim" for home open style template.
49: 'location' => '', // Location slug. Should be a name like sorrento.
50: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
51: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
52: 'sortby' => '', // Options: price, date, status : Default date.
53: 'sort_order' => 'DESC', // Sort by ASC or DESC.
54: 'pagination' => 'on', // Enable or disable pagination.
55: 'instance_id' => '1', // Set instance ID when using multiple shortcodes on the same page.
56: 'class' => '', // Custom class.
57: ),
58: $atts
59: );
60:
61: $post_type = $attributes['post_type'];
62: $status = $attributes['status'];
63: $limit = $attributes['limit'];
64: $feature = $attributes['feature'];
65: $feature_id = $attributes['feature_id'];
66: $offset = $attributes['offset'];
67: $template = $attributes['template'];
68: $location = $attributes['location'];
69: $tools_top = $attributes['tools_top'];
70: $tools_bottom = $attributes['tools_bottom'];
71: $sortby = $attributes['sortby'];
72: $sort_order = $attributes['sort_order'];
73: $pagination = $attributes['pagination'];
74: $instance_id = $attributes['instance_id'];
75: $class = $attributes['class'];
76:
77: if ( empty( $post_type ) ) {
78: return;
79: }
80:
81: if ( is_string( $post_type ) && 'rental' === $post_type ) {
82: $meta_key_price = 'property_rent';
83: } else {
84: $meta_key_price = 'property_price';
85: }
86:
87: $sort_options = array(
88: 'price' => $meta_key_price,
89: 'date' => 'post_date',
90: );
91:
92: if ( ! is_array( $post_type ) ) {
93: $post_type = array_map( 'trim', explode( ',', $post_type ) );
94: }
95: ob_start();
96:
97: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
98: $args = array(
99: 'post_type' => $post_type,
100: 'posts_per_page' => $limit,
101: 'paged' => $paged,
102: );
103:
104: // Offset query does not work with pagination.
105: if ( ! empty( $offset ) ) {
106: $args['offset'] = $offset;
107: $pagination = 'off'; // Disable pagination when offset is used.
108: }
109:
110: if ( ! empty( $feature ) ) {
111: if ( ! is_array( $feature ) ) {
112: $feature = explode( ',', $feature );
113: $feature = array_map( 'trim', $feature );
114:
115: $args['tax_query'][] = array(
116: 'taxonomy' => 'tax_feature',
117: 'field' => 'slug',
118: 'terms' => $feature,
119: );
120: }
121: }
122:
123: if ( ! empty( $feature_id ) ) {
124: if ( ! is_array( $feature_id ) ) {
125: $feature_id = explode( ',', $feature_id );
126: $feature_id = array_map( 'trim', $feature_id );
127:
128: $args['tax_query'][] = array(
129: 'taxonomy' => 'tax_feature',
130: 'field' => 'id',
131: 'terms' => $feature_id,
132: );
133: }
134: }
135:
136: if ( ! empty( $location ) ) {
137: if ( ! is_array( $location ) ) {
138: $location = explode( ',', $location );
139: $location = array_map( 'trim', $location );
140:
141: $args['tax_query'][] = array(
142: 'taxonomy' => 'location',
143: 'field' => 'slug',
144: 'terms' => $location,
145: );
146: }
147: }
148:
149: if ( ! empty( $status ) ) {
150: if ( ! is_array( $status ) ) {
151: $status = explode( ',', $status );
152: $status = array_map( 'trim', $status );
153:
154: $args['meta_query'][] = array(
155: 'key' => 'property_status',
156: 'value' => $status,
157: 'compare' => 'IN',
158: );
159: }
160: }
161: if ( ! empty( $sortby ) ) {
162:
163: if ( 'price' === $sortby ) {
164: $args['orderby'] = 'meta_value_num';
165: $args['meta_key'] = $meta_key_price;
166: } elseif ( 'status' === $sortby ) {
167: $args['orderby'] = 'meta_value';
168: $args['meta_key'] = 'property_status';
169: } else {
170: $args['orderby'] = 'post_date';
171: }
172: $args['order'] = $sort_order;
173: }
174:
175: $args['instance_id'] = $attributes['instance_id'];
176: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
177: $args = epl_add_orderby_args( $args, 'shortcode', 'listing_feature' );
178:
179: // Option to filter args.
180: $args = apply_filters( 'epl_shortcode_listing_feature_args', $args, $attributes );
181:
182: $query_open = new WP_Query( $args );
183: if ( $query_open->have_posts() ) { ?>
184: <div class="loop epl-shortcode">
185: <div class="loop-content epl-shortcode-listing-feature <?php echo ' ' . esc_attr( epl_template_class( $template, 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>">
186: <?php
187: if ( 'on' === $tools_top ) {
188: do_action( 'epl_property_loop_start', $attributes );
189: }
190: while ( $query_open->have_posts() ) {
191: $query_open->the_post();
192: $template = str_replace( '_', '-', $template );
193: epl_property_blog( $template );
194: }
195: if ( 'on' === $tools_bottom ) {
196: do_action( 'epl_property_loop_end' );
197: }
198: ?>
199: </div>
200: <div class="loop-footer">
201: <?php
202: if ( 'on' === $pagination ) {
203: do_action( 'epl_pagination', array( 'query' => $query_open ) );
204: }
205: ?>
206: </div>
207: </div>
208: <?php
209: } else {
210: do_action( 'epl_shortcode_results_message' );
211: }
212: wp_reset_postdata();
213: return ob_get_clean();
214: }
215: add_shortcode( 'listing_feature', 'epl_shortcode_listing_tax_feature_callback' );
216: