( $referer_title ) ) : ''; $referrer = $post_data['referrer'] ?? ''; $referrer = is_string( $referrer ) ? esc_url_raw( wp_unslash( $referrer ) ) : ''; $results = Action_Runner::execute_actions( $actions, $form_data, $widget_settings, [ 'post_id' => $post_id, 'form_id' => $form_id, 'form_name' => $form_name, 'field_metadata' => $field_metadata, 'referer_title' => $referer_title, 'referrer' => $referrer, 'cssid_map' => $cssid_map, 'files' => $context_files, 'file_field_settings' => $file_field_settings, ] ); $this->send_response( $results['actionResults'], $results['allActionsSucceeded'], $results['failedActions'] ); } private function is_nonce_valid( array $post_data ): bool { $nonce = $post_data['_nonce'] ?? ''; if ( ! $nonce ) { return false; } return wp_verify_nonce( $nonce, self::NONCE_ACTION ); } private function convert_form_fields_to_data( array $form_fields ): array { $form_data = []; foreach ( $form_fields as $field ) { if ( ! is_array( $field ) ) { continue; } $id = sanitize_text_field( $field['id'] ?? '' ); $value = $field['value'] ?? ''; if ( ! $id ) { continue; } if ( is_array( $value ) ) { $form_data[ $id ] = array_map( 'sanitize_text_field', $value ); } else { $type = sanitize_text_field( $field['type'] ?? 'text' ); if ( 'textarea' === $type ) { $form_data[ $id ] = sanitize_textarea_field( $value ); } else { $form_data[ $id ] = sanitize_text_field( $value ); } } } return $form_data; } private function resolve_form_name( string $posted_form_name, string $form_id ): string { return ! empty( $posted_form_name ) ? $posted_form_name : $form_id; } private function extract_field_metadata( array $form_fields ): array { $metadata = []; foreach ( $form_fields as $field ) { if ( ! is_array( $field ) ) { continue; } $id = sanitize_text_field( $field['id'] ?? '' ); if ( ! $id ) { continue; } $options = isset( $field['options'] ) && is_string( $field['options'] ) ? json_decode( $field['options'], true ) : null; $metadata[ $id ] = [ 'label' => sanitize_text_field( $field['label'] ?? '' ), 'type' => sanitize_text_field( $field['type'] ?? '' ), 'options' => is_array( $options ) ? $options : null, ]; } return $metadata; } private function find_form_element( int $post_id, string $form_id ) { $document = Plugin::elementor()->documents->get( $post_id ); if ( ! $document ) { return new \WP_Error( 'document_not_found', __( 'Document not found', 'elementor-pro' ) ); } $element_data = $document->get_elements_data(); $form_element = ElementorUtils::find_element_recursive( $element_data, $form_id ); if ( empty( $form_element ) ) { return new \WP_Error( 'form_not_found', __( 'Form element not found', 'elementor-pro' ) ); } return $form_element; } private function resolve_widget_settings( array $form_element, int $post_id ): array { $resolved = $this->resolve_raw_settings( $form_element, $post_id ); if ( ! isset( $resolved['actions-after-submit'] ) && isset( $resolved['email'] ) ) { $resolved['actions-after-submit'] = [ 'email' ]; } return $resolved; } private function resolve_raw_settings( array $element, int $post_id ): array { $settings = $element['settings'] ?? []; $settings = $this->resolve_dynamic_tags_in_settings( $settings, $post_id ); return Settings_Resolver::resolve( $settings ); } /** * @param array|string $value * @param int $post_id * @return array|string|null */ private function resolve_dynamic_tags_in_settings( $value, int $post_id ) { if ( ! is_array( $value ) ) { return $value; } if ( Dynamic_Prop_Type::is_dynamic_prop_value( $value ) ) { if ( ! empty( $value['disabled'] ) ) { return null; } $tag_data = $value['value'] ?? []; $tag_name = $tag_data['name'] ?? ''; $tag_settings = $tag_data['settings'] ?? []; if ( empty( $tag_name ) ) { return null; } Plugin::elementor()->db->switch_to_post( $post_id ); try { return Plugin::elementor()->dynamic_tags->get_tag_data_content( null, $tag_name, $tag_settings ); } finally { Plugin::elementor()->db->restore_current_post(); } } return array_map( function ( $item ) use ( $post_id ) { return $this->resolve_dynamic_tags_in_settings( $item, $post_id ); }, $value ); } private function build_cssid_map( array $form_element, int $post_id ): array { $map = []; $this->collect_cssid_mappings( $form_element['elements'] ?? [], $map, $post_id ); return $map; } private function collect_cssid_mappings( array $elements, array &$map, int $post_id ): void { foreach ( $elements as $element ) { if ( ! empty( $element['elements'] ) ) { $this->collect_cssid_mappings( $element['elements'], $map, $post_id ); } if ( ! $this->is_atomic_form_field_element( $element ) ) { continue; } $element_id = $element['id'] ?? ''; $cssid = $this->get_field_cssid( $element, $post_id ); if ( $element_id && '' !== $cssid && ! isset( $map[ $cssid ] ) ) { $map[ $cssid ] = $element_id; } } } private function is_atomic_form_field_element( array $element ): bool { if ( 'widget' !== ( $element['elType'] ?? '' ) ) { return false; } $widget_type = $element['widgetType'] ?? ''; return in_array( $widget_type, self::FORM_FIELD_WIDGET_TYPES, true ); } private function get_field_cssid( array $element, int $post_id ): string { $resolved = $this->resolve_raw_settings( $element, $post_id ); $cssid = $resolved['_cssid'] ?? ''; if ( ! is_string( $cssid ) || '' === $cssid ) { return ''; } return sanitize_text_field( $cssid ); } private function send_invalid_form_response(): void { wp_send_json_error( [ 'message' => Ajax_Handler::get_default_message( Ajax_Handler::INVALID_FORM, [] ), ] ); } private function send_error_response( string $message = '' ): void { wp_send_json_error( [ 'message' => '' !== $message ? $message : Ajax_Handler::get_default_message( Ajax_Handler::ERROR, [] ), ] ); } private function send_response( array $action_results, bool $all_actions_succeeded, array $failed_actions ): void { $response_data = [ 'actionResults' => $action_results, 'allActionsSucceeded' => $all_actions_succeeded, 'failedActions' => $failed_actions, ]; if ( $all_actions_succeeded ) { wp_send_json_success( [ 'message' => Ajax_Handler::get_default_message( Ajax_Handler::SUCCESS, [] ), 'data' => $response_data, ] ); } else { $has_success = ! empty( $action_results ) && count( $failed_actions ) < count( $action_results ); if ( $has_success ) { wp_send_json_success( [ 'message' => Ajax_Handler::get_default_message( Ajax_Handler::SUCCESS, [] ), 'data' => $response_data, ] ); } else { wp_send_json_error( [ 'message' => Ajax_Handler::get_default_message( Ajax_Handler::ERROR, [] ), 'data' => $response_data, ] ); } } } public function __construct() { add_action( 'wp_ajax_elementor_pro_atomic_forms_send_form', [ $this, 'ajax_send_form' ] ); add_action( 'wp_ajax_nopriv_elementor_pro_atomic_forms_send_form', [ $this, 'ajax_send_form' ] ); } }