PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0-beta2
Elementor Website Builder – more than just a page builder v4.2.0-beta2
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / modules / atomic-widgets / elements / atomic-form / atomic-form.php

atomic-form.php in Elementor Website Builder – more than just a page builder 4.2.0-beta2, at modules/atomic-widgets/elements/atomic-form/atomic-form.php

531 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\AtomicWidgets\Elements\Atomic_Form;
3
4 use Elementor\Modules\AtomicWidgets\Controls\Section;
5 use Elementor\Modules\AtomicWidgets\Controls\Types\Chips_Control;
6 use Elementor\Modules\AtomicWidgets\Controls\Types\Email_Form_Action_Control;
7 use Elementor\Modules\AtomicWidgets\Controls\Types\Switch_Control;
8 use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control;
9 use Elementor\Modules\AtomicWidgets\Controls\Types\Toggle_Control;
10 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph\Atomic_Paragraph;
11 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Success_Message\Form_Success_Message;
12 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Error_Message\Form_Error_Message;
13 use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
14 use Elementor\Modules\AtomicWidgets\Elements\Base\Element_Builder;
15 use Elementor\Modules\AtomicWidgets\Elements\Base\Has_Element_Template;
16 use Elementor\Modules\AtomicWidgets\Elements\Base\Widget_Builder;
17 use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager;
18 use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type;
19 use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
20 use Elementor\Modules\AtomicWidgets\PropTypes\Emails_Prop_Type;
21 use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type;
22 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type;
23 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
24 use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
25 use Elementor\Modules\AtomicWidgets\PropTypes\Html_V3_Prop_Type;
26 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Array_Prop_Type;
27 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
28 use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
29 use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
30 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
31 use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type;
32
33 if ( ! defined( 'ABSPATH' ) ) {
34 exit; // Exit if accessed directly.
35 }
36
37 class Atomic_Form extends Atomic_Element_Base {
38 use Has_Element_Template;
39
40 const BASE_STYLE_KEY = 'base';
41
42 public static $widget_description = 'A form container that holds form field widgets (labels, inputs, textareas, checkboxes, submit button) and status messages.';
43
44 public const ACTION_EMAIL = 'email';
45 public const ACTION_COLLECT_SUBMISSIONS = 'collect-submissions';
46 public const ACTION_WEBHOOK = 'webhook';
47 public const METADATA_REMOTE_IP = 'remote_ip';
48 public const METADATA_USER_AGENT = 'user_agent';
49
50
51
52 public function __construct( $data = [], $args = null ) {
53 parent::__construct( $data, $args );
54 $this->meta( 'is_container', true );
55 }
56
57 public static function get_default_recipient_email(): string {
58 return sanitize_email( (string) get_option( 'admin_email', '' ) );
59 }
60
61 public static function get_default_sender_email(): string {
62 return sanitize_email( (string) 'email@' . wp_parse_url( home_url(), PHP_URL_HOST ) );
63 }
64
65 public static function get_type() {
66 return 'e-form';
67 }
68
69 public static function get_element_type(): string {
70 return self::get_type();
71 }
72
73 public function get_title() {
74 return esc_html__( 'Atomic form', 'elementor' );
75 }
76
77 public function get_keywords() {
78 return [ 'atomic', 'form' ];
79 }
80
81 public function get_icon() {
82 return 'eicon-atomic-form';
83 }
84
85 public static function get_base_props_schema(): array {
86 return self::define_props_schema();
87 }
88
89 protected static function define_props_schema(): array {
90 $submissions_metadata_dependencies = Dependency_Manager::make()
91 ->where( [
92 'operator' => 'contains',
93 'path' => [ 'actions-after-submit' ],
94 'value' => self::ACTION_COLLECT_SUBMISSIONS,
95 'effect' => 'hide',
96 ] )
97 ->get();
98
99 $webhook_dependencies = Dependency_Manager::make()
100 ->where( [
101 'operator' => 'contains',
102 'path' => [ 'actions-after-submit' ],
103 'value' => self::ACTION_WEBHOOK,
104 'effect' => 'hide',
105 ] )
106 ->get();
107
108 $props = [
109 'classes' => Classes_Prop_Type::make()
110 ->default( [] ),
111 'form-name' => String_Prop_Type::make()
112 ->default( __( 'Form', 'elementor' ) ),
113 'webmcp-autosubmit' => Boolean_Prop_Type::make()
114 ->default( false ),
115 'form-state' => String_Prop_Type::make()
116 ->enum( [ 'default', 'success', 'error' ] )
117 ->default( 'default' )
118 ->meta( 'generates_class', 'form-state-{value}' ),
119 'actions-after-submit' => String_Array_Prop_Type::make()
120 ->initial_value( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] )
121 ->default( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ),
122 'submissions_metadata' => String_Array_Prop_Type::make()
123 ->set_dependencies( $submissions_metadata_dependencies )
124 ->default( [
125 String_Prop_Type::generate( self::METADATA_REMOTE_IP ),
126 String_Prop_Type::generate( self::METADATA_USER_AGENT ),
127 ] ),
128 ];
129
130 $props = array_merge( $props, self::get_emails_prop_settings(), [
131 'webhook_url' => String_Prop_Type::make()
132 ->set_dependencies( $webhook_dependencies )
133 ->meta( Overridable_Prop_Type::ignore() )
134 ->default( '' ),
135 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ),
136 ] );
137
138 return $props;
139 }
140
141 protected function define_atomic_controls(): array {
142 $state_control = Toggle_Control::bind_to( 'form-state' )
143 ->add_options( [
144 'default' => [
145 'title' => __( 'Normal', 'elementor' ),
146 ],
147 'success' => [
148 'title' => __( 'Success', 'elementor' ),
149 ],
150 'error' => [
151 'title' => __( 'Error', 'elementor' ),
152 ],
153 ] )
154 ->set_exclusive( true )
155 ->set_convert_options( true )
156 ->set_size( 'tiny' )
157 ->set_full_width( true )
158 ->set_label( __( 'States', 'elementor' ) )
159 ->set_meta( [ 'topDivider' => true ] );
160
161 $email_control_settings = $this->get_emails_control_settings();
162 $form_action_chips = $email_control_settings['form-action-chips'];
163 $email_controls = $email_control_settings['email-controls'];
164
165 if ( class_exists( '\ElementorPro\License\API' ) ) {
166 $has_form_submissions_feature = \ElementorPro\License\API::is_licence_has_feature( 'form-submissions' );
167
168 if ( $has_form_submissions_feature ) {
169 $form_action_chips = array_merge( $form_action_chips, [
170 [
171 'label' => __( 'Collect submissions', 'elementor' ),
172 'value' => self::ACTION_COLLECT_SUBMISSIONS,
173 ],
174 ] );
175 }
176 }
177
178 $content_controls = [
179 Text_Control::bind_to( 'form-name' )
180 ->set_label( __( 'Form name', 'elementor' ) ),
181 $state_control,
182 Chips_Control::bind_to( 'actions-after-submit' )
183 ->set_options( array_merge( $form_action_chips, [
184 [
185 'label' => __( 'Webhook', 'elementor' ),
186 'value' => self::ACTION_WEBHOOK,
187 ],
188 ] ) )
189 ->set_label( __( 'Actions after submit', 'elementor' ) )
190 ->set_meta( [ 'topDivider' => true ] ),
191 ];
192
193 return [
194 Section::make()
195 ->set_label( __( 'Content', 'elementor' ) )
196 ->set_id( 'content' )
197 ->set_items( $content_controls ),
198 ...$email_controls,
199 Section::make()
200 ->set_label( __( 'Collect submissions', 'elementor' ) )
201 ->set_items( [
202 Chips_Control::bind_to( 'submissions_metadata' )
203 ->set_options( [
204 [
205 'label' => __( 'User IP', 'elementor' ),
206 'value' => self::METADATA_REMOTE_IP,
207 ],
208 [
209 'label' => __( 'User Agent', 'elementor' ),
210 'value' => self::METADATA_USER_AGENT,
211 ],
212 ] )
213 ->set_label( __( 'Include metadata', 'elementor' ) ),
214 ] ),
215 Section::make()
216 ->set_label( __( 'Webhook', 'elementor' ) )
217 ->set_items( [
218 Text_Control::bind_to( 'webhook_url' )
219 ->set_placeholder( __( 'https://your-webhook-url.com', 'elementor' ) )
220 ->set_label( __( 'Webhook URL', 'elementor' ) ),
221 ] ),
222 Section::make()
223 ->set_label( __( 'Settings', 'elementor' ) )
224 ->set_id( 'settings' )
225 ->set_items( [
226 Text_Control::bind_to( '_cssid' )
227 ->set_label( __( 'ID', 'elementor' ) )
228 ->set_meta( $this->get_css_id_control_meta() ),
229 Switch_Control::bind_to( 'webmcp-autosubmit' )
230 ->set_label( __( 'Agent auto-submit', 'elementor' ) ),
231 ] ),
232 ];
233 }
234
235 protected function define_base_settings(): array {
236 $settings = [];
237
238 foreach ( self::build_email_action_defaults() as $key => $default_email ) {
239 $settings[ $key ] = Emails_Prop_Type::generate( $default_email );
240 }
241
242 return $settings;
243 }
244
245 protected function define_base_styles(): array {
246 return [
247 static::BASE_STYLE_KEY => Style_Definition::make()
248 ->add_variant(
249 Style_Variant::make()
250 ->set_breakpoint( Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP )
251 ->add_prop( 'display', String_Prop_Type::generate( 'flex' ) )
252 ->add_prop( 'flex', String_Prop_Type::generate( '1' ) )
253 ->add_prop( 'flex-direction', String_Prop_Type::generate( 'row' ) )
254 ->add_prop( 'flex-wrap', String_Prop_Type::generate( 'wrap' ) )
255 ->add_prop( 'align-items', String_Prop_Type::generate( 'flex-start' ) )
256 ->add_prop( 'align-content', String_Prop_Type::generate( 'start' ) )
257 ->add_prop( 'gap', Size_Prop_Type::generate( [
258 'size' => 10,
259 'unit' => 'px',
260 ] ) )
261 ->add_prop( 'padding', Size_Prop_Type::generate( [
262 'size' => 20,
263 'unit' => 'px',
264 ] ) )
265 ),
266 static::BASE_STYLE_KEY . ' .e-form-checkbox-row' => Style_Definition::make()
267 ->add_variant(
268 Style_Variant::make()
269 ->add_prop( 'align-items', String_Prop_Type::generate( 'center' ) )
270 ->add_prop( 'gap', Size_Prop_Type::generate( [
271 'size' => 8,
272 'unit' => 'px',
273 ] ) )
274 ->add_prop( 'padding', Size_Prop_Type::generate( [
275 'size' => 0,
276 'unit' => 'px',
277 ] ) )
278 ),
279 ];
280 }
281
282 protected function define_panel_categories(): array {
283 return [ 'atomic-form' ];
284 }
285
286 protected function define_default_html_tag() {
287 return 'form';
288 }
289
290 protected function define_default_children() {
291
292 $prefix = 'e-form-';
293
294 $children = [
295 $this->build_label( __( 'First name', 'elementor' ), $prefix . 'first-name' ),
296 $this->build_input( __( 'First name', 'elementor' ), 'text', $prefix . 'first-name' ),
297
298 $this->build_label( __( 'Last name', 'elementor' ), $prefix . 'last-name' ),
299 $this->build_input( __( 'Last name', 'elementor' ), 'text', $prefix . 'last-name' ),
300
301 $this->build_label( __( 'Email', 'elementor' ), $prefix . 'email' ),
302 $this->build_input( __( '[email protected]', 'elementor' ), 'email', $prefix . 'email' ),
303
304 $this->build_label( __( 'Message', 'elementor' ), $prefix . 'message' ),
305 $this->build_input( __( 'Your message', 'elementor' ), 'textarea', $prefix . 'message' ),
306 ];
307
308 $children[] = $this->build_checkbox_row( __( 'Checkbox', 'elementor' ), $prefix . 'checkbox' );
309
310 $children[] = Widget_Builder::make( 'e-form-submit-button' )
311 ->settings( [
312 'text' => Html_V3_Prop_Type::generate( [
313 'content' => String_Prop_Type::generate( __( 'Submit', 'elementor' ) ),
314 'children' => [],
315 ] ),
316 ] )
317 ->build();
318 $children[] = $this->build_status_message(
319 __( 'Great! We’ve received your information.', 'elementor' ),
320 'success',
321 __( 'Success message', 'elementor' )
322 );
323 $children[] = $this->build_status_message(
324 __( 'We couldn’t process your submission. Please retry', 'elementor' ),
325 'error',
326 __( 'Error message', 'elementor' )
327 );
328
329 return $children;
330 }
331
332 private function build_checkbox_row( string $label_text, string $checkbox_id ): array {
333 $checkbox = Widget_Builder::make( 'e-form-checkbox' )
334 ->settings( [
335 '_cssid' => String_Prop_Type::generate( $checkbox_id ),
336 ] )
337 ->build();
338
339 $label = $this->build_label( $label_text, $checkbox_id );
340
341 return Element_Builder::make( 'e-flexbox' )
342 ->children( [ $checkbox, $label ] )
343 ->settings( [
344 'classes' => Classes_Prop_Type::generate( [ 'e-form-checkbox-row' ] ),
345 ] )
346 ->build();
347 }
348
349 private function build_label( string $text, string $input_id ): array {
350 return Widget_Builder::make( 'e-form-label' )
351 ->settings( [
352 'text' => Html_V3_Prop_Type::generate( [
353 'content' => String_Prop_Type::generate( $text ),
354 'children' => [],
355 ] ),
356 'input-id' => String_Prop_Type::generate( $input_id ),
357 ] )
358 ->build();
359 }
360
361 private function build_input( string $placeholder, string $type = 'text', $input_id = '' ): array {
362 if ( 'textarea' === $type ) {
363 return Widget_Builder::make( 'e-form-textarea' )
364 ->settings( [
365 'placeholder' => String_Prop_Type::generate( $placeholder ),
366 'rows' => Number_Prop_Type::generate( 4 ),
367 '_cssid' => String_Prop_Type::generate( $input_id ),
368 ] )
369 ->build();
370 }
371
372 return Widget_Builder::make( 'e-form-input' )
373 ->settings( [
374 'placeholder' => String_Prop_Type::generate( $placeholder ),
375 'type' => String_Prop_Type::generate( $type ),
376 '_cssid' => String_Prop_Type::generate( $input_id ),
377 ] )
378 ->build();
379 }
380
381 private function build_status_message( string $message, string $state, string $title ): array {
382 $paragraph_value = Html_V3_Prop_Type::generate( [
383 'content' => String_Prop_Type::generate( $message ),
384 'children' => [],
385 ] );
386
387 $element_type = 'success' === $state
388 ? Form_Success_Message::get_element_type()
389 : Form_Error_Message::get_element_type();
390
391 return Element_Builder::make( $element_type )
392 ->meta( [ 'required' => true ] )
393 ->settings( [
394 'attributes' => Attributes_Prop_Type::generate( [
395 Key_Value_Prop_Type::generate( [] ),
396 ] ),
397 ] )
398 ->editor_settings( [
399 'title' => $title,
400 ] )
401 ->children( [
402 Widget_Builder::make( Atomic_Paragraph::get_element_type() )
403 ->settings( [
404 'paragraph' => $paragraph_value,
405 ] )
406 ->build(),
407 ] )
408 ->build();
409 }
410
411 protected function get_templates(): array {
412 return [
413 'elementor/elements/atomic-form' => __DIR__ . '/atomic-form.html.twig',
414 ];
415 }
416
417 protected function build_template_context(): array {
418 $context = $this->build_base_template_context();
419
420 $context['form_state'] = 'default';
421
422 $form_name = (string) ( $this->get_atomic_settings()['form-name'] ?? '' );
423 $context['webmcp_tool_name'] = Webmcp_Utils::build_tool_name( $form_name, (string) $this->get_id() );
424 $context['webmcp_tool_description'] = Webmcp_Utils::build_tool_description( $form_name );
425
426 return $context;
427 }
428
429 public static function is_instance_form( $instance ): bool {
430 return $instance instanceof Atomic_Form;
431 }
432
433 public function render_markdown(): string {
434 return '';
435 }
436
437 private static function get_emails_prop_settings(): array {
438 $props = [];
439
440 foreach ( self::build_email_action_defaults() as $key => $default_value ) {
441 $props[ $key ] = Emails_Prop_Type::make()
442 ->set_dependencies( self::make_action_dependency( $key ) )
443 ->meta( Overridable_Prop_Type::ignore() )
444 ->initial_value( $default_value )
445 ->default( $default_value );
446 }
447
448 return $props;
449 }
450
451 private function get_emails_control_settings(): array {
452 $form_action_chips = [];
453 $email_controls = [];
454
455 for ( $i = 0; $i < self::get_email_action_count(); $i++ ) {
456 $key = self::get_email_action_key( $i );
457 $label = self::get_email_action_label( $i );
458
459 $form_action_chips[] = [
460 'label' => $label,
461 'value' => $key,
462 ];
463
464 $email_controls[] = Section::make()
465 ->set_label( $label )
466 ->set_items( [
467 Email_Form_Action_Control::bind_to( $key )
468 ->set_free_chips( true )
469 ->set_label( $label ),
470 ] );
471 }
472
473 return [
474 'form-action-chips' => $form_action_chips,
475 'email-controls' => $email_controls,
476 ];
477 }
478
479 private static function get_email_action_key( int $index ): string {
480 return 0 === $index
481 ? self::ACTION_EMAIL
482 : self::ACTION_EMAIL . '_' . ( $index + 1 );
483 }
484
485 private static function get_email_action_label( int $index ): string {
486 if ( 0 === $index ) {
487 return __( 'Email', 'elementor' );
488 }
489
490 // translators: %d is the index of the email action.
491 return sprintf( __( 'Email %d', 'elementor' ), $index + 1 );
492 }
493
494 private static function build_email_action_defaults(): array {
495 $defaults = [];
496 $default_email = self::get_default_email_value();
497
498 for ( $i = 0; $i < self::get_email_action_count(); $i++ ) {
499 $key = self::get_email_action_key( $i );
500 $defaults[ $key ] = $default_email;
501 }
502
503 return $defaults;
504 }
505
506 private static function get_default_email_value(): array {
507 return [
508 'to' => String_Array_Prop_Type::generate( [
509 String_Prop_Type::generate( self::get_default_recipient_email() ),
510 ] ),
511 'from' => String_Prop_Type::generate( self::get_default_sender_email() ),
512 'message' => String_Prop_Type::generate( '[all-fields]' ),
513 ];
514 }
515
516 private static function make_action_dependency( string $action_key ): ?array {
517 return Dependency_Manager::make()
518 ->where( [
519 'operator' => 'contains',
520 'path' => [ 'actions-after-submit' ],
521 'value' => $action_key,
522 'effect' => 'hide',
523 ] )
524 ->get();
525 }
526
527 private static function get_email_action_count(): int {
528 return apply_filters( 'elementor/atomic/form/email_action_count', 1 );
529 }
530 }
531