← All changes
|
modules/atomic-widgets/elements/atomic-form/atomic-form.php
+78
-31
4.0.9
→
4.1.0-beta1
View file →
| @@ -36,17 +36,31 @@ | ||
| 36 | 36 | use Has_Element_Template; |
| 37 | 37 | |
| 38 | 38 | const BASE_STYLE_KEY = 'base'; |
| 39 | 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'; | |
| 40 | 43 | public const ACTION_COLLECT_SUBMISSIONS = 'collect-submissions'; |
| 44 | + public const ACTION_WEBHOOK = 'webhook'; | |
| 41 | 45 | public const METADATA_REMOTE_IP = 'remote_ip'; |
| 42 | 46 | public const METADATA_USER_AGENT = 'user_agent'; |
| 43 | 47 | |
| 48 | + | |
| 49 | + | |
| 44 | 50 | public function __construct( $data = [], $args = null ) { |
| 45 | 51 | parent::__construct( $data, $args ); |
| 46 | 52 | $this->meta( 'is_container', true ); |
| 47 | 53 | } |
| 48 | 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 | + | |
| 49 | 63 | public static function get_type() { |
| 50 | 64 | return 'e-form'; |
| 51 | 65 | } |
| 52 | 66 | |
| @@ -70,9 +84,9 @@ | ||
| 70 | 84 | $email_dependencies = Dependency_Manager::make() |
| 71 | 85 | ->where( [ |
| 72 | 86 | 'operator' => 'contains', |
| 73 | 87 | 'path' => [ 'actions-after-submit' ], |
| 74 | - 'value' => 'email', | |
| 88 | + 'value' => self::ACTION_EMAIL, | |
| 75 | 89 | 'effect' => 'hide', |
| 76 | 90 | ] ) |
| 77 | 91 | ->get(); |
| 78 | 92 | |
| @@ -84,8 +98,17 @@ | ||
| 84 | 98 | 'effect' => 'hide', |
| 85 | 99 | ] ) |
| 86 | 100 | ->get(); |
| 87 | 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 | + | |
| 88 | 111 | return [ |
| 89 | 112 | 'classes' => Classes_Prop_Type::make() |
| 90 | 113 | ->default( [] ), |
| 91 | 114 | 'form-name' => String_Prop_Type::make() |
| @@ -94,9 +117,9 @@ | ||
| 94 | 117 | ->enum( [ 'default', 'success', 'error' ] ) |
| 95 | 118 | ->default( 'default' ) |
| 96 | 119 | ->meta( 'generates_class', 'form-state-{value}' ), |
| 97 | 120 | 'actions-after-submit' => String_Array_Prop_Type::make() |
| 98 | - ->default( [ String_Prop_Type::generate( 'email' ) ] ), | |
| 121 | + ->default( [ String_Prop_Type::generate( self::ACTION_EMAIL ) ] ), | |
| 99 | 122 | 'submissions_metadata' => String_Array_Prop_Type::make() |
| 100 | 123 | ->set_dependencies( $submissions_metadata_dependencies ) |
| 101 | 124 | ->default( [ |
| 102 | 125 | String_Prop_Type::generate( self::METADATA_REMOTE_IP ), |
| @@ -104,9 +127,16 @@ | ||
| 104 | 127 | ] ), |
| 105 | 128 | 'email' => Email_Prop_Type::make() |
| 106 | 129 | ->set_dependencies( $email_dependencies ) |
| 107 | 130 | ->meta( Overridable_Prop_Type::ignore() ) |
| 108 | - ->default( [] ), | |
| 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( '' ), | |
| 109 | 139 | 'attributes' => Attributes_Prop_Type::make()->meta( Overridable_Prop_Type::ignore() ), |
| 110 | 140 | ]; |
| 111 | 141 | } |
| 112 | 142 | |
| @@ -145,16 +175,24 @@ | ||
| 145 | 175 | ->set_label( __( 'Actions after submit', 'elementor' ) ) |
| 146 | 176 | ->set_meta( [ 'topDivider' => true ] ) |
| 147 | 177 | ->set_options( [ |
| 148 | 178 | [ |
| 179 | + 'label' => __( 'Email', 'elementor' ), | |
| 180 | + 'value' => self::ACTION_EMAIL, | |
| 181 | + ], | |
| 182 | + [ | |
| 149 | 183 | 'label' => __( 'Collect submissions', 'elementor' ), |
| 150 | 184 | 'value' => self::ACTION_COLLECT_SUBMISSIONS, |
| 151 | 185 | ], |
| 152 | 186 | [ |
| 153 | - 'label' => __( 'Email', 'elementor' ), | |
| 154 | - 'value' => 'email', | |
| 187 | + 'label' => __( 'Webhook', 'elementor' ), | |
| 188 | + 'value' => self::ACTION_WEBHOOK, | |
| 155 | 189 | ], |
| 156 | 190 | ] ), |
| 191 | + Email_Form_Action_Control::bind_to( 'email' ) | |
| 192 | + ->set_meta( [ | |
| 193 | + 'topDivider' => true, | |
| 194 | + ] ), | |
| 157 | 195 | Chips_Control::bind_to( 'submissions_metadata' ) |
| 158 | 196 | ->set_label( __( 'Include metadata', 'elementor' ) ) |
| 159 | 197 | ->set_meta( [ 'topDivider' => true ] ) |
| 160 | 198 | ->set_options( [ |
| @@ -166,12 +204,12 @@ | ||
| 166 | 204 | 'label' => __( 'User Agent', 'elementor' ), |
| 167 | 205 | 'value' => self::METADATA_USER_AGENT, |
| 168 | 206 | ], |
| 169 | 207 | ] ), |
| 170 | - Email_Form_Action_Control::bind_to( 'email' ) | |
| 171 | - ->set_meta( [ | |
| 172 | - 'topDivider' => true, | |
| 173 | - ] ), | |
| 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 ] ), | |
| 174 | 212 | ] ), |
| 175 | 213 | Section::make() |
| 176 | 214 | ->set_label( __( 'Settings', 'elementor' ) ) |
| 177 | 215 | ->set_id( 'settings' ) |
| @@ -231,9 +269,9 @@ | ||
| 231 | 269 | protected function define_default_children() { |
| 232 | 270 | |
| 233 | 271 | $prefix = 'e-form-'; |
| 234 | 272 | |
| 235 | - return [ | |
| 273 | + $children = [ | |
| 236 | 274 | $this->build_label( __( 'First name', 'elementor' ), $prefix . 'first-name' ), |
| 237 | 275 | $this->build_input( __( 'First name', 'elementor' ), 'text', $prefix . 'first-name' ), |
| 238 | 276 | |
| 239 | 277 | $this->build_label( __( 'Last name', 'elementor' ), $prefix . 'last-name' ), |
| @@ -243,30 +281,32 @@ | ||
| 243 | 281 | $this->build_input( __( '[email protected]', 'elementor' ), 'email', $prefix . 'email' ), |
| 244 | 282 | |
| 245 | 283 | $this->build_label( __( 'Message', 'elementor' ), $prefix . 'message' ), |
| 246 | 284 | $this->build_input( __( 'Your message', 'elementor' ), 'textarea', $prefix . 'message' ), |
| 285 | + ]; | |
| 247 | 286 | |
| 248 | - $this->build_checkbox_row( __( 'Checkbox', 'elementor' ), $prefix . 'checkbox' ), | |
| 287 | + $children[] = $this->build_checkbox_row( __( 'Checkbox', 'elementor' ), $prefix . 'checkbox' ); | |
| 249 | 288 | |
| 250 | - Widget_Builder::make( 'e-form-submit-button' ) | |
| 251 | - ->settings( [ | |
| 252 | - 'text' => Html_V3_Prop_Type::generate( [ | |
| 253 | - 'content' => String_Prop_Type::generate( __( 'Submit', 'elementor' ) ), | |
| 254 | - 'children' => [], | |
| 255 | - ] ), | |
| 256 | - ] ) | |
| 257 | - ->build(), | |
| 258 | - $this->build_status_message( | |
| 259 | - __( 'Great! We’ve received your information.', 'elementor' ), | |
| 260 | - 'success', | |
| 261 | - __( 'Success message', 'elementor' ) | |
| 262 | - ), | |
| 263 | - $this->build_status_message( | |
| 264 | - __( 'We couldn’t process your submission. Please retry', 'elementor' ), | |
| 265 | - 'error', | |
| 266 | - __( 'Error message', 'elementor' ) | |
| 267 | - ), | |
| 268 | - ]; | |
| 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; | |
| 269 | 309 | } |
| 270 | 310 | |
| 271 | 311 | private function build_checkbox_row( string $label_text, string $checkbox_id ): array { |
| 272 | 312 | $checkbox = Widget_Builder::make( 'e-form-checkbox' ) |
| @@ -342,9 +382,8 @@ | ||
| 342 | 382 | 'paragraph' => $paragraph_value, |
| 343 | 383 | ] ) |
| 344 | 384 | ->build(), |
| 345 | 385 | ] ) |
| 346 | - ->is_locked( true ) | |
| 347 | 386 | ->build(); |
| 348 | 387 | } |
| 349 | 388 | |
| 350 | 389 | protected function get_templates(): array { |
| @@ -358,6 +397,14 @@ | ||
| 358 | 397 | |
| 359 | 398 | $context['form_state'] = 'default'; |
| 360 | 399 | |
| 361 | 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 ''; | |
| 362 | 409 | } |
| 363 | 410 | } |