__( 'Maileon', 'advanced-form-integration' ),
'tasks' => [
'subscribe' => __( 'Add Contact', 'advanced-form-integration' ),
]
];
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_maileon_settings_tab', 10, 1 );
function adfoin_maileon_settings_tab( $providers ) {
$providers['maileon'] = __( 'Maileon', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_maileon_settings_view', 10, 1 );
function adfoin_maileon_settings_view( $current_tab ) {
if( $current_tab != 'maileon' ) {
return;
}
if ( ! class_exists( 'ADFOIN_Account_Manager' ) ) {
require_once plugin_dir_path( __FILE__ ) . '../../includes/class-adfoin-account-manager.php';
}
$fields = array(
array(
'name' => 'apiKey',
'label' => __( 'API Key', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'placeholder' => __( 'Enter API Key', 'advanced-form-integration' ),
'show_in_table' => true,
),
);
$instructions = sprintf(
'
- %s
- %s
- %s
',
__( 'Go to Settings > API Keys in Maileon.', 'advanced-form-integration' ),
__( 'Create a new API Key (v1).', 'advanced-form-integration' ),
__( 'Paste the API Key below and save.', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'maileon', __( 'Maileon', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_maileon_credentials', 'adfoin_get_maileon_credentials', 10, 0 );
function adfoin_get_maileon_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( 'maileon' );
}
add_action( 'wp_ajax_adfoin_save_maileon_credentials', 'adfoin_save_maileon_credentials', 10, 0 );
function adfoin_save_maileon_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( 'maileon', array( 'apiKey' ) );
}
// Legacy single-account import: surfaces old `adfoin_maileon_*` options
// as a Legacy Account record when the new credentials store is empty.
add_action( 'plugins_loaded', function() {
if ( class_exists( 'ADFOIN_Account_Manager' ) ) {
ADFOIN_Account_Manager::register_legacy_option_importer( 'maileon', array(
'apiKey' => 'adfoin_maileon_api_key',
) );
}
}, 20 );
add_action( 'adfoin_action_fields', 'adfoin_maileon_action_fields' );
function adfoin_maileon_action_fields() {
?>
$method,
'timeout' => 30,
'headers' => [
'Content-Type' => 'application/vnd.maileon.api+json',
'Accept' => 'application/vnd.maileon.api+xml; charset=utf-8',
'Authorization' => 'Basic ' . base64_encode( $api_key ),
]
];
if ('POST' == $method || 'PUT' == $method) {
$args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
add_action( 'wp_ajax_adfoin_get_maileon_fields', 'adfoin_get_maileon_fields' );
function adfoin_get_maileon_fields() {
adfoin_verify_nonce();
$cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : '';
$standard_fields = [
[ 'key' => 'email', 'value' => 'Email', 'description' => ''],
[ 'key' => 'firstName', 'value' => 'First Name', 'description' => ''],
[ 'key' => 'lastName', 'value' => 'Last Name', 'description' => ''],
[ 'key' => 'title', 'value' => 'Title', 'description' => ''],
[ 'key' => 'organization', 'value' => 'Organization', 'description' => ''],
[ 'key' => 'gender', 'value' => 'Gender', 'description' => ''],
[ 'key' => 'birthday', 'value' => 'Birthday', 'description' => ''],
[ 'key' => 'address', 'value' => 'Address', 'description' => ''],
[ 'key' => 'city', 'value' => 'City', 'description' => ''],
[ 'key' => 'zip', 'value' => 'Zip', 'description' => ''],
[ 'key' => 'region', 'value' => 'Region', 'description' => ''],
[ 'key' => 'state', 'value' => 'State', 'description' => ''],
[ 'key' => 'country', 'value' => 'Country', 'description' => ''],
[ 'key' => 'src', 'value' => 'src', 'description' => '']
];
$custom_fields = adfoin_get_maileon_custom_fields( $cred_id );
$all_fields = array_merge( $standard_fields, $custom_fields );
wp_send_json_success( $all_fields );
}
function adfoin_get_maileon_custom_fields( $cred_id = '' ) {
$response = adfoin_maileon_request( 'contacts/fields/custom', 'GET', array(), array(), $cred_id );
$xml = wp_remote_retrieve_body( $response );
if ( !is_wp_error( $response ) ) {
$xmlObject = simplexml_load_string( $xml );
$fields = [];
foreach ( $xmlObject->custom_field as $field ) {
$fields[] = [
'key' => 'custom__' . ( string ) $field->type. '__' . ( string ) $field->name,
'value' => ( string ) $field->name
];
}
}
return $fields;
}
add_action( 'adfoin_maileon_job_queue', 'adfoin_maileon_job_queue', 10, 1 );
function adfoin_maileon_job_queue( $data ) {
adfoin_maileon_send_data( $data['record'], $data['posted_data'] );
}
/*
* Handles sending data to maileon API
*/
function adfoin_maileon_send_data( $record, $posted_data ) {
$record_data = json_decode( $record['data'], true );
if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? [], $posted_data ) ) return;
$data = $record_data['field_data'];
$task = $record['task'];
$cred_id = isset( $data['credId'] ) ? $data['credId'] : '';
// Backward compatibility: if no credId, use first available credential
if ( empty( $cred_id ) ) {
$all_credentials = adfoin_read_credentials( 'maileon' );
if ( ! empty( $all_credentials ) ) {
$cred_id = $all_credentials[0]['id'];
}
}
if( $task == 'subscribe' ) {
$email = empty( $data['email'] ) ? '' : adfoin_get_parsed_values( $data['email'], $posted_data );
$request_data = array(
'standard_fields' => array_filter( array(
'FIRSTNAME' => isset( $data['firstName'] ) ? adfoin_get_parsed_values( $data['firstName'], $posted_data ) : '',
'LASTNAME' => isset( $data['lastName'] ) ? adfoin_get_parsed_values( $data['lastName'], $posted_data ) : '',
'TITLE' => isset( $data['title'] ) ? adfoin_get_parsed_values( $data['title'], $posted_data ) : '',
'ORGANIZATION' => isset( $data['organization'] ) ? adfoin_get_parsed_values( $data['organization'], $posted_data ) : '',
'GENDER' => isset( $data['gender'] ) ? adfoin_get_parsed_values( $data['gender'], $posted_data ) : '',
'BIRTHDAY' => isset( $data['birthday'] ) ? adfoin_get_parsed_values( $data['birthday'], $posted_data ) : '',
'ADDRESS' => isset( $data['address'] ) ? adfoin_get_parsed_values( $data['address'], $posted_data ) : '',
'CITY' => isset( $data['city'] ) ? adfoin_get_parsed_values( $data['city'], $posted_data ) : '',
'ZIP' => isset( $data['zip'] ) ? adfoin_get_parsed_values( $data['zip'], $posted_data ) : '',
'REGION' => isset( $data['region'] ) ? adfoin_get_parsed_values( $data['region'], $posted_data ) : '',
'STATE' => isset( $data['state'] ) ? adfoin_get_parsed_values( $data['state'], $posted_data ) : '',
'COUNTRY' => isset( $data['country'] ) ? adfoin_get_parsed_values( $data['country'], $posted_data ) : '',
) ),
);
$url_attributes = array_filter( array(
'permission' => isset( $data['permission'] ) ? $data['permission'] : 1,
'doi' => isset( $data['doi'] ) ? $data['doi'] : false,
'doiplus' => isset( $data['doiplus'] ) ? $data['doiplus'] : false,
'src' => isset( $data['src'] ) ? $data['src'] : '',
) );
if( isset( $data['update'] ) && $data['update'] == 'true' ) {
$url_attributes['sync_mode'] = 1;
}
$url_attributes = array_filter( $url_attributes );
$url_param = add_query_arg( $url_attributes, $email );
$custom_fields = [];
foreach ( $data as $key => $value ) {
if ( strpos( $key, 'custom__' ) !== false && !empty( $value ) ) {
$parsed_value = adfoin_get_parsed_values( $value, $posted_data );
if( empty( $parsed_value ) ) continue;
$field = explode( '__', str_replace( 'custom__', '', $key ) );
if ( $field[0] == 'boolean' ) {
$parsed_value = ( strtolower( $parsed_value ) == 'yes' || $parsed_value == 'true' || $parsed_value == 1 ) ? true : false;
}
$custom_fields[$field[1]] = $parsed_value;
}
}
if ( !empty( $custom_fields ) ) {
$request_data['custom_fields'] = $custom_fields;
}
if ( adfoin_maileon_contact_exists( $email, $cred_id ) ) {
$return = adfoin_maileon_request( 'contacts/email/' . $url_param, 'PUT', $request_data, $record, $cred_id );
} else {
$return = adfoin_maileon_request( 'contacts/email/' . $url_param, 'POST', $request_data, $record, $cred_id );
}
}
return;
}
function adfoin_maileon_contact_exists( $email, $cred_id = '' ) {
$response = adfoin_maileon_request( 'contacts/email/' . $email, 'GET', array(), array(), $cred_id );
$xml = wp_remote_retrieve_body( $response );
if ( !is_wp_error( $response ) ) {
$xmlObject = simplexml_load_string( $xml );
if ( isset( $xmlObject->email ) && $xmlObject->email == $email ) {
return true;
}
}
return false;
}