__( 'Academy LMS', 'advanced-form-integration' ), 'tasks' => array( 'enroll' => __( 'Enroll to a course', 'advanced-form-integration' ), 'unenroll' => __( 'Unenroll from a course', 'advanced-form-integration' ) ) ); return $actions; } add_filter( 'adfoin_settings_tabs', 'adfoin_academylms_settings_tab', 10, 1 ); function adfoin_academylms_settings_tab( $providers ) { $providers['academylms'] = __( 'Academy LMS', 'advanced-form-integration' ); return $providers; } add_action( 'adfoin_settings_view', 'adfoin_academylms_settings_view', 10, 1 ); function adfoin_academylms_settings_view( $current_tab ) { if( $current_tab != 'academylms' ) { return; } $nonce = wp_create_nonce( 'adfoin_academylms_settings' ); ?>

Make sure that the plugin is activated. Then, set up a new integration and proceed.


'academy_courses', 'post_status' => 'publish', 'posts_per_page' => -1, ) ); $courses = wp_list_pluck( $course_list, 'post_title', 'ID' ); wp_send_json_success( $courses ); } add_action( 'wp_ajax_adfoin_get_academylms_lessons', 'adfoin_get_academylms_lessons', 10, 0 ); function adfoin_get_academylms_lessons() { // Security Check adfoin_verify_nonce(); $lesson_list = \Academy\Traits\Lessons::get_lessons(); $lessons = array(); foreach( $lesson_list as $lesson ) { $lessons[$lesson->ID] = $lesson->lesson_title; } wp_send_json_success( $lessons ); } add_action( 'adfoin_academylms_job_queue', 'adfoin_academylms_job_queue', 10, 1 ); function adfoin_academylms_job_queue( $data ) { adfoin_academylms_send_data( $data['record'], $data['posted_data'] ); } /* * Handles sending data to academylms API */ function adfoin_academylms_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']; $course_id = empty( $data['courseId'] ) ? '' : adfoin_get_parsed_values( $data['courseId'], $posted_data ); // $lesson_id = empty( $data['lessonId'] ) ? '' : adfoin_get_parsed_values( $data['lessonId'], $posted_data ); $task = $record['task']; $email = isset( $data['email'] ) ? adfoin_get_parsed_values( $data['email'], $posted_data ) : ''; if( $task == 'enroll' ) { adfoin_academylms_enroll_course( $course_id, $email, $data, $posted_data ); } elseif( $task == 'unenroll' ) { adfoin_academylms_unenroll_course( $course_id, $email ); } else { return; } return; } function adfoin_academylms_enroll_course( $course_id, $email, $data, $posted_data ) { $course_id = intval( $course_id ); $user_id = email_exists( $email ); if( !$user_id ) { $first_name = isset( $data['firstName'] ) ? adfoin_get_parsed_values( $data['firstName'], $posted_data ) : ''; $last_name = isset( $data['lastName'] ) ? adfoin_get_parsed_values( $data['lastName'], $posted_data ) : ''; $username = isset( $data['username'] ) ? adfoin_get_parsed_values( $data['username'], $posted_data ) : ''; $password = isset( $data['password'] ) ? adfoin_get_parsed_values( $data['password'], $posted_data ) : ''; if( !$password ) { $password = wp_generate_password( 12, false ); } $user_id = wp_create_user( $username, $password, $email ); wp_update_user( array( 'ID' => $user_id, 'first_name' => $first_name, 'last_name' => $last_name ) ); } if( $course_id && $user_id ) { $user = new WP_User( $user_id ); $user->add_role( 'academy_student' ); add_filter( 'is_course_purchasable', '__return_false', 10 ); $result = \Academy\Helper::do_enroll( $course_id, $user_id ); remove_filter( 'is_course_purchasable', '__return_false', 10 ); } } function adfoin_academylms_unenroll_course( $course_id, $email ) { $course_id = intval( $course_id ); $user_id = email_exists( $email ); if( $course_id && $user_id ) { global $wpdb; $query = $wpdb->prepare( "SELECT ID, post_author, post_date, post_date_gmt, post_title, post_status as enrolled_status FROM {$wpdb->posts} WHERE post_type = %s AND post_parent = %d AND post_author = %d", 'academy_enrolled', $course_id, $user_id ); $enrolled = $wpdb->get_row( $query ); if( $enrolled ) { $wpdb->delete( $wpdb->posts, array( 'post_type' => 'academy_enrolled', 'post_author' => $user_id, 'post_parent' => $course_id ) ); $order_id = get_post_meta( $enrolled->ID, 'academy_enrolled_by_order_id', true ); delete_post_meta( $enrolled->ID, 'academy_enrolled_by_order_id' ); delete_post_meta( $enrolled->ID, 'academy_enrolled_by_product_id' ); delete_post_meta( $order_id, 'is_academy_order_for_course' ); delete_post_meta( $order_id, 'academy_order_for_course_id_' . $course_id ); delete_user_meta( $user_id, 'academy_course_' . $course_id . '_completed_topics' ); } } }