__( 'Sendinblue', 'advanced-form-integration' ), 'tasks' => array( 'subscribe' => __( 'Subscribe To List', 'advanced-form-integration' ) ) ); return $actions; } /** * Get Sendinblue credentials by ID * * @param string $cred_id Credential ID (optional, can be from $_POST) * @return array Array with 'api_key' key, or empty string if not found */ function adfoin_sendinblue_get_credentials( $cred_id = '' ) { // If no cred_id provided, try to get from POST if ( empty( $cred_id ) && isset( $_POST['credId'] ) ) { $cred_id = sanitize_text_field( wp_unslash( $_POST['credId'] ) ); } $api_key = ''; if ( $cred_id ) { $credentials = adfoin_read_credentials( 'sendinblue' ); foreach( $credentials as $single ) { if( $single['id'] == $cred_id ) { $api_key = $single['api_key']; break; } } } else { // Fallback to old options if no cred_id provided $api_key = get_option( 'adfoin_sendinblue_api_key' ) ? get_option( 'adfoin_sendinblue_api_key' ) : ''; } return array( 'api_key' => $api_key ); } add_filter( 'adfoin_settings_tabs', 'adfoin_sendinblue_settings_tab', 10, 1 ); function adfoin_sendinblue_settings_tab( $providers ) { $providers['sendinblue'] = __( 'Sendinblue', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_sendinblue_settings_view', 10, 1 ); function adfoin_sendinblue_settings_view( $current_tab ) { if( $current_tab != 'sendinblue' ) { return; } // Load Account Manager if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } // Migrate old settings if they exist and no new credentials exist $old_api_key = get_option( 'adfoin_sendinblue_api_key' ); $existing_creds = adfoin_read_credentials( 'sendinblue' ); if ( $old_api_key && empty( $existing_creds ) ) { $new_cred = array( 'id' => wp_generate_uuid4(), 'title' => 'Default Account (Legacy)', 'api_key' => $old_api_key ); adfoin_save_credentials( 'sendinblue', array( $new_cred ) ); } $fields = array( array( 'name' => 'api_key', 'label' => __( 'API Key', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'placeholder' => __( 'Please enter API Key', 'advanced-form-integration' ), 'mask' => true, 'show_in_table' => true, ), ); $instructions = '
  1. ' . __( 'Go to Brevo API Keys page.', 'advanced-form-integration' ) . '
  2. ' . __( 'Generate a new API Key and copy it.', 'advanced-form-integration' ) . '
  3. ' . __( 'Enter the API Key in the field above.', 'advanced-form-integration' ) . '
  4. ' . __( 'Click "Add Account" and save your credentials.', 'advanced-form-integration' ) . '
'; ADFOIN_Account_Manager::render_settings_view( 'sendinblue', 'Sendinblue', $fields, $instructions ); } // AJAX Hooks for Account Manager add_action( 'wp_ajax_adfoin_get_sendinblue_credentials', 'adfoin_get_sendinblue_credentials' ); function adfoin_get_sendinblue_credentials() { if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } ADFOIN_Account_Manager::ajax_get_credentials( 'sendinblue' ); } add_action( 'wp_ajax_adfoin_save_sendinblue_credentials', 'adfoin_save_sendinblue_credentials' ); function adfoin_save_sendinblue_credentials() { if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } ADFOIN_Account_Manager::ajax_save_credentials( 'sendinblue', array( 'api_key' ) ); } add_action( 'wp_ajax_adfoin_get_sendinblue_credentials_list', 'adfoin_sendinblue_get_credentials_list_ajax' ); function adfoin_sendinblue_get_credentials_list_ajax() { if ( ! wp_verify_nonce( $_POST['_nonce'], 'advanced-form-integration' ) ) { return; } if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } $fields = array( array( 'name' => 'api_key', 'mask' => true ), ); ADFOIN_Account_Manager::ajax_get_credentials_list( 'sendinblue', $fields ); } add_action( 'admin_post_adfoin_save_sendinblue_api_key', 'adfoin_save_sendinblue_api_key', 10, 0 ); function adfoin_save_sendinblue_api_key() { // Security Check // Authorization check adfoin_require_manage_options(); if (! wp_verify_nonce( $_POST['_nonce'], 'adfoin_sendinblue_settings' ) ) { die( __( 'Security check Failed', 'advanced-form-integration' ) ); } $api_key = sanitize_text_field( wp_unslash( $_POST["adfoin_sendinblue_api_key"] ) ); // Save tokens update_option( "adfoin_sendinblue_api_key", $api_key ); advanced_form_integration_redirect( "admin.php?page=advanced-form-integration-settings&tab=sendinblue" ); } add_action( 'adfoin_add_js_fields', 'adfoin_sendinblue_js_fields', 10, 1 ); function adfoin_sendinblue_js_fields( $field_data ) {} add_action( 'adfoin_action_fields', 'adfoin_sendinblue_action_fields' ); function adfoin_sendinblue_action_fields() { ?> 15, 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $api_key ) ); $page = 0; $limit = 50; $has_value = true; $all_data = array(); while( $has_value ) { $offset = $page * $limit; // Sendinblue rebranded to Brevo in 2023; api.sendinblue.com still // CNAMEs but is officially deprecated. The plugin keeps a separate // platforms/brevo/ folder for the new integration; legacy users on // this slug get redirected to the canonical host. $url = "https://api.brevo.com/v3/contacts/lists?limit={$limit}&offset={$offset}"; $data = wp_remote_request( $url, $args ); if( is_wp_error( $data ) ) { wp_send_json_error( $data->get_error_message() ); return; } $body = json_decode( $data["body"] ); if( empty( $body->lists ) ) { $has_value = false; } else { $lists = wp_list_pluck( $body->lists, 'name', 'id' ); $all_data = $all_data + $lists; $page++; } } wp_send_json_success( $all_data ); } add_action( 'adfoin_sendinblue_job_queue', 'adfoin_sendinblue_job_queue', 10, 1 ); function adfoin_sendinblue_job_queue( $data ) { adfoin_sendinblue_send_data( $data['record'], $data['posted_data'] ); } /* * Handles sending data to Sendinblue API */ function adfoin_sendinblue_send_data( $record, $posted_data ) { $record_data = json_decode( $record["data"], true ); $field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array(); $cred_id = isset( $field_data['credId'] ) ? $field_data['credId'] : ''; $credentials = adfoin_sendinblue_get_credentials( $cred_id ); $api_key = $credentials['api_key']; if( !$api_key ) { return; } if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) { return; } $data = $record_data["field_data"]; $task = $record["task"]; if( $task == "subscribe" ) { $list_id = $data["listId"]; $email = empty( $data["email"] ) ? "" : adfoin_get_parsed_values( $data["email"], $posted_data ); $first_name = empty( $data["firstName"] ) ? "" : adfoin_get_parsed_values( $data["firstName"], $posted_data ); $last_name = empty( $data["lastName"] ) ? "" : adfoin_get_parsed_values( $data["lastName"], $posted_data ); $sms = empty( $data["sms"] ) ? "" : adfoin_get_parsed_values( $data["sms"], $posted_data ); $data = array( "email" => $email, "attributes" => array( "SMS" => $sms, "FIRSTNAME" => $first_name, "LASTNAME" => $last_name, "FIENAME" => $last_name, "VORNAME" => $first_name, "NACHNAME" => $last_name, "NOMBRE" => $first_name, "APELLIDOS" => $last_name, "NOME" => $first_name, "COGNOME" => $last_name, "SOBRENOME" => $last_name, "PRENOM" => $first_name, "NOM" => $last_name ), 'listIds' => array( intval( $list_id ) ), 'updateEnabled' => true ); $data = array_filter( $data ); $url = "https://api.brevo.com/v3/contacts"; $args = array( 'timeout' => 30, 'headers' => array( 'Content-Type' => 'application/json', 'api-key' => $api_key ), 'body' => wp_json_encode( $data ) ); $return = wp_remote_post( $url, $args ); adfoin_add_to_log( $return, $url, $args, $record ); } return; }