__( 'WebinarJam', 'advanced-form-integration' ),
'tasks' => array(
'register_webinar' => __( 'Register to webinar', 'advanced-form-integration' )
)
);
return $actions;
}
/**
* Get WebinarJam credentials by ID
*
* @param string $cred_id Credential ID (optional, can be from $_POST)
* @return array Array with 'api_token' key, or empty string if not found
*/
function adfoin_webinarjam_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_token = '';
if ( $cred_id ) {
$credentials = adfoin_read_credentials( 'webinarjam' );
foreach( $credentials as $single ) {
if( $single['id'] == $cred_id ) {
$api_token = $single['api_token'];
break;
}
}
} else {
// Fallback to old options if no cred_id provided
$api_token = get_option( 'adfoin_webinarjam_api_token' ) ? get_option( 'adfoin_webinarjam_api_token' ) : '';
}
return array(
'api_token' => $api_token
);
}
add_filter( 'adfoin_settings_tabs', 'adfoin_webinarjam_settings_tab', 10, 1 );
function adfoin_webinarjam_settings_tab( $providers ) {
$providers['webinarjam'] = __( 'WebinarJam', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_webinarjam_settings_view', 10, 1 );
function adfoin_webinarjam_settings_view( $current_tab ) {
if( $current_tab != 'webinarjam' ) {
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_token = get_option( 'adfoin_webinarjam_api_token' );
$existing_creds = adfoin_read_credentials( 'webinarjam' );
if ( $old_api_token && empty( $existing_creds ) ) {
$new_cred = array(
'id' => wp_generate_uuid4(),
'title' => 'Default Account',
'api_token' => $old_api_token
);
adfoin_save_credentials( 'webinarjam', array( $new_cred ) );
}
$fields = array(
array(
'name' => 'api_token',
'label' => __( 'API Token', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'placeholder' => __( 'Please enter API Token', 'advanced-form-integration' ),
'mask' => true, // Mask API token in table
'show_in_table' => true,
),
);
$instructions = '
- ' . __( 'Go to My webinars, click ADVANCED menu of any listed webinar.', 'advanced-form-integration' ) . '
- ' . __( 'Go to API custom integration and copy API Key.', 'advanced-form-integration' ) . '
- ' . __( 'Enter your API Token above.', 'advanced-form-integration' ) . '
- ' . __( 'Click "Add Account" and save your credentials.', 'advanced-form-integration' ) . '
';
ADFOIN_Account_Manager::render_settings_view( 'webinarjam', 'WebinarJam', $fields, $instructions );
}
// AJAX Hooks for Account Manager
add_action( 'wp_ajax_adfoin_get_webinarjam_credentials', 'adfoin_get_webinarjam_credentials' );
function adfoin_get_webinarjam_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( 'webinarjam' );
}
add_action( 'wp_ajax_adfoin_save_webinarjam_credentials', 'adfoin_save_webinarjam_credentials' );
function adfoin_save_webinarjam_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( 'webinarjam', array( 'api_token' ) );
}
add_action( 'wp_ajax_adfoin_get_webinarjam_credentials_list', 'adfoin_webinarjam_get_credentials_list_ajax' );
function adfoin_webinarjam_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_token', 'mask' => true ),
);
ADFOIN_Account_Manager::ajax_get_credentials_list( 'webinarjam', $fields );
}
add_action( 'adfoin_add_js_fields', 'adfoin_webinarjam_js_fields', 10, 1 );
function adfoin_webinarjam_js_fields( $field_data ) {}
add_action( 'adfoin_action_fields', 'adfoin_webinarjam_action_fields' );
function adfoin_webinarjam_action_fields() {
?>
30,
'method' => $method,
'headers' => array(
'Content-Type' => 'application/x-www-form-urlencoded',
),
'body' => array_merge( $data, array( 'api_key' => $api_token ) ),
);
$response = wp_remote_request( $url, $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
add_action( 'wp_ajax_adfoin_get_webinarjam_webinars', 'adfoin_get_webinarjam_webinars', 10, 0 );
/*
* Get Webinarjam accounts
*/
function adfoin_get_webinarjam_webinars() {
// Security Check
adfoin_verify_nonce();
$credentials = adfoin_webinarjam_get_credentials();
$api_token = $credentials['api_token'];
if( !$api_token ) {
wp_send_json_error();
return;
}
$response = adfoin_webinarjam_request( 'webinars', 'POST' );
if( !is_wp_error( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ) );
$lists = wp_list_pluck( $body->webinars, 'name', 'webinar_id' );
wp_send_json_success( $lists );
} else {
wp_send_json_error();
}
}
add_action( 'wp_ajax_adfoin_get_webinarjam_schedules', 'adfoin_get_webinarjam_schedules', 10, 0 );
/*
* Get Webinarjam list
*/
function adfoin_get_webinarjam_schedules() {
// Security Check
adfoin_verify_nonce();
$credentials = adfoin_webinarjam_get_credentials();
$api_token = $credentials['api_token'];
if( !$api_token ) {
wp_send_json_error();
return;
}
$webinar_id = $_POST["webinarId"] ? sanitize_text_field( wp_unslash( $_POST["webinarId"] ) ) : "";
if( ! $webinar_id ) {
wp_send_json_error();
}
$response = adfoin_webinarjam_request( 'webinar', 'POST', array( 'webinar_id' => $webinar_id ) );
if( !is_wp_error( $response ) ) {
$body = json_decode( wp_remote_retrieve_body( $response ) );
$schedules = wp_list_pluck( $body->webinar->schedules, 'date', 'schedule' );
wp_send_json_success( $schedules );
} else {
wp_send_json_error();
}
}
add_action( 'adfoin_webinarjam_job_queue', 'adfoin_webinarjam_job_queue', 10, 1 );
function adfoin_webinarjam_job_queue( $data ) {
adfoin_webinarjam_send_data( $data['record'], $data['posted_data'] );
}
/*
* Handles sending data to Webinarjam API
*/
function adfoin_webinarjam_send_data( $record, $posted_data ) {
$record_data = json_decode( $record['data'], true );
$field_data = isset( $record_data['field_data'] ) ? $record_data['field_data'] : array();
$cred_id = isset( $field_data['credId'] ) ? $field_data['credId'] : '';
$credentials = adfoin_webinarjam_get_credentials( $cred_id );
$api_token = $credentials['api_token'];
if( !$api_token ) {
return;
}
if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) {
return;
}
$data = $record_data['field_data'];
$task = $record['task'];
$webinar_id = empty( $data['webinarId'] ) ? '' : $data['webinarId'];
$schedule_id = empty( $data['scheduleId'] ) ? '' : $data['scheduleId'];
$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 );
$ip_address = empty( $data['ipAddress'] ) ? '' : adfoin_get_parsed_values( $data['ipAddress'], $posted_data );
$country_code = empty( $data['phoneCountryCode'] ) ? '' : adfoin_get_parsed_values( $data['phoneCountryCode'], $posted_data );
$phone = empty( $data['phone'] ) ? '' : adfoin_get_parsed_values( $data['phone'], $posted_data );
$timezone = empty( $data['timezone'] ) ? '' : adfoin_get_parsed_values( $data['timezone'], $posted_data );
$date = empty( $data['date'] ) ? '' : adfoin_get_parsed_values( $data['date'], $posted_data );
if( $task == 'register_webinar' ) {
$endpoint = 'register';
$request_data = array_filter(array(
'webinar_id' => $webinar_id,
'schedule' => $schedule_id,
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'phone_country_code' => $country_code,
'phone' => $phone,
'ip_address' => $ip_address,
'timezone' => $timezone,
'date' => $date,
));
$response = adfoin_webinarjam_request( $endpoint, 'POST', $request_data, $record, $cred_id );
adfoin_add_to_log( $response, $endpoint, $request_data, $record );
}
return;
}