__( 'Dotdigital', 'advanced-form-integration' ), 'tasks' => array( 'add_contact' => __( 'Create or Update Contact', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_dotdigital_settings_tab', 10, 1 ); function adfoin_dotdigital_settings_tab( $providers ) { $providers['dotdigital'] = __( 'Dotdigital', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_dotdigital_settings_view', 10, 1 ); function adfoin_dotdigital_settings_view( $current_tab ) { if ( 'dotdigital' !== $current_tab ) { return; } if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) { require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php'; } $fields = array( array( 'name' => 'apiUser', 'label' => __( 'API Username', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'placeholder' => 'apiuser-xxxxx@apiconnector.com', 'show_in_table' => true, ), array( 'name' => 'apiPass', 'label' => __( 'API Password', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => true, 'show_in_table' => false, ), array( 'name' => 'region', 'label' => __( 'Region', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'placeholder' => 'r1', 'show_in_table' => true, ), ); $instructions = sprintf( '
  1. %s
  2. %s
  3. %s
', esc_html__( 'In Dotdigital go to Settings → Access → API users and create a new API user. Copy the email-style username and password.', 'advanced-form-integration' ), esc_html__( 'Note the region from your account URL or the API user page (e.g. r1, r2, r3).', 'advanced-form-integration' ), esc_html__( 'Paste them below. AFI calls https://{region}-api.dotdigital.com/v2/ with HTTP Basic auth.', 'advanced-form-integration' ) ); ADFOIN_Account_Manager::render_settings_view( 'dotdigital', __( 'Dotdigital', 'advanced-form-integration' ), $fields, $instructions ); } add_action( 'wp_ajax_adfoin_get_dotdigital_credentials', 'adfoin_get_dotdigital_credentials', 10, 0 ); function adfoin_get_dotdigital_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_list( 'dotdigital' ); } add_action( 'wp_ajax_adfoin_save_dotdigital_credentials', 'adfoin_save_dotdigital_credentials', 10, 0 ); function adfoin_save_dotdigital_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( 'dotdigital', array( 'apiUser', 'apiPass', 'region' ) ); } function adfoin_dotdigital_credentials_list() { foreach ( adfoin_read_credentials( 'dotdigital' ) as $option ) { printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) ); } } /** * Migrate legacy single-option credentials into the multi-account store. */ add_action( 'plugins_loaded', function () { if ( class_exists( 'ADFOIN_Account_Manager' ) ) { ADFOIN_Account_Manager::register_legacy_option_importer( 'dotdigital', array( 'apiUser' => 'adfoin_dotdigital_api_user', 'apiPass' => 'adfoin_dotdigital_api_pass', 'region' => 'adfoin_dotdigital_api_region', ) ); } }, 20 ); add_action( 'adfoin_action_fields', 'adfoin_dotdigital_action_fields' ); function adfoin_dotdigital_action_fields() { ?> $email, 'optInType' => isset( $field_data['optInType'] ) ? (string) $field_data['optInType'] : 'Unknown', 'dataFields' => array(), ); $simple = array( 'firstName' => 'FIRSTNAME', 'lastName' => 'LASTNAME', ); foreach ( $simple as $key => $df_key ) { if ( empty( $field_data[ $key ] ) ) { continue; } $value = trim( (string) adfoin_get_parsed_values( $field_data[ $key ], $posted_data ) ); if ( '' !== $value ) { $payload['dataFields'][] = array( 'key' => $df_key, 'value' => $value ); } } $payload = apply_filters( 'adfoin_dotdigital_contact_payload', $payload, $field_data, $posted_data ); $response = adfoin_dotdigital_request( 'contacts', 'POST', $payload, $record, $cred_id ); if ( is_wp_error( $response ) ) { return; } // Legacy fallback: older accounts still return 409 on existing contact. if ( 409 === (int) wp_remote_retrieve_response_code( $response ) ) { $payload['matchIdentifiers'] = array( 'email' => $email ); adfoin_dotdigital_request( 'contacts', 'PUT', $payload, $record, $cred_id ); } } if ( ! function_exists( 'adfoin_dotdigital_request' ) ) : /** * Call the Dotdigital v2 API. * * @param string $endpoint Path under /v2/. * @param string $method HTTP verb. * @param mixed $data Body (POST/PUT) or query (GET). * @param array $record Submission record for logging. * @param string $cred_id Saved credential id. * * @return array|WP_Error */ function adfoin_dotdigital_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) { $api_user = ''; $api_pass = ''; $region = ''; if ( $cred_id && function_exists( 'adfoin_get_credentials_by_id' ) ) { $credentials = adfoin_get_credentials_by_id( 'dotdigital', $cred_id ); if ( is_array( $credentials ) ) { $api_user = isset( $credentials['apiUser'] ) ? trim( (string) $credentials['apiUser'] ) : ''; $api_pass = isset( $credentials['apiPass'] ) ? trim( (string) $credentials['apiPass'] ) : ''; $region = isset( $credentials['region'] ) ? trim( (string) $credentials['region'] ) : ''; } } if ( ! $api_user ) { $api_user = (string) get_option( 'adfoin_dotdigital_api_user', '' ); } if ( ! $api_pass ) { $api_pass = (string) get_option( 'adfoin_dotdigital_api_pass', '' ); } if ( ! $region ) { $region = (string) get_option( 'adfoin_dotdigital_api_region', '' ); } if ( ! $api_user || ! $api_pass || ! $region ) { return new WP_Error( 'dotdigital_missing_credentials', __( 'Dotdigital credentials are not configured.', 'advanced-form-integration' ) ); } $url = sprintf( 'https://%s-api.dotdigital.com/v2/', $region ) . ltrim( $endpoint, '/' ); $method = strtoupper( $method ); $args = array( 'timeout' => 30, 'method' => $method, 'headers' => array( 'Authorization' => 'Basic ' . base64_encode( $api_user . ':' . $api_pass ), 'Accept' => 'application/json', ), ); if ( in_array( $method, array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $data ); } elseif ( ! empty( $data ) && is_array( $data ) ) { $url = add_query_arg( $data, $url ); } $response = wp_remote_request( $url, $args ); if ( $record ) { adfoin_add_to_log( $response, $url, $args, $record ); } return $response; } endif;