` on every request. * * @link https://api.maropost.com/api/ */ add_filter( 'adfoin_action_providers', 'adfoin_maropost_actions', 10, 1 ); function adfoin_maropost_actions( $actions ) { $actions['maropost'] = array( 'title' => __( 'Maropost', 'advanced-form-integration' ), 'tasks' => array( 'create_contact' => __( 'Create or Update Contact', 'advanced-form-integration' ), ), ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_maropost_settings_tab', 10, 1 ); function adfoin_maropost_settings_tab( $providers ) { $providers['maropost'] = __( 'Maropost', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_maropost_settings_view', 10, 1 ); function adfoin_maropost_settings_view( $current_tab ) { if ( 'maropost' !== $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' => 'accountId', 'label' => __( 'Account ID', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'placeholder' => __( 'Numeric account ID from your Maropost dashboard', 'advanced-form-integration' ), 'show_in_table' => true, ), array( 'name' => 'apiToken', 'label' => __( 'Auth Token', 'advanced-form-integration' ), 'type' => 'text', 'required' => true, 'mask' => true, 'placeholder' => __( 'Generate under Connections → API in Maropost', 'advanced-form-integration' ), 'show_in_table' => true, ), array( 'name' => 'host', 'label' => __( 'API Host (optional)', 'advanced-form-integration' ), 'type' => 'text', 'required' => false, 'placeholder' => 'https://api.maropost.com', 'show_in_table' => false, ), ); $instructions = sprintf( '
https://api.maropost.com'
)
);
ADFOIN_Account_Manager::render_settings_view( 'maropost', __( 'Maropost', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_maropost_credentials', 'adfoin_get_maropost_credentials', 10, 0 );
function adfoin_get_maropost_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( 'maropost' );
}
add_action( 'wp_ajax_adfoin_save_maropost_credentials', 'adfoin_save_maropost_credentials', 10, 0 );
function adfoin_save_maropost_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( 'maropost', array( 'accountId', 'apiToken', 'host' ) );
}
/**
* Render ', 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( 'maropost', array(
'accountId' => 'adfoin_maropost_account_id',
'apiToken' => 'adfoin_maropost_api_key',
'host' => 'adfoin_maropost_host',
) );
}
}, 20 );
add_action( 'adfoin_action_fields', 'adfoin_maropost_action_fields' );
function adfoin_maropost_action_fields() {
?>
__( 'No Maropost account selected.', 'advanced-form-integration' ) ) );
}
$response = adfoin_maropost_request( '/lists.json', 'GET', array(), array(), $cred_id );
if ( is_wp_error( $response ) ) {
wp_send_json_error( array( 'message' => $response->get_error_message() ) );
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $body ) || ! is_array( $body ) ) {
wp_send_json_error();
}
$lists = array();
foreach ( $body as $list ) {
if ( isset( $list['id'], $list['name'] ) ) {
$lists[ $list['id'] ] = $list['name'];
}
}
wp_send_json_success( $lists );
}
add_action( 'adfoin_maropost_job_queue', 'adfoin_maropost_job_queue', 10, 1 );
function adfoin_maropost_job_queue( $data ) {
adfoin_maropost_send_data( $data['record'], $data['posted_data'] );
}
function adfoin_maropost_send_data( $record, $posted_data ) {
$record_data = json_decode( $record['data'], true );
if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) {
return;
}
$data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array();
$task = isset( $record['task'] ) ? $record['task'] : '';
$cred_id = isset( $data['credId'] ) ? $data['credId'] : '';
if ( 'create_contact' !== $task ) {
return;
}
if ( empty( $cred_id ) ) {
adfoin_add_to_log( new WP_Error( 'maropost_missing_cred', __( 'No Maropost account selected.', 'advanced-form-integration' ) ), '', array(), $record );
return;
}
$email = empty( $data['email'] ) ? '' : trim( adfoin_get_parsed_values( $data['email'], $posted_data ) );
if ( empty( $email ) ) {
return;
}
$params = array();
$params[] = 'contact[email]=' . rawurlencode( $email );
$first_name = adfoin_get_parsed_values( $data['firstName'] ?? '', $posted_data );
$last_name = adfoin_get_parsed_values( $data['lastName'] ?? '', $posted_data );
if ( $first_name ) {
$params[] = 'contact[first_name]=' . rawurlencode( $first_name );
}
if ( $last_name ) {
$params[] = 'contact[last_name]=' . rawurlencode( $last_name );
}
$list_id = isset( $data['listId'] ) ? $data['listId'] : '';
if ( $list_id ) {
$params[] = 'add_list_ids[]=' . rawurlencode( $list_id );
}
$body = implode( '&', array_filter( $params ) );
adfoin_maropost_request( '/contacts.json', 'POST', $body, $record, $cred_id );
}
/**
* Maropost API request.
*
* @param string $endpoint Path under /accounts/{id}/.
* @param string $method HTTP verb.
* @param mixed $data Body (string or array).
* @param array $record Submission record for logging.
* @param string $cred_id Saved credential id.
*
* @return array|WP_Error
*/
function adfoin_maropost_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) {
$host = '';
$account_id = '';
$api_token = '';
if ( $cred_id && function_exists( 'adfoin_get_credentials_by_id' ) ) {
$credentials = adfoin_get_credentials_by_id( 'maropost', $cred_id );
if ( is_array( $credentials ) ) {
$host = isset( $credentials['host'] ) ? trim( (string) $credentials['host'] ) : '';
$account_id = isset( $credentials['accountId'] ) ? trim( (string) $credentials['accountId'] ) : '';
$api_token = isset( $credentials['apiToken'] ) ? trim( (string) $credentials['apiToken'] ) : '';
}
}
// Fallback to legacy single-option storage.
if ( ! $api_token ) {
$host = $host ?: get_option( 'adfoin_maropost_host', '' );
$account_id = $account_id ?: get_option( 'adfoin_maropost_account_id', '' );
$api_token = $api_token ?: get_option( 'adfoin_maropost_api_key', '' );
}
if ( ! $host ) {
$host = 'https://api.maropost.com';
}
$host = untrailingslashit( $host );
if ( ! $account_id || ! $api_token ) {
return new WP_Error( 'maropost_missing_credentials', __( 'Maropost credentials are not configured.', 'advanced-form-integration' ) );
}
$endpoint = '/' . ltrim( $endpoint, '/' );
$base_url = $host . '/accounts/' . rawurlencode( $account_id );
$request_url = add_query_arg(
array(
'auth_token' => $api_token,
),
$base_url . $endpoint
);
$args = array(
'timeout' => 30,
'method' => $method,
);
if ( in_array( strtoupper( $method ), array( 'POST', 'PUT', 'PATCH', 'DELETE' ), true ) ) {
if ( is_string( $data ) ) {
$args['body'] = $data;
$args['headers'] = isset( $args['headers'] ) ? $args['headers'] : array();
$args['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
} else {
$args['body'] = $data;
}
}
$response = wp_remote_request( $request_url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $request_url, $args, $record );
}
return $response;
}