__( 'Flodesk', 'advanced-form-integration' ),
'tasks' => [
'subscribe' => __( 'Subscribe', 'advanced-form-integration' ),
]
];
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_flodesk_settings_tab', 10 );
function adfoin_flodesk_settings_tab( $providers ) {
$providers['flodesk'] = __( 'Flodesk', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_flodesk_settings_view', 10 );
function adfoin_flodesk_settings_view( $current_tab ) {
if( $current_tab != 'flodesk' ) {
return;
}
$title = __( 'Flodesk', 'advanced-form-integration' );
$key = 'flodesk';
$arguments = wp_json_encode([
'platform' => 'flodesk',
'fields' => [
[
'key' => 'apiKey',
'label' => __( 'API Key', 'advanced-form-integration' ),
'hidden' => true
]
]
]);
$instructions = sprintf(
'
- %s
- %s
',
__('Go to Profile > Integrations > API Keys.', 'advanced-form-integration'),
__('Crate API Key and copy.', 'advanced-form-integration')
);
echo adfoin_platform_settings_template( $title, $key, $arguments, $instructions );
}
function adfoin_flodesk_credentials_list() {
$credentials = adfoin_read_credentials( 'flodesk' );
foreach ( $credentials as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
add_action( 'adfoin_action_fields', 'adfoin_flodesk_action_fields' );
function adfoin_flodesk_action_fields() {
?>
30,
'method' => $method,
'headers' => array(
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'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_flodesk_credentials', 'adfoin_get_flodesk_credentials', 10, 0 );
/*
* Get Flodesk credentials
*/
function adfoin_get_flodesk_credentials() {
adfoin_verify_nonce();
$all_credentials = adfoin_read_credentials( 'flodesk' );
wp_send_json_success( $all_credentials );
}
add_action( 'wp_ajax_adfoin_save_flodesk_credentials', 'adfoin_save_flodesk_credentials', 10, 0 );
/*
* Save Flodesk credentials
*/
function adfoin_save_flodesk_credentials() {
adfoin_verify_nonce();
$platform = sanitize_text_field( wp_unslash( $_POST['platform'] ) );
if( 'flodesk' == $platform ) {
$data = adfoin_array_map_recursive( 'sanitize_text_field', $_POST['data'] );
adfoin_save_credentials( $platform, $data );
}
wp_send_json_success();
}
add_action( 'wp_ajax_adfoin_get_flodesk_segments', 'adfoin_get_flodesk_segments', 10, 0 );
/*
* Get Flodesk subscriber lists
*/
function adfoin_get_flodesk_segments() {
adfoin_verify_nonce();
$cred_id = sanitize_text_field( wp_unslash( $_POST['credId'] ) );
$page = 1;
$per_page = 100;
$all_segments = [];
do {
$data = adfoin_flodesk_request( 'segments?page=' . $page . '&per_page=' . $per_page, 'GET', array(), array(), $cred_id );
if( is_wp_error( $data ) ) {
wp_send_json_error();
}
$body = json_decode( wp_remote_retrieve_body( $data ), true );
$segments = wp_list_pluck( $body['data'], 'name', 'id' );
$all_segments = array_merge( $all_segments, $segments );
$page++;
} while ( count( $body['data'] ) == $per_page );
wp_send_json_success( $all_segments );
}
add_action( 'adfoin_flodesk_job_queue', 'adfoin_flodesk_job_queue', 10, 1 );
function adfoin_flodesk_job_queue( $data ) {
adfoin_flodesk_send_data( $data['record'], $data['posted_data'] );
}
/*
* Handles sending data to flodesk API
*/
function adfoin_flodesk_send_data( $record, $posted_data ) {
$record_data = json_decode( $record['data'], true );
if ( isset( $record_data["action_data"]["cl"] ) && adfoin_check_conditional_logic( $record_data["action_data"]["cl"], $posted_data ) ) return;
$data = $record_data['field_data'];
$segment_id = isset( $data['segmentId'] ) ? $data['segmentId'] : '';
$cred_id = isset( $data['credId'] ) ? $data['credId'] : '';
$doptin = isset( $data['doptin'] ) && $data['doptin'] ? true : false;
$task = $record['task'];
if( $task == 'subscribe' ) {
$subscriber_data = array_filter(
array(
'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 ),
'double_optin' => $doptin,
)
);
$return = adfoin_flodesk_request( 'subscribers', 'POST', $subscriber_data, $record, $cred_id );
if( !is_wp_error( $return ) && !empty( $segment_id ) ) {
adfoin_flodesk_request( "subscribers/{$subscriber_data['email']}/segments", 'POST', array('segment_ids' => array($segment_id)), $record, $cred_id );
}
}
return;
}