1: <?php
2: /**
3: * SHORTCODE :: Open For Inspection [listing_open]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingOpen
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.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 Open Shortcode [listing_open]
21: *
22: * This shortcode allows for you to specify the property type(s) using
23: * [listing_open post_type="property,rental"] option. You can also
24: * limit the number of entries that display. using [epl-property-open limit="5"]
25: *
26: * @param array $atts Shortcode attributes.
27: *
28: * @return false|string
29: * @since 1.0
30: * @since 3.4.48 Fixed class name.
31: * @since 3.5.5 Sorting not working.
32: */
33: function epl_shortcode_property_open_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: 'limit' => '-1', // Number of maximum posts to show.
43: 'template' => false, // Template. slim, table.
44: 'location' => '', // Location slug. Should be a name like sorrento.
45: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
46: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
47: 'sortby' => '', // Options: price, date : Default date.
48: 'sort_order' => 'DESC', // Sort by ASC or DESC.
49: 'pagination' => 'on', // Enable or disable pagination.
50: 'instance_id' => '1', // Set instance ID when using multiple shortcodes on the same page.
51: 'class' => '', // Custom class.
52:
53: ),
54: $atts
55: );
56:
57: $post_type = $attributes['post_type'];
58: $limit = $attributes['limit'];
59: $template = $attributes['template'];
60: $location = $attributes['location'];
61: $tools_top = $attributes['tools_top'];
62: $tools_bottom = $attributes['tools_bottom'];
63: $sortby = $attributes['sortby'];
64: $sort_order = $attributes['sort_order'];
65: $pagination = $attributes['pagination'];
66: $instance_id = $attributes['instance_id'];
67: $class = $attributes['class'];
68:
69: if ( is_string( $post_type ) && 'rental' === $post_type ) {
70: $meta_key_price = 'property_rent';
71: } else {
72: $meta_key_price = 'property_price';
73: }
74:
75: $sort_options = array(
76: 'price' => $meta_key_price,
77: 'date' => 'post_date',
78: );
79:
80: ob_start();
81: if ( ! is_array( $post_type ) ) {
82: $post_type = array_map( 'trim', explode( ',', $post_type ) );
83: }
84:
85: $args = array(
86: 'post_type' => $post_type,
87: 'posts_per_page' => $limit,
88: 'meta_key' => 'property_inspection_times',
89: 'meta_query' => array(
90: array(
91: 'key' => 'property_inspection_times',
92: 'value' => '^\s*$',
93: 'compare' => 'NOT REGEXP',
94: ),
95: array(
96: 'key' => 'property_status',
97: 'value' => array( 'leased', 'sold' ),
98: 'compare' => 'NOT IN',
99: ),
100: ),
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 ( '' !== $sortby ) {
117:
118: if ( 'price' === $sortby ) {
119: $args['orderby'] = 'meta_value_num';
120: $args['meta_key'] = $meta_key_price;
121: } elseif ( 'status' === $sortby ) {
122: $args['orderby'] = 'meta_value';
123: $args['meta_key'] = 'property_status';
124: } else {
125: $args['orderby'] = 'post_date';
126: }
127: $args['order'] = $sort_order;
128: }
129:
130: $args['instance_id'] = $attributes['instance_id'];
131: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
132: $args = epl_add_orderby_args( $args, 'shortcode', 'listing_open' );
133:
134: // Option to filter args.
135: $args = apply_filters( 'epl_shortcode_listing_open_args', $args, $attributes );
136:
137: $query_open = new WP_Query( $args );
138: if ( $query_open->have_posts() ) { ?>
139: <div class="loop epl-shortcode">
140: <div class="loop-content epl-shortcode-listing-open <?php echo ' ' . esc_attr( epl_template_class( $template, 'archive' ) ) . ' ' . esc_attr( $attributes['class'] ); ?>">
141: <?php
142: if ( 'on' === $tools_top ) {
143: do_action( 'epl_property_loop_start', $attributes );
144: }
145: while ( $query_open->have_posts() ) {
146: $query_open->the_post();
147:
148: $template = str_replace( '_', '-', $template );
149: epl_property_blog( $template );
150: }
151: if ( 'on' === $tools_bottom ) {
152: do_action( 'epl_property_loop_end' );
153: }
154:
155: ?>
156: </div>
157: <div class="loop-footer">
158: <?php
159: if ( 'on' === $pagination ) {
160: do_action( 'epl_pagination', array( 'query' => $query_open ) );
161: }
162: ?>
163: </div>
164: </div>
165: <?php
166: } else {
167: do_action( 'epl_shortcode_results_message', 'open' );
168: }
169: wp_reset_postdata();
170: return ob_get_clean();
171: }
172: add_shortcode( 'home_open_list', 'epl_shortcode_property_open_callback' );
173: add_shortcode( 'listing_open', 'epl_shortcode_property_open_callback' );
174: