1: <?php
2: /**
3: * Elementor Property Meta Widget.
4: *
5: * @package EPL
6: * @subpackage PageBuilders/Elementor/Widgets/Elements
7: * @since 3.6.0
8: */
9:
10: if ( ! defined( 'ABSPATH' ) ) {
11: exit;
12: }
13:
14: /** Render an arbitrary EPL listing meta field. */
15: class EPL_Elementor_Property_Meta 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-property-meta';
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__( 'Custom Field', '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-meta-data';
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-elements' );
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( 'property', 'meta', 'custom field', 'value', '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_content', array( 'label' => esc_html__( 'Meta', 'easy-property-listings' ) ) );
70:
71: $this->add_control(
72: 'meta_key',
73: array(
74: 'label' => esc_html__( 'Meta Key', 'easy-property-listings' ),
75: 'type' => \Elementor\Controls_Manager::TEXT,
76: 'placeholder' => 'property_land_area',
77: 'label_block' => true,
78: )
79: );
80:
81: $this->add_control(
82: 'format',
83: array(
84: 'label' => esc_html__( 'Format', 'easy-property-listings' ),
85: 'type' => \Elementor\Controls_Manager::SELECT,
86: 'default' => 'text',
87: 'options' => array(
88: 'text' => esc_html__( 'Text', 'easy-property-listings' ),
89: 'number' => esc_html__( 'Number', 'easy-property-listings' ),
90: 'currency' => esc_html__( 'Currency', 'easy-property-listings' ),
91: 'date' => esc_html__( 'Date', 'easy-property-listings' ),
92: 'yes_no' => esc_html__( 'Yes / No', 'easy-property-listings' ),
93: 'phone' => esc_html__( 'Phone Link', 'easy-property-listings' ),
94: 'email' => esc_html__( 'Email Link', 'easy-property-listings' ),
95: 'url' => esc_html__( 'URL Link', 'easy-property-listings' ),
96: ),
97: )
98: );
99:
100: $this->add_control( 'decimal_places', array( 'label' => esc_html__( 'Decimal Places', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::NUMBER, 'min' => 0, 'max' => 6, 'default' => 0, 'condition' => array( 'format' => 'number' ) ) );
101: $this->add_control( 'date_format', array( 'label' => esc_html__( 'Date Format', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'j F Y', 'condition' => array( 'format' => 'date' ) ) );
102: $this->add_control( 'yes_label', array( 'label' => esc_html__( 'Yes Label', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => esc_html__( 'Yes', 'easy-property-listings' ), 'condition' => array( 'format' => 'yes_no' ) ) );
103: $this->add_control( 'no_label', array( 'label' => esc_html__( 'No Label', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => esc_html__( 'No', 'easy-property-listings' ), 'condition' => array( 'format' => 'yes_no' ) ) );
104:
105: $this->add_control(
106: 'value_map',
107: array(
108: 'label' => esc_html__( 'Value Labels', 'easy-property-listings' ),
109: 'type' => \Elementor\Controls_Manager::TEXTAREA,
110: 'description' => esc_html__( 'Optional, one mapping per line: stored_value|Display Label', 'easy-property-listings' ),
111: 'placeholder' => "yes|Available\nno|Unavailable",
112: )
113: );
114:
115: $this->add_control( 'prefix', array( 'label' => esc_html__( 'Prefix', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT ) );
116: $this->add_control( 'suffix', array( 'label' => esc_html__( 'Suffix', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT ) );
117: $this->add_control( 'fallback', array( 'label' => esc_html__( 'Empty Value', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::TEXT ) );
118: $this->add_control(
119: 'html_tag',
120: array(
121: 'label' => esc_html__( 'HTML Tag', 'easy-property-listings' ),
122: 'type' => \Elementor\Controls_Manager::SELECT,
123: 'default' => 'div',
124: 'options' => array( 'div' => 'div', 'span' => 'span', 'p' => 'p', 'h2' => 'h2', 'h3' => 'h3', 'h4' => 'h4' ),
125: )
126: );
127: $this->add_control(
128: 'icon',
129: array(
130: 'label' => esc_html__( 'Icon', 'easy-property-listings' ),
131: 'type' => \Elementor\Controls_Manager::ICONS,
132: )
133: );
134: $this->add_control(
135: 'icon_position',
136: array(
137: 'label' => esc_html__( 'Icon Position', 'easy-property-listings' ),
138: 'type' => \Elementor\Controls_Manager::SELECT,
139: 'default' => 'before',
140: 'options' => array(
141: 'before' => esc_html__( 'Before', 'easy-property-listings' ),
142: 'after' => esc_html__( 'After', 'easy-property-listings' ),
143: ),
144: 'condition' => array( 'icon[value]!' => '' ),
145: )
146: );
147: $this->end_controls_section();
148:
149: $this->start_controls_section( 'section_style', array( 'label' => esc_html__( 'Style', 'easy-property-listings' ), 'tab' => \Elementor\Controls_Manager::TAB_STYLE ) );
150: $this->add_control( 'color', array( 'label' => esc_html__( 'Color', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::COLOR, 'selectors' => array( '{{WRAPPER}} .epl-property-meta' => 'color: {{VALUE}};' ) ) );
151: $this->add_group_control( \Elementor\Group_Control_Typography::get_type(), array( 'name' => 'typography', 'selector' => '{{WRAPPER}} .epl-property-meta' ) );
152: $this->add_responsive_control( 'alignment', array( 'label' => esc_html__( 'Alignment', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::CHOOSE, 'options' => array( 'left' => array( 'title' => esc_html__( 'Left', 'easy-property-listings' ), 'icon' => 'eicon-text-align-left' ), 'center' => array( 'title' => esc_html__( 'Center', 'easy-property-listings' ), 'icon' => 'eicon-text-align-center' ), 'right' => array( 'title' => esc_html__( 'Right', 'easy-property-listings' ), 'icon' => 'eicon-text-align-right' ) ), 'selectors' => array( '{{WRAPPER}} .epl-property-meta' => 'text-align: {{VALUE}};' ) ) );
153: $this->add_control( 'icon_heading', array( 'label' => esc_html__( 'Icon', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::HEADING, 'separator' => 'before' ) );
154: $this->add_control( 'icon_color', array( 'label' => esc_html__( 'Icon Color', 'easy-property-listings' ), 'type' => \Elementor\Controls_Manager::COLOR, 'selectors' => array( '{{WRAPPER}} .epl-property-meta-icon i' => 'color: {{VALUE}};', '{{WRAPPER}} .epl-property-meta-icon svg' => 'fill: {{VALUE}};' ) ) );
155: $this->add_responsive_control(
156: 'icon_size',
157: array(
158: 'label' => esc_html__( 'Icon Size', 'easy-property-listings' ),
159: 'type' => \Elementor\Controls_Manager::SLIDER,
160: 'size_units' => array( 'px', 'em', 'rem' ),
161: 'range' => array( 'px' => array( 'min' => 6, 'max' => 200 ) ),
162: 'default' => array( 'size' => 16, 'unit' => 'px' ),
163: 'selectors' => array( '{{WRAPPER}} .epl-property-meta-icon' => 'font-size: {{SIZE}}{{UNIT}};', '{{WRAPPER}} .epl-property-meta-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ),
164: )
165: );
166: $this->add_responsive_control(
167: 'icon_spacing',
168: array(
169: 'label' => esc_html__( 'Icon Spacing', 'easy-property-listings' ),
170: 'type' => \Elementor\Controls_Manager::SLIDER,
171: 'size_units' => array( 'px', 'em', 'rem' ),
172: 'range' => array( 'px' => array( 'min' => 0, 'max' => 100 ) ),
173: 'default' => array( 'size' => 8, 'unit' => 'px' ),
174: 'selectors' => array( '{{WRAPPER}} .epl-property-meta-content' => 'gap: {{SIZE}}{{UNIT}};' ),
175: )
176: );
177: $this->end_controls_section();
178: }
179:
180: /**
181: * Render the widget output.
182: *
183: * @since 3.6.0 New: Elementor widgets (beta).
184: * @since 3.6.0 Security: won't show a hidden street address or price, or private meta.
185: * @since 3.6.0 Fix: rentals check Rent Display, so a displayed rent isn't hidden.
186: */
187: protected function render() {
188: $property = EPL_Elementor::setup_listing_context();
189: if ( ! $property ) {
190: if ( EPL_Elementor::is_editor() ) {
191: echo '<div class="epl-elementor-placeholder">' . esc_html__( 'Property Meta - No listings found.', 'easy-property-listings' ) . '</div>';
192: }
193: EPL_Elementor::restore_listing_context();
194: return;
195: }
196:
197: $settings = $this->get_settings_for_display();
198: $key = sanitize_key( $settings['meta_key'] );
199:
200: // Never show a street address or price the listing hides, or private (_ prefixed) meta.
201: $blocked = array();
202: if ( ! $property->show_street_address() ) {
203: $blocked = array( 'property_address_street', 'property_address_street_number', 'property_address_sub_number', 'property_address_lot_number', 'property_address_coordinates' );
204: }
205: // Rentals show their rent with Rent Display; every other type uses Price Display.
206: $price_display_key = 'rental' === $property->post_type ? 'property_rent_display' : 'property_price_display';
207: if ( 'yes' !== $property->get_property_meta( $price_display_key ) ) {
208: $blocked = array_merge( $blocked, array( 'property_price', 'property_price_view', 'property_sold_price', 'property_rent', 'property_rent_view', 'property_com_rent' ) );
209: }
210: /**
211: * Filters the meta keys the Elementor Property Meta widget won't show for this listing.
212: *
213: * @since 3.6.0
214: *
215: * @param array $blocked Meta keys.
216: * @param EPL_Property_Meta $property The listing.
217: */
218: $blocked = (array) apply_filters( 'epl_elementor_meta_blocked_keys', $blocked, $property );
219: if ( '' === $key || is_protected_meta( $key, 'post' ) || in_array( $key, $blocked, true ) ) {
220: $key = '';
221: }
222:
223: $value = $key ? $property->get_property_meta( $key ) : '';
224: if ( is_array( $value ) ) {
225: $value = implode( ', ', array_map( 'sanitize_text_field', $value ) );
226: }
227: $value = $this->map_value( (string) $value, $settings['value_map'] );
228: $html = $this->format_value( $value, $settings );
229:
230: if ( '' === $html ) {
231: $html = esc_html( $settings['fallback'] );
232: }
233: if ( '' !== $html ) {
234: $tag = \Elementor\Utils::validate_html_tag( $settings['html_tag'] );
235: $icon = '';
236: if ( ! empty( $settings['icon']['value'] ) ) {
237: ob_start();
238: \Elementor\Icons_Manager::render_icon( $settings['icon'], array( 'aria-hidden' => 'true' ) );
239: $icon = '<span class="epl-property-meta-icon">' . ob_get_clean() . '</span>';
240: }
241: $value_html = '<span class="epl-property-meta-value"><span class="epl-property-meta-prefix">' . esc_html( $settings['prefix'] ) . '</span>' . wp_kses_post( $html ) . '<span class="epl-property-meta-suffix">' . esc_html( $settings['suffix'] ) . '</span></span>';
242: $content = 'after' === $settings['icon_position'] ? $value_html . $icon : $icon . $value_html;
243: echo '<' . esc_attr( $tag ) . ' class="epl-property-meta epl-property-meta-' . esc_attr( $key ) . '"><span class="epl-property-meta-content">' . $content . '</span></' . esc_attr( $tag ) . '>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Icon Manager and value formatter escape their output.
244: }
245: EPL_Elementor::restore_listing_context();
246: }
247:
248: /**
249: * Map value.
250: *
251: * @since 3.6.0 New: Elementor widgets (beta).
252: */
253: private function map_value( $value, $map ) {
254: foreach ( preg_split( '/\r\n|\r|\n/', (string) $map ) as $line ) {
255: $parts = array_map( 'trim', explode( '|', $line, 2 ) );
256: if ( 2 === count( $parts ) && $value === $parts[0] ) {
257: return $parts[1];
258: }
259: }
260: return $value;
261: }
262:
263: /**
264: * Format value.
265: *
266: * @since 3.6.0 New: Elementor widgets (beta).
267: */
268: private function format_value( $value, $settings ) {
269: if ( '' === $value ) {
270: return '';
271: }
272: switch ( $settings['format'] ) {
273: case 'number':
274: return is_numeric( $value ) ? esc_html( number_format_i18n( (float) $value, absint( $settings['decimal_places'] ) ) ) : esc_html( $value );
275: case 'currency':
276: return is_numeric( $value ) ? esc_html( epl_currency_formatted_amount( $value ) ) : esc_html( $value );
277: case 'date':
278: $timestamp = is_numeric( $value ) ? (int) $value : strtotime( $value );
279: return $timestamp ? esc_html( wp_date( $settings['date_format'], $timestamp ) ) : esc_html( $value );
280: case 'yes_no':
281: return esc_html( in_array( strtolower( $value ), array( '1', 'yes', 'on', 'true' ), true ) ? $settings['yes_label'] : $settings['no_label'] );
282: case 'phone':
283: return '<a href="tel:' . esc_attr( preg_replace( '/[^0-9+]/', '', $value ) ) . '">' . esc_html( $value ) . '</a>';
284: case 'email':
285: return is_email( $value ) ? '<a href="mailto:' . esc_attr( $value ) . '">' . esc_html( $value ) . '</a>' : esc_html( $value );
286: case 'url':
287: return '<a href="' . esc_url( $value ) . '">' . esc_html( $value ) . '</a>';
288: default:
289: return esc_html( $value );
290: }
291: }
292: }
293: