← All changes
|
modules/atomic-widgets/elements/atomic-form/atomic-form.php
+64
-183
4.2.0-beta2
→
4.1.4
View file →
| @@ -3,9 +3,8 @@ | ||
| 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; | |
| 8 | 7 | use Elementor\Modules\AtomicWidgets\Controls\Types\Text_Control; |
| 9 | 8 | use Elementor\Modules\AtomicWidgets\Controls\Types\Toggle_Control; |
| 10 | 9 | use Elementor\Modules\AtomicWidgets\Elements\Atomic_Paragraph\Atomic_Paragraph; |
| 11 | 10 | use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Success_Message\Form_Success_Message; |
| @@ -16,11 +15,10 @@ | ||
| 16 | 15 | use Elementor\Modules\AtomicWidgets\Elements\Base\Widget_Builder; |
| 17 | 16 | use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 18 | 17 | use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 19 | 18 | use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 20 | -use Elementor\Modules\AtomicWidgets\PropTypes\Emails_Prop_Type; | |
| 19 | +use Elementor\Modules\AtomicWidgets\PropTypes\Email_Prop_Type; | |
| 21 | 20 | use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type; |
| 22 | -use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type; | |
| 23 | 21 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type; |
| 24 | 22 | use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 25 | 23 | use Elementor\Modules\AtomicWidgets\PropTypes\Html_V3_Prop_Type; |
| 26 | 24 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Array_Prop_Type; |
| @@ -81,13 +79,18 @@ | ||
| 81 | 79 | public function get_icon() { |
| 82 | 80 | return 'eicon-atomic-form'; |
| 83 | 81 | } |
| 84 | 82 | |
| 85 | - public static function get_base_props_schema(): array { | |
| 86 | - return self::define_props_schema(); | |
| 87 | - } | |
| 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(); | |
| 88 | 92 | |
| 89 | - protected static function define_props_schema(): array { | |
| 90 | 93 | $submissions_metadata_dependencies = Dependency_Manager::make() |
| 91 | 94 | ->where( [ |
| 92 | 95 | 'operator' => 'contains', |
| 93 | 96 | 'path' => [ 'actions-after-submit' ], |
| @@ -104,21 +107,18 @@ | ||
| 104 | 107 | 'effect' => 'hide', |
| 105 | 108 | ] ) |
| 106 | 109 | ->get(); |
| 107 | 110 | |
| 108 | - $props = [ | |
| 111 | + return [ | |
| 109 | 112 | 'classes' => Classes_Prop_Type::make() |
| 110 | 113 | ->default( [] ), |
| 111 | 114 | 'form-name' => String_Prop_Type::make() |
| 112 | 115 | ->default( __( 'Form', 'elementor' ) ), |
| 113 | - 'webmcp-autosubmit' => Boolean_Prop_Type::make() | |
| 114 | - ->default( false ), | |
| 115 | 116 | 'form-state' => String_Prop_Type::make() |
| 116 | 117 | ->enum( [ 'default', 'success', 'error' ] ) |
| 117 | 118 | ->default( 'default' ) |
| 118 | 119 | ->meta( 'generates_class', 'form-state-{value}' ), |
| 119 | 120 | 'actions-after-submit' => String_Array_Prop_Type::make() |
| 120 | - ->initial_value( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ) | |
| 121 | 121 | ->default( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ), |
| 122 | 122 | 'submissions_metadata' => String_Array_Prop_Type::make() |
| 123 | 123 | ->set_dependencies( $submissions_metadata_dependencies ) |
| 124 | 124 | ->default( [ |
| @@ -124,83 +124,78 @@ | ||
| 124 | 124 | ->default( [ |
| 125 | 125 | String_Prop_Type::generate( self::METADATA_REMOTE_IP ), |
| 126 | 126 | String_Prop_Type::generate( self::METADATA_USER_AGENT ), |
| 127 | 127 | ] ), |
| 128 | - ]; | |
| 129 | - | |
| 130 | - $props = array_merge( $props, self::get_emails_prop_settings(), [ | |
| 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 | + ] ), | |
| 131 | 135 | 'webhook_url' => String_Prop_Type::make() |
| 132 | 136 | ->set_dependencies( $webhook_dependencies ) |
| 133 | 137 | ->meta( Overridable_Prop_Type::ignore() ) |
| 134 | 138 | ->default( '' ), |
| 135 | 139 | 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), |
| 136 | - ] ); | |
| 137 | - | |
| 138 | - return $props; | |
| 140 | + ]; | |
| 139 | 141 | } |
| 140 | 142 | |
| 141 | 143 | protected function define_atomic_controls(): array { |
| 142 | 144 | $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 | 145 | ->set_label( __( 'States', 'elementor' ) ) |
| 159 | 146 | ->set_meta( [ 'topDivider' => true ] ); |
| 160 | 147 | |
| 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, | |
| 148 | + if ( $state_control instanceof Toggle_Control ) { | |
| 149 | + $state_control | |
| 150 | + ->add_options( [ | |
| 151 | + 'default' => [ | |
| 152 | + 'title' => __( 'Normal', 'elementor' ), | |
| 173 | 153 | ], |
| 174 | - ] ); | |
| 175 | - } | |
| 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 ); | |
| 176 | 165 | } |
| 177 | 166 | |
| 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 | 167 | return [ |
| 194 | 168 | Section::make() |
| 195 | 169 | ->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 | 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 | + ] ), | |
| 202 | 195 | Chips_Control::bind_to( 'submissions_metadata' ) |
| 196 | + ->set_label( __( 'Include metadata', 'elementor' ) ) | |
| 197 | + ->set_meta( [ 'topDivider' => true ] ) | |
| 203 | 198 | ->set_options( [ |
| 204 | 199 | [ |
| 205 | 200 | 'label' => __( 'User IP', 'elementor' ), |
| 206 | 201 | 'value' => self::METADATA_REMOTE_IP, |
| @@ -208,17 +203,13 @@ | ||
| 208 | 203 | [ |
| 209 | 204 | 'label' => __( 'User Agent', 'elementor' ), |
| 210 | 205 | 'value' => self::METADATA_USER_AGENT, |
| 211 | 206 | ], |
| 212 | - ] ) | |
| 213 | - ->set_label( __( 'Include metadata', 'elementor' ) ), | |
| 214 | - ] ), | |
| 215 | - Section::make() | |
| 216 | - ->set_label( __( 'Webhook', 'elementor' ) ) | |
| 217 | - ->set_items( [ | |
| 207 | + ] ), | |
| 218 | 208 | Text_Control::bind_to( 'webhook_url' ) |
| 209 | + ->set_label( __( 'Webhook URL', 'elementor' ) ) | |
| 219 | 210 | ->set_placeholder( __( 'https://your-webhook-url.com', 'elementor' ) ) |
| 220 | - ->set_label( __( 'Webhook URL', 'elementor' ) ), | |
| 211 | + ->set_meta( [ 'topDivider' => true ] ), | |
| 221 | 212 | ] ), |
| 222 | 213 | Section::make() |
| 223 | 214 | ->set_label( __( 'Settings', 'elementor' ) ) |
| 224 | 215 | ->set_id( 'settings' ) |
| @@ -225,24 +216,12 @@ | ||
| 225 | 216 | ->set_items( [ |
| 226 | 217 | Text_Control::bind_to( '_cssid' ) |
| 227 | 218 | ->set_label( __( 'ID', 'elementor' ) ) |
| 228 | 219 | ->set_meta( $this->get_css_id_control_meta() ), |
| 229 | - Switch_Control::bind_to( 'webmcp-autosubmit' ) | |
| 230 | - ->set_label( __( 'Agent auto-submit', 'elementor' ) ), | |
| 231 | 220 | ] ), |
| 232 | 221 | ]; |
| 233 | 222 | } |
| 234 | 223 | |
| 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 | 224 | protected function define_base_styles(): array { |
| 246 | 225 | return [ |
| 247 | 226 | static::BASE_STYLE_KEY => Style_Definition::make() |
| 248 | 227 | ->add_variant( |
| @@ -418,12 +397,8 @@ | ||
| 418 | 397 | $context = $this->build_base_template_context(); |
| 419 | 398 | |
| 420 | 399 | $context['form_state'] = 'default'; |
| 421 | 400 | |
| 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 | 401 | return $context; |
| 427 | 402 | } |
| 428 | 403 | |
| 429 | 404 | public static function is_instance_form( $instance ): bool { |
| @@ -431,100 +406,6 @@ | ||
| 431 | 406 | } |
| 432 | 407 | |
| 433 | 408 | public function render_markdown(): string { |
| 434 | 409 | 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 | 410 | } |
| 530 | 411 | } |