1: <?php
2: /**
3: * Elementor Listing Results Widget
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor/Widgets/Archive
7: * @since 3.6.0
8: */
9:
10: // Exit if accessed directly.
11: if ( ! defined( 'ABSPATH' ) ) {
12: exit;
13: }
14:
15: /**
16: * EPL_Elementor_Listing_Results Class
17: *
18: * Thin Elementor wrapper around `[listing_results]`. Iterates WordPress's
19: * real main query, so it is the building block for an "Archive Page" —
20: * drop it (with EPL Listing Search and EPL Pagination) on any page assigned
21: * as the listings archive page, and it renders the actual archive, search
22: * or taxonomy results with real pagination.
23: *
24: * @since 3.6.0
25: */
26: class EPL_Elementor_Listing_Results extends \Elementor\Widget_Base {
27: use EPL_Elementor_Dynamic_Widget;
28:
29: /**
30: * Get widget name.
31: *
32: * @return string
33: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
34: */
35: public function get_name() {
36: return 'epl-listing-results';
37: }
38:
39: /**
40: * Get widget title.
41: *
42: * Renamed from "EPL Listing Results" to "EPL Archive Results" for
43: * clarity — get_name() (the internal widgetType, e.g. in already-saved
44: * page JSON) is left unchanged so existing pages keep rendering.
45: *
46: * @return string
47: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
48: */
49: public function get_title() {
50: return esc_html__( 'Listings Results', 'easy-property-listings' );
51: }
52:
53: /**
54: * Get widget icon.
55: *
56: * @return string
57: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
58: */
59: public function get_icon() {
60: return 'eicon-archive-title';
61: }
62:
63: /**
64: * Get widget categories.
65: *
66: * @return array
67: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
68: */
69: public function get_categories() {
70: return array( 'epl-archive' );
71: }
72:
73: /**
74: * Get widget keywords.
75: *
76: * @return array
77: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
78: */
79: public function get_keywords() {
80: return array( 'listing', 'results', 'archive', 'query', 'loop', 'property', 'epl' );
81: }
82:
83: /**
84: * Register widget controls.
85: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
86: */
87: protected function register_controls() {
88: $this->start_controls_section(
89: 'section_content',
90: array(
91: 'label' => esc_html__( 'Listing Results', 'easy-property-listings' ),
92: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
93: )
94: );
95:
96: $this->add_control(
97: 'results_notice',
98: array(
99: 'type' => \Elementor\Controls_Manager::RAW_HTML,
100: 'raw' => esc_html__( 'Shows the archive, search or location listings. Use it on your Listings Archive Page.', 'easy-property-listings' ),
101: 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
102: )
103: );
104:
105: $this->add_control(
106: 'card_template_id',
107: array(
108: 'label' => esc_html__( 'Listing Card', 'easy-property-listings' ),
109: 'type' => \Elementor\Controls_Manager::SELECT2,
110: 'label_block' => true,
111: 'options' => $this->get_card_template_options(),
112: 'default' => (string) EPL_Elementor_Template_Seeder::get_default_card_id(),
113: 'description' => esc_html__( 'The card design for this page. Edit cards in Elementor > Templates.', 'easy-property-listings' ),
114: )
115: );
116:
117: $this->add_control(
118: 'tools_top',
119: array(
120: 'label' => esc_html__( 'Show Tools Above Results', 'easy-property-listings' ),
121: 'type' => \Elementor\Controls_Manager::SWITCHER,
122: 'label_on' => esc_html__( 'Yes', 'easy-property-listings' ),
123: 'label_off' => esc_html__( 'No', 'easy-property-listings' ),
124: 'return_value' => 'yes',
125: 'default' => '',
126: 'description' => esc_html__( 'Sorter and grid/list view toggle.', 'easy-property-listings' ),
127: )
128: );
129:
130: $this->add_control(
131: 'tools_bottom',
132: array(
133: 'label' => esc_html__( 'Show Tools Below Results', 'easy-property-listings' ),
134: 'type' => \Elementor\Controls_Manager::SWITCHER,
135: 'label_on' => esc_html__( 'Yes', 'easy-property-listings' ),
136: 'label_off' => esc_html__( 'No', 'easy-property-listings' ),
137: 'return_value' => 'yes',
138: 'default' => '',
139: )
140: );
141:
142: $this->add_control(
143: 'pagination',
144: array(
145: 'label' => esc_html__( 'Show Pagination', 'easy-property-listings' ),
146: 'type' => \Elementor\Controls_Manager::SWITCHER,
147: 'label_on' => esc_html__( 'Yes', 'easy-property-listings' ),
148: 'label_off' => esc_html__( 'No', 'easy-property-listings' ),
149: 'return_value' => 'yes',
150: 'default' => 'yes',
151: )
152: );
153:
154: $this->end_controls_section();
155:
156: $this->start_controls_section(
157: 'section_layout',
158: array(
159: 'label' => esc_html__( 'Grid Layout', 'easy-property-listings' ),
160: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
161: )
162: );
163:
164: $this->add_control(
165: 'grid_mode',
166: array(
167: 'label' => esc_html__( 'Layout', 'easy-property-listings' ),
168: 'type' => \Elementor\Controls_Manager::SELECT,
169: 'default' => 'columns',
170: 'options' => array(
171: 'columns' => esc_html__( 'Fixed Columns', 'easy-property-listings' ),
172: 'auto' => esc_html__( 'Auto-Fit by Card Width', 'easy-property-listings' ),
173: ),
174: 'selectors' => array(
175: '{{WRAPPER}} .epl-template-blog' => 'display: grid;',
176: '{{WRAPPER}} .epl-template-blog > *' => 'min-width: 0; box-sizing: border-box;',
177: ),
178: )
179: );
180:
181: $this->add_responsive_control(
182: 'grid_columns',
183: array(
184: 'label' => esc_html__( 'Columns', 'easy-property-listings' ),
185: 'type' => \Elementor\Controls_Manager::SELECT,
186: 'default' => '3',
187: 'tablet_default' => '2',
188: 'mobile_default' => '1',
189: 'options' => array(
190: '1' => '1',
191: '2' => '2',
192: '3' => '3',
193: '4' => '4',
194: '5' => '5',
195: '6' => '6',
196: ),
197: 'condition' => array( 'grid_mode' => 'columns' ),
198: 'selectors' => array(
199: '{{WRAPPER}} .epl-template-blog' => 'grid-template-columns: repeat({{VALUE}}, minmax(0, 1fr));',
200: ),
201: )
202: );
203:
204: $this->add_responsive_control(
205: 'card_min_width',
206: array(
207: 'label' => esc_html__( 'Minimum Card Width', 'easy-property-listings' ),
208: 'type' => \Elementor\Controls_Manager::SLIDER,
209: 'size_units' => array( 'px' ),
210: 'range' => array(
211: 'px' => array(
212: 'min' => 150,
213: 'max' => 600,
214: ),
215: ),
216: 'default' => array(
217: 'size' => 280,
218: 'unit' => 'px',
219: ),
220: 'condition' => array( 'grid_mode' => 'auto' ),
221: 'selectors' => array(
222: '{{WRAPPER}} .epl-template-blog' => 'grid-template-columns: repeat(auto-fill, minmax({{SIZE}}{{UNIT}}, 1fr));',
223: ),
224: )
225: );
226:
227: $this->add_responsive_control(
228: 'grid_gap',
229: array(
230: 'label' => esc_html__( 'Gap', 'easy-property-listings' ),
231: 'type' => \Elementor\Controls_Manager::GAPS,
232: 'size_units' => array( 'px', '%', 'em' ),
233: 'default' => array(
234: 'row' => '32',
235: 'column' => '24',
236: 'unit' => 'px',
237: 'isLinked' => false,
238: ),
239: 'selectors' => array(
240: '{{WRAPPER}} .epl-template-blog' => 'gap: {{ROW}}{{UNIT}} {{COLUMN}}{{UNIT}};',
241: ),
242: )
243: );
244:
245: $this->end_controls_section();
246: }
247:
248: /**
249: * Published EPL Listing Card documents, keyed by post ID, plus a "Site
250: * Default" option meaning "defer to whichever card is marked default".
251: *
252: * @return array
253: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
254: */
255: private function get_card_template_options() {
256: $options = array(
257: '' => esc_html__( 'EPL Built-in Card', 'easy-property-listings' ),
258: );
259:
260: $cards = get_posts(
261: array(
262: 'post_type' => 'elementor_library',
263: 'post_status' => 'publish',
264: 'posts_per_page' => -1,
265: 'orderby' => 'title',
266: 'order' => 'ASC',
267: 'meta_key' => '_elementor_template_type', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- elementor_library is small; matches existing EPL Elementor queries.
268: 'meta_value' => EPL_Elementor_Loop_Card_Document::TYPE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
269: )
270: );
271:
272: $bundled_default = EPL_Elementor_Template_Seeder::get_default_card_id();
273:
274: foreach ( $cards as $card ) {
275: $options[ $card->ID ] = ( $card->ID === $bundled_default )
276: /* translators: %s: template name. */
277: ? sprintf( esc_html__( '%s (Default)', 'easy-property-listings' ), $card->post_title )
278: : $card->post_title;
279: }
280:
281: return $options;
282: }
283:
284: /**
285: * Render widget output.
286: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
287: */
288: protected function render() {
289: if ( EPL_Elementor::is_editor() ) {
290: $this->render_editor_preview();
291: return;
292: }
293:
294: $settings = $this->get_settings_for_display();
295: $atts = array(
296: 'tools_top' => ! empty( $settings['tools_top'] ) ? 'on' : 'off',
297: 'tools_bottom' => ! empty( $settings['tools_bottom'] ) ? 'on' : 'off',
298: 'pagination' => ! empty( $settings['pagination'] ) ? 'on' : 'off',
299: );
300:
301: $card_override = ! empty( $settings['card_template_id'] ) ? absint( $settings['card_template_id'] ) : 0;
302: if ( $card_override ) {
303: EPL_Elementor_Template_Router::begin_card_override( $card_override );
304: }
305:
306: echo epl_listing_results_loop_callback( $atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in the shortcode callback.
307:
308: if ( $card_override ) {
309: EPL_Elementor_Template_Router::end_card_override();
310: }
311: }
312:
313: /**
314: * Editor canvas has no real archive/search/taxonomy query, so render a
315: * small, clearly-labelled sample loop instead of nothing.
316: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
317: */
318: private function render_editor_preview() {
319: echo '<div class="epl-elementor-listing-results-preview">';
320: echo '<p class="epl-elementor-editor-notice">' . esc_html__( 'Preview only — shows the real archive, search or taxonomy results on the front end.', 'easy-property-listings' ) . '</p>';
321:
322: $sample_posts = get_posts(
323: array(
324: 'post_type' => array_keys( epl_get_active_post_types() ),
325: 'post_status' => 'publish',
326: 'posts_per_page' => 6,
327: )
328: );
329:
330: if ( empty( $sample_posts ) ) {
331: echo '<p>' . esc_html__( 'No published listings found to preview.', 'easy-property-listings' ) . '</p>';
332: echo '</div>';
333: return;
334: }
335:
336: $settings = $this->get_settings_for_display();
337: $card_override = ! empty( $settings['card_template_id'] ) ? absint( $settings['card_template_id'] ) : 0;
338: if ( $card_override ) {
339: EPL_Elementor_Template_Router::begin_card_override( $card_override );
340: }
341:
342: global $post, $property;
343: $original_post = $post;
344: $original_property = isset( $property ) ? $property : null;
345:
346: echo '<div class="epl-template-blog">';
347: foreach ( $sample_posts as $sample_post ) {
348: $post = $sample_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restored below.
349: $property = new EPL_Property_Meta( $sample_post ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restored below.
350: setup_postdata( $post );
351: do_action( 'epl_property_blog' );
352: }
353: echo '</div>';
354:
355: if ( $card_override ) {
356: EPL_Elementor_Template_Router::end_card_override();
357: }
358:
359: $post = $original_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restore the editor's own post.
360: $property = $original_property; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restore.
361: if ( $post instanceof WP_Post ) {
362: setup_postdata( $post );
363: } else {
364: wp_reset_postdata();
365: }
366:
367: echo '</div>';
368: }
369: }
370: