Skip to content

Instantly share code, notes, and snippets.

@growdev
Last active October 2, 2017 21:27
Show Gist options
  • Save growdev/d8c7c980c6cbb7a0757e064b7a5fa5b4 to your computer and use it in GitHub Desktop.
Save growdev/d8c7c980c6cbb7a0757e064b7a5fa5b4 to your computer and use it in GitHub Desktop.
Hook into Sensei start and end course.
<?php
/**
* Hook in and do an action when a user starts a course.
* This works for WooCommerce purchased courses and free courses.
*/
add_filter( 'sensei_user_course_start', 'convertkit_sensei_user_course_start', 10, 2 );
function convertkit_sensei_user_course_start( $user_id, $course_id ) {
if ( $user_id ) {
$user = get_userdata( $user_id );
$email = $user->user_email;
// Zap URL
$url = '';
$payload = array(
'email' => $email,
'user_id' => $user_id,
'action' => 'started',
'course_id' => $course_id,
);
// Setup request args.
$http_args = array(
'method' => 'POST',
'timeout' => MINUTE_IN_SECONDS,
'redirection' => 0,
'httpversion' => '1.0',
'blocking' => true,
'user-agent' => 'ConvertKit Webhook',
'body' => trim( json_encode( $payload ) ),
'headers' => array( 'Content-Type' => 'application/json' ),
'cookies' => array(),
);
// Webhook away!
$response = wp_safe_remote_request( $url, $http_args );
}
}
/**
* Hook in and do an action when a user finishes a course.
*/
add_action( 'sensei_user_course_end', 'convertkit_sensei_user_course_end', 10, 2 );
function convertkit_sensei_user_course_end( $user_id, $course_id ) {
if ( $user_id ) {
$user = get_userdata( $user_id );
$email = $user->user_email;
// Zap URL
$url = '';
$payload = array(
'email' => $email,
'user_id' => $user_id,
'action' => 'completed',
'course_id' => $course_id,
);
// Setup request args.
$http_args = array(
'method' => 'POST',
'timeout' => MINUTE_IN_SECONDS,
'redirection' => 0,
'httpversion' => '1.0',
'blocking' => true,
'user-agent' => 'ConvertKit Webhook',
'body' => trim( json_encode( $payload ) ),
'headers' => array( 'Content-Type' => 'application/json' ),
'cookies' => array(),
);
// Webhook away!
$response = wp_safe_remote_request( $url, $http_args );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment