1: <?php
2: /**
3: * Elementor Listing Search 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_Search Class
17: *
18: * @since 3.6.0
19: */
20: class EPL_Elementor_Listing_Search 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-listing-search';
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__( 'Listing Search', '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-form-vertical';
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-archive' );
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( 'listing', 'search', 'property', 'filter', '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__( 'Search Form', 'easy-property-listings' ),
83: 'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
84: )
85: );
86:
87: // The WordPress widget, shortcode and extensions all use this schema.
88: // Consuming it here keeps Elementor in sync when EPL adds or filters fields.
89: foreach ( epl_search_widget_fields() as $field ) {
90: $this->register_search_control( $field );
91: }
92:
93: $this->end_controls_section();
94:
95: // Style Section.
96: $this->start_controls_section(
97: 'section_style',
98: array(
99: 'label' => esc_html__( 'Style', 'easy-property-listings' ),
100: 'tab' => \Elementor\Controls_Manager::TAB_STYLE,
101: )
102: );
103:
104: $this->add_control(
105: 'button_color',
106: array(
107: 'label' => esc_html__( 'Button Color', 'easy-property-listings' ),
108: 'type' => \Elementor\Controls_Manager::COLOR,
109: 'selectors' => array(
110: '{{WRAPPER}} .epl-search-btn' => 'background-color: {{VALUE}};',
111: ),
112: )
113: );
114:
115: $this->add_control(
116: 'button_text_color',
117: array(
118: 'label' => esc_html__( 'Button Text Color', 'easy-property-listings' ),
119: 'type' => \Elementor\Controls_Manager::COLOR,
120: 'selectors' => array(
121: '{{WRAPPER}} .epl-search-btn' => 'color: {{VALUE}};',
122: ),
123: )
124: );
125:
126: $this->end_controls_section();
127: }
128:
129: /**
130: * Render widget output.
131: * @since 3.6.0 New: Elementor widgets (beta).
132: */
133: protected function render() {
134: $settings = $this->get_settings_for_display();
135: $atts = array();
136:
137: foreach ( epl_search_widget_fields() as $field ) {
138: $key = $field['key'];
139: if ( array_key_exists( $key, $settings ) ) {
140: $atts[ $key ] = $settings[ $key ];
141: }
142: }
143:
144: echo '<div class="epl-search-widget">';
145:
146: if ( ! empty( $atts['title'] ) ) {
147: echo '<h3 class="epl-search-title">' . esc_html( $atts['title'] ) . '</h3>';
148: }
149:
150: // Call the [listing_search] callback directly: no shortcode string to
151: // break on brackets/quotes in user values, and no double escaping.
152: echo epl_shortcode_listing_search_callback( $atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in template.
153:
154: echo '</div>';
155: }
156:
157: /**
158: * Convert a canonical EPL WordPress-widget field to an Elementor control.
159: *
160: * @param array $field EPL search widget field definition.
161: * @since 3.6.0 New: Elementor widgets (beta).
162: */
163: private function register_search_control( $field ) {
164: if ( empty( $field['key'] ) || empty( $field['type'] ) ) {
165: return;
166: }
167:
168: $key = sanitize_key( $field['key'] );
169: $default = isset( $field['default'] ) ? $field['default'] : '';
170: $args = array(
171: 'label' => isset( $field['label'] ) ? $field['label'] : $key,
172: 'default' => $default,
173: 'description' => isset( $field['help'] ) ? wp_strip_all_tags( $field['help'] ) : '',
174: );
175:
176: switch ( $field['type'] ) {
177: case 'checkbox':
178: $args['type'] = \Elementor\Controls_Manager::SWITCHER;
179: $args['label_on'] = esc_html__( 'Yes', 'easy-property-listings' );
180: $args['label_off'] = esc_html__( 'No', 'easy-property-listings' );
181: $args['return_value'] = 'on';
182: break;
183:
184: case 'select':
185: $args['type'] = ! empty( $field['multiple'] ) ? \Elementor\Controls_Manager::SELECT2 : \Elementor\Controls_Manager::SELECT;
186: $args['options'] = isset( $field['options'] ) ? $field['options'] : array();
187: $args['multiple'] = ! empty( $field['multiple'] );
188: $args['label_block'] = true;
189: break;
190:
191: case 'number':
192: $args['type'] = \Elementor\Controls_Manager::NUMBER;
193: break;
194:
195: case 'textarea':
196: $args['type'] = \Elementor\Controls_Manager::TEXTAREA;
197: break;
198:
199: case 'hidden':
200: $args['type'] = \Elementor\Controls_Manager::HIDDEN;
201: break;
202:
203: case 'text':
204: default:
205: $args['type'] = \Elementor\Controls_Manager::TEXT;
206: $args['label_block'] = true;
207: break;
208: }
209:
210: $this->add_control( $key, $args );
211: }
212: }
213: