1: <?php
2: /**
3: * SHORTCODE :: Listing Results [listing_results]
4: *
5: * @package EPL
6: * @subpackage Shortcode/ListingResults
7: * @copyright Copyright (c) 2023, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.5.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Is this page showing listing results?
19: *
20: * True on an EPL search, a listing type archive, a location, feature or business category
21: * archive, or any archive whose listings are EPL listings. Reads the page WordPress is
22: * showing, so it still works inside page builder templates (Elementor switches the current
23: * post to its template while rendering).
24: *
25: * @since 3.6 New: used by [listing_results].
26: *
27: * @return bool
28: */
29: function epl_is_listing_results_page() {
30: global $wp_the_query;
31:
32: $is_results = ( function_exists( 'epl_is_search' ) && true === epl_is_search() ) || is_epl_core_post();
33:
34: if ( ! $is_results && $wp_the_query instanceof WP_Query && ! $wp_the_query->is_singular() ) {
35: $is_results = $wp_the_query->is_post_type_archive( epl_get_core_post_types() )
36: || $wp_the_query->is_tax( array( 'location', 'tax_feature', 'tax_business_listing' ) )
37: || ( $wp_the_query->is_archive() && ! empty( $wp_the_query->posts ) && is_epl_core_post( get_post_type( $wp_the_query->posts[0] ) ) );
38: }
39:
40: /**
41: * Filters whether [listing_results] treats this page as listing results.
42: *
43: * @since 3.6
44: *
45: * @param bool $is_results True on EPL searches and listing archives.
46: */
47: return (bool) apply_filters( 'epl_is_listing_results_page', $is_results );
48: }
49:
50: /**
51: * Is a page builder showing its editor or preview?
52: *
53: * Elementor, Divi, WPBakery, Beaver Builder, Bricks and Oxygen.
54: *
55: * @since 3.6 New: so [listing_results] shows sample listings while a template is designed.
56: *
57: * @return bool
58: */
59: function epl_is_page_builder_preview() {
60: // phpcs:disable WordPress.Security.NonceVerification
61: $preview = isset( $_GET['elementor-preview'] ) || isset( $_GET['et_fb'] ) || isset( $_GET['vc_editable'] ) || isset( $_GET['fl_builder'] ) || isset( $_GET['ct_builder'] )
62: || ( isset( $_GET['bricks'] ) && 'run' === $_GET['bricks'] );
63: // Elementor renders widgets over AJAX in its editor.
64: if ( ! $preview && wp_doing_ajax() && isset( $_REQUEST['action'] ) && 'elementor_ajax' === $_REQUEST['action'] ) {
65: $preview = true;
66: }
67: // phpcs:enable WordPress.Security.NonceVerification
68: if ( ! $preview && class_exists( '\Elementor\Plugin' ) && isset( \Elementor\Plugin::$instance ) ) {
69: $elementor = \Elementor\Plugin::$instance;
70: // The editor page renders each widget once when it opens (post.php?action=elementor).
71: if ( isset( $elementor->editor ) && is_object( $elementor->editor ) && method_exists( $elementor->editor, 'is_edit_mode' ) && $elementor->editor->is_edit_mode() ) {
72: $preview = true;
73: } elseif ( isset( $elementor->preview ) && is_object( $elementor->preview ) && method_exists( $elementor->preview, 'is_preview_mode' ) && $elementor->preview->is_preview_mode() ) {
74: $preview = true;
75: }
76: }
77:
78: /**
79: * Filters whether a page builder editor or preview is showing.
80: *
81: * @since 3.6
82: *
83: * @param bool $preview True in a page builder editor or preview.
84: */
85: return (bool) apply_filters( 'epl_is_page_builder_preview', $preview );
86: }
87:
88: /**
89: * Page Builder Archive / Search Results Loop
90: * Load the Easy Property Listings Loop using a shortcode. For use in Page Builders like Elementor, Divi, WP Bakery, Visual Composer.
91: *
92: * On search results and listing archives it shows the page's listings. On a normal page or
93: * post, and in a page builder editor, it shows the latest listings, with paging. Anywhere
94: * else (blog, author or other archives) it shows nothing.
95: *
96: * @param array $atts Shortcode options.
97: *
98: * @return false|string
99: * @since 3.5.0
100: * @since 3.6 Works in Elementor templates (it showed nothing when the template switched the current post) and on location, feature and empty archives. Shows the latest listings in page builder editors. New options: template, class, title and preview_limit.
101: * @since 3.6 Fix: title="on" in a page builder editor shows a plain title, so an editor redraw can't fail.
102: * @since 3.6 The wrapper has its own class, epl-shortcode-listing-results, like the other listing shortcodes.
103: * @since 3.6 On a normal page or post it shows the latest listings with paging (it showed nothing). Other archives stay empty.
104: */
105: function epl_listing_results_loop_callback( $atts ) {
106:
107: $attributes = shortcode_atts(
108: array(
109: 'tools_top' => 'off', // Tools before the loop like Sorter and Grid on or off.
110: 'tools_bottom' => 'off', // Tools after the loop like pagination on or off.
111: 'pagination' => 'on', // Enable or disable pagination.
112: 'template' => '', // Listing template, for example card, slim or table, as in [listing].
113: 'class' => '', // Extra wrapper class.
114: 'title' => 'off', // Show the archive or search title.
115: 'preview_limit' => '', // Listings to show in a page builder editor. Default: Settings > Reading.
116: 'instance_id' => '1', // Matches the sort dropdown when the shortcode shows the latest listings.
117: ),
118: $atts
119: );
120:
121: // $preview: the shortcode runs its own query (latest listings) instead of the page's.
122: $preview = false;
123:
124: if ( epl_is_listing_results_page() ) {
125: global $wp_query;
126: $query = $wp_query;
127: } elseif ( epl_is_page_builder_preview() || is_singular() ) {
128: // A page builder editor, or a normal page or post: show the latest listings, with paging.
129: // Other archives (blog, author, other post types) stay empty: a site-wide archive
130: // template with this shortcode must not fill them with listings.
131: $preview = true;
132: $paged = max( 1, (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) );
133: $args = array(
134: 'post_type' => array_keys( epl_get_active_post_types() ),
135: 'post_status' => 'publish',
136: 'posts_per_page' => epl_is_page_builder_preview() && '' !== $attributes['preview_limit'] ? (int) $attributes['preview_limit'] : (int) get_option( 'posts_per_page' ),
137: 'paged' => $paged,
138: 'instance_id' => sanitize_text_field( $attributes['instance_id'] ),
139: );
140: $query = new WP_Query( epl_add_orderby_args( $args, 'shortcode', 'listing_results' ) );
141: } else {
142: return '';
143: }
144:
145: $template = str_replace( '_', '-', sanitize_key( $attributes['template'] ) );
146: $wrapper_class = 'epl-shortcode-listing-results ' . epl_template_class( $template, 'archive' );
147: if ( '' !== $attributes['class'] ) {
148: $wrapper_class .= ' ' . $attributes['class'];
149: }
150:
151: ob_start();
152:
153: if ( $query->have_posts() ) :
154: ?>
155:
156: <?php if ( 'on' === $attributes['title'] ) : ?>
157: <header class="archive-header entry-header loop-header">
158: <h4 class="archive-title loop-title">
159: <?php
160: if ( $preview ) {
161: // The archive title steps through the page's main query, not this one.
162: esc_html_e( 'Listings', 'easy-property-listings' );
163: } else {
164: do_action( 'epl_the_archive_title' );
165: }
166: ?>
167: </h4>
168: </header>
169: <?php endif; ?>
170:
171: <div class="<?php echo esc_attr( $wrapper_class ); ?>">
172: <?php
173: if ( 'on' === $attributes['tools_top'] ) {
174: do_action( 'epl_property_loop_start', $attributes );
175: }
176: ?>
177:
178: <?php
179: while ( $query->have_posts() ) :
180: $query->the_post();
181: if ( '' === $template ) {
182: do_action( 'epl_property_blog' );
183: } else {
184: epl_property_blog( $template );
185: }
186: endwhile;
187: ?>
188:
189: <?php
190: if ( 'on' === $attributes['tools_bottom'] ) {
191: do_action( 'epl_property_loop_end' );
192: }
193: ?>
194: </div>
195:
196: <div class="loop-footer">
197: <!-- Previous/Next page navigation -->
198: <div class="loop-utility clearfix">
199: <?php
200: if ( 'on' === $attributes['pagination'] ) {
201: if ( $preview ) {
202: do_action( 'epl_pagination', array( 'query' => $query ) );
203: } else {
204: do_action( 'epl_pagination' );
205: }
206: }
207: ?>
208: </div>
209: </div>
210:
211: <?php else : ?>
212:
213: <?php do_action( 'epl_property_search_not_found' ); ?>
214:
215: <?php
216: endif;
217:
218: if ( $preview ) {
219: wp_reset_postdata();
220: }
221:
222: return ob_get_clean();
223: }
224: add_shortcode( 'listing_results', 'epl_listing_results_loop_callback' );
225: