PluginProbe
Hello Plus / 1.2.0
Hello Plus v1.2.0
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.7.0 1.7.1 1.7.2 1.7.3 All 30 releases
hello-plus / modules / forms / classes / form-base.php

form-base.php in Hello Plus 1.2.0, at modules/forms/classes/form-base.php

288 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace HelloPlus\Modules\Forms\Classes;
3
4 use Elementor\Utils;
5 use Elementor\Widget_Base;
6 use Elementor\Icons_Manager;
7 use HelloPlus\Modules\Theme\Module as Theme_Module;
8 use HelloPlus\Modules\Forms\Module;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 abstract class Form_Base extends Widget_Base {
15
16 public function on_export( $element ) {
17 /** @var \HelloPlus\Modules\Forms\Classes\Action_Base[] $actions */
18 $actions = Module::instance()->actions_registrar->get();
19
20 foreach ( $actions as $action ) {
21 $new_element_data = $action->on_export( $element );
22 if ( null !== $new_element_data ) {
23 $element = $new_element_data;
24 }
25 }
26
27 return $element;
28 }
29
30 public static function get_button_sizes(): array {
31 return [
32 'xs' => esc_html__( 'Extra Small', 'hello-plus' ),
33 'sm' => esc_html__( 'Small', 'hello-plus' ),
34 'md' => esc_html__( 'Medium', 'hello-plus' ),
35 'lg' => esc_html__( 'Large', 'hello-plus' ),
36 'xl' => esc_html__( 'Extra Large', 'hello-plus' ),
37 ];
38 }
39
40 public function make_textarea_field( $item, $item_index, $instance ): string {
41 $this->add_render_attribute( 'textarea' . $item_index, [
42 'class' => [
43 'elementor-field-textual',
44 'ehp-form__field',
45 'eph-form__textarea',
46 esc_attr( $item['css_classes'] ),
47 'elementor-size-' . $item['input_size'],
48 ],
49 'name' => $this->get_attribute_name( $item ),
50 'id' => $this->get_attribute_id( $item ),
51 'rows' => $item['rows'],
52 ] );
53
54 if ( $item['placeholder'] ) {
55 $this->add_render_attribute( 'textarea' . $item_index, 'placeholder', $item['placeholder'] );
56 }
57
58 if ( $item['required'] ) {
59 $this->add_required_attribute( 'textarea' . $item_index );
60 }
61
62 $value = empty( $item['field_value'] ) ? '' : $item['field_value'];
63
64 return '<textarea ' . $this->get_render_attribute_string( 'textarea' . $item_index ) . '>' . $value . '</textarea>';
65 }
66
67 public function make_select_field( $item, $i ) {
68 $this->add_render_attribute(
69 [
70 'select-wrapper' . $i => [
71 'class' => [
72 'ehp-form__field',
73 'elementor-field',
74 'ehp-form__select',
75 'remove-before',
76 esc_attr( $item['css_classes'] ),
77 ],
78 ],
79 'select' . $i => [
80 'name' => $this->get_attribute_name( $item ) . ( ! empty( $item['allow_multiple'] ) ? '[]' : '' ),
81 'id' => $this->get_attribute_id( $item ),
82 'class' => [
83 'elementor-field-textual',
84 'elementor-size-' . $item['input_size'],
85 ],
86 ],
87 ]
88 );
89
90 if ( $item['required'] ) {
91 $this->add_required_attribute( 'select' . $i );
92 }
93
94 if ( $item['allow_multiple'] ) {
95 $this->add_render_attribute( 'select' . $i, 'multiple' );
96 if ( ! empty( $item['select_size'] ) ) {
97 $this->add_render_attribute( 'select' . $i, 'size', $item['select_size'] );
98 }
99 }
100
101 $options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] );
102
103 if ( ! $options ) {
104 return '';
105 }
106
107 ob_start();
108 ?>
109 <div <?php $this->print_render_attribute_string( 'select-wrapper' . $i ); ?>>
110 <div class="select-caret-down-wrapper">
111 <?php
112 if ( ! $item['allow_multiple'] ) {
113 $icon = [
114 'library' => 'eicons',
115 'value' => 'eicon-caret-down',
116 'position' => 'right',
117 ];
118 Icons_Manager::render_icon( $icon, [ 'aria-hidden' => 'true' ] );
119 }
120 ?>
121 </div>
122 <select <?php $this->print_render_attribute_string( 'select' . $i ); ?>>
123 <?php
124 foreach ( $options as $key => $option ) {
125 $option_id = esc_attr( $item['custom_id'] . $key );
126 $option_value = esc_attr( $option );
127 $option_label = $option;
128
129 if ( false !== strpos( $option, '|' ) ) {
130 list( $label, $value ) = explode( '|', $option );
131 $option_value = esc_attr( $value );
132 $option_label = $label;
133 }
134
135 $this->add_render_attribute( $option_id, 'value', $option_value );
136
137 // Support multiple selected values
138 if ( ! empty( $item['field_value'] ) && in_array( $option_value, explode( ',', $item['field_value'] ), true ) ) {
139 $this->add_render_attribute( $option_id, 'selected', 'selected' );
140 } ?>
141 <option <?php $this->print_render_attribute_string( $option_id ); ?>><?php
142 // PHPCS - $option_label is already escaped
143 echo esc_html( $option_label ); ?></option>
144 <?php } ?>
145 </select>
146 </div>
147 <?php
148
149 $select = ob_get_clean();
150 return $select;
151 }
152
153 protected function make_radio_checkbox_field( $item, $item_index, $type ): string {
154 $options = preg_split( "/\\r\\n|\\r|\\n/", $item['field_options'] );
155 $html = '';
156 if ( $options ) {
157 $html .= '<div class="elementor-field-subgroup ' . esc_attr( $item['css_classes'] ) . ' ' . esc_attr( $item['inline_list'] ) . '">';
158 foreach ( $options as $key => $option ) {
159 $element_id = esc_attr( $item['custom_id'] ) . $key;
160 $html_id = $this->get_attribute_id( $item ) . '-' . $key;
161 $option_label = $option;
162 $option_value = $option;
163 if ( false !== strpos( $option, '|' ) ) {
164 list( $option_label, $option_value ) = explode( '|', $option );
165 }
166
167 $this->add_render_attribute(
168 $element_id,
169 [
170 'type' => $type,
171 'value' => $option_value,
172 'id' => $html_id,
173 'name' => $this->get_attribute_name( $item ) . ( ( 'checkbox' === $type && count( $options ) > 1 ) ? '[]' : '' ),
174 ]
175 );
176
177 if ( ! empty( $item['field_value'] ) && $option_value === $item['field_value'] ) {
178 $this->add_render_attribute( $element_id, 'checked', 'checked' );
179 }
180
181 if ( $item['required'] && 'radio' === $type ) {
182 $this->add_required_attribute( $element_id );
183 }
184
185 $html .= '<span class="elementor-field-option"><input ' . $this->get_render_attribute_string( $element_id ) . '> <label for="' . $html_id . '">' . $option_label . '</label></span>';
186 }
187 $html .= '</div>';
188 }
189
190 return $html;
191 }
192
193 public function form_fields_render_attributes( $i, $instance, $item ) {
194 $this->add_render_attribute(
195 [
196 'field-group' . $i => [
197 'class' => [
198 'ehp-form__field-group',
199 'is-field-type-' . $item['field_type'],
200 'is-field-group-' . $item['custom_id'],
201 ],
202 ],
203 'input' . $i => [
204 'type' => $item['field_type'],
205 'name' => $this->get_attribute_name( $item ),
206 'id' => $this->get_attribute_id( $item ),
207 'class' => [
208 'ehp-form__field',
209 'elementor-size-' . $item['input_size'],
210 empty( $item['css_classes'] ) ? '' : esc_attr( $item['css_classes'] ),
211 ],
212 ],
213 'label' . $i => [
214 'for' => $this->get_attribute_id( $item ),
215 'class' => 'ehp-form__field-label',
216 ],
217 ]
218 );
219
220 if ( empty( $item['width'] ) ) {
221 $item['width'] = '100';
222 }
223
224 $this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-' . $item['width'] );
225
226 if ( $item['allow_multiple'] ) {
227 $this->add_render_attribute( 'field-group' . $i, 'class', 'elementor-field-type-' . $item['field_type'] . '-multiple' );
228 }
229
230 if ( ! empty( $item['width_tablet'] ) ) {
231 $this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-md-' . $item['width_tablet'] );
232 }
233
234 if ( ! empty( $item['width_mobile'] ) ) {
235 $this->add_render_attribute( 'field-group' . $i, 'class', 'has-width-sm-' . $item['width_mobile'] );
236 }
237
238 // Allow zero as placeholder.
239 if ( ! Utils::is_empty( $item['placeholder'] ) ) {
240 $this->add_render_attribute( 'input' . $i, 'placeholder', $item['placeholder'] );
241 }
242
243 if ( ! empty( $item['field_value'] ) ) {
244 $this->add_render_attribute( 'input' . $i, 'value', $item['field_value'] );
245 }
246
247 if ( ! $instance['show_labels'] ) {
248 $this->add_render_attribute( 'label' . $i, 'class', 'elementor-screen-only' );
249 }
250
251 if ( ! empty( $item['required'] ) ) {
252 $class = 'is-field-required';
253 if ( ! empty( $instance['mark_required'] ) ) {
254 $class .= ' is-mark-required';
255 }
256 $this->add_render_attribute( 'field-group' . $i, 'class', $class );
257 $this->add_required_attribute( 'input' . $i );
258 }
259
260 if ( 'yes' === $instance['field_border_switcher'] ) {
261 $this->add_render_attribute( 'field-group' . $i, 'class', 'has-border' );
262 }
263
264 if ( ! empty( $instance['fields_shape'] ) ) {
265 $this->add_render_attribute( 'field-group' . $i, 'class', 'has-shape-' . $instance['fields_shape'] );
266 }
267 }
268
269 public function render_plain_content() {}
270
271 public function get_attribute_name( $item ): string {
272 return "form_fields[{$item['custom_id']}]";
273 }
274
275 public function get_attribute_id( $item ): string {
276 return 'form-field-' . esc_attr( $item['custom_id'] );
277 }
278
279 private function add_required_attribute( $element ) {
280 $this->add_render_attribute( $element, 'required', 'required' );
281 $this->add_render_attribute( $element, 'aria-required', 'true' );
282 }
283
284 public function get_categories(): array {
285 return [ Theme_Module::HELLOPLUS_EDITOR_CATEGORY_SLUG ];
286 }
287 }
288