PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-beta2
Elementor Website Builder – more than just a page builder v4.1.0-beta2
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 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 All 453 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.1.0-beta2, at modules/atomic-widgets/elements/atomic-form/atomic-form.php

411 lines 13.2 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\Text_Control;
8 use Elementor\Modules\AtomicWidgets\Controls\Types\Toggle_Control;
9 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph\Atomic_Paragraph;
10 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Success_Message\Form_Success_Message;
11 use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Error_Message\Form_Error_Message;
12 use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base;
13 use Elementor\Modules\AtomicWidgets\Elements\Base\Element_Builder;
14 use Elementor\Modules\AtomicWidgets\Elements\Base\Has_Element_Template;
15 use Elementor\Modules\AtomicWidgets\Elements\Base\Widget_Builder;
16 use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager;
17 use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type;
18 use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type;
19 use Elementor\Modules\AtomicWidgets\PropTypes\Email_Prop_Type;
20 use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type;
21 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type;
22 use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type;
23 use Elementor\Modules\AtomicWidgets\PropTypes\Html_V3_Prop_Type;
24 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Array_Prop_Type;
25 use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type;
26 use Elementor\Modules\AtomicWidgets\Styles\Style_Definition;
27 use Elementor\Modules\AtomicWidgets\Styles\Style_Variant;
28 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
29 use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type;
30
31 if ( ! defined( 'ABSPATH' ) ) {
32 exit; // Exit if accessed directly.
33 }
34
35 class Atomic_Form extends Atomic_Element_Base {
36 use Has_Element_Template;
37
38 const BASE_STYLE_KEY = 'base';
39
40 public static $widget_description = 'A form container that holds form field widgets (labels, inputs, textareas, checkboxes, submit button) and status messages.';
41
42 public const ACTION_EMAIL = 'email';
43 public const ACTION_COLLECT_SUBMISSIONS = 'collect-submissions';
44 public const ACTION_WEBHOOK = 'webhook';
45 public const METADATA_REMOTE_IP = 'remote_ip';
46 public const METADATA_USER_AGENT = 'user_agent';
47
48
49
50 public function __construct( $data = [], $args = null ) {
51 parent::__construct( $data, $args );
52 $this->meta( 'is_container', true );
53 }
54
55 public static function get_default_recipient_email(): string {
56 return sanitize_email( (string) get_option( 'admin_email', '' ) );
57 }
58
59 public static function get_default_sender_email(): string {
60 return sanitize_email( (string) 'email@' . wp_parse_url( home_url(), PHP_URL_HOST ) );
61 }
62
63 public static function get_type() {
64 return 'e-form';
65 }
66
67 public static function get_element_type(): string {
68 return self::get_type();
69 }
70
71 public function get_title() {
72 return esc_html__( 'Atomic form', 'elementor' );
73 }
74
75 public function get_keywords() {
76 return [ 'atomic', 'form' ];
77 }
78
79 public function get_icon() {
80 return 'eicon-atomic-form';
81 }
82
83 protected static function define_props_schema(): array {
84 $email_dependencies = Dependency_Manager::make()
85 ->where( [
86 'operator' => 'contains',
87 'path' => [ 'actions-after-submit' ],
88 'value' => self::ACTION_EMAIL,
89 'effect' => 'hide',
90 ] )
91 ->get();
92
93 $submissions_metadata_dependencies = Dependency_Manager::make()
94 ->where( [
95 'operator' => 'contains',
96 'path' => [ 'actions-after-submit' ],
97 'value' => self::ACTION_COLLECT_SUBMISSIONS,
98 'effect' => 'hide',
99 ] )
100 ->get();
101
102 $webhook_dependencies = Dependency_Manager::make()
103 ->where( [
104 'operator' => 'contains',
105 'path' => [ 'actions-after-submit' ],
106 'value' => self::ACTION_WEBHOOK,
107 'effect' => 'hide',
108 ] )
109 ->get();
110
111 return [
112 'classes' => Classes_Prop_Type::make()
113 ->default( [] ),
114 'form-name' => String_Prop_Type::make()
115 ->default( __( 'Form', 'elementor' ) ),
116 'form-state' => String_Prop_Type::make()
117 ->enum( [ 'default', 'success', 'error' ] )
118 ->default( 'default' )
119 ->meta( 'generates_class', 'form-state-{value}' ),
120 'actions-after-submit' => String_Array_Prop_Type::make()
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 'email' => Email_Prop_Type::make()
129 ->set_dependencies( $email_dependencies )
130 ->meta( Overridable_Prop_Type::ignore() )
131 ->default( [
132 'to' => String_Prop_Type::generate( self::get_default_recipient_email() ),
133 'from' => String_Prop_Type::generate( self::get_default_sender_email() ),
134 ] ),
135 'webhook_url' => String_Prop_Type::make()
136 ->set_dependencies( $webhook_dependencies )
137 ->meta( Overridable_Prop_Type::ignore() )
138 ->default( '' ),
139 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ),
140 ];
141 }
142
143 protected function define_atomic_controls(): array {
144 $state_control = Toggle_Control::bind_to( 'form-state' )
145 ->set_label( __( 'States', 'elementor' ) )
146 ->set_meta( [ 'topDivider' => true ] );
147
148 if ( $state_control instanceof Toggle_Control ) {
149 $state_control
150 ->add_options( [
151 'default' => [
152 'title' => __( 'Normal', 'elementor' ),
153 ],
154 'success' => [
155 'title' => __( 'Success', 'elementor' ),
156 ],
157 'error' => [
158 'title' => __( 'Error', 'elementor' ),
159 ],
160 ] )
161 ->set_exclusive( true )
162 ->set_convert_options( true )
163 ->set_size( 'tiny' )
164 ->set_full_width( true );
165 }
166
167 return [
168 Section::make()
169 ->set_label( __( 'Content', 'elementor' ) )
170 ->set_items( [
171 Text_Control::bind_to( 'form-name' )
172 ->set_label( __( 'Form name', 'elementor' ) ),
173 $state_control,
174 Chips_Control::bind_to( 'actions-after-submit' )
175 ->set_label( __( 'Actions after submit', 'elementor' ) )
176 ->set_meta( [ 'topDivider' => true ] )
177 ->set_options( [
178 [
179 'label' => __( 'Email', 'elementor' ),
180 'value' => self::ACTION_EMAIL,
181 ],
182 [
183 'label' => __( 'Collect submissions', 'elementor' ),
184 'value' => self::ACTION_COLLECT_SUBMISSIONS,
185 ],
186 [
187 'label' => __( 'Webhook', 'elementor' ),
188 'value' => self::ACTION_WEBHOOK,
189 ],
190 ] ),
191 Email_Form_Action_Control::bind_to( 'email' )
192 ->set_meta( [
193 'topDivider' => true,
194 ] ),
195 Chips_Control::bind_to( 'submissions_metadata' )
196 ->set_label( __( 'Include metadata', 'elementor' ) )
197 ->set_meta( [ 'topDivider' => true ] )
198 ->set_options( [
199 [
200 'label' => __( 'User IP', 'elementor' ),
201 'value' => self::METADATA_REMOTE_IP,
202 ],
203 [
204 'label' => __( 'User Agent', 'elementor' ),
205 'value' => self::METADATA_USER_AGENT,
206 ],
207 ] ),
208 Text_Control::bind_to( 'webhook_url' )
209 ->set_label( __( 'Webhook URL', 'elementor' ) )
210 ->set_placeholder( __( 'https://your-webhook-url.com', 'elementor' ) )
211 ->set_meta( [ 'topDivider' => true ] ),
212 ] ),
213 Section::make()
214 ->set_label( __( 'Settings', 'elementor' ) )
215 ->set_id( 'settings' )
216 ->set_items( [
217 Text_Control::bind_to( '_cssid' )
218 ->set_label( __( 'ID', 'elementor' ) )
219 ->set_meta( $this->get_css_id_control_meta() ),
220 ] ),
221 ];
222 }
223
224 protected function define_base_styles(): array {
225 return [
226 static::BASE_STYLE_KEY => Style_Definition::make()
227 ->add_variant(
228 Style_Variant::make()
229 ->set_breakpoint( Breakpoints_Manager::BREAKPOINT_KEY_DESKTOP )
230 ->add_prop( 'display', String_Prop_Type::generate( 'flex' ) )
231 ->add_prop( 'flex', String_Prop_Type::generate( '1' ) )
232 ->add_prop( 'flex-direction', String_Prop_Type::generate( 'row' ) )
233 ->add_prop( 'flex-wrap', String_Prop_Type::generate( 'wrap' ) )
234 ->add_prop( 'align-items', String_Prop_Type::generate( 'flex-start' ) )
235 ->add_prop( 'align-content', String_Prop_Type::generate( 'start' ) )
236 ->add_prop( 'gap', Size_Prop_Type::generate( [
237 'size' => 10,
238 'unit' => 'px',
239 ] ) )
240 ->add_prop( 'padding', Size_Prop_Type::generate( [
241 'size' => 20,
242 'unit' => 'px',
243 ] ) )
244 ),
245 static::BASE_STYLE_KEY . ' .e-form-checkbox-row' => Style_Definition::make()
246 ->add_variant(
247 Style_Variant::make()
248 ->add_prop( 'align-items', String_Prop_Type::generate( 'center' ) )
249 ->add_prop( 'gap', Size_Prop_Type::generate( [
250 'size' => 8,
251 'unit' => 'px',
252 ] ) )
253 ->add_prop( 'padding', Size_Prop_Type::generate( [
254 'size' => 0,
255 'unit' => 'px',
256 ] ) )
257 ),
258 ];
259 }
260
261 protected function define_panel_categories(): array {
262 return [ 'atomic-form' ];
263 }
264
265 protected function define_default_html_tag() {
266 return 'form';
267 }
268
269 protected function define_default_children() {
270
271 $prefix = 'e-form-';
272
273 $children = [
274 $this->build_label( __( 'First name', 'elementor' ), $prefix . 'first-name' ),
275 $this->build_input( __( 'First name', 'elementor' ), 'text', $prefix . 'first-name' ),
276
277 $this->build_label( __( 'Last name', 'elementor' ), $prefix . 'last-name' ),
278 $this->build_input( __( 'Last name', 'elementor' ), 'text', $prefix . 'last-name' ),
279
280 $this->build_label( __( 'Email', 'elementor' ), $prefix . 'email' ),
281 $this->build_input( __( 'your@mail.com', 'elementor' ), 'email', $prefix . 'email' ),
282
283 $this->build_label( __( 'Message', 'elementor' ), $prefix . 'message' ),
284 $this->build_input( __( 'Your message', 'elementor' ), 'textarea', $prefix . 'message' ),
285 ];
286
287 $children[] = $this->build_checkbox_row( __( 'Checkbox', 'elementor' ), $prefix . 'checkbox' );
288
289 $children[] = Widget_Builder::make( 'e-form-submit-button' )
290 ->settings( [
291 'text' => Html_V3_Prop_Type::generate( [
292 'content' => String_Prop_Type::generate( __( 'Submit', 'elementor' ) ),
293 'children' => [],
294 ] ),
295 ] )
296 ->build();
297 $children[] = $this->build_status_message(
298 __( 'Great! We’ve received your information.', 'elementor' ),
299 'success',
300 __( 'Success message', 'elementor' )
301 );
302 $children[] = $this->build_status_message(
303 __( 'We couldn’t process your submission. Please retry', 'elementor' ),
304 'error',
305 __( 'Error message', 'elementor' )
306 );
307
308 return $children;
309 }
310
311 private function build_checkbox_row( string $label_text, string $checkbox_id ): array {
312 $checkbox = Widget_Builder::make( 'e-form-checkbox' )
313 ->settings( [
314 '_cssid' => String_Prop_Type::generate( $checkbox_id ),
315 ] )
316 ->build();
317
318 $label = $this->build_label( $label_text, $checkbox_id );
319
320 return Element_Builder::make( 'e-flexbox' )
321 ->children( [ $checkbox, $label ] )
322 ->settings( [
323 'classes' => Classes_Prop_Type::generate( [ 'e-form-checkbox-row' ] ),
324 ] )
325 ->build();
326 }
327
328 private function build_label( string $text, string $input_id ): array {
329 return Widget_Builder::make( 'e-form-label' )
330 ->settings( [
331 'text' => Html_V3_Prop_Type::generate( [
332 'content' => String_Prop_Type::generate( $text ),
333 'children' => [],
334 ] ),
335 'input-id' => String_Prop_Type::generate( $input_id ),
336 ] )
337 ->build();
338 }
339
340 private function build_input( string $placeholder, string $type = 'text', $input_id = '' ): array {
341 if ( 'textarea' === $type ) {
342 return Widget_Builder::make( 'e-form-textarea' )
343 ->settings( [
344 'placeholder' => String_Prop_Type::generate( $placeholder ),
345 'rows' => Number_Prop_Type::generate( 4 ),
346 '_cssid' => String_Prop_Type::generate( $input_id ),
347 ] )
348 ->build();
349 }
350
351 return Widget_Builder::make( 'e-form-input' )
352 ->settings( [
353 'placeholder' => String_Prop_Type::generate( $placeholder ),
354 'type' => String_Prop_Type::generate( $type ),
355 '_cssid' => String_Prop_Type::generate( $input_id ),
356 ] )
357 ->build();
358 }
359
360 private function build_status_message( string $message, string $state, string $title ): array {
361 $paragraph_value = Html_V3_Prop_Type::generate( [
362 'content' => String_Prop_Type::generate( $message ),
363 'children' => [],
364 ] );
365
366 $element_type = 'success' === $state
367 ? Form_Success_Message::get_element_type()
368 : Form_Error_Message::get_element_type();
369
370 return Element_Builder::make( $element_type )
371 ->settings( [
372 'attributes' => Attributes_Prop_Type::generate( [
373 Key_Value_Prop_Type::generate( [] ),
374 ] ),
375 ] )
376 ->editor_settings( [
377 'title' => $title,
378 ] )
379 ->children( [
380 Widget_Builder::make( Atomic_Paragraph::get_element_type() )
381 ->settings( [
382 'paragraph' => $paragraph_value,
383 ] )
384 ->build(),
385 ] )
386 ->build();
387 }
388
389 protected function get_templates(): array {
390 return [
391 'elementor/elements/atomic-form' => __DIR__ . '/atomic-form.html.twig',
392 ];
393 }
394
395 protected function build_template_context(): array {
396 $context = $this->build_base_template_context();
397
398 $context['form_state'] = 'default';
399
400 return $context;
401 }
402
403 public static function is_instance_form( $instance ): bool {
404 return $instance instanceof Atomic_Form;
405 }
406
407 public function render_markdown(): string {
408 return '';
409 }
410 }
411