← All changes
|
modules/atomic-widgets/elements/atomic-form/atomic-form.php
+46
-21
4.2.0-beta1
→
4.3.1
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; |
| @@ -11,8 +12,9 @@ | ||
| 11 | 12 | use Elementor\Modules\AtomicWidgets\Elements\Atomic_Form\Form_Error_Message\Form_Error_Message; |
| 12 | 13 | use Elementor\Modules\AtomicWidgets\Elements\Base\Atomic_Element_Base; |
| 13 | 14 | use Elementor\Modules\AtomicWidgets\Elements\Base\Element_Builder; |
| 14 | 15 | use Elementor\Modules\AtomicWidgets\Elements\Base\Has_Element_Template; |
| 16 | +use Elementor\Modules\AtomicWidgets\Elements\Base\Html_Tag_Computer; | |
| 15 | 17 | use Elementor\Modules\AtomicWidgets\Elements\Base\Widget_Builder; |
| 16 | 18 | use Elementor\Modules\AtomicWidgets\PropDependencies\Manager as Dependency_Manager; |
| 17 | 19 | use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 18 | 20 | use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| @@ -17,11 +19,12 @@ | ||
| 17 | 19 | use Elementor\Modules\AtomicWidgets\PropTypes\Attributes_Prop_Type; |
| 18 | 20 | use Elementor\Modules\AtomicWidgets\PropTypes\Classes_Prop_Type; |
| 19 | 21 | use Elementor\Modules\AtomicWidgets\PropTypes\Emails_Prop_Type; |
| 20 | 22 | use Elementor\Modules\AtomicWidgets\PropTypes\Key_Value_Prop_Type; |
| 23 | +use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Boolean_Prop_Type; | |
| 21 | 24 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\Number_Prop_Type; |
| 22 | 25 | use Elementor\Modules\AtomicWidgets\PropTypes\Size_Prop_Type; |
| 23 | -use Elementor\Modules\AtomicWidgets\PropTypes\Html_V3_Prop_Type; | |
| 26 | +use Elementor\Modules\AtomicWidgets\PropTypes\Escaped_Html_Prop_Type; | |
| 24 | 27 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Array_Prop_Type; |
| 25 | 28 | use Elementor\Modules\AtomicWidgets\PropTypes\Primitives\String_Prop_Type; |
| 26 | 29 | use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 27 | 30 | use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| @@ -26,8 +29,9 @@ | ||
| 26 | 29 | use Elementor\Modules\AtomicWidgets\Styles\Style_Definition; |
| 27 | 30 | use Elementor\Modules\AtomicWidgets\Styles\Style_Variant; |
| 28 | 31 | use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager; |
| 29 | 32 | use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type; |
| 33 | +use Elementor\Plugin; | |
| 30 | 34 | |
| 31 | 35 | if ( ! defined( 'ABSPATH' ) ) { |
| 32 | 36 | exit; // Exit if accessed directly. |
| 33 | 37 | } |
| @@ -49,8 +53,9 @@ | ||
| 49 | 53 | |
| 50 | 54 | public function __construct( $data = [], $args = null ) { |
| 51 | 55 | parent::__construct( $data, $args ); |
| 52 | 56 | $this->meta( 'is_container', true ); |
| 57 | + $this->meta( 'is_compound', true ); | |
| 53 | 58 | } |
| 54 | 59 | |
| 55 | 60 | public static function get_default_recipient_email(): string { |
| 56 | 61 | return sanitize_email( (string) get_option( 'admin_email', '' ) ); |
| @@ -79,8 +84,16 @@ | ||
| 79 | 84 | public function get_icon() { |
| 80 | 85 | return 'eicon-atomic-form'; |
| 81 | 86 | } |
| 82 | 87 | |
| 88 | + public static function get_computed_html_tag( array $settings ): string { | |
| 89 | + return Html_Tag_Computer::compute( $settings, 'form' ); | |
| 90 | + } | |
| 91 | + | |
| 92 | + public static function get_base_props_schema(): array { | |
| 93 | + return self::define_props_schema(); | |
| 94 | + } | |
| 95 | + | |
| 83 | 96 | protected static function define_props_schema(): array { |
| 84 | 97 | $submissions_metadata_dependencies = Dependency_Manager::make() |
| 85 | 98 | ->where( [ |
| 86 | 99 | 'operator' => 'contains', |
| @@ -103,8 +116,10 @@ | ||
| 103 | 116 | 'classes' => Classes_Prop_Type::make() |
| 104 | 117 | ->default( [] ), |
| 105 | 118 | 'form-name' => String_Prop_Type::make() |
| 106 | 119 | ->default( __( 'Form', 'elementor' ) ), |
| 120 | + 'webmcp-autosubmit' => Boolean_Prop_Type::make() | |
| 121 | + ->default( false ), | |
| 107 | 122 | 'form-state' => String_Prop_Type::make() |
| 108 | 123 | ->enum( [ 'default', 'success', 'error' ] ) |
| 109 | 124 | ->default( 'default' ) |
| 110 | 125 | ->meta( 'generates_class', 'form-state-{value}' ), |
| @@ -153,8 +168,21 @@ | ||
| 153 | 168 | $email_control_settings = $this->get_emails_control_settings(); |
| 154 | 169 | $form_action_chips = $email_control_settings['form-action-chips']; |
| 155 | 170 | $email_controls = $email_control_settings['email-controls']; |
| 156 | 171 | |
| 172 | + if ( class_exists( '\ElementorPro\License\API' ) ) { | |
| 173 | + $has_form_submissions_feature = \ElementorPro\License\API::is_licence_has_feature( 'form-submissions' ); | |
| 174 | + | |
| 175 | + if ( $has_form_submissions_feature ) { | |
| 176 | + $form_action_chips = array_merge( $form_action_chips, [ | |
| 177 | + [ | |
| 178 | + 'label' => __( 'Collect submissions', 'elementor' ), | |
| 179 | + 'value' => self::ACTION_COLLECT_SUBMISSIONS, | |
| 180 | + ], | |
| 181 | + ] ); | |
| 182 | + } | |
| 183 | + } | |
| 184 | + | |
| 157 | 185 | $content_controls = [ |
| 158 | 186 | Text_Control::bind_to( 'form-name' ) |
| 159 | 187 | ->set_label( __( 'Form name', 'elementor' ) ), |
| 160 | 188 | $state_control, |
| @@ -160,12 +188,8 @@ | ||
| 160 | 188 | $state_control, |
| 161 | 189 | Chips_Control::bind_to( 'actions-after-submit' ) |
| 162 | 190 | ->set_options( array_merge( $form_action_chips, [ |
| 163 | 191 | [ |
| 164 | - 'label' => __( 'Collect submissions', 'elementor' ), | |
| 165 | - 'value' => self::ACTION_COLLECT_SUBMISSIONS, | |
| 166 | - ], | |
| 167 | - [ | |
| 168 | 192 | 'label' => __( 'Webhook', 'elementor' ), |
| 169 | 193 | 'value' => self::ACTION_WEBHOOK, |
| 170 | 194 | ], |
| 171 | 195 | ] ) ) |
| @@ -208,8 +232,10 @@ | ||
| 208 | 232 | ->set_items( [ |
| 209 | 233 | Text_Control::bind_to( '_cssid' ) |
| 210 | 234 | ->set_label( __( 'ID', 'elementor' ) ) |
| 211 | 235 | ->set_meta( $this->get_css_id_control_meta() ), |
| 236 | + Switch_Control::bind_to( 'webmcp-autosubmit' ) | |
| 237 | + ->set_label( __( 'Agent auto-submit', 'elementor' ) ), | |
| 212 | 238 | ] ), |
| 213 | 239 | ]; |
| 214 | 240 | } |
| 215 | 241 | |
| @@ -263,12 +289,8 @@ | ||
| 263 | 289 | protected function define_panel_categories(): array { |
| 264 | 290 | return [ 'atomic-form' ]; |
| 265 | 291 | } |
| 266 | 292 | |
| 267 | - protected function define_default_html_tag() { | |
| 268 | - return 'form'; | |
| 269 | - } | |
| 270 | - | |
| 271 | 293 | protected function define_default_children() { |
| 272 | 294 | |
| 273 | 295 | $prefix = 'e-form-'; |
| 274 | 296 | |
| @@ -289,12 +311,9 @@ | ||
| 289 | 311 | $children[] = $this->build_checkbox_row( __( 'Checkbox', 'elementor' ), $prefix . 'checkbox' ); |
| 290 | 312 | |
| 291 | 313 | $children[] = Widget_Builder::make( 'e-form-submit-button' ) |
| 292 | 314 | ->settings( [ |
| 293 | - 'text' => Html_V3_Prop_Type::generate( [ | |
| 294 | - 'content' => String_Prop_Type::generate( __( 'Submit', 'elementor' ) ), | |
| 295 | - 'children' => [], | |
| 296 | - ] ), | |
| 315 | + 'text' => Escaped_Html_Prop_Type::generate( __( 'Submit', 'elementor' ) ), | |
| 297 | 316 | ] ) |
| 298 | 317 | ->build(); |
| 299 | 318 | $children[] = $this->build_status_message( |
| 300 | 319 | __( 'Great! We’ve received your information.', 'elementor' ), |
| @@ -329,12 +348,9 @@ | ||
| 329 | 348 | |
| 330 | 349 | private function build_label( string $text, string $input_id ): array { |
| 331 | 350 | return Widget_Builder::make( 'e-form-label' ) |
| 332 | 351 | ->settings( [ |
| 333 | - 'text' => Html_V3_Prop_Type::generate( [ | |
| 334 | - 'content' => String_Prop_Type::generate( $text ), | |
| 335 | - 'children' => [], | |
| 336 | - ] ), | |
| 352 | + 'text' => Escaped_Html_Prop_Type::generate( $text ), | |
| 337 | 353 | 'input-id' => String_Prop_Type::generate( $input_id ), |
| 338 | 354 | ] ) |
| 339 | 355 | ->build(); |
| 340 | 356 | } |
| @@ -359,12 +375,9 @@ | ||
| 359 | 375 | ->build(); |
| 360 | 376 | } |
| 361 | 377 | |
| 362 | 378 | private function build_status_message( string $message, string $state, string $title ): array { |
| 363 | - $paragraph_value = Html_V3_Prop_Type::generate( [ | |
| 364 | - 'content' => String_Prop_Type::generate( $message ), | |
| 365 | - 'children' => [], | |
| 366 | - ] ); | |
| 379 | + $paragraph_value = Escaped_Html_Prop_Type::generate( $message ); | |
| 367 | 380 | |
| 368 | 381 | $element_type = 'success' === $state |
| 369 | 382 | ? Form_Success_Message::get_element_type() |
| 370 | 383 | : Form_Error_Message::get_element_type(); |
| @@ -399,9 +412,21 @@ | ||
| 399 | 412 | $context = $this->build_base_template_context(); |
| 400 | 413 | |
| 401 | 414 | $context['form_state'] = 'default'; |
| 402 | 415 | |
| 416 | + if ( ! $this->is_webmcp_enabled() ) { | |
| 417 | + return $context; | |
| 418 | + } | |
| 419 | + | |
| 420 | + $form_name = (string) ( $this->get_atomic_settings()['form-name'] ?? '' ); | |
| 421 | + $context['webmcp_tool_name'] = Webmcp_Utils::build_tool_name( $form_name, (string) $this->get_id() ); | |
| 422 | + $context['webmcp_tool_description'] = Webmcp_Utils::build_tool_description( $form_name ); | |
| 423 | + | |
| 403 | 424 | return $context; |
| 425 | + } | |
| 426 | + | |
| 427 | + private function is_webmcp_enabled(): bool { | |
| 428 | + return ! Plugin::$instance->editor->is_edit_mode(); | |
| 404 | 429 | } |
| 405 | 430 | |
| 406 | 431 | public static function is_instance_form( $instance ): bool { |
| 407 | 432 | return $instance instanceof Atomic_Form; |