← All changes
|
modules/atomic-widgets/elements/atomic-form/atomic-form.php
+193
-64
4.1.0-beta1
→
4.2.4
View file →
| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | |
| 4 | 4 | use Elementor\Modules\AtomicWidgets\Controls\Section; |
| 5 | 5 | use Elementor\Modules\AtomicWidgets\Controls\Types\Chips_Control; |
| 6 | 6 | use Elementor\Modules\AtomicWidgets\Controls\Types\Email_Form_Action_Control; |
| 7 | +use Elementor\Modules\AtomicWidgets\Controls\Types\Switch_Control; | |
| 7 | 8 | use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control; |
| 8 | 9 | use Elementor\Modules\AtomicWidgets\Controls\Types\Toggle_Control; |
| 9 | 10 | use Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph\Atomic_Paragraph; |
| 10 | 11 | use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Success_Message\Form_Success_Message; |
| @@ -15,10 +16,11 @@ | ||
| 15 | 16 | use Elementor\Modules\AtomicWidgets\Elements\Base\Widget_Builder; |
| 16 | 17 | use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 17 | 18 | use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 18 | 19 | use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 19 | -use Elementor\Modules\AtomicWidgets\PropTypes\Email_Prop_Type; | |
| 20 | +use Elementor\Modules\AtomicWidgets\PropTypes\Emails_Prop_Type; | |
| 20 | 21 | use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type; |
| 22 | +use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type; | |
| 21 | 23 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type; |
| 22 | 24 | use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 23 | 25 | use Elementor\Modules\AtomicWidgets\PropTypes\Html_V3_Prop_Type; |
| 24 | 26 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Array_Prop_Type; |
| @@ -26,8 +28,9 @@ | ||
| 26 | 28 | use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 27 | 29 | use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| 28 | 30 | use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 29 | 31 | use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 32 | +use Elementor\Plugin; | |
| 30 | 33 | |
| 31 | 34 | if ( ! defined( 'ABSPATH' ) ) { |
| 32 | 35 | exit; // Exit if accessed directly. |
| 33 | 36 | } |
| @@ -79,18 +82,13 @@ | ||
| 79 | 82 | public function get_icon() { |
| 80 | 83 | return 'eicon-atomic-form'; |
| 81 | 84 | } |
| 82 | 85 | |
| 86 | + public static function get_base_props_schema(): array { | |
| 87 | + return self::define_props_schema(); | |
| 88 | + } | |
| 89 | + | |
| 83 | 90 | 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 | 91 | $submissions_metadata_dependencies = Dependency_Manager::make() |
| 94 | 92 | ->where( [ |
| 95 | 93 | 'operator' => 'contains', |
| 96 | 94 | 'path' => [ 'actions-after-submit' ], |
| @@ -107,18 +105,21 @@ | ||
| 107 | 105 | 'effect' => 'hide', |
| 108 | 106 | ] ) |
| 109 | 107 | ->get(); |
| 110 | 108 | |
| 111 | - return [ | |
| 109 | + $props = [ | |
| 112 | 110 | 'classes' => Classes_Prop_Type::make() |
| 113 | 111 | ->default( [] ), |
| 114 | 112 | 'form-name' => String_Prop_Type::make() |
| 115 | 113 | ->default( __( 'Form', 'elementor' ) ), |
| 114 | + 'webmcp-autosubmit' => Boolean_Prop_Type::make() | |
| 115 | + ->default( false ), | |
| 116 | 116 | 'form-state' => String_Prop_Type::make() |
| 117 | 117 | ->enum( [ 'default', 'success', 'error' ] ) |
| 118 | 118 | ->default( 'default' ) |
| 119 | 119 | ->meta( 'generates_class', 'form-state-{value}' ), |
| 120 | 120 | 'actions-after-submit' => String_Array_Prop_Type::make() |
| 121 | + ->initial_value( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ) | |
| 121 | 122 | ->default( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ), |
| 122 | 123 | 'submissions_metadata' => String_Array_Prop_Type::make() |
| 123 | 124 | ->set_dependencies( $submissions_metadata_dependencies ) |
| 124 | 125 | ->default( [ |
| @@ -124,78 +125,83 @@ | ||
| 124 | 125 | ->default( [ |
| 125 | 126 | String_Prop_Type::generate( self::METADATA_REMOTE_IP ), |
| 126 | 127 | String_Prop_Type::generate( self::METADATA_USER_AGENT ), |
| 127 | 128 | ] ), |
| 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 | - ] ), | |
| 129 | + ]; | |
| 130 | + | |
| 131 | + $props = array_merge( $props, self::get_emails_prop_settings(), [ | |
| 135 | 132 | 'webhook_url' => String_Prop_Type::make() |
| 136 | 133 | ->set_dependencies( $webhook_dependencies ) |
| 137 | 134 | ->meta( Overridable_Prop_Type::ignore() ) |
| 138 | 135 | ->default( '' ), |
| 139 | 136 | 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), |
| 140 | - ]; | |
| 137 | + ] ); | |
| 138 | + | |
| 139 | + return $props; | |
| 141 | 140 | } |
| 142 | 141 | |
| 143 | 142 | protected function define_atomic_controls(): array { |
| 144 | 143 | $state_control = Toggle_Control::bind_to( 'form-state' ) |
| 144 | + ->add_options( [ | |
| 145 | + 'default' => [ | |
| 146 | + 'title' => __( 'Normal', 'elementor' ), | |
| 147 | + ], | |
| 148 | + 'success' => [ | |
| 149 | + 'title' => __( 'Success', 'elementor' ), | |
| 150 | + ], | |
| 151 | + 'error' => [ | |
| 152 | + 'title' => __( 'Error', 'elementor' ), | |
| 153 | + ], | |
| 154 | + ] ) | |
| 155 | + ->set_exclusive( true ) | |
| 156 | + ->set_convert_options( true ) | |
| 157 | + ->set_size( 'tiny' ) | |
| 158 | + ->set_full_width( true ) | |
| 145 | 159 | ->set_label( __( 'States', 'elementor' ) ) |
| 146 | 160 | ->set_meta( [ 'topDivider' => true ] ); |
| 147 | 161 | |
| 148 | - if ( $state_control instanceof Toggle_Control ) { | |
| 149 | - $state_control | |
| 150 | - ->add_options( [ | |
| 151 | - 'default' => [ | |
| 152 | - 'title' => __( 'Normal', 'elementor' ), | |
| 162 | + $email_control_settings = $this->get_emails_control_settings(); | |
| 163 | + $form_action_chips = $email_control_settings['form-action-chips']; | |
| 164 | + $email_controls = $email_control_settings['email-controls']; | |
| 165 | + | |
| 166 | + if ( class_exists( '\ElementorPro\License\API' ) ) { | |
| 167 | + $has_form_submissions_feature = \ElementorPro\License\API::is_licence_has_feature( 'form-submissions' ); | |
| 168 | + | |
| 169 | + if ( $has_form_submissions_feature ) { | |
| 170 | + $form_action_chips = array_merge( $form_action_chips, [ | |
| 171 | + [ | |
| 172 | + 'label' => __( 'Collect submissions', 'elementor' ), | |
| 173 | + 'value' => self::ACTION_COLLECT_SUBMISSIONS, | |
| 153 | 174 | ], |
| 154 | - 'success' => [ | |
| 155 | - 'title' => __( 'Success', 'elementor' ), | |
| 175 | + ] ); | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 179 | + $content_controls = [ | |
| 180 | + Text_Control::bind_to( 'form-name' ) | |
| 181 | + ->set_label( __( 'Form name', 'elementor' ) ), | |
| 182 | + $state_control, | |
| 183 | + Chips_Control::bind_to( 'actions-after-submit' ) | |
| 184 | + ->set_options( array_merge( $form_action_chips, [ | |
| 185 | + [ | |
| 186 | + 'label' => __( 'Webhook', 'elementor' ), | |
| 187 | + 'value' => self::ACTION_WEBHOOK, | |
| 156 | 188 | ], |
| 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 | - } | |
| 189 | + ] ) ) | |
| 190 | + ->set_label( __( 'Actions after submit', 'elementor' ) ) | |
| 191 | + ->set_meta( [ 'topDivider' => true ] ), | |
| 192 | + ]; | |
| 166 | 193 | |
| 167 | 194 | return [ |
| 168 | 195 | Section::make() |
| 169 | 196 | ->set_label( __( 'Content', 'elementor' ) ) |
| 197 | + ->set_id( 'content' ) | |
| 198 | + ->set_items( $content_controls ), | |
| 199 | + ...$email_controls, | |
| 200 | + Section::make() | |
| 201 | + ->set_label( __( 'Collect submissions', 'elementor' ) ) | |
| 170 | 202 | ->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 | 203 | Chips_Control::bind_to( 'submissions_metadata' ) |
| 196 | - ->set_label( __( 'Include metadata', 'elementor' ) ) | |
| 197 | - ->set_meta( [ 'topDivider' => true ] ) | |
| 198 | 204 | ->set_options( [ |
| 199 | 205 | [ |
| 200 | 206 | 'label' => __( 'User IP', 'elementor' ), |
| 201 | 207 | 'value' => self::METADATA_REMOTE_IP, |
| @@ -203,13 +209,17 @@ | ||
| 203 | 209 | [ |
| 204 | 210 | 'label' => __( 'User Agent', 'elementor' ), |
| 205 | 211 | 'value' => self::METADATA_USER_AGENT, |
| 206 | 212 | ], |
| 207 | - ] ), | |
| 213 | + ] ) | |
| 214 | + ->set_label( __( 'Include metadata', 'elementor' ) ), | |
| 215 | + ] ), | |
| 216 | + Section::make() | |
| 217 | + ->set_label( __( 'Webhook', 'elementor' ) ) | |
| 218 | + ->set_items( [ | |
| 208 | 219 | Text_Control::bind_to( 'webhook_url' ) |
| 209 | - ->set_label( __( 'Webhook URL', 'elementor' ) ) | |
| 210 | 220 | ->set_placeholder( __( 'https://your-webhook-url.com', 'elementor' ) ) |
| 211 | - ->set_meta( [ 'topDivider' => true ] ), | |
| 221 | + ->set_label( __( 'Webhook URL', 'elementor' ) ), | |
| 212 | 222 | ] ), |
| 213 | 223 | Section::make() |
| 214 | 224 | ->set_label( __( 'Settings', 'elementor' ) ) |
| 215 | 225 | ->set_id( 'settings' ) |
| @@ -216,12 +226,24 @@ | ||
| 216 | 226 | ->set_items( [ |
| 217 | 227 | Text_Control::bind_to( '_cssid' ) |
| 218 | 228 | ->set_label( __( 'ID', 'elementor' ) ) |
| 219 | 229 | ->set_meta( $this->get_css_id_control_meta() ), |
| 230 | + Switch_Control::bind_to( 'webmcp-autosubmit' ) | |
| 231 | + ->set_label( __( 'Agent auto-submit', 'elementor' ) ), | |
| 220 | 232 | ] ), |
| 221 | 233 | ]; |
| 222 | 234 | } |
| 223 | 235 | |
| 236 | + protected function define_base_settings(): array { | |
| 237 | + $settings = []; | |
| 238 | + | |
| 239 | + foreach ( self::build_email_action_defaults() as $key => $default_email ) { | |
| 240 | + $settings[ $key ] = Emails_Prop_Type::generate( $default_email ); | |
| 241 | + } | |
| 242 | + | |
| 243 | + return $settings; | |
| 244 | + } | |
| 245 | + | |
| 224 | 246 | protected function define_base_styles(): array { |
| 225 | 247 | return [ |
| 226 | 248 | static::BASE_STYLE_KEY => Style_Definition::make() |
| 227 | 249 | ->add_variant( |
| @@ -367,8 +389,9 @@ | ||
| 367 | 389 | ? Form_Success_Message::get_element_type() |
| 368 | 390 | : Form_Error_Message::get_element_type(); |
| 369 | 391 | |
| 370 | 392 | return Element_Builder::make( $element_type ) |
| 393 | + ->meta( [ 'required' => true ] ) | |
| 371 | 394 | ->settings( [ |
| 372 | 395 | 'attributes' => Attributes_Prop_Type::generate( [ |
| 373 | 396 | Key_Value_Prop_Type::generate( [] ), |
| 374 | 397 | ] ), |
| @@ -396,11 +419,23 @@ | ||
| 396 | 419 | $context = $this->build_base_template_context(); |
| 397 | 420 | |
| 398 | 421 | $context['form_state'] = 'default'; |
| 399 | 422 | |
| 423 | + if ( ! $this->is_webmcp_enabled() ) { | |
| 424 | + return $context; | |
| 425 | + } | |
| 426 | + | |
| 427 | + $form_name = (string) ( $this->get_atomic_settings()['form-name'] ?? '' ); | |
| 428 | + $context['webmcp_tool_name'] = Webmcp_Utils::build_tool_name( $form_name, (string) $this->get_id() ); | |
| 429 | + $context['webmcp_tool_description'] = Webmcp_Utils::build_tool_description( $form_name ); | |
| 430 | + | |
| 400 | 431 | return $context; |
| 401 | 432 | } |
| 402 | 433 | |
| 434 | + private function is_webmcp_enabled(): bool { | |
| 435 | + return ! Plugin::$instance->editor->is_edit_mode(); | |
| 436 | + } | |
| 437 | + | |
| 403 | 438 | public static function is_instance_form( $instance ): bool { |
| 404 | 439 | return $instance instanceof Atomic_Form; |
| 405 | 440 | } |
| 406 | 441 | |
| @@ -405,6 +440,100 @@ | ||
| 405 | 440 | } |
| 406 | 441 | |
| 407 | 442 | public function render_markdown(): string { |
| 408 | 443 | return ''; |
| 444 | + } | |
| 445 | + | |
| 446 | + private static function get_emails_prop_settings(): array { | |
| 447 | + $props = []; | |
| 448 | + | |
| 449 | + foreach ( self::build_email_action_defaults() as $key => $default_value ) { | |
| 450 | + $props[ $key ] = Emails_Prop_Type::make() | |
| 451 | + ->set_dependencies( self::make_action_dependency( $key ) ) | |
| 452 | + ->meta( Overridable_Prop_Type::ignore() ) | |
| 453 | + ->initial_value( $default_value ) | |
| 454 | + ->default( $default_value ); | |
| 455 | + } | |
| 456 | + | |
| 457 | + return $props; | |
| 458 | + } | |
| 459 | + | |
| 460 | + private function get_emails_control_settings(): array { | |
| 461 | + $form_action_chips = []; | |
| 462 | + $email_controls = []; | |
| 463 | + | |
| 464 | + for ( $i = 0; $i < self::get_email_action_count(); $i++ ) { | |
| 465 | + $key = self::get_email_action_key( $i ); | |
| 466 | + $label = self::get_email_action_label( $i ); | |
| 467 | + | |
| 468 | + $form_action_chips[] = [ | |
| 469 | + 'label' => $label, | |
| 470 | + 'value' => $key, | |
| 471 | + ]; | |
| 472 | + | |
| 473 | + $email_controls[] = Section::make() | |
| 474 | + ->set_label( $label ) | |
| 475 | + ->set_items( [ | |
| 476 | + Email_Form_Action_Control::bind_to( $key ) | |
| 477 | + ->set_free_chips( true ) | |
| 478 | + ->set_label( $label ), | |
| 479 | + ] ); | |
| 480 | + } | |
| 481 | + | |
| 482 | + return [ | |
| 483 | + 'form-action-chips' => $form_action_chips, | |
| 484 | + 'email-controls' => $email_controls, | |
| 485 | + ]; | |
| 486 | + } | |
| 487 | + | |
| 488 | + private static function get_email_action_key( int $index ): string { | |
| 489 | + return 0 === $index | |
| 490 | + ? self::ACTION_EMAIL | |
| 491 | + : self::ACTION_EMAIL . '_' . ( $index + 1 ); | |
| 492 | + } | |
| 493 | + | |
| 494 | + private static function get_email_action_label( int $index ): string { | |
| 495 | + if ( 0 === $index ) { | |
| 496 | + return __( 'Email', 'elementor' ); | |
| 497 | + } | |
| 498 | + | |
| 499 | + // translators: %d is the index of the email action. | |
| 500 | + return sprintf( __( 'Email %d', 'elementor' ), $index + 1 ); | |
| 501 | + } | |
| 502 | + | |
| 503 | + private static function build_email_action_defaults(): array { | |
| 504 | + $defaults = []; | |
| 505 | + $default_email = self::get_default_email_value(); | |
| 506 | + | |
| 507 | + for ( $i = 0; $i < self::get_email_action_count(); $i++ ) { | |
| 508 | + $key = self::get_email_action_key( $i ); | |
| 509 | + $defaults[ $key ] = $default_email; | |
| 510 | + } | |
| 511 | + | |
| 512 | + return $defaults; | |
| 513 | + } | |
| 514 | + | |
| 515 | + private static function get_default_email_value(): array { | |
| 516 | + return [ | |
| 517 | + 'to' => String_Array_Prop_Type::generate( [ | |
| 518 | + String_Prop_Type::generate( self::get_default_recipient_email() ), | |
| 519 | + ] ), | |
| 520 | + 'from' => String_Prop_Type::generate( self::get_default_sender_email() ), | |
| 521 | + 'message' => String_Prop_Type::generate( '[all-fields]' ), | |
| 522 | + ]; | |
| 523 | + } | |
| 524 | + | |
| 525 | + private static function make_action_dependency( string $action_key ): ?array { | |
| 526 | + return Dependency_Manager::make() | |
| 527 | + ->where( [ | |
| 528 | + 'operator' => 'contains', | |
| 529 | + 'path' => [ 'actions-after-submit' ], | |
| 530 | + 'value' => $action_key, | |
| 531 | + 'effect' => 'hide', | |
| 532 | + ] ) | |
| 533 | + ->get(); | |
| 534 | + } | |
| 535 | + | |
| 536 | + private static function get_email_action_count(): int { | |
| 537 | + return apply_filters( 'elementor/atomic/form/email_action_count', 1 ); | |
| 409 | 538 | } |
| 410 | 539 | } |