1: <?php
2: /**
3: * Elementor Property Address Widget
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor/Widgets/Elements
7: * @since 3.6.0
8: */
9:
10: // Exit if accessed directly.
11: if ( ! defined( 'ABSPATH' ) ) {
12: exit;
13: }
14:
15: /**
16: * EPL_Elementor_Property_Address Class
17: *
18: * @since 3.6.0
19: */
20: class EPL_Elementor_Property_Address extends \Elementor\Widget_Base {
21: use EPL_Elementor_Dynamic_Widget;
22:
23: /**
24: * Get widget name.
25: *
26: * @return string
27: * @since 3.6.0 New: Elementor widgets (beta).
28: */
29: public function get_name() {
30: return 'epl-property-address';
31: }
32:
33: /**
34: * Get widget title.
35: *
36: * @return string
37: * @since 3.6.0 New: Elementor widgets (beta).
38: */
39: public function get_title() {
40: return esc_html__( 'Address', 'easy-property-listings' );
41: }
42:
43: /**
44: * Get widget icon.
45: *
46: * @return string
47: * @since 3.6.0 New: Elementor widgets (beta).
48: */
49: public function get_icon() {
50: return 'eicon-animation-text';
51: }
52:
53: /**
54: * Get widget categories.
55: *
56: * @return array
57: * @since 3.6.0 New: Elementor widgets (beta).
58: */
59: public function get_categories() {
60: return array( 'epl-elements' );
61: }
62:
63: /**
64: * Get widget keywords.
65: *
66: * @return array
67: * @since 3.6.0 New: Elementor widgets (beta).
68: */
69: public function get_keywords() {
70: return array( 'property', 'address', 'location', 'epl' );
71: }
72:
73: /**
74: * Register widget controls.
75: * @since 3.6.0 New: Elementor widgets (beta).
76: */
77: protected function register_controls() {
78:
79: $this->start_controls_section(
80: 'section_content',
81: array(
82: 'label' => esc_html__( 'Content', 'easy-property-listings' ),
83: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
84: )
85: );
86:
87: $this->add_control(
88: 'link_to_listing',
89: array(
90: 'label' => esc_html__( 'Link to Listing', 'easy-property-listings' ),
91: 'type' => \Elementor\Controls_Manager::SWITCHER,
92: 'label_on' => esc_html__( 'Yes', 'easy-property-listings' ),
93: 'label_off' => esc_html__( 'No', 'easy-property-listings' ),
94: 'return_value' => 'yes',
95: 'default' => '',
96: )
97: );
98:
99: $this->add_control(
100: 'display_mode',
101: array(
102: 'label' => esc_html__( 'Display', 'easy-property-listings' ),
103: 'type' => \Elementor\Controls_Manager::SELECT,
104: 'default' => 'full',
105: 'options' => array(
106: 'full' => esc_html__( 'Full Address', 'easy-property-listings' ),
107: 'suburb' => esc_html__( 'Suburb Only', 'easy-property-listings' ),
108: 'street' => esc_html__( 'Street Only', 'easy-property-listings' ),
109: 'custom' => esc_html__( 'Custom Parts', 'easy-property-listings' ),
110: ),
111: 'description' => esc_html__( 'Full address, suburb only, street only, or choose your own combination of address parts.', 'easy-property-listings' ),
112: )
113: );
114:
115: $this->add_control(
116: 'address_parts',
117: array(
118: 'label' => esc_html__( 'Address Parts', 'easy-property-listings' ),
119: 'type' => \Elementor\Controls_Manager::SELECT2,
120: 'multiple' => true,
121: 'label_block' => true,
122: 'options' => self::get_address_part_options(),
123: 'default' => array( 'sub_number', 'street_number', 'street' ),
124: 'description' => esc_html__( 'Select which address components to display. Parts always output in natural address order regardless of selection order.', 'easy-property-listings' ),
125: 'condition' => array(
126: 'display_mode' => 'custom',
127: ),
128: )
129: );
130:
131: $this->add_control(
132: 'show_icon',
133: array(
134: 'label' => esc_html__( 'Show Icon', 'easy-property-listings' ),
135: 'type' => \Elementor\Controls_Manager::SWITCHER,
136: 'label_on' => esc_html__( 'Show', 'easy-property-listings' ),
137: 'label_off' => esc_html__( 'Hide', 'easy-property-listings' ),
138: 'return_value' => 'yes',
139: 'default' => 'no',
140: )
141: );
142:
143: $this->add_control(
144: 'icon',
145: array(
146: 'label' => esc_html__( 'Icon', 'easy-property-listings' ),
147: 'type' => \Elementor\Controls_Manager::ICONS,
148: 'default' => array(
149: 'value' => 'fas fa-map-marker-alt',
150: 'library' => 'fa-solid',
151: ),
152: 'condition' => array(
153: 'show_icon' => 'yes',
154: ),
155: )
156: );
157:
158: $this->end_controls_section();
159:
160: // Style Section.
161: $this->start_controls_section(
162: 'section_style',
163: array(
164: 'label' => esc_html__( 'Style', 'easy-property-listings' ),
165: 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
166: )
167: );
168:
169: $this->add_control(
170: 'text_color',
171: array(
172: 'label' => esc_html__( 'Color', 'easy-property-listings' ),
173: 'type' => \Elementor\Controls_Manager::COLOR,
174: 'selectors' => array(
175: '{{WRAPPER}} .epl-property-address' => 'color: {{VALUE}};',
176: '{{WRAPPER}} .epl-property-address a' => 'color: {{VALUE}};',
177: ),
178: )
179: );
180:
181: $this->add_group_control(
182: \Elementor\Group_Control_Typography::get_type(),
183: array(
184: 'name' => 'typography',
185: 'selector' => '{{WRAPPER}} .epl-property-address',
186: )
187: );
188:
189: $this->add_control(
190: 'icon_color',
191: array(
192: 'label' => esc_html__( 'Icon Color', 'easy-property-listings' ),
193: 'type' => \Elementor\Controls_Manager::COLOR,
194: 'selectors' => array(
195: '{{WRAPPER}} .epl-icon-wrapper i' => 'color: {{VALUE}};',
196: '{{WRAPPER}} .epl-icon-wrapper svg' => 'fill: {{VALUE}};',
197: '{{WRAPPER}} .epl-icon-wrapper svg path' => 'fill: {{VALUE}};',
198: ),
199: 'condition' => array(
200: 'show_icon' => 'yes',
201: ),
202: )
203: );
204:
205: $this->add_responsive_control(
206: 'icon_size',
207: array(
208: 'label' => esc_html__( 'Icon Size', 'easy-property-listings' ),
209: 'type' => \Elementor\Controls_Manager::SLIDER,
210: 'size_units' => array( 'px', 'em', 'rem', 'vw' ),
211: 'range' => array(
212: 'px' => array(
213: 'min' => 10,
214: 'max' => 100,
215: ),
216: ),
217: 'selectors' => array(
218: '{{WRAPPER}} .epl-icon-wrapper i' => 'font-size: {{SIZE}}{{UNIT}};',
219: '{{WRAPPER}} .epl-icon-wrapper svg' => 'width: {{SIZE}}{{UNIT}}; height: auto;',
220: ),
221: 'condition' => array(
222: 'show_icon' => 'yes',
223: ),
224: )
225: );
226:
227: $this->add_responsive_control(
228: 'text_align',
229: array(
230: 'label' => esc_html__( 'Alignment', 'easy-property-listings' ),
231: 'type' => \Elementor\Controls_Manager::CHOOSE,
232: 'options' => array(
233: 'left' => array(
234: 'title' => esc_html__( 'Left', 'easy-property-listings' ),
235: 'icon' => 'eicon-text-align-left',
236: ),
237: 'center' => array(
238: 'title' => esc_html__( 'Center', 'easy-property-listings' ),
239: 'icon' => 'eicon-text-align-center',
240: ),
241: 'right' => array(
242: 'title' => esc_html__( 'Right', 'easy-property-listings' ),
243: 'icon' => 'eicon-text-align-right',
244: ),
245: ),
246: 'selectors' => array(
247: '{{WRAPPER}} .epl-property-address' => 'text-align: {{VALUE}};',
248: ),
249: )
250: );
251:
252: $this->add_responsive_control(
253: 'minimum_height',
254: array(
255: 'label' => esc_html__( 'Minimum Height', 'easy-property-listings' ),
256: 'type' => \Elementor\Controls_Manager::SLIDER,
257: 'size_units' => array( 'px' ),
258: 'range' => array(
259: 'px' => array(
260: 'min' => 0,
261: 'max' => 100,
262: ),
263: ),
264: 'default' => array(
265: 'size' => 25,
266: 'unit' => 'px',
267: ),
268: 'description' => esc_html__( 'Keeps the container from collapsing (e.g. suburb-only) so cards stay aligned in a grid.', 'easy-property-listings' ),
269: 'selectors' => array(
270: '{{WRAPPER}} .epl-property-address' => 'min-height: {{SIZE}}{{UNIT}};',
271: ),
272: )
273: );
274:
275: $this->end_controls_section();
276: }
277:
278: /**
279: * Render widget output.
280: * @since 3.6.0 New: Elementor widgets (beta).
281: */
282: protected function render() {
283: $property = EPL_Elementor::setup_listing_context();
284: $is_editor = \Elementor\Plugin::$instance->editor->is_edit_mode();
285:
286: if ( ! $property ) {
287: if ( $is_editor ) {
288: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'Property Address - No listings found.', 'easy-property-listings' ) . '</div>';
289: }
290: EPL_Elementor::restore_listing_context();
291: return;
292: }
293:
294: $settings = $this->get_settings_for_display();
295:
296: echo '<div class="epl-property-address">';
297:
298: if ( 'yes' === $settings['show_icon'] ) {
299: echo '<span class="epl-icon-wrapper">';
300: \Elementor\Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) );
301: echo '</span>';
302: echo '&nbsp;';
303: }
304:
305: if ( 'yes' === $settings['link_to_listing'] ) {
306: echo '<a href="' . esc_url( get_permalink( $property->post->ID ) ) . '">';
307: }
308:
309: $display_mode = isset( $settings['display_mode'] ) ? $settings['display_mode'] : 'full';
310:
311: // Backward compatibility with earlier builds that used a "Show Suburb Only" switch.
312: if ( 'full' === $display_mode && isset( $settings['show_suburb_profile'] ) && 'yes' === $settings['show_suburb_profile'] ) {
313: $display_mode = 'suburb';
314: }
315:
316: switch ( $display_mode ) {
317: case 'suburb':
318: // Suburb only, respecting commercial/business suburb display rules.
319: do_action( 'epl_property_suburb' );
320: break;
321:
322: case 'street':
323: case 'custom':
324: // Selected address parts only.
325: echo wp_kses_post( $this->get_address_parts_html( $display_mode, $settings ) );
326: break;
327:
328: case 'full':
329: default:
330: // Full address. Core respects the property_address_display option,
331: // commercial suburb display and the city/country field settings.
332: do_action( 'epl_property_address' );
333: break;
334: }
335:
336: if ( 'yes' === $settings['link_to_listing'] ) {
337: echo '</a>';
338: }
339:
340: echo '</div>';
341:
342: EPL_Elementor::restore_listing_context();
343: }
344:
345: /**
346: * Available address parts and their control labels.
347: *
348: * @since 3.6.0
349: *
350: * @return array Machine key => translated label.
351: */
352: public static function get_address_part_options() {
353: return array(
354: 'sub_number' => esc_html__( 'Unit / Sub Number', 'easy-property-listings' ),
355: 'lot_number' => esc_html__( 'Lot Number', 'easy-property-listings' ),
356: 'street_number' => esc_html__( 'Street Number', 'easy-property-listings' ),
357: 'street' => esc_html__( 'Street Name', 'easy-property-listings' ),
358: 'suburb' => esc_html__( 'Suburb', 'easy-property-listings' ),
359: 'city' => esc_html__( 'City', 'easy-property-listings' ),
360: 'state' => esc_html__( 'State', 'easy-property-listings' ),
361: 'postal_code' => esc_html__( 'Postal Code', 'easy-property-listings' ),
362: 'country' => esc_html__( 'Country', 'easy-property-listings' ),
363: );
364: }
365:
366: /**
367: * Build the address output from the selected parts.
368: *
369: * Mirrors the Template Builder behaviour: parts are emitted in their natural
370: * address order (not selection order), each followed by its default separator,
371: * with the trailing separator on the final part stripped. Street level parts
372: * still respect the property_address_display privacy option and country is
373: * only output when explicitly selected, both enforced by epl_get_the_address().
374: *
375: * @since 3.6.0
376: *
377: * @param string $mode Either 'street' (preset) or 'custom' (user selection).
378: * @param array $settings Widget settings.
379: * @return string Escaped address HTML/text.
380: */
381: protected function get_address_parts_html( $mode, $settings ) {
382:
383: // Canonical part order and default separators (matches epl_get_the_address()).
384: $all_parts = array(
385: 'sub_number' => '/',
386: 'lot_number' => ' ',
387: 'street_number' => ' ',
388: 'street' => ', ',
389: 'suburb' => ' ',
390: 'city' => ' ',
391: 'state' => ' ',
392: 'postal_code' => ' ',
393: 'country' => ' ',
394: );
395:
396: if ( 'street' === $mode ) {
397: $parts = array( 'sub_number', 'lot_number', 'street_number', 'street' );
398: } else {
399: $parts = ( isset( $settings['address_parts'] ) && is_array( $settings['address_parts'] ) )
400: ? $settings['address_parts']
401: : array();
402: }
403:
404: if ( empty( $parts ) ) {
405: return '';
406: }
407:
408: // Build args in canonical order, keeping only the enabled parts.
409: $address_args = array();
410: foreach ( $all_parts as $part => $separator ) {
411: if ( in_array( $part, $parts, true ) ) {
412: $address_args[ $part ] = $separator;
413: }
414: }
415:
416: if ( empty( $address_args ) ) {
417: return '';
418: }
419:
420: // Strip the trailing separator on the final part.
421: $keys = array_keys( $address_args );
422: $last_key = end( $keys );
423: if ( $last_key ) {
424: $address_args[ $last_key ] = '';
425: }
426:
427: // Country is only output when explicitly selected.
428: $show_country = isset( $address_args['country'] );
429:
430: $address = epl_get_the_address( array_keys( $address_args ), $address_args, $show_country );
431:
432: return trim( (string) $address );
433: }
434: }
435: