1: <?php
2: /**
3: * SHORTCODE :: Listing Advanced [listing_advanced]
4: *
5: * @package EPL
6: * @subpackage Classes/ListingAdvanced
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.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: * EPL_Advanced_Shortcode_Listing Class
21: *
22: * @since 3.3.0
23: */
24: class EPL_Advanced_Shortcode_Listing {
25:
26: /**
27: * Default arguments.
28: *
29: * @since 3.3.0
30: * @var array $default_args Default arguments.
31: */
32: public $default_args;
33:
34: /**
35: * Attributes passed to shortcode.
36: *
37: * @since 3.3.0
38: * @var array $atts Shortcode attributes.
39: */
40: public $atts;
41:
42: /**
43: * Attributes passed to shortcode and defaults.
44: *
45: * @since 3.3.0
46: * @var array $attributes Shortcode attributes.
47: */
48: public $attributes;
49:
50: /**
51: * Array of arguments passed to WP_Query.
52: *
53: * @since 3.3.0
54: * @var array $args Arguments.
55: */
56: public $args;
57:
58: /**
59: * Meta price key.
60: *
61: * @since 3.3.0
62: * @var string $meta_key_price Default price meta value.
63: */
64: public $meta_key_price = 'property_price';
65:
66: /**
67: * Query object.
68: *
69: * @since 3.3.0
70: * @var object $query_open instance of WP_Query object.
71: */
72: public $query_open;
73:
74: /**
75: * Shortcode name: picks the args filter (epl_shortcode_{name}_args) and the sorting
76: * context. [listing] runs on this class with the name 'listing'.
77: *
78: * @since 3.6
79: * @var string $shortcode Shortcode name.
80: */
81: public $shortcode = 'listing_advanced';
82:
83: /**
84: * Construct the shortcode.
85: *
86: * @since 3.3.0
87: * @param array $atts Shortcode attributes.
88: * @param array $overrides Array of variables to override defaults.
89: */
90: public function __construct( $atts, $overrides = array() ) {
91: $this->atts = $atts;
92: $this->shortcode_atts();
93: $this->override_atts( $overrides );
94: $this->build_query();
95: $this->set_query();
96: }
97:
98: /**
99: * Override Attributes.
100: *
101: * @since 3.3.0
102: * @param array $overrides Array of variables to override defaults.
103: */
104: public function override_atts( $overrides ) {
105:
106: if ( ! empty( $overrides ) ) {
107:
108: foreach ( $overrides as $key => $value ) {
109: $this->set_attribute( $key, $value );
110: }
111: }
112: }
113:
114: /**
115: * Get default post types.
116: *
117: * @since 3.3
118: */
119: public function get_default_post_types() {
120:
121: $property_types = epl_get_active_post_types();
122: if ( ! empty( $property_types ) ) {
123: $property_types = array_keys( $property_types );
124: }
125:
126: return $property_types;
127: }
128:
129: /**
130: * Get meta key price.
131: *
132: * The rent when the shortcode lists rentals only, otherwise the price.
133: *
134: * @since 3.3
135: * @since 3.6 Fix: post_type="rental" with sortby="price" sorted by property_price, because post_type is a list by the time this runs. Now sorts by property_rent, as [listing] did.
136: */
137: public function get_meta_key_price() {
138:
139: $post_type = $this->attributes['post_type'];
140: if ( is_array( $post_type ) && 1 === count( $post_type ) ) {
141: $post_type = reset( $post_type );
142: }
143:
144: if ( is_string( $post_type ) && 'rental' === $post_type ) {
145: $this->meta_key_price = 'property_rent';
146: } else {
147: $this->meta_key_price = 'property_price';
148: }
149:
150: return $this->meta_key_price;
151: }
152:
153: /**
154: * Get default options.
155: *
156: * @since 3.3
157: */
158: public function get_default_args() {
159:
160: /**
161: *
162: * Template : used to render each listing in loop.
163: *
164: * Default to loop-listing-blog-default.php
165: *
166: * To override copy loop-listing-blog-default.php from EPL/templates/content to
167: * active_theme/easypropertylistings and rename the file to loop-listing-blog-{$template}.php
168: *
169: *
170: * wrap_template : Used to render wrapper of listings render using shortcode.
171: *
172: * Default to shortcode-listing.php
173: *
174: * to override copy shortcode-listing.php from EPL/templates/content to
175: * active_theme/easypropertylistings and rename the file to
176: * shortcode-listing-{$wrap_template}.php
177: */
178:
179: $this->default_args = array(
180: 'post_type' => $this->get_default_post_types(), // Post Type.
181: 'status' => array( 'current', 'sold', 'leased' ),
182: 'commercial_listing_type' => '', // Listing Type, sale lease and both.
183: 'feature' => '', // Feature slug.
184: 'feature_id' => '', // Feature ID.
185: 'limit' => '10', // Number of maximum posts to show.
186: 'offset' => '', // Offset posts. When used, pagination is disabled.
187: 'author' => '', // Author of listings.
188: 'agent' => '', // listings by agent.
189: 'featured' => 0, // Featured listings.
190: 'open_house' => false, // only show open house.
191: 'auction' => false, // only show properties for auction.
192: 'class' => 'epl-shortcode-listing', // wrapper class.
193: 'wrap_template' => '', // explained in detail below.
194: 'template' => false, // Template can be set to "slim" for home open style template.
195: 'location' => '', // Location slug. Should be a name like sorrento.
196: 'location_id' => '', // Location ID.
197: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
198: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
199: 'sortby' => '', // Options: price, date : Default date.
200: 'orderby_clause' => '', // order by two different pieces of postmeta (for example, suburb first and state second).
201: 'sort_order' => 'DESC', // Sort by ASC or DESC.
202: 'query_object' => '', // only for internal use . if provided use it instead of custom query.
203: 'pagination' => 'on', // Enable or disable pagination.
204: 'post__in' => '', // show only these posts.
205: 'post__not_in' => '', // dont show these posts.
206: 'instance_id' => '1', // when using multiple shortcodes on once page.
207: );
208:
209: return $this->default_args;
210: }
211:
212: /**
213: * Shortcode attributes
214: *
215: * @since 3.3
216: */
217: public function shortcode_atts() {
218: $this->attributes = shortcode_atts( $this->get_default_args(), $this->atts );
219: }
220:
221: /**
222: * Set attributes
223: *
224: * @since 3.3
225: * @param string $key Meta key.
226: * @param string $value Meta value.
227: */
228: public function set_attribute( $key, $value ) {
229:
230: if ( isset( $this->attributes[ $key ] ) ) {
231: $this->attributes[ $key ] = $value;
232: }
233: }
234:
235: /**
236: * Get attributes
237: *
238: * @param string $key Meta key.
239: *
240: * @return mixed|null
241: * @since 3.3
242: */
243: public function get_attribute( $key ) {
244:
245: return isset( $this->attributes[ $key ] ) ? $this->attributes[ $key ] : null;
246: }
247:
248: /**
249: * Set the post type
250: *
251: * @since 3.3
252: * @since 3.6 Security: post types visitors can't view (contacts) are left out, so post_type="epl_contact" finds nothing.
253: */
254: public function set_post_type() {
255: if ( ! is_array( $this->attributes['post_type'] ) ) {
256: $this->attributes['post_type'] =
257: array_map( 'trim', explode( ',', $this->attributes['post_type'] ) );
258: }
259:
260: // Leave out registered post types visitors can't view, such as contacts. Unregistered
261: // types stay, so a switched off listing type still finds nothing, as before.
262: $post_types = array();
263: foreach ( $this->attributes['post_type'] as $post_type ) {
264: if ( ! is_string( $post_type ) || ( post_type_exists( $post_type ) && ! epl_search_can_show_post_type( $post_type ) ) ) {
265: continue;
266: }
267: $post_types[] = $post_type;
268: }
269: if ( empty( $post_types ) ) {
270: $post_types = array( 'epl_no_post_type' ); // Not registered: the query finds nothing.
271: }
272: $this->attributes['post_type'] = $post_types;
273: }
274:
275: /**
276: * Set initial options
277: *
278: * @since 3.3
279: */
280: public function set_initial_args() {
281:
282: $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
283: $this->args = array(
284: 'post_type' => $this->attributes['post_type'],
285: 'posts_per_page' => $this->attributes['limit'],
286: 'paged' => absint( $paged ),
287: );
288: }
289:
290: /**
291: * Set the offset
292: *
293: * @since 3.3
294: */
295: public function set_offset() {
296:
297: // Offset query does not work with pagination.
298: if ( ! empty( $this->attributes['offset'] ) ) {
299: $this->args['offset'] = $this->attributes['offset'];
300: $this->attributes['pagination'] = 'off'; // Disable pagination when offset is used.
301: }
302: }
303:
304: /**
305: * Set posts in
306: *
307: * @since 3.3
308: */
309: public function set_post__in() {
310: if ( ! empty( $this->attributes['post__in'] ) ) {
311: $post__in = array_map( 'trim', explode( ',', $this->attributes['post__in'] ) );
312: if ( ! empty( $post__in ) ) {
313: $this->args['post__in'] = $post__in;
314: }
315: }
316: }
317:
318: /**
319: * Set posts not in
320: *
321: * @since 3.3
322: */
323: public function set_post__not_in() {
324: if ( ! empty( $this->attributes['post__not_in'] ) ) {
325: $post__not_in = array_map( 'trim', explode( ',', $this->attributes['post__not_in'] ) );
326: if ( ! empty( $post__not_in ) ) {
327: $this->args['post__not_in'] = $post__not_in;
328: }
329: }
330: }
331:
332: /**
333: * Process EPL attributes
334: *
335: * @since 3.3
336: */
337: public function process_epl_atts() {
338:
339: $this->args['meta_query'] = epl_parse_atts( $this->atts );
340: }
341:
342: /**
343: * Set the author
344: *
345: * @since 3.3
346: */
347: public function set_author() {
348:
349: // Listings of specified author.
350: if ( ! empty( $this->attributes['author'] ) ) {
351: $this->attributes['author'] = (array) $this->attributes['author'];
352: if ( is_array( $this->attributes['author'] ) ) {
353: $author_ids = array_map( 'epl_get_author_id_from_name', $this->attributes['author'] );
354: $this->attributes['author'] = implode( ',', $author_ids );
355: }
356: $this->args['author'] = trim( $this->attributes['author'] );
357: }
358: }
359:
360: /**
361: * Set the agent
362: *
363: * @since 3.3
364: * @since 3.5 support for third & fourth agent
365: */
366: public function set_agent() {
367:
368: // Listings by specified agent.
369: if ( ! empty( $this->attributes['agent'] ) ) {
370: $this->attributes['agent'] = array_map( 'trim', explode( ',', $this->attributes['agent'] ) );
371: $this->attributes['agent'] = array_filter( $this->attributes['agent'] );
372:
373: $agent_meta_query = array(
374: 'relation' => 'OR',
375: );
376:
377: foreach ( $this->attributes['agent'] as $single_agent ) {
378: $agent_meta_query[] = array(
379: 'key' => 'property_agent',
380: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
381: 'compare' => 'IN',
382: );
383: $agent_meta_query[] = array(
384: 'key' => 'property_second_agent',
385: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
386: 'compare' => 'IN',
387: );
388:
389: if ( in_array( 'commercial', $this->attributes['post_type'], true ) ||
390: in_array( 'commercial_land', $this->attributes['post_type'], true ) ||
391: in_array( 'business', $this->attributes['post_type'], true )
392: ) {
393:
394: $agent_meta_query[] = array(
395: 'key' => 'property_third_agent',
396: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
397: 'compare' => 'IN',
398: );
399: $agent_meta_query[] = array(
400: 'key' => 'property_fourth_agent',
401: 'value' => array( $single_agent, sanitize_user( $single_agent ) ),
402: 'compare' => 'IN',
403: );
404:
405: }
406: }
407:
408: $this->args['meta_query']['agent_clause'] = $agent_meta_query;
409: }
410: }
411:
412: /**
413: * Set featured
414: *
415: * @since 3.3
416: */
417: public function set_featured() {
418:
419: // Featured listings.
420: if ( $this->attributes['featured'] ) {
421: $this->args['meta_query']['featured_clause'] = array(
422: 'key' => 'property_featured',
423: 'value' => 'yes',
424: );
425: }
426: }
427:
428: /**
429: * Set auction
430: *
431: * @since 3.3
432: */
433: public function set_auction() {
434:
435: // Auction only listings.
436: if ( $this->attributes['auction'] ) {
437: $this->args['meta_query']['property_authority_clause'] = array(
438: 'key' => 'property_authority',
439: 'value' => 'auction',
440: );
441: }
442: }
443:
444: /**
445: * Set open for inspection
446: *
447: * @since 3.3
448: */
449: public function set_open_house() {
450:
451: // Open house only.
452: if ( $this->attributes['open_house'] ) {
453: $this->args['meta_query']['open_house_clause'] = array(
454: 'key' => 'property_inspection_times',
455: 'value' => '',
456: 'compare' => '!=',
457: );
458: }
459: }
460:
461: /**
462: * Set location taxonomy query
463: *
464: * @since 3.3
465: */
466: public function set_location_tax_query() {
467:
468: // Location taxonomy.
469: // Query by slug.
470: if ( ! empty( $this->attributes['location'] ) ) {
471: if ( ! is_array( $this->attributes['location'] ) ) {
472: $this->attributes['location'] = array_map( 'trim', explode( ',', $this->attributes['location'] ) );
473:
474: $this->args['tax_query']['tax_location_clause'] = array(
475: 'taxonomy' => 'location',
476: 'field' => 'slug',
477: 'terms' => $this->attributes['location'],
478: );
479: }
480: }
481:
482: // Query by ID.
483: if ( ! empty( $this->attributes['location_id'] ) ) {
484: if ( ! is_array( $this->attributes['location_id'] ) ) {
485: $this->attributes['location_id'] = array_map( 'trim', explode( ',', $this->attributes['location_id'] ) );
486:
487: $this->args['tax_query']['tax_location_id_clause'] = array(
488: 'taxonomy' => 'location',
489: 'field' => 'id',
490: 'terms' => $this->attributes['location_id'],
491: );
492: }
493: }
494: }
495:
496: /**
497: * Set feeatures taxonomy query
498: *
499: * @since 3.3
500: */
501: public function set_features_tax_query() {
502:
503: // Features taxonomy.
504: // Query by slug.
505: if ( ! empty( $this->attributes['feature'] ) ) {
506: if ( ! is_array( $this->attributes['feature'] ) ) {
507: $this->attributes['feature'] = array_map( 'trim', explode( ',', $this->attributes['feature'] ) );
508:
509: $this->args['tax_query']['tax_feature_clause'] = array(
510: 'taxonomy' => 'tax_feature',
511: 'field' => 'slug',
512: 'terms' => $this->attributes['feature'],
513: );
514: }
515: }
516: // Query by ID.
517: if ( ! empty( $this->attributes['feature_id'] ) ) {
518: if ( ! is_array( $this->attributes['feature_id'] ) ) {
519: $this->attributes['feature_id'] = array_map( 'trim', explode( ',', $this->attributes['feature_id'] ) );
520:
521: $this->args['tax_query']['tax_feature_id_clause'] = array(
522: 'taxonomy' => 'tax_feature',
523: 'field' => 'id',
524: 'terms' => $this->attributes['feature_id'],
525: );
526: }
527: }
528: }
529:
530: /**
531: * Set status
532: *
533: * @since 3.3
534: */
535: public function set_status() {
536:
537: if ( ! empty( $this->attributes['status'] ) ) {
538: if ( ! is_array( $this->attributes['status'] ) ) {
539: $this->attributes['status'] = array_map( 'trim', explode( ',', $this->attributes['status'] ) );
540:
541: $this->args['meta_query']['property_status_clause'] = array(
542: 'key' => 'property_status',
543: 'value' => $this->attributes['status'],
544: 'compare' => 'IN',
545: );
546:
547: add_filter( 'epl_sorting_options', 'epl_sorting_options_callback' );
548: }
549: }
550: }
551:
552: /**
553: * Set commercial listing type
554: *
555: * @since 3.3
556: */
557: public function set_commercial_listing_type() {
558:
559: /** Commercial listing type */
560: if ( ! empty( $this->attributes['commercial_listing_type'] ) ) {
561: if ( ! is_array( $this->attributes['commercial_listing_type'] ) ) {
562: $this->attributes['commercial_listing_type'] = array_map( 'trim', explode( ',', $this->attributes['commercial_listing_type'] ) );
563:
564: $this->args['meta_query']['property_com_listing_type_clause'] = array(
565: 'key' => 'property_com_listing_type',
566: 'value' => $this->attributes['commercial_listing_type'],
567: 'compare' => 'IN',
568: );
569: }
570: }
571: }
572:
573: /**
574: * Set orderby
575: *
576: * @since 3.3
577: */
578: public function set_orderby() {
579:
580: if ( ! empty( $this->attributes['sortby'] ) ) {
581: if ( 'price' === $this->attributes['sortby'] ) {
582: $this->args['orderby'] = 'meta_value_num';
583: $this->args['meta_key'] = $this->get_meta_key_price();
584: } elseif ( 'rand' === $this->attributes['sortby'] ) {
585: $this->args['orderby'] = 'rand';
586: } elseif ( 'status' === $this->attributes['sortby'] ) {
587: $this->args['orderby'] = 'meta_value';
588: $this->args['meta_key'] = 'property_status';
589: } else {
590: $this->args['orderby'] = 'post_date';
591: $this->args['order'] = 'DESC';
592: }
593: $this->args['order'] = $this->attributes['sort_order'];
594: }
595: }
596:
597: /**
598: * Set orderby clause
599: *
600: * @since 3.3
601: * @since 3.4.35 Fixed the issue : [listing_advanced] not marked as EPL shortcode in WP_Query object
602: * @since 3.6 Uses the shortcode name for the sorting context and the args filter, so [listing] keeps epl_shortcode_listing_args.
603: * @since 3.6 Fix: with sortby set, orderby_clause crashed on PHP 8; a clause without |ASC or |DESC gave a warning (now ASC).
604: */
605: public function set_orderby_clause() {
606:
607: /**
608: * Advance orderby using named meta query clauses, will override default orderby.
609: */
610:
611: if ( ! empty( $this->attributes['orderby_clause'] ) ) {
612:
613: $this->attributes['orderby_clause'] =
614: array_map( 'trim', explode( ',', $this->attributes['orderby_clause'] ) );
615:
616: // orderby_clause replaces any sortby order; start from a list.
617: if ( ! isset( $this->args['orderby'] ) || ! is_array( $this->args['orderby'] ) ) {
618: $this->args['orderby'] = array();
619: }
620:
621: foreach ( $this->attributes['orderby_clause'] as $oc ) {
622: $oc_array = explode( '|', $oc );
623: $clause = trim( $oc_array[0] );
624: $order = isset( $oc_array[1] ) && 'DESC' === strtoupper( trim( $oc_array[1] ) ) ? 'DESC' : 'ASC';
625:
626: if ( '' !== $clause ) {
627: $this->args['orderby'][ $clause ] = $order;
628: }
629: }
630:
631: unset( $this->args['order'] );
632: }
633:
634: $this->args['instance_id'] = $this->attributes['instance_id'];
635:
636: // add sortby arguments to query, if listings sorted by $_GET['sortby'];.
637: $this->args = epl_add_orderby_args( $this->args, 'shortcode', $this->shortcode );
638: // Option to filter args: epl_shortcode_listing_advanced_args, or epl_shortcode_listing_args for [listing].
639: $this->args = apply_filters( 'epl_shortcode_' . $this->shortcode . '_args', $this->args, $this->attributes );
640: }
641:
642: /**
643: * Set query
644: *
645: * @since 3.3
646: */
647: public function set_query() {
648:
649: $this->query_open = new WP_Query( $this->args );
650:
651: if ( is_object( $this->attributes['query_object'] ) ) {
652: $this->query_open = $this->attributes['query_object'];
653: }
654: }
655:
656: /**
657: * Get the template
658: *
659: * @since 3.3
660: * @since 3.6 Security: wrap_template is limited to letters, numbers, - and _ (it could reach other files with ../). Underscores now become dashes, as intended.
661: */
662: public function get_wrap_template() {
663:
664: $wrap = str_replace( '_', '-', sanitize_key( (string) $this->attributes['wrap_template'] ) );
665:
666: $wrap_template = '' === $wrap ? 'shortcode-listing.php' : 'shortcode-listing-' . $wrap . '.php';
667:
668: return $wrap_template;
669: }
670:
671: /**
672: * Build the query
673: *
674: * @since 3.3
675: */
676: public function build_query() {
677:
678: $this->set_post_type();
679: $this->set_initial_args();
680: $this->set_offset();
681: $this->set_post__in();
682: $this->set_post__not_in();
683: $this->process_epl_atts();
684: $this->set_author();
685: $this->set_agent();
686: $this->set_featured();
687: $this->set_auction();
688: $this->set_open_house();
689: $this->set_location_tax_query();
690: $this->set_features_tax_query();
691: $this->set_status();
692: $this->set_commercial_listing_type();
693: $this->set_orderby();
694: $this->set_orderby_clause();
695: }
696:
697: /**
698: * Render the shortcode
699: *
700: * @since 3.3
701: * @since 3.6 Fix: a wrap_template that doesn't exist falls back to shortcode-listing.php instead of PHP warnings and no listings.
702: * @since 3.6 The wrapper always has the shortcode's own class (get_wrapper_class()).
703: */
704: public function render() {
705: $wrap_template = $this->get_wrap_template();
706: $attributes = $this->attributes;
707:
708: $attributes['class'] = $this->get_wrapper_class();
709:
710: ob_start();
711: epl_get_template_part(
712: $wrap_template,
713: array(
714: 'attributes' => $attributes,
715: 'query_open' => $this->query_open,
716: ),
717: 'shortcode-listing.php'
718: );
719:
720: return ob_get_clean();
721: }
722:
723: /**
724: * Wrapper classes: the shortcode's own classes, then any class option
725: *
726: * Every listing shortcode always has its own class, like the other listing shortcodes
727: * (epl-shortcode-listing-category and so on): epl-shortcode-listing for [listing] and
728: * [listing_advanced], plus epl-shortcode-listing-advanced for [listing_advanced]. The
729: * class option adds to them instead of replacing them.
730: *
731: * @return string
732: *
733: * @since 3.6 New: [listing] had no wrapper class, and a class option removed epl-shortcode-listing.
734: */
735: public function get_wrapper_class() {
736: $classes = array( 'epl-shortcode-listing' );
737:
738: if ( 'listing' !== $this->shortcode ) {
739: $classes[] = 'epl-shortcode-' . str_replace( '_', '-', sanitize_key( $this->shortcode ) );
740: }
741:
742: $extra = isset( $this->attributes['class'] ) ? preg_split( '/\s+/', trim( (string) $this->attributes['class'] ) ) : array();
743:
744: return implode( ' ', array_unique( array_filter( array_merge( $classes, (array) $extra ) ) ) );
745: }
746: }
747: