start_controls_section( 'abdwp_becs_brevo_section', array( 'label' => esc_html__( 'Brevo Subscribe', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'condition' => array( 'submit_actions' => $this->get_name(), ), ) ); $list_options = $this->get_list_options(); if ( ! empty( $list_options ) ) { $widget->add_control( 'abdwp_becs_brevo_synced_list_ids', array( 'label' => esc_html__( 'Brevo Lists', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::SELECT2, 'multiple' => true, 'options' => $list_options, 'label_block' => true, 'description' => esc_html__( 'Lists synced from Settings > AbdullahWP Contact Sync for Brevo and Elementor Forms.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); } $widget->add_control( 'abdwp_becs_brevo_list_ids', array( 'label' => esc_html__( 'Manual List IDs', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => '2,7,12', 'description' => esc_html__( 'Fallback list IDs, separated by commas. Used together with selected synced lists.', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'label_block' => true, ) ); $widget->add_control( 'abdwp_becs_brevo_email_field', array( 'label' => esc_html__( 'Email Field ID', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'email', 'description' => esc_html__( 'Use the Elementor form field ID, not the label.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); $widget->add_control( 'abdwp_becs_brevo_firstname_field', array( 'label' => esc_html__( 'First Name Field ID', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'first_name', 'description' => esc_html__( 'Optional. Sends to the Brevo FIRSTNAME attribute.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); $widget->add_control( 'abdwp_becs_brevo_lastname_field', array( 'label' => esc_html__( 'Last Name Field ID', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'last_name', 'description' => esc_html__( 'Optional. Sends to the Brevo LASTNAME attribute.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); $widget->add_control( 'abdwp_becs_brevo_phone_field', array( 'label' => esc_html__( 'Phone Field ID', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXT, 'default' => 'phone', 'description' => esc_html__( 'Optional. Sends to the Brevo SMS attribute.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); $widget->add_control( 'abdwp_becs_brevo_custom_attributes', array( 'label' => esc_html__( 'Custom Attribute Mapping', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::TEXTAREA, 'placeholder' => "COMPANY=company\nJOB_TITLE=job_title", 'description' => esc_html__( 'Optional. One Brevo attribute and Elementor field ID per line: ATTRIBUTE=field_id.', 'abdullahwp-contact-sync-for-brevo-elementor' ), ) ); $widget->add_control( 'abdwp_becs_brevo_update_existing', array( 'label' => esc_html__( 'Update Existing Contact', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => esc_html__( 'Yes', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'label_off' => esc_html__( 'No', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'return_value' => 'yes', 'default' => 'yes', ) ); $widget->add_control( 'abdwp_becs_brevo_fail_on_error', array( 'label' => esc_html__( 'Show Error If Brevo Fails', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => esc_html__( 'Yes', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'label_off' => esc_html__( 'No', 'abdullahwp-contact-sync-for-brevo-elementor' ), 'return_value' => 'yes', 'default' => '', ) ); $widget->end_controls_section(); } /** * Sends the submitted form data to Brevo. * * @param object $record Elementor form record. * @param object $ajax_handler Elementor AJAX handler. */ public function run( $record, $ajax_handler ): void { $settings = (array) $record->get( 'form_settings' ); $fields = $this->get_form_fields( $record ); $api_key = Plugin::get_api_key(); if ( '' === $api_key ) { $this->handle_error( __( 'Brevo API key is missing.', 'abdullahwp-contact-sync-for-brevo-elementor' ), $settings, $ajax_handler ); return; } $list_ids = $this->parse_list_ids( $settings ); if ( empty( $list_ids ) ) { $this->handle_error( __( 'Brevo list ID is missing.', 'abdullahwp-contact-sync-for-brevo-elementor' ), $settings, $ajax_handler ); return; } $email_field = sanitize_key( $settings['abdwp_becs_brevo_email_field'] ?? 'email' ); $email = isset( $fields[ $email_field ] ) ? sanitize_email( $fields[ $email_field ] ) : ''; if ( empty( $email ) || ! is_email( $email ) ) { $this->handle_error( __( 'A valid email address is missing.', 'abdullahwp-contact-sync-for-brevo-elementor' ), $settings, $ajax_handler ); return; } $payload = array( 'email' => $email, 'listIds' => $list_ids, 'updateEnabled' => isset( $settings['abdwp_becs_brevo_update_existing'] ) && 'yes' === $settings['abdwp_becs_brevo_update_existing'], ); $attributes = $this->build_attributes( $settings, $fields ); if ( ! empty( $attributes ) ) { $payload['attributes'] = $attributes; } $client = new Brevo_Client( $api_key ); $result = $client->create_or_update_contact( $payload ); if ( ! $result['success'] ) { $this->handle_error( $result['message'], $settings, $ajax_handler ); } } /** * Removes site-specific Brevo settings from exported Elementor templates. * * @param array $element Exported Elementor element. */ public function on_export( $element ): array { unset( $element['settings']['abdwp_becs_brevo_synced_list_ids'], $element['settings']['abdwp_becs_brevo_list_ids'], $element['settings']['abdwp_becs_brevo_email_field'], $element['settings']['abdwp_becs_brevo_firstname_field'], $element['settings']['abdwp_becs_brevo_lastname_field'], $element['settings']['abdwp_becs_brevo_phone_field'], $element['settings']['abdwp_becs_brevo_custom_attributes'], $element['settings']['abdwp_becs_brevo_update_existing'], $element['settings']['abdwp_becs_brevo_fail_on_error'] ); return $element; } /** * Builds Elementor SELECT2 options from synced Brevo lists. */ private function get_list_options(): array { $options = array(); foreach ( Plugin::get_saved_lists() as $list ) { if ( empty( $list['id'] ) ) { continue; } $options[ (string) absint( $list['id'] ) ] = sprintf( '%s (%d)', $list['name'] ?? __( 'Untitled list', 'abdullahwp-contact-sync-for-brevo-elementor' ), absint( $list['id'] ) ); } return $options; } /** * Normalizes Elementor submitted fields by field ID. * * @param object $record Elementor form record. */ private function get_form_fields( $record ): array { $raw_fields = $record->get( 'fields' ); $fields = array(); if ( ! is_array( $raw_fields ) ) { return $fields; } foreach ( $raw_fields as $id => $field ) { $fields[ sanitize_key( $id ) ] = isset( $field['value'] ) ? $this->normalize_field_value( $field['value'] ) : ''; } return $fields; } /** * Converts Elementor field values into sanitized strings. * * @param mixed $value Raw Elementor field value. */ private function normalize_field_value( $value ): string { if ( is_array( $value ) ) { $value = implode( ', ', array_map( 'sanitize_text_field', $value ) ); } return sanitize_text_field( (string) $value ); } /** * Combines synced and manual list IDs. * * @param array $settings Elementor form action settings. */ private function parse_list_ids( array $settings ): array { $list_ids = array(); if ( ! empty( $settings['abdwp_becs_brevo_synced_list_ids'] ) ) { $synced_ids = (array) $settings['abdwp_becs_brevo_synced_list_ids']; $list_ids = array_merge( $list_ids, $synced_ids ); } if ( ! empty( $settings['abdwp_becs_brevo_list_ids'] ) ) { $manual_ids = explode( ',', (string) $settings['abdwp_becs_brevo_list_ids'] ); $list_ids = array_merge( $list_ids, $manual_ids ); } $list_ids = array_map( 'absint', $list_ids ); $list_ids = array_filter( $list_ids ); return array_values( array_unique( $list_ids ) ); } /** * Builds Brevo contact attributes from field mappings. * * @param array $settings Elementor form action settings. * @param array $fields Submitted Elementor fields. */ private function build_attributes( array $settings, array $fields ): array { $attributes = array(); $standard = array( 'FIRSTNAME' => $settings['abdwp_becs_brevo_firstname_field'] ?? '', 'LASTNAME' => $settings['abdwp_becs_brevo_lastname_field'] ?? '', 'SMS' => $settings['abdwp_becs_brevo_phone_field'] ?? '', ); foreach ( $standard as $attribute => $field_id ) { $field_id = sanitize_key( $field_id ); if ( '' !== $field_id && ! empty( $fields[ $field_id ] ) ) { $attributes[ $attribute ] = $fields[ $field_id ]; } } $custom_mapping = (string) ( $settings['abdwp_becs_brevo_custom_attributes'] ?? '' ); foreach ( preg_split( '/\r\n|\r|\n/', $custom_mapping ) as $line ) { $parts = array_map( 'trim', explode( '=', $line, 2 ) ); if ( 2 !== count( $parts ) || '' === $parts[0] || '' === $parts[1] ) { continue; } $attribute = preg_replace( '/[^A-Z0-9_]/', '', strtoupper( $parts[0] ) ); $field_id = sanitize_key( $parts[1] ); if ( '' !== $attribute && '' !== $field_id && ! empty( $fields[ $field_id ] ) ) { $attributes[ $attribute ] = $fields[ $field_id ]; } } return $attributes; } /** * Logs or surfaces Brevo failures according to form settings. * * @param string $message Internal error message. * @param array $settings Elementor form action settings. * @param object $ajax_handler Elementor AJAX handler. */ private function handle_error( string $message, array $settings, $ajax_handler ): void { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( '[AbdullahWP Contact Sync for Brevo and Elementor Forms] ' . $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log } $fail_on_error = isset( $settings['abdwp_becs_brevo_fail_on_error'] ) && 'yes' === $settings['abdwp_becs_brevo_fail_on_error']; if ( $fail_on_error && is_object( $ajax_handler ) && method_exists( $ajax_handler, 'add_error_message' ) ) { $ajax_handler->add_error_message( esc_html__( 'Subscription failed. Please try again.', 'abdullahwp-contact-sync-for-brevo-elementor' ) ); } } }