1: <?php
2: /**
3: * Elementor Free EPL Listings Widget.
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor/Widgets/Archive
7: * @since 3.6.0
8: */
9:
10: if ( ! defined( 'ABSPATH' ) ) {
11: exit;
12: }
13:
14: /** Render EPL-native listing templates when Elementor Loop Grid is unavailable. */
15: class EPL_Elementor_Listings extends \Elementor\Widget_Base {
16: use EPL_Elementor_Dynamic_Widget;
17:
18: /**
19: * Widget name.
20: *
21: * @since 3.6.0 New: Elementor widgets (beta).
22: */
23: public function get_name() {
24: return 'epl-listings';
25: }
26:
27: /**
28: * Widget title shown in the Elementor panel.
29: *
30: * @since 3.6.0 New: Elementor widgets (beta).
31: */
32: public function get_title() {
33: return esc_html__( 'EPL Listings', 'easy-property-listings' );
34: }
35:
36: /**
37: * Widget icon.
38: *
39: * @since 3.6.0 New: Elementor widgets (beta).
40: */
41: public function get_icon() {
42: return 'eicon-posts-grid';
43: }
44:
45: /**
46: * Widget categories.
47: *
48: * @since 3.6.0 New: Elementor widgets (beta).
49: */
50: public function get_categories() {
51: return array( 'epl-archive' );
52: }
53:
54: /**
55: * Search keywords for the Elementor panel.
56: *
57: * @since 3.6.0 New: Elementor widgets (beta).
58: */
59: public function get_keywords() {
60: return array( 'listing', 'grid', 'property', 'archive', 'template', 'epl' );
61: }
62:
63: /**
64: * Register the widget controls.
65: *
66: * @since 3.6.0 New: Elementor widgets (beta).
67: */
68: protected function register_controls() {
69: $this->start_controls_section(
70: 'section_query',
71: array(
72: 'label' => esc_html__( 'Query', 'easy-property-listings' ),
73: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
74: )
75: );
76:
77: $this->add_control(
78: 'query_source',
79: array(
80: 'label' => esc_html__( 'Source', 'easy-property-listings' ),
81: 'type' => \Elementor\Controls_Manager::SELECT,
82: 'default' => 'custom',
83: 'options' => array(
84: 'custom' => esc_html__( 'Custom Query', 'easy-property-listings' ),
85: 'current_archive' => esc_html__( 'Current Archive Query', 'easy-property-listings' ),
86: ),
87: )
88: );
89:
90: $this->add_control(
91: 'post_type',
92: array(
93: 'label' => esc_html__( 'Post Types', 'easy-property-listings' ),
94: 'type' => \Elementor\Controls_Manager::SELECT2,
95: 'multiple' => true,
96: 'label_block' => true,
97: 'options' => epl_get_active_post_types(),
98: 'default' => array( 'property' ),
99: 'condition' => array( 'query_source' => 'custom' ),
100: )
101: );
102:
103: $status_options = EPL_Elementor::get_status_options();
104: unset( $status_options[''], $status_options['withdrawn'], $status_options['offmarket'] );
105: $this->add_control(
106: 'status',
107: array(
108: 'label' => esc_html__( 'Listing Status', 'easy-property-listings' ),
109: 'type' => \Elementor\Controls_Manager::SELECT2,
110: 'multiple' => true,
111: 'label_block' => true,
112: 'options' => $status_options,
113: 'default' => array( 'current' ),
114: 'description' => esc_html__( 'Leave empty to include all statuses EPL permits on the front end.', 'easy-property-listings' ),
115: 'condition' => array( 'query_source' => 'custom' ),
116: )
117: );
118:
119: $this->add_control(
120: 'limit',
121: array(
122: 'label' => esc_html__( 'Listings Per Page', 'easy-property-listings' ),
123: 'type' => \Elementor\Controls_Manager::NUMBER,
124: 'min' => 1,
125: 'max' => 100,
126: 'default' => 10,
127: 'condition' => array( 'query_source' => 'custom' ),
128: )
129: );
130:
131: $this->add_control(
132: 'orderby',
133: array(
134: 'label' => esc_html__( 'Order By', 'easy-property-listings' ),
135: 'type' => \Elementor\Controls_Manager::SELECT,
136: 'default' => 'date',
137: 'options' => array(
138: 'date' => esc_html__( 'Date', 'easy-property-listings' ),
139: 'price' => esc_html__( 'Price', 'easy-property-listings' ),
140: 'title' => esc_html__( 'Title', 'easy-property-listings' ),
141: 'rand' => esc_html__( 'Random', 'easy-property-listings' ),
142: 'menu_order' => esc_html__( 'Menu Order', 'easy-property-listings' ),
143: ),
144: 'condition' => array( 'query_source' => 'custom' ),
145: )
146: );
147:
148: $this->add_control(
149: 'order',
150: array(
151: 'label' => esc_html__( 'Order', 'easy-property-listings' ),
152: 'type' => \Elementor\Controls_Manager::SELECT,
153: 'default' => 'DESC',
154: 'options' => array(
155: 'DESC' => esc_html__( 'Descending', 'easy-property-listings' ),
156: 'ASC' => esc_html__( 'Ascending', 'easy-property-listings' ),
157: ),
158: 'condition' => array( 'query_source' => 'custom' ),
159: )
160: );
161: $this->end_controls_section();
162:
163: $this->start_controls_section(
164: 'section_layout',
165: array(
166: 'label' => esc_html__( 'Layout', 'easy-property-listings' ),
167: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
168: )
169: );
170: $this->add_control(
171: 'renderer',
172: array(
173: 'label' => esc_html__( 'Renderer', 'easy-property-listings' ),
174: 'type' => \Elementor\Controls_Manager::SELECT,
175: 'default' => 'epl',
176: 'options' => array(
177: 'epl' => esc_html__( 'EPL Template', 'easy-property-listings' ),
178: 'card' => esc_html__( 'EPL Card Template', 'easy-property-listings' ),
179: ),
180: )
181: );
182:
183: $this->add_control(
184: 'template',
185: array(
186: 'label' => esc_html__( 'EPL Template', 'easy-property-listings' ),
187: 'type' => \Elementor\Controls_Manager::SELECT,
188: 'default' => 'default',
189: 'options' => array(
190: 'default' => esc_html__( 'Default', 'easy-property-listings' ),
191: 'card' => esc_html__( 'Card', 'easy-property-listings' ),
192: 'slim' => esc_html__( 'Slim', 'easy-property-listings' ),
193: 'table' => esc_html__( 'Table', 'easy-property-listings' ),
194: 'table-open' => esc_html__( 'Table with Inspection', 'easy-property-listings' ),
195: ),
196: 'condition' => array( 'renderer' => 'epl' ),
197: )
198: );
199:
200: $this->add_control(
201: 'card_template_id',
202: array(
203: 'label' => esc_html__( 'Listing Card', 'easy-property-listings' ),
204: 'type' => \Elementor\Controls_Manager::SELECT,
205: 'options' => $this->get_elementor_card_templates(),
206: 'default' => '',
207: 'description' => esc_html__( 'Only EPL Listing Card templates can be used here.', 'easy-property-listings' ),
208: 'condition' => array( 'renderer' => 'card' ),
209: )
210: );
211:
212: $this->add_control(
213: 'card_template_help',
214: array(
215: 'type' => \Elementor\Controls_Manager::RAW_HTML,
216: 'raw' => sprintf(
217: '<p style="color:#666;font-size:12px;">%s <a href="%s" target="_blank">%s</a></p>',
218: esc_html__( 'No template?', 'easy-property-listings' ),
219: esc_url( \Elementor\Plugin::$instance->documents->get_create_new_post_url( 'elementor_library', EPL_Elementor_Loop_Card_Document::TYPE ) ),
220: esc_html__( 'Create an EPL Listing Card', 'easy-property-listings' )
221: ),
222: 'content_classes' => 'elementor-panel-alert',
223: 'condition' => array( 'renderer' => 'card' ),
224: )
225: );
226: $this->add_responsive_control(
227: 'columns',
228: array(
229: 'label' => esc_html__( 'Columns', 'easy-property-listings' ),
230: 'type' => \Elementor\Controls_Manager::SELECT,
231: 'default' => '3',
232: 'tablet_default' => '2',
233: 'mobile_default' => '1',
234: 'options' => array( '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6' ),
235: 'selectors' => array( '{{WRAPPER}} .epl-elementor-listings-grid' => '--epl-elementor-listings-columns: {{VALUE}};' ),
236: )
237: );
238: $this->add_control(
239: 'show_pagination',
240: array(
241: 'label' => esc_html__( 'Pagination', 'easy-property-listings' ),
242: 'type' => \Elementor\Controls_Manager::SWITCHER,
243: 'label_on' => esc_html__( 'Show', 'easy-property-listings' ),
244: 'label_off' => esc_html__( 'Hide', 'easy-property-listings' ),
245: 'return_value' => 'yes',
246: 'default' => 'yes',
247: )
248: );
249: $this->end_controls_section();
250:
251: $this->start_controls_section(
252: 'section_grid_style',
253: array(
254: 'label' => esc_html__( 'Grid', 'easy-property-listings' ),
255: 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
256: )
257: );
258: $this->add_responsive_control(
259: 'column_gap',
260: array(
261: 'label' => esc_html__( 'Column Gap', 'easy-property-listings' ),
262: 'type' => \Elementor\Controls_Manager::SLIDER,
263: 'size_units' => array( 'px', 'em', 'rem' ),
264: 'default' => array( 'size' => 24, 'unit' => 'px' ),
265: 'selectors' => array( '{{WRAPPER}} .epl-elementor-listings-grid' => 'column-gap: {{SIZE}}{{UNIT}};' ),
266: )
267: );
268: $this->add_responsive_control(
269: 'row_gap',
270: array(
271: 'label' => esc_html__( 'Row Gap', 'easy-property-listings' ),
272: 'type' => \Elementor\Controls_Manager::SLIDER,
273: 'size_units' => array( 'px', 'em', 'rem' ),
274: 'default' => array( 'size' => 32, 'unit' => 'px' ),
275: 'selectors' => array( '{{WRAPPER}} .epl-elementor-listings-grid' => 'row-gap: {{SIZE}}{{UNIT}};' ),
276: )
277: );
278: $this->end_controls_section();
279: }
280:
281: /**
282: * Render the widget output.
283: *
284: * @since 3.6.0 New: Elementor widgets (beta).
285: */
286: protected function render() {
287: $settings = $this->get_settings_for_display();
288: $query = $this->get_listings_query( $settings );
289:
290: if ( ! $query->have_posts() ) {
291: if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
292: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'No listings found for this query.', 'easy-property-listings' ) . '</div>';
293: } else {
294: do_action( 'epl_shortcode_results_message', 'default' );
295: }
296: return;
297: }
298:
299: global $property;
300: $original_property = isset( $property ) ? $property : null;
301: $use_card = 'card' === $settings['renderer'] && ! empty( $settings['card_template_id'] );
302: $card_template_id = $use_card ? absint( $settings['card_template_id'] ) : 0;
303: $template = ! empty( $settings['template'] ) ? sanitize_key( $settings['template'] ) : 'default';
304:
305: echo '<div class="epl-elementor-listings">';
306: echo '<div class="epl-elementor-listings-grid epl-clearfix">';
307: while ( $query->have_posts() ) {
308: $query->the_post();
309: $property = new EPL_Property_Meta( get_post() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- EPL template context restored below.
310: echo '<div class="epl-elementor-listings-item">';
311: // A chosen card can be deleted after the fact; fall back to the PHP
312: // template rather than rendering an empty listing.
313: if ( ! $use_card || ! EPL_Elementor_Template_Router::render_card( $card_template_id ) ) {
314: epl_property_blog( $template );
315: }
316: echo '</div>';
317: }
318: echo '</div>';
319:
320: if ( 'yes' === $settings['show_pagination'] ) {
321: epl_pagination( array( 'query' => $query ) );
322: }
323: echo '</div>';
324:
325: wp_reset_postdata();
326: $property = $original_property; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restore caller context.
327: }
328:
329: /** Get published EPL Listing Card documents. */
330: /**
331: * Get elementor card templates.
332: *
333: * @since 3.6.0 New: Elementor templates for free Elementor (beta).
334: */
335: private function get_elementor_card_templates() {
336: $templates = array(
337: '' => esc_html__( '— Select Listing Card —', 'easy-property-listings' ),
338: );
339:
340: $template_query = new \WP_Query(
341: array(
342: 'post_type' => 'elementor_library',
343: 'posts_per_page' => -1,
344: 'post_status' => 'publish',
345: 'orderby' => 'title',
346: 'order' => 'ASC',
347: 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- elementor_library is small; matches existing EPL Elementor queries.
348: array(
349: 'key' => '_elementor_template_type',
350: 'value' => EPL_Elementor_Loop_Card_Document::TYPE,
351: ),
352: ),
353: )
354: );
355:
356: foreach ( $template_query->posts as $post ) {
357: $templates[ $post->ID ] = $post->post_title;
358: }
359:
360: return $templates;
361: }
362:
363: /** Build either the archive query or an EPL-aware custom query. */
364: /**
365: * Get listings query.
366: *
367: * @since 3.6.0 New: Elementor widgets (beta).
368: */
369: private function get_listings_query( $settings ) {
370: if ( 'current_archive' === $settings['query_source'] && isset( $GLOBALS['wp_query'] ) && $GLOBALS['wp_query'] instanceof WP_Query ) {
371: $query = clone $GLOBALS['wp_query'];
372: $query->rewind_posts();
373:
374: // Theme Builder preview requests can retain the template-library main
375: // query. Fall back to listings so the editor never shows template posts.
376: $first_post = ! empty( $query->posts[0] ) ? $query->posts[0] : null;
377: if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() || ( $first_post instanceof WP_Post && is_epl_post( $first_post->post_type ) ) ) {
378: return $query;
379: }
380: }
381:
382: $post_types = ! empty( $settings['post_type'] ) ? (array) $settings['post_type'] : array_keys( epl_get_active_post_types() );
383: $post_types = array_values( array_intersect( array_map( 'sanitize_key', $post_types ), array_keys( epl_get_active_post_types() ) ) );
384: if ( empty( $post_types ) ) {
385: $post_types = array_keys( epl_get_active_post_types() );
386: }
387: $statuses = ! empty( $settings['status'] ) ? array_filter( array_map( 'sanitize_key', (array) $settings['status'] ) ) : array();
388: $paged = max( 1, absint( get_query_var( 'paged' ) ), absint( get_query_var( 'page' ) ) );
389: $args = array(
390: 'post_type' => $post_types,
391: 'posts_per_page' => max( 1, absint( $settings['limit'] ) ),
392: 'paged' => $paged,
393: 'order' => 'ASC' === $settings['order'] ? 'ASC' : 'DESC',
394: 'orderby' => sanitize_key( $settings['orderby'] ),
395: 'meta_query' => array(),
396: );
397:
398: if ( 'price' === $args['orderby'] && 1 === count( $post_types ) ) {
399: $args['orderby'] = 'meta_value_num';
400: $args['meta_key'] = is_epl_rental_post( reset( $post_types ) ) ? 'property_rent' : 'property_price';
401: } elseif ( 'price' === $args['orderby'] ) {
402: // EPL synchronizes every listing type to this normalized numeric key,
403: // allowing sale, rental, rural and commercial results to sort together.
404: $args['orderby'] = 'meta_value_num';
405: $args['meta_key'] = 'property_price_global';
406: }
407:
408: if ( $statuses ) {
409: $args['meta_query'][] = array( 'key' => 'property_status', 'value' => $statuses, 'compare' => 'IN' );
410: } else {
411: $args['meta_query'][] = array(
412: 'relation' => 'OR',
413: array( 'key' => 'property_status', 'value' => epl_hide_listing_statuses(), 'compare' => 'NOT IN' ),
414: array( 'key' => 'property_status', 'compare' => 'NOT EXISTS' ),
415: );
416: }
417:
418: $args = apply_filters( 'epl_elementor_listings_query_args', $args, $settings );
419: return new WP_Query( $args );
420: }
421: }
422: