1: <?php
2: /**
3: * Elementor Listing Advanced 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: /** Elementor interface for the canonical [listing_advanced] implementation. */
15: class EPL_Elementor_Listing_Advanced 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-listing-advanced';
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__( 'Listings Loop', '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-group';
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', 'advanced', 'property', 'query', 'shortcode', '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( 'section_query', array( 'label' => esc_html__( 'Query', 'easy-property-listings' ) ) );
70: $this->add_control(
71: 'post_type',
72: array(
73: 'label' => esc_html__( 'Post Types', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2,
74: 'options' => epl_get_active_post_types(), 'multiple' => true, 'label_block' => true,
75: 'default' => array_keys( epl_get_active_post_types() ),
76: )
77: );
78: $status_options = EPL_Elementor::get_status_options();
79: unset( $status_options[''], $status_options['withdrawn'], $status_options['offmarket'] );
80: $this->add_control(
81: 'status',
82: array(
83: 'label' => esc_html__( 'Statuses', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2,
84: 'options' => $status_options, 'multiple' => true, 'label_block' => true,
85: 'default' => array( 'current', 'sold', 'leased' ),
86: )
87: );
88: $this->add_control(
89: 'commercial_listing_type',
90: array(
91: 'label' => esc_html__( 'Commercial Listing Type', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2,
92: 'options' => epl_get_property_com_listing_type_opts(), 'multiple' => true, 'label_block' => true,
93: )
94: );
95: $this->add_control( 'limit', array( 'label' => esc_html__( 'Listings Per Page', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 1, 'max' => 100, 'default' => 12 ) );
96: $this->add_control( 'offset', array( 'label' => esc_html__( 'Offset', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'default' => 0 ) );
97: $this->add_control( 'post__in', array( 'label' => esc_html__( 'Include Listing IDs', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Comma-separated post IDs.', 'easy-property-listings' ) ) );
98: $this->add_control( 'post__not_in', array( 'label' => esc_html__( 'Exclude Listing IDs', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Comma-separated post IDs.', 'easy-property-listings' ) ) );
99: $this->end_controls_section();
100:
101: $this->start_controls_section( 'section_filters', array( 'label' => esc_html__( 'Filters', 'easy-property-listings' ) ) );
102: $this->add_control(
103: 'location_id',
104: array(
105: 'label' => epl_tax_location_label(), 'type' => \Elementor\Controls_Manager::SELECT2,
106: 'options' => $this->get_term_options( 'location' ), 'multiple' => true, 'label_block' => true,
107: )
108: );
109: $this->add_control(
110: 'feature_id',
111: array(
112: 'label' => esc_html__( 'Features', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2,
113: 'options' => $this->get_term_options( 'tax_feature' ), 'multiple' => true, 'label_block' => true,
114: )
115: );
116: $this->add_control( 'location', array( 'label' => esc_html__( 'Location Slugs', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Optional comma-separated slugs.', 'easy-property-listings' ) ) );
117: $this->add_control( 'feature', array( 'label' => esc_html__( 'Feature Slugs', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Optional comma-separated slugs.', 'easy-property-listings' ) ) );
118: $this->add_control( 'author', array( 'label' => esc_html__( 'WordPress Authors', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2, 'options' => $this->get_author_options(), 'multiple' => true, 'label_block' => true ) );
119: $this->add_control( 'agent', array( 'label' => esc_html__( 'Listing Agent Usernames', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Comma-separated EPL agent usernames.', 'easy-property-listings' ) ) );
120: $this->add_control( 'featured', $this->switcher_args( esc_html__( 'Featured Only', 'easy-property-listings' ) ) );
121: $this->add_control( 'open_house', $this->switcher_args( esc_html__( 'Open for Inspection Only', 'easy-property-listings' ) ) );
122: $this->add_control( 'auction', $this->switcher_args( esc_html__( 'Auction Only', 'easy-property-listings' ) ) );
123: $this->end_controls_section();
124:
125: $this->start_controls_section( 'section_dynamic_filters', array( 'label' => esc_html__( 'Dynamic Meta Filters', 'easy-property-listings' ) ) );
126: $this->add_control(
127: 'dynamic_filters_note',
128: array(
129: 'type' => \Elementor\Controls_Manager::RAW_HTML,
130: 'raw' => esc_html__( 'Use a filter in Advanced Orderby Clauses as its meta key plus “_clause”.', 'easy-property-listings' ),
131: 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info',
132: )
133: );
134: $filter_repeater = new \Elementor\Repeater();
135: $filter_repeater->add_control(
136: 'meta_key',
137: array(
138: 'label' => esc_html__( 'Meta Key', 'easy-property-listings' ),
139: 'type' => \Elementor\Controls_Manager::TEXT,
140: 'label_block' => true,
141: 'placeholder' => 'property_bedrooms',
142: 'description' => esc_html__( 'Enter the meta key without the leading underscore.', 'easy-property-listings' ),
143: )
144: );
145: $filter_repeater->add_control(
146: 'compare',
147: array(
148: 'label' => esc_html__( 'Compare', 'easy-property-listings' ),
149: 'type' => \Elementor\Controls_Manager::SELECT,
150: 'default' => 'equal',
151: 'options' => array(
152: 'equal' => esc_html__( 'Equal', 'easy-property-listings' ),
153: 'min' => esc_html__( 'Minimum (>=)', 'easy-property-listings' ),
154: 'max' => esc_html__( 'Maximum (<=)', 'easy-property-listings' ),
155: 'not_equal' => esc_html__( 'Not Equal', 'easy-property-listings' ),
156: 'like' => esc_html__( 'Like', 'easy-property-listings' ),
157: 'not_like' => esc_html__( 'Not Like', 'easy-property-listings' ),
158: 'exists' => esc_html__( 'Exists', 'easy-property-listings' ),
159: 'not_exists' => esc_html__( 'Not Exists', 'easy-property-listings' ),
160: 'between' => esc_html__( 'Between', 'easy-property-listings' ),
161: 'not_between' => esc_html__( 'Not Between', 'easy-property-listings' ),
162: 'in' => esc_html__( 'In', 'easy-property-listings' ),
163: 'not_in' => esc_html__( 'Not In', 'easy-property-listings' ),
164: ),
165: )
166: );
167: $filter_repeater->add_control(
168: 'value',
169: array(
170: 'label' => esc_html__( 'Value', 'easy-property-listings' ),
171: 'type' => \Elementor\Controls_Manager::TEXT,
172: 'label_block' => true,
173: 'placeholder' => esc_html__( 'Use commas for Between and In comparisons', 'easy-property-listings' ),
174: 'condition' => array( 'compare!' => array( 'exists', 'not_exists' ) ),
175: )
176: );
177: $this->add_control(
178: 'dynamic_filters',
179: array(
180: 'label' => esc_html__( 'Meta Filters', 'easy-property-listings' ),
181: 'type' => \Elementor\Controls_Manager::REPEATER,
182: 'fields' => $filter_repeater->get_controls(),
183: 'default' => array(),
184: 'title_field' => '<# var key = (meta_key || "Meta filter").replace(/^_+/, ""); #>{{{ key }}} <small>{{{ compare || "equal" }}}</small>',
185: )
186: );
187: $this->end_controls_section();
188:
189: $this->start_controls_section( 'section_order', array( 'label' => esc_html__( 'Ordering', 'easy-property-listings' ) ) );
190: $this->add_control(
191: 'sortby',
192: array(
193: 'label' => esc_html__( 'Order By', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'date',
194: 'options' => array( 'date' => esc_html__( 'Date', 'easy-property-listings' ), 'price' => esc_html__( 'Price', 'easy-property-listings' ), 'rand' => esc_html__( 'Random', 'easy-property-listings' ), 'status' => esc_html__( 'Status', 'easy-property-listings' ) ),
195: )
196: );
197: $this->add_control( 'sort_order', array( 'label' => esc_html__( 'Direction', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'DESC', 'options' => array( 'DESC' => esc_html__( 'Descending', 'easy-property-listings' ), 'ASC' => esc_html__( 'Ascending', 'easy-property-listings' ) ) ) );
198: $this->add_control( 'orderby_clause', array( 'label' => esc_html__( 'Advanced Orderby Clauses', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'label_block' => true, 'description' => esc_html__( 'Comma-separated clause|direction pairs.', 'easy-property-listings' ) ) );
199: $this->end_controls_section();
200:
201: $this->start_controls_section( 'section_output', array( 'label' => esc_html__( 'Output', 'easy-property-listings' ) ) );
202: $renderers = array( 'epl' => esc_html__( 'EPL Template', 'easy-property-listings' ) );
203: $templates = $this->get_elementor_loop_templates();
204: if ( $templates ) {
205: $renderers['elementor'] = esc_html__( 'Elementor Loop / EPL Card Template', 'easy-property-listings' );
206: }
207: $this->add_control( 'renderer', array( 'label' => esc_html__( 'Renderer', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'epl', 'options' => $renderers ) );
208: $this->add_control(
209: 'template',
210: array(
211: 'label' => esc_html__( 'EPL Template', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => 'default', 'condition' => array( 'renderer' => 'epl' ),
212: 'options' => array( 'default' => esc_html__( 'Default', 'easy-property-listings' ), 'card' => esc_html__( 'Card', 'easy-property-listings' ), 'slim' => esc_html__( 'Slim', 'easy-property-listings' ), 'table' => esc_html__( 'Table', 'easy-property-listings' ), 'table-open' => esc_html__( 'Table with Inspection', 'easy-property-listings' ) ),
213: )
214: );
215: if ( $templates ) {
216: $this->add_control( 'elementor_template_id', array( 'label' => esc_html__( 'Loop / Card Template', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT2, 'options' => $templates, 'label_block' => true, 'condition' => array( 'renderer' => 'elementor' ) ) );
217: $this->add_control(
218: 'list_template_id',
219: array(
220: 'label' => esc_html__( 'List View Loop Template', 'easy-property-listings' ),
221: 'type' => \Elementor\Controls_Manager::SELECT2,
222: 'options' => $templates,
223: 'label_block' => true,
224: 'description' => esc_html__( 'Optional. Used in List view. Needs Archive Tools on.', 'easy-property-listings' ),
225: 'condition' => array( 'renderer' => 'elementor' ),
226: )
227: );
228: $this->add_control(
229: 'editor_preview_view',
230: array(
231: 'label' => esc_html__( 'Editor Preview View', 'easy-property-listings' ),
232: 'type' => \Elementor\Controls_Manager::CHOOSE,
233: 'default' => 'grid',
234: 'options' => array(
235: 'grid' => array(
236: 'title' => esc_html__( 'Grid', 'easy-property-listings' ),
237: 'icon' => 'eicon-gallery-grid',
238: ),
239: 'list' => array(
240: 'title' => esc_html__( 'List', 'easy-property-listings' ),
241: 'icon' => 'eicon-editor-list-ul',
242: ),
243: ),
244: 'toggle' => false,
245: 'description' => esc_html__( 'Preview only — choose which template shows on the editor canvas. The live site uses the visitor’s grid/list selection.', 'easy-property-listings' ),
246: 'condition' => array(
247: 'renderer' => 'elementor',
248: 'list_template_id!' => '',
249: ),
250: )
251: );
252: }
253: $this->add_control( 'tools_top', $this->switcher_args( esc_html__( 'Archive Tools Above', 'easy-property-listings' ), 'on', 'off' ) );
254: $this->add_control( 'tools_bottom', $this->switcher_args( esc_html__( 'Archive Tools Below', 'easy-property-listings' ), 'on', 'off' ) );
255: $this->add_control(
256: 'pagination',
257: array(
258: 'label' => esc_html__( 'Pagination', 'easy-property-listings' ),
259: 'type' => \Elementor\Controls_Manager::SELECT,
260: 'default' => 'epl',
261: 'options' => array(
262: 'off' => esc_html__( 'None', 'easy-property-listings' ),
263: 'epl' => esc_html__( 'EPL', 'easy-property-listings' ),
264: ),
265: )
266: );
267: $this->add_control(
268: 'epl_pagination_style',
269: array(
270: 'label' => esc_html__( 'EPL Pagination Type', 'easy-property-listings' ),
271: 'type' => \Elementor\Controls_Manager::SELECT,
272: 'default' => 'default',
273: 'options' => array(
274: 'fancy' => esc_html__( 'Fancy', 'easy-property-listings' ),
275: 'default' => esc_html__( 'WordPress Default', 'easy-property-listings' ),
276: ),
277: 'condition' => array( 'pagination' => 'epl' ),
278: )
279: );
280: $this->add_responsive_control( 'columns', array( 'label' => esc_html__( 'Grid Columns', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SELECT, 'default' => '3', 'tablet_default' => '2', 'mobile_default' => '1', 'options' => array( '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6' ), 'selectors' => array( '{{WRAPPER}} .epl-elementor-advanced-grid' => '--epl-elementor-advanced-columns: {{VALUE}};' ) ) );
281: if ( $templates ) {
282: $this->add_responsive_control(
283: 'list_columns',
284: array(
285: 'label' => esc_html__( 'List Columns', 'easy-property-listings' ),
286: 'type' => \Elementor\Controls_Manager::SELECT,
287: 'default' => '1',
288: 'tablet_default' => '1',
289: 'mobile_default' => '1',
290: 'options' => array( '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6' ),
291: 'description' => esc_html__( 'Columns used while the archive is in List view.', 'easy-property-listings' ),
292: 'selectors' => array(
293: '{{WRAPPER}} .epl-elementor-advanced-grid.epl-ea-dual-view.is-list-view' => '--epl-elementor-advanced-columns: {{VALUE}};',
294: ),
295: 'condition' => array(
296: 'renderer' => 'elementor',
297: 'list_template_id!' => '',
298: ),
299: )
300: );
301: }
302: $this->end_controls_section();
303:
304: $this->start_controls_section( 'section_grid_style', array( 'label' => esc_html__( 'Grid', 'easy-property-listings' ), 'tab' => \Elementor\Controls_Manager::TAB_STYLE ) );
305: $this->add_responsive_control( 'column_gap', array( 'label' => esc_html__( 'Column Gap', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SLIDER, 'size_units' => array( 'px', 'em', 'rem' ), 'default' => array( 'size' => 24, 'unit' => 'px' ), 'selectors' => array( '{{WRAPPER}} .epl-elementor-advanced-grid' => 'column-gap: {{SIZE}}{{UNIT}};' ) ) );
306: $this->add_responsive_control( 'row_gap', array( 'label' => esc_html__( 'Row Gap', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::SLIDER, 'size_units' => array( 'px', 'em', 'rem' ), 'default' => array( 'size' => 32, 'unit' => 'px' ), 'selectors' => array( '{{WRAPPER}} .epl-elementor-advanced-grid' => 'row-gap: {{SIZE}}{{UNIT}};' ) ) );
307: $this->end_controls_section();
308:
309: $this->start_controls_section( 'section_style', array( 'label' => esc_html__( 'Pagination', 'easy-property-listings' ), 'tab' => \Elementor\Controls_Manager::TAB_STYLE ) );
310: $this->add_responsive_control(
311: 'align',
312: array(
313: 'label' => esc_html__( 'Alignment', 'easy-property-listings' ),
314: 'type' => \Elementor\Controls_Manager::CHOOSE,
315: 'options' => array(
316: 'left' => array( 'title' => esc_html__( 'Left', 'easy-property-listings' ), 'icon' => 'eicon-text-align-left' ),
317: 'center' => array( 'title' => esc_html__( 'Center', 'easy-property-listings' ), 'icon' => 'eicon-text-align-center' ),
318: 'right' => array( 'title' => esc_html__( 'Right', 'easy-property-listings' ), 'icon' => 'eicon-text-align-right' ),
319: ),
320: 'default' => 'center',
321: 'selectors' => array( '{{WRAPPER}}' => 'text-align: {{VALUE}};' ),
322: )
323: );
324: $this->add_group_control( \Elementor\Group_Control_Typography::get_type(), array( 'name' => 'typography', 'selector' => '{{WRAPPER}} .epl-pagination a, {{WRAPPER}} .epl-pagination span, {{WRAPPER}} .epl-paginate-default-wrapper a' ) );
325: $this->end_controls_section();
326: }
327:
328: /**
329: * Render the widget output.
330: *
331: * @since 3.6.0 New: Elementor widgets (beta).
332: */
333: protected function render() {
334: $settings = $this->get_settings_for_display();
335: $atts = $this->build_shortcode_attributes( $settings );
336: $shortcode = new EPL_Advanced_Shortcode_Listing( $atts );
337:
338: if ( 'elementor' !== $settings['renderer'] || empty( $settings['elementor_template_id'] ) || ! $this->get_elementor_loop_templates() ) {
339: echo $shortcode->render(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- EPL templates escape their output.
340: } else {
341: $this->render_elementor_template( $shortcode, absint( $settings['elementor_template_id'] ), absint( $settings['list_template_id'] ?? 0 ), $settings['editor_preview_view'] ?? 'grid' );
342: }
343:
344: $pagination = empty( $settings['offset'] ) && 'off' !== ( $settings['pagination'] ?? 'epl' ) ? 'epl' : 'off';
345: $pagination_style = ! empty( $settings['epl_pagination_style'] ) ? $settings['epl_pagination_style'] : 'default';
346: $this->render_pagination( $shortcode->query_open, $pagination, $pagination_style );
347: }
348:
349: /**
350: * Build shortcode attributes.
351: *
352: * @since 3.6.0 New: Elementor widgets (beta).
353: */
354: private function build_shortcode_attributes( $settings ) {
355: $keys = array( 'post_type', 'status', 'commercial_listing_type', 'feature', 'feature_id', 'limit', 'offset', 'author', 'agent', 'featured', 'open_house', 'auction', 'template', 'location', 'location_id', 'tools_top', 'tools_bottom', 'sortby', 'orderby_clause', 'sort_order', 'post__in', 'post__not_in' );
356: $atts = array(
357: 'instance_id' => 'elementor-' . $this->get_id(),
358: 'class' => 'epl-elementor-advanced-grid',
359: // Pagination is rendered below so EPL and Elementor providers share the
360: // exact same query and never produce duplicate navigation.
361: 'pagination' => 'off',
362: );
363: foreach ( $keys as $key ) {
364: $value = isset( $settings[ $key ] ) ? $settings[ $key ] : '';
365: $atts[ $key ] = is_array( $value ) ? implode( ',', array_filter( $value, 'strlen' ) ) : $value;
366: }
367:
368: $compare_suffixes = array(
369: 'equal' => '',
370: 'min' => '_min',
371: 'max' => '_max',
372: 'not_equal' => '_not_equal',
373: 'like' => '_like',
374: 'not_like' => '_not_like',
375: 'exists' => '_exists',
376: 'not_exists' => '_not_exists',
377: 'between' => '_between',
378: 'not_between' => '_not_between',
379: 'in' => '_in',
380: 'not_in' => '_not_in',
381: );
382:
383: foreach ( isset( $settings['dynamic_filters'] ) ? (array) $settings['dynamic_filters'] : array() as $filter ) {
384: $meta_key = isset( $filter['meta_key'] ) ? ltrim( sanitize_key( $filter['meta_key'] ), '_' ) : '';
385: $compare = isset( $filter['compare'] ) ? sanitize_key( $filter['compare'] ) : 'equal';
386: if ( '' === $meta_key || ! isset( $compare_suffixes[ $compare ] ) ) {
387: continue;
388: }
389: $attribute = '_' . $meta_key . $compare_suffixes[ $compare ];
390: $atts[ $attribute ] = in_array( $compare, array( 'exists', 'not_exists' ), true ) ? '' : (string) ( $filter['value'] ?? '' );
391: }
392:
393: return apply_filters( 'epl_elementor_listing_advanced_atts', $atts, $settings, $this );
394: }
395:
396: /**
397: * Render elementor template.
398: *
399: * @since 3.6.0 New: Elementor widgets (beta).
400: * @since 3.6.0 Security: only renders a published Loop Item or EPL Listing Card template.
401: */
402: private function render_elementor_template( $shortcode, $template_id, $list_template_id = 0, $editor_view = 'grid' ) {
403: // Only a published Loop Item or EPL Listing Card template, so a draft or private page can't be rendered here.
404: if ( 'publish' !== get_post_status( $template_id ) || ! in_array( get_post_meta( $template_id, '_elementor_template_type', true ), array( 'loop-item', 'epl-loop-card' ), true ) ) {
405: return;
406: }
407: if ( $list_template_id && ( 'publish' !== get_post_status( $list_template_id ) || ! in_array( get_post_meta( $list_template_id, '_elementor_template_type', true ), array( 'loop-item', 'epl-loop-card' ), true ) ) ) {
408: $list_template_id = 0;
409: }
410:
411: $query = $shortcode->query_open;
412: if ( ! $query->have_posts() ) {
413: if ( \Elementor\Plugin::$instance->editor->is_edit_mode() ) {
414: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'No listings found.', 'easy-property-listings' ) . '</div>';
415: } else {
416: do_action( 'epl_shortcode_results_message', 'default' );
417: }
418: return;
419: }
420:
421: global $property;
422: $original_property = isset( $property ) ? $property : null;
423:
424: // A distinct list template turns on dual rendering: each item outputs both
425: // templates and the grid/list switch toggles which one is visible via CSS.
426: $dual_view = $list_template_id && $list_template_id !== $template_id;
427: $grid_classes = 'epl-elementor-advanced-grid';
428: if ( $dual_view ) {
429: // In the editor the canvas has no visitor toggle, so the dedicated
430: // preview control decides which template is shown; the live site
431: // resolves from the visitor's saved preference.
432: $view = \Elementor\Plugin::$instance->editor->is_edit_mode()
433: ? ( 'list' === $editor_view ? 'list' : 'grid' )
434: : $this->get_initial_view();
435: $grid_classes .= ' epl-ea-dual-view ' . ( 'list' === $view ? 'is-list-view' : 'is-grid-view' );
436: }
437:
438: // Elementor only needs a template's CSS printed once per page.
439: $css_loaded = array();
440:
441: if ( 'on' === $shortcode->attributes['tools_top'] ) {
442: do_action( 'epl_property_loop_start', $shortcode->attributes );
443: }
444: echo '<div class="' . esc_attr( $grid_classes ) . '">';
445: while ( $query->have_posts() ) {
446: $query->the_post();
447: $property = new EPL_Property_Meta( get_post() ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- EPL loop context restored below.
448: echo '<div class="epl-elementor-advanced-item">';
449: if ( $dual_view ) {
450: echo '<div class="epl-ea-view epl-ea-view--grid">';
451: $this->render_loop_item( $template_id, $css_loaded );
452: echo '</div>';
453: echo '<div class="epl-ea-view epl-ea-view--list">';
454: $this->render_loop_item( $list_template_id, $css_loaded );
455: echo '</div>';
456: } else {
457: $this->render_loop_item( $template_id, $css_loaded );
458: }
459: echo '</div>';
460: }
461: echo '</div>';
462: if ( 'on' === $shortcode->attributes['tools_bottom'] ) {
463: do_action( 'epl_property_loop_end' );
464: }
465: wp_reset_postdata();
466: $property = $original_property; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Restore caller context.
467: }
468:
469: /**
470: * Render a single Elementor loop item, printing each template's CSS only once.
471: *
472: * @param int $template_id Elementor loop-item template ID.
473: * @param array $css_loaded Map of template IDs whose CSS has already printed.
474: * @since 3.6.0 New: Elementor widgets (beta), grid and list dual view.
475: */
476: private function render_loop_item( $template_id, &$css_loaded ) {
477: $with_css = empty( $css_loaded[ $template_id ] );
478: $css_loaded[ $template_id ] = true;
479: echo \Elementor\Plugin::$instance->frontend->get_builder_content_for_display( $template_id, $with_css ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Elementor-rendered template.
480: }
481:
482: /**
483: * Initial grid/list view for a dual-view widget.
484: *
485: * Mirrors the front-end switch logic: the visitor's saved preference cookie
486: * wins, otherwise the site's default archive view setting, otherwise list.
487: * Resolved server-side so the correct template shows without a flash.
488: *
489: * @return string Either 'grid' or 'list'.
490: * @since 3.6.0 New: Elementor widgets (beta), grid and list dual view.
491: */
492: private function get_initial_view() {
493: if ( isset( $_COOKIE['preferredView'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only display preference.
494: $view = sanitize_key( wp_unslash( $_COOKIE['preferredView'] ) );
495: if ( in_array( $view, array( 'list', 'grid' ), true ) ) {
496: return $view;
497: }
498: }
499:
500: return 'grid' === epl_get_option( 'display_archive_view_type', 'list' ) ? 'grid' : 'list';
501: }
502:
503: /** Render the selected navigation provider for this widget's private query. */
504: /**
505: * Render pagination.
506: *
507: * @since 3.6.0 New: Elementor widgets (beta).
508: */
509: private function render_pagination( $query, $provider, $pagination_style = 'default' ) {
510: if ( ! $query instanceof WP_Query || $query->max_num_pages < 2 || 'off' === $provider ) {
511: return;
512: }
513: EPL_Elementor::render_epl_pagination( $query, $pagination_style );
514: }
515:
516: /**
517: * Elementor Loop Grid templates (Pro, when available) plus EPL Listing
518: * Card templates (always). Both render the same way via render_loop_item().
519: *
520: * @since 3.6.0 New: Elementor widgets (beta).
521: */
522: private function get_elementor_loop_templates() {
523: $options = array();
524:
525: $widgets_manager = isset( \Elementor\Plugin::$instance->widgets_manager ) ? \Elementor\Plugin::$instance->widgets_manager : null;
526: if ( $widgets_manager && $widgets_manager->get_widget_types( 'loop-grid' ) ) {
527: $pro_posts = get_posts( array( 'post_type' => 'elementor_library', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => '_elementor_template_type', 'meta_value' => 'loop-item' ) ); // phpcs:ignore WordPress.DB.SlowDBQuery
528: foreach ( $pro_posts as $post ) {
529: $options[ $post->ID ] = $post->post_title;
530: }
531: }
532:
533: $card_posts = get_posts( array( 'post_type' => 'elementor_library', 'post_status' => 'publish', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC', 'meta_key' => '_elementor_template_type', 'meta_value' => EPL_Elementor_Loop_Card_Document::TYPE ) ); // phpcs:ignore WordPress.DB.SlowDBQuery
534: foreach ( $card_posts as $post ) {
535: $options[ $post->ID ] = $post->post_title;
536: }
537:
538: return $options;
539: }
540:
541: /**
542: * Get term options.
543: *
544: * @since 3.6.0 New: Elementor widgets (beta).
545: */
546: private function get_term_options( $taxonomy ) {
547: $options = array();
548: $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false ) );
549: if ( ! is_wp_error( $terms ) ) {
550: foreach ( $terms as $term ) {
551: $options[ $term->term_id ] = $term->name;
552: }
553: }
554: return $options;
555: }
556:
557: /**
558: * Get author options.
559: *
560: * @since 3.6.0 New: Elementor widgets (beta).
561: * @since 3.6.0 Security: lists only users who can write posts (agents), not every user login.
562: */
563: private function get_author_options() {
564: $options = array();
565: foreach ( get_users( array( 'fields' => array( 'user_login', 'display_name' ), 'capability' => array( 'edit_posts' ) ) ) as $user ) {
566: $options[ $user->user_login ] = $user->display_name;
567: }
568: return $options;
569: }
570:
571: /**
572: * Switcher args.
573: *
574: * @since 3.6.0 New: Elementor widgets (beta).
575: */
576: private function switcher_args( $label, $on = 'yes', $off = '', $default = '' ) {
577: return array( 'label' => $label, 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => esc_html__( 'Yes', 'easy-property-listings' ), 'label_off' => esc_html__( 'No', 'easy-property-listings' ), 'return_value' => $on, 'default' => $default, 'off_value' => $off );
578: }
579: }
580: