__( 'SendFox', 'advanced-form-integration' ),
'tasks' => array(
'subscribe' => __( 'Subscribe To List', 'advanced-form-integration' )
)
);
return $actions;
}
/**
* Get Sendfox 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_sendfox_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( 'sendfox' );
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_sendfox_api_key' ) ? get_option( 'adfoin_sendfox_api_key' ) : '';
}
return array(
'api_key' => $api_key
);
}
add_filter( 'adfoin_settings_tabs', 'adfoin_sendfox_settings_tab', 10, 1 );
function adfoin_sendfox_settings_tab( $providers ) {
$providers['sendfox'] = __( 'SendFox', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_sendfox_settings_view', 10, 1 );
function adfoin_sendfox_settings_view( $current_tab ) {
if( $current_tab != 'sendfox' ) {
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_sendfox_api_key' );
$existing_creds = adfoin_read_credentials( 'sendfox' );
if ( $old_api_key && empty( $existing_creds ) ) {
$new_cred = array(
'id' => wp_generate_uuid4(),
'title' => 'Default Account',
'api_key' => $old_api_key
);
adfoin_save_credentials( 'sendfox', array( $new_cred ) );
}
$fields = array(
array(
'name' => 'api_key',
'label' => __( 'SendFox Personal Access Token', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Enter Access Token', 'advanced-form-integration' ),
'description' => __( 'Go to https://sendfox.com/account/oauth and click "Create New Token"', 'advanced-form-integration' ),
'mask' => true, // Mask API key in table
'show_in_table' => true,
),
);
$instructions = '
- ' . __( 'Go to your SendFox account.', 'advanced-form-integration' ) . '
- ' . __( 'Navigate to https://sendfox.com/account/oauth.', 'advanced-form-integration' ) . '
- ' . __( 'Click "Create New Token" and copy the token.', 'advanced-form-integration' ) . '
- ' . __( 'Enter your Access Token above.', 'advanced-form-integration' ) . '
- ' . __( 'Click "Add Account" and save your credentials.', 'advanced-form-integration' ) . '
';
ADFOIN_Account_Manager::render_settings_view( 'sendfox', 'SendFox', $fields, $instructions );
}
// AJAX Hooks for Account Manager
add_action( 'wp_ajax_adfoin_get_sendfox_credentials', 'adfoin_get_sendfox_credentials' );
function adfoin_get_sendfox_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( 'sendfox' );
}
add_action( 'wp_ajax_adfoin_save_sendfox_credentials', 'adfoin_save_sendfox_credentials' );
function adfoin_save_sendfox_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( 'sendfox', array( 'api_key' ) );
}
add_action( 'wp_ajax_adfoin_get_sendfox_credentials_list', 'adfoin_sendfox_get_credentials_list_ajax' );
function adfoin_sendfox_get_credentials_list_ajax() {
adfoin_verify_nonce();
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( 'sendfox', $fields );
}
add_action( 'adfoin_add_js_fields', 'adfoin_sendfox_js_fields', 10, 1 );
function adfoin_sendfox_js_fields( $field_data ) { }
add_action( 'adfoin_action_fields', 'adfoin_sendfox_action_fields' );
function adfoin_sendfox_action_fields() {
?>
30,
'method' => $method,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key,
),
);
if ( in_array( $method, array( 'POST', 'PUT', 'PATCH' ) ) ) {
$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_sendfox_list', 'adfoin_get_sendfox_list', 10, 0 );
/*
* Get Mailchimp subscriber lists
*/
function adfoin_get_sendfox_list() {
// Security Check
adfoin_verify_nonce();
$cred_id = isset( $_POST['credId'] ) ? sanitize_text_field( wp_unslash( $_POST['credId'] ) ) : '';
$lists = array();
$data = adfoin_sendfox_request( 'lists', 'GET', array(), array(), $cred_id );
if( !is_wp_error( $data ) ) {
$body = json_decode( wp_remote_retrieve_body( $data ), true );
$lists = $body['data'];
$lists_total = absint( $body['total'] );
$list_per_page = absint( $body['per_page'] );
$pagination_needed = absint( $lists_total / $list_per_page ) + 1;
if( $pagination_needed >= 2) {
$response_pages = array();
$response_body = array();
for( $i = 2; $i <= $pagination_needed; $i++ ){
$response_pages[$i] = adfoin_sendfox_request( 'lists?page=' . $i, 'GET', array(), array(), $cred_id );
$response_body[$i] = json_decode( wp_remote_retrieve_body( $response_pages[$i] ), true );
if( $response_body[$i]['data'] && is_array( $response_body[$i]['data'] ) )
{
$lists = array_merge( $lists, $response_body[$i]['data'] );
}
}
}
$final_list = wp_list_pluck( $lists, 'name', 'id' );
wp_send_json_success( $final_list );
} else {
wp_send_json_error();
}
}
add_action( 'adfoin_sendfox_job_queue', 'adfoin_sendfox_job_queue', 10, 1 );
function adfoin_sendfox_job_queue( $data ) {
adfoin_sendfox_send_data( $data['record'], $data['posted_data'] );
}
/*
* Handles sending data to SendFox API
*/
function adfoin_sendfox_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 = $record_data['field_data'];
$list_id = isset( $data['listId'] ) ? $data['listId'] : '';
$task = $record['task'];
$cred_id = isset( $data['credId'] ) ? $data['credId'] : '';
$email = empty( $data['email'] ) ? '' : trim( adfoin_get_parsed_values( $data['email'], $posted_data ) );
if ( $task == 'subscribe' ) {
if ( ! $email ) {
return;
}
$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 );
$ip_address = empty( $data['ipAddress'] ) ? '' : adfoin_get_parsed_values( $data['ipAddress'], $posted_data );
$subscriber_data = array(
'email' => $email,
);
if ( $first_name ) { $subscriber_data['first_name'] = $first_name; }
if ( $last_name ) { $subscriber_data['last_name'] = $last_name; }
if ( $ip_address ) { $subscriber_data['ip_address'] = $ip_address; }
if ( $list_id ) {
$subscriber_data['lists'] = array( (int) $list_id );
}
$return = adfoin_sendfox_request( 'contacts', 'POST', $subscriber_data, $record, $cred_id );
}
}