PluginProbe
Hello Plus / trunk
Hello Plus vtrunk
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 / render / widget-form-render.php

widget-form-render.php in Hello Plus trunk, at modules/forms/classes/render/widget-form-render.php

350 lines 11.0 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\Render;
3
4 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 use HelloPlus\Modules\Forms\Widgets\Ehp_Form;
9 use HelloPlus\Includes\Utils;
10 use Elementor\Icons_Manager;
11
12 use HelloPlus\Classes\{
13 Ehp_Column_Structure,
14 Ehp_Full_Height,
15 Ehp_Image,
16 Ehp_Shapes,
17 Widget_Utils,
18 };
19
20 class Widget_Form_Render {
21 protected Ehp_Form $widget;
22 protected array $settings;
23
24 const LAYOUT_CLASSNAME = 'ehp-form';
25
26 public function render() {
27 $layout_full_height_controls = $this->settings['box_full_screen_height_controls'] ?? '';
28 $layout_preset = $this->settings['layout_preset'] ?? '';
29
30 $layout_classnames = [
31 self::LAYOUT_CLASSNAME,
32 'has-layout-preset-' . $this->settings['layout_preset'],
33 ];
34
35 if ( ! empty( $layout_full_height_controls ) ) {
36 foreach ( $layout_full_height_controls as $breakpoint ) {
37 $layout_classnames[] = ' is-full-height-' . $breakpoint;
38 }
39 }
40
41 if ( 'yes' === $this->settings['show_box_border'] ) {
42 $layout_classnames[] = 'has-border';
43 }
44
45 if ( 'yes' === $this->settings['image_stretch'] ) {
46 $layout_classnames[] = 'has-image-stretch';
47 }
48
49 $shapes = new Ehp_Shapes( $this->widget, [
50 'container_prefix' => 'box',
51 'render_attribute' => 'layout',
52 'widget_name' => 'form',
53 ] );
54 $shapes->add_shape_attributes();
55
56 $ehp_full_height = new Ehp_Full_Height( $this->widget );
57 $ehp_full_height->add_full_height_attributes();
58
59 $column_structure = new Ehp_Column_Structure( $this->widget, [
60 'render_attribute' => 'layout',
61 ] );
62 $column_structure->add_column_structure_attributes();
63
64 $this->widget->add_render_attribute( 'layout', [
65 'name' => $this->settings['form_name'] ?? '',
66 'class' => $layout_classnames,
67 'method' => 'post',
68 'id' => $this->settings['form_id'] ?? '',
69 ] );
70
71 $this->widget->add_render_attribute( 'wrapper', [
72 'class' => self::LAYOUT_CLASSNAME . '__wrapper',
73 ] );
74
75 $referer_title = trim( wp_title( '', false ) );
76
77 if ( ! $referer_title && is_home() ) {
78 $referer_title = get_option( 'blogname' );
79 }
80
81 $this->widget->add_render_attribute( 'overlay', [
82 'class' => self::LAYOUT_CLASSNAME . '__overlay',
83 ] );
84
85 $this->widget->add_render_attribute( 'content', 'class', self::LAYOUT_CLASSNAME . '__content' );
86
87 ?>
88 <form <?php $this->widget->print_render_attribute_string( 'layout' ); ?>>
89 <?php if ( 'engage' !== $layout_preset ) {
90 $this->render_text_container();
91 } else {
92 $this->render_image_container();
93 } ?>
94 <input type="hidden" name="post_id" value="<?php echo (int) Utils::get_current_post_id(); ?>"/>
95 <input type="hidden" name="form_id" value="<?php echo esc_attr( $this->widget->get_id() ); ?>"/>
96 <input type="hidden" name="referer_title" value="<?php echo esc_attr( $referer_title ); ?>"/>
97
98 <?php if ( is_singular() ) {
99 // `queried_id` may be different from `post_id` on Single theme builder templates.
100 ?>
101 <input type="hidden" name="queried_id" value="<?php echo (int) get_the_ID(); ?>"/>
102 <?php } ?>
103
104 <div <?php $this->widget->print_render_attribute_string( 'content' ); ?>>
105 <?php if ( 'engage' === $layout_preset ) {
106 $this->render_text_container();
107 } ?>
108 <div <?php $this->widget->print_render_attribute_string( 'wrapper' ); ?>>
109 <?php
110 foreach ( $this->settings['form_fields'] as $item_index => $item ) :
111 $item['input_size'] = $this->settings['input_size'];
112 $this->widget->form_fields_render_attributes( $item_index, $this->settings, $item );
113
114 $field_type = $item['field_type'];
115
116 /**
117 * Render form field.
118 *
119 * Filters the field rendered by Elementor forms.
120 *
121 * @param array $item The field value.
122 * @param int $item_index The field index.
123 * @param Ehp_Form $this An instance of the form.
124 *
125 * @since 1.0.0
126 *
127 */
128 $item = apply_filters( 'hello_plus/forms/render/item', $item, $item_index, $this );
129
130 /**
131 * Render form field.
132 *
133 * Filters the field rendered by Elementor forms.
134 *
135 * The dynamic portion of the hook name, `$field_type`, refers to the field type.
136 *
137 * @param array $item The field value.
138 * @param int $item_index The field index.
139 * @param Ehp_Form $this An instance of the form.
140 *
141 * @since 1.0.0
142 *
143 */
144 $item = apply_filters( "hello_plus/forms/render/item/{$field_type}", $item, $item_index, $this );
145
146 $print_label = ! in_array( $item['field_type'], [ 'hidden', 'html', 'step' ], true );
147 ?>
148 <div <?php $this->widget->print_render_attribute_string( 'field-group' . $item_index ); ?>>
149 <?php
150 if ( $print_label && $item['field_label'] ) {
151 ?>
152 <label <?php $this->widget->print_render_attribute_string( 'label' . $item_index ); ?>>
153 <?php
154 echo esc_html( $item['field_label'] ); ?>
155 </label>
156 <?php
157 }
158
159 switch ( $item['field_type'] ) :
160 case 'textarea':
161 echo wp_kses(
162 $this->widget->make_textarea_field( $item, $item_index, $this->settings ),
163 [
164 'textarea' => [
165 'cols' => true,
166 'rows' => true,
167 'name' => true,
168 'id' => true,
169 'class' => true,
170 'style' => true,
171 'placeholder' => true,
172 'maxlength' => true,
173 'required' => true,
174 'readonly' => true,
175 'disabled' => true,
176 ],
177 ]
178 );
179 break;
180
181 case 'select':
182 echo wp_kses( $this->widget->make_select_field( $item, $item_index ), [
183 'select' => [
184 'name' => true,
185 'id' => true,
186 'class' => true,
187 'style' => true,
188 'required' => true,
189 'disabled' => true,
190 ],
191 'option' => [
192 'value' => true,
193 'selected' => true,
194 ],
195 ] );
196 break;
197
198 case 'text':
199 case 'email':
200 $this->widget->add_render_attribute( 'input' . $item_index, 'class', 'elementor-field-textual' );
201 ?>
202 <input size="1" <?php $this->widget->print_render_attribute_string( 'input' . $item_index ); ?>>
203 <?php
204 break;
205
206 default:
207 $field_type = $item['field_type'];
208
209 /**
210 * Hello+ form field render.
211 *
212 * Fires when a field is rendered in the frontend. This hook allows developers to
213 * add functionality when from fields are rendered.
214 *
215 * The dynamic portion of the hook name, `$field_type`, refers to the field type.
216 *
217 * @param array $item The field value.
218 * @param int $item_index The field index.
219 * @param Ehp_Form $this An instance of the form.
220 *
221 * @since 1.0.0
222 *
223 */
224 do_action( "hello_plus/forms/render_field/{$field_type}", $item, $item_index, $this->widget );
225 endswitch;
226 ?>
227 </div>
228 <?php endforeach; ?>
229 <?php $this->render_button(); ?>
230 </div>
231 </div>
232 <div <?php $this->widget->print_render_attribute_string( 'overlay' ); ?>></div>
233 </form>
234 <?php
235 }
236
237 protected function render_button(): void {
238 $button_classnames = [ self::LAYOUT_CLASSNAME . '__button' ];
239 $submit_group_classnames = [ self::LAYOUT_CLASSNAME . '__submit-group' ];
240
241 if ( ! empty( $this->settings['button_width'] ) ) {
242 $submit_group_classnames[] = 'has-width-' . $this->settings['button_width'];
243
244 if ( ! empty( $this->settings['button_width_tablet'] ) ) {
245 $submit_group_classnames[] = 'has-width-md-' . $this->settings['button_width_tablet'];
246 }
247
248 if ( ! empty( $this->settings['button_width_mobile'] ) ) {
249 $submit_group_classnames[] = 'has-width-sm-' . $this->settings['button_width_mobile'];
250 }
251 }
252
253 if ( ! empty( $this->settings['button_align'] ) ) {
254 $submit_group_classnames[] = 'has-button-align-' . $this->settings['button_align'];
255
256 if ( ! empty( $this->settings['button_align_tablet'] ) ) {
257 $submit_group_classnames[] = 'has-button-align-md-' . $this->settings['button_align_tablet'];
258 }
259
260 if ( ! empty( $this->settings['button_align_mobile'] ) ) {
261 $submit_group_classnames[] = 'has-button-align-sm-' . $this->settings['button_align_mobile'];
262 }
263 }
264
265 $this->widget->add_render_attribute( 'submit-group', [
266 'class' => $submit_group_classnames,
267 ] );
268
269 if ( 'yes' === $this->settings['button_border_switcher'] ) {
270 $button_classnames[] = 'has-border';
271 }
272
273 $shapes = new Ehp_Shapes( $this->widget, [
274 'container_prefix' => 'button',
275 'render_attribute' => 'button',
276 'widget_name' => 'form',
277 ] );
278 $shapes->add_shape_attributes();
279
280 if ( ! empty( $this->settings['button_type'] ) ) {
281 $button_classnames[] = 'is-type-' . $this->settings['button_type'];
282 }
283
284 if ( $this->settings['button_hover_animation'] ) {
285 $button_classnames[] = 'elementor-animation-' . $this->settings['button_hover_animation'];
286 }
287
288 $this->widget->add_render_attribute( 'button', [
289 'class' => $button_classnames,
290 'type' => 'submit',
291 ] );
292
293 if ( ! empty( $this->settings['button_css_id'] ) ) {
294 $this->widget->add_render_attribute( 'button', 'id', $this->settings['button_css_id'] );
295 }
296
297 $this->widget->add_render_attribute( 'button-text', [
298 'class' => self::LAYOUT_CLASSNAME . '__button-text',
299 ] );
300 ?>
301 <div <?php $this->widget->print_render_attribute_string( 'submit-group' ); ?>>
302 <button <?php $this->widget->print_render_attribute_string( 'button' ); ?>>
303 <?php if ( ! empty( $this->settings['selected_button_icon'] ) || ! empty( $this->settings['selected_button_icon']['value'] ) ) : ?>
304 <?php
305 Icons_Manager::render_icon( $this->settings['selected_button_icon'],
306 [
307 'aria-hidden' => 'true',
308 'class' => self::LAYOUT_CLASSNAME . '__button-icon',
309 ],
310 );
311 ?>
312 <?php endif; ?>
313
314 <?php if ( ! empty( $this->settings['button_text'] ) ) : ?>
315 <span <?php $this->widget->print_render_attribute_string( 'button-text' ); ?>><?php $this->widget->print_unescaped_setting( 'button_text' ); ?></span>
316 <?php endif; ?>
317 </button>
318 </div>
319 <?php
320 }
321
322 protected function render_text_container(): void {
323 $heading_classname = self::LAYOUT_CLASSNAME . '__heading';
324 $description_classname = self::LAYOUT_CLASSNAME . '__description';
325
326 $this->widget->add_render_attribute( 'text-container', [
327 'class' => self::LAYOUT_CLASSNAME . '__text-container',
328 ] );
329 ?>
330 <div <?php $this->widget->print_render_attribute_string( 'text-container' ); ?>>
331 <?php
332 Widget_Utils::maybe_render_text_html( $this->widget, 'text_heading', $heading_classname, $this->settings['text_heading'], $this->settings['text_heading_tag'] );
333 Widget_Utils::maybe_render_text_html( $this->widget, 'text_description', $description_classname, $this->settings['text_description'] );
334 ?>
335 </div>
336 <?php
337 }
338
339 protected function render_image_container() {
340 $image = new Ehp_Image( $this->widget, [
341 'widget_name' => 'form',
342 ] );
343 $image->render();
344 }
345 public function __construct( Ehp_Form $widget ) {
346 $this->widget = $widget;
347 $this->settings = $widget->get_settings_for_display();
348 }
349 }
350