Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active January 12, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/281990f69df22ecbab19860373d96d3b to your computer and use it in GitHub Desktop.
Save andrewlimaza/281990f69df22ecbab19860373d96d3b to your computer and use it in GitHub Desktop.
Enrol users into a Sensei LMS course from Zapier [WP Zapier]
<?php
/**
* Enrol a learner to a Sensei LMS course when creating or updating a user from Zapier.
* Requires Sensei LMS and WP Zapier plugin.
*
* See https://yoohooplugins.com/plugins/zapier-integration/
*/
function my_wp_zapier_enroll_user_sensei_lms( $user_id ) {
// Bail if Sensei LMS isn't active.
if ( !class_exists( 'Sensei_Utils' ) ) {
return;
}
if ( empty( $user_id ) ) {
return;
}
if ( strpos( $_REQUEST['sensei_course'], ',') ) {
$course_id = explode(',', $_REQUEST['sensei_course'] );
} else {
$course_id = intval( $_REQUEST['sensei_course'] );
}
if ( empty( $course_id ) ) {
return;
}
if ( is_array( $course_id ) ) {
foreach( $course_id as $course ) {
if ( ! empty( $course ) ) {
$result = Sensei_Utils::user_start_course($user_id, intval( $course ) );
$manual_enrol = Sensei_Course_Manual_Enrolment_Provider::instance();
$enrolled = $manual_enrol->enrol_learner( $user_id, $course_id );
}
}
} else {
$result = Sensei_Utils::user_start_course($user_id, $course_id);
$manual_enrol = Sensei_Course_Manual_Enrolment_Provider::instance();
$enrolled = $manual_enrol->enrol_learner( $user_id, $course_id );
}
}
add_action( 'wp_zapier_after_create_user', 'my_wp_zapier_enroll_user_sensei_lms', 10, 1 );
add_action( 'wp_zapier_after_update_user', 'my_wp_zapier_enroll_user_sensei_lms', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment