__( 'Teachable', 'advanced-form-integration' ),
'tasks' => array(
'create_user' => __( 'Create User', 'advanced-form-integration' ),
'enroll_user' => __( 'Enroll User in Course', 'advanced-form-integration' ),
),
);
return $actions;
}
add_filter( 'adfoin_settings_tabs', 'adfoin_teachable_settings_tab', 10, 1 );
function adfoin_teachable_settings_tab( $providers ) {
$providers['teachable'] = __( 'Teachable', 'advanced-form-integration' );
return $providers;
}
add_action( 'adfoin_settings_view', 'adfoin_teachable_settings_view', 10, 1 );
function adfoin_teachable_settings_view( $current_tab ) {
if ( 'teachable' !== $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' => 'apiKey',
'label' => __( 'API Key', 'advanced-form-integration' ),
'type' => 'text',
'required' => true,
'mask' => true,
'placeholder' => __( 'Paste your Teachable API key', 'advanced-form-integration' ),
'show_in_table' => true,
),
);
$instructions = sprintf(
'
- %s
- %s
- %s
- %s
',
esc_html__( 'Sign in to your Teachable school as the school owner.', 'advanced-form-integration' ),
esc_html__( 'Open Settings → API in the school admin sidebar.', 'advanced-form-integration' ),
esc_html__( 'Click "Create API key", give it a descriptive name (e.g. WordPress), and copy the generated key.', 'advanced-form-integration' ),
esc_html__( 'Paste the key below. AFI calls https://developers.teachable.com/v1/ with this key in the apiKey header.', 'advanced-form-integration' )
);
ADFOIN_Account_Manager::render_settings_view( 'teachable', __( 'Teachable', 'advanced-form-integration' ), $fields, $instructions );
}
add_action( 'wp_ajax_adfoin_get_teachable_credentials', 'adfoin_get_teachable_credentials', 10, 0 );
function adfoin_get_teachable_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( 'teachable' );
}
add_action( 'wp_ajax_adfoin_save_teachable_credentials', 'adfoin_save_teachable_credentials', 10, 0 );
function adfoin_save_teachable_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( 'teachable', array( 'apiKey' ) );
}
function adfoin_teachable_credentials_list() {
foreach ( adfoin_read_credentials( 'teachable' ) as $option ) {
printf( '', esc_attr( $option['id'] ), esc_html( $option['title'] ) );
}
}
add_action( 'adfoin_action_fields', 'adfoin_teachable_action_fields' );
function adfoin_teachable_action_fields() {
?>
'email', 'value' => __( 'Email (required — used to look up or create the user)', 'advanced-form-integration' ), 'required' => true ),
array( 'key' => 'name', 'value' => __( 'Name (used when creating a new user if email is not found)', 'advanced-form-integration' ) ),
array( 'key' => 'course_id', 'value' => __( 'Course ID (required, integer)', 'advanced-form-integration' ), 'required' => true ),
);
} else {
// create_user (default)
$fields = array(
array( 'key' => 'name', 'value' => __( 'Name (required)', 'advanced-form-integration' ), 'required' => true ),
array( 'key' => 'email', 'value' => __( 'Email (required)', 'advanced-form-integration' ), 'required' => true ),
array( 'key' => 'password', 'value' => __( 'Password (optional — leave blank to let Teachable send a signup email)', 'advanced-form-integration' ) ),
array( 'key' => 'role', 'value' => __( 'Role (student / owner / admin — defaults to student)', 'advanced-form-integration' ) ),
);
}
wp_send_json_success( $fields );
}
add_action( 'adfoin_teachable_job_queue', 'adfoin_teachable_job_queue', 10, 1 );
function adfoin_teachable_job_queue( $data ) {
adfoin_teachable_send_data( $data['record'], $data['posted_data'] );
}
function adfoin_teachable_send_data( $record, $posted_data ) {
$task = $record['task'] ?? '';
if ( ! in_array( $task, array( 'create_user', 'enroll_user' ), true ) ) {
return;
}
$record_data = json_decode( $record['data'], true );
if ( adfoin_check_conditional_logic( $record_data['action_data']['cl'] ?? array(), $posted_data ) ) {
return;
}
$field_data = $record_data['field_data'] ?? array();
$cred_id = $field_data['credId'] ?? '';
if ( ! $cred_id ) {
return;
}
// Resolve all field-mapped values up-front.
$reserved = array( 'credId' => 1 );
$values = array();
foreach ( $field_data as $key => $value ) {
if ( isset( $reserved[ $key ] ) ) {
continue;
}
$parsed = adfoin_get_parsed_values( $value, $posted_data );
if ( '' !== $parsed && null !== $parsed ) {
$values[ $key ] = $parsed;
}
}
if ( 'create_user' === $task ) {
if ( empty( $values['name'] ) || empty( $values['email'] ) ) {
return;
}
$payload = array(
'name' => (string) $values['name'],
'email' => (string) $values['email'],
'role' => ! empty( $values['role'] ) ? (string) $values['role'] : 'student',
);
if ( ! empty( $values['password'] ) ) {
$payload['password'] = (string) $values['password'];
}
adfoin_teachable_request( 'users', 'POST', $payload, $record, $cred_id );
return;
}
// enroll_user
if ( empty( $values['email'] ) || empty( $values['course_id'] ) ) {
return;
}
$course_id = (int) $values['course_id'];
if ( $course_id <= 0 ) {
return;
}
$email = (string) $values['email'];
$user_id = 0;
// 1) Look up existing user by email.
$lookup = adfoin_teachable_request( 'users', 'GET', array( 'email' => $email ), $record, $cred_id );
if ( ! is_wp_error( $lookup ) ) {
$body = json_decode( wp_remote_retrieve_body( $lookup ), true );
// Teachable returns { users: [ { id, ... } ] } — but defensively
// handle both that and a bare array response.
$candidates = array();
if ( is_array( $body ) ) {
if ( isset( $body['users'] ) && is_array( $body['users'] ) ) {
$candidates = $body['users'];
} elseif ( isset( $body[0] ) ) {
$candidates = $body;
}
}
foreach ( $candidates as $candidate ) {
if ( is_array( $candidate ) && ! empty( $candidate['id'] ) ) {
// Prefer an exact email match if multiple come back.
if ( ! empty( $candidate['email'] ) && strcasecmp( (string) $candidate['email'], $email ) === 0 ) {
$user_id = (int) $candidate['id'];
break;
}
if ( ! $user_id ) {
$user_id = (int) $candidate['id'];
}
}
}
}
// 2) If no user, create one. Name is helpful but not strictly required
// by every Teachable endpoint — fall back to the email local-part.
if ( ! $user_id ) {
$create_name = ! empty( $values['name'] ) ? (string) $values['name'] : substr( $email, 0, strpos( $email, '@' ) ?: strlen( $email ) );
$create_payload = array(
'name' => $create_name,
'email' => $email,
'role' => 'student',
);
$created = adfoin_teachable_request( 'users', 'POST', $create_payload, $record, $cred_id );
if ( is_wp_error( $created ) ) {
return;
}
$created_body = json_decode( wp_remote_retrieve_body( $created ), true );
if ( is_array( $created_body ) ) {
if ( ! empty( $created_body['id'] ) ) {
$user_id = (int) $created_body['id'];
} elseif ( ! empty( $created_body['user']['id'] ) ) {
$user_id = (int) $created_body['user']['id'];
}
}
}
if ( ! $user_id ) {
return;
}
// 3) Enroll.
$enroll_payload = array(
'user_id' => (int) $user_id,
'course_id' => $course_id,
);
adfoin_teachable_request( 'enroll', 'POST', $enroll_payload, $record, $cred_id );
}
if ( ! function_exists( 'adfoin_teachable_request' ) ) :
/**
* Authenticated JSON request against https://developers.teachable.com/v1/.
* Auth header is `apiKey: {key}` (Teachable-specific — not Authorization).
*/
function adfoin_teachable_request( $endpoint, $method = 'GET', $data = array(), $record = array(), $cred_id = '' ) {
$credentials = adfoin_get_credentials_by_id( 'teachable', $cred_id );
if ( ! is_array( $credentials ) || empty( $credentials['apiKey'] ) ) {
return new WP_Error( 'teachable_missing_credentials', __( 'Teachable API key not configured.', 'advanced-form-integration' ) );
}
$url = 'https://developers.teachable.com/v1/' . ltrim( $endpoint, '/' );
$method = strtoupper( $method );
$args = array(
'timeout' => 30,
'method' => $method,
'headers' => array(
'apiKey' => $credentials['apiKey'],
'Accept' => 'application/json',
),
);
if ( in_array( $method, array( 'POST', 'PUT', 'PATCH' ), true ) ) {
$args['headers']['Content-Type'] = 'application/json';
$args['body'] = wp_json_encode( $data );
} elseif ( 'GET' === $method && is_array( $data ) && ! empty( $data ) ) {
$url = add_query_arg( array_map( 'rawurlencode', $data ), $url );
}
$response = wp_remote_request( esc_url_raw( $url ), $args );
if ( $record ) {
adfoin_add_to_log( $response, $url, $args, $record );
}
return $response;
}
endif;