Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active October 3, 2019 14:30
Show Gist options
  • Save wpmudev-sls/5fb3f7646bd5ea41af4968e5296a3517 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/5fb3f7646bd5ea41af4968e5296a3517 to your computer and use it in GitHub Desktop.
[ CoursePress 2 ] - Email Notifications on Completion. Sends an email notification to the Student upon Course Completion
<?php
/**
* Plugin Name: [CoursePress 2] - Email notifications on completion
* Plugin URI: https://premium.wpmudev.org/
* Description: Sends an email notification to the student upon course completion (as of 2.2.2)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_CoursePress_Completion_Email' ) ) {
class WPMUDEV_CoursePress_Completion_Email {
private $courses = array(
'27',
'100'
);
private $admins = array(
'laura@wpmudev.host',
'kayla@wpmudev.host'
);
private $survey_url = 'http://www.example.com';
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_CoursePress_Completion_Email();
}
return self::$_instance;
}
private function __construct() {
$this->init();
}
private function init(){
add_filter( 'the_content', array( $this, 'wpmudev_coursepress_completion_email' ) );
}
public function wpmudev_coursepress_completion_email( $content ){
global $wp, $post;
// Check if is the course we need
if( ! in_array( $post->ID, $this->courses ) ){
return $content;
}
// Check if user has already got an email
if( metadata_exists( 'post', $post->ID, 'course_completed_email_' . get_current_user_id() ) ){
return $content;
}
// Upon completion, do the following
if ( ! empty( $wp->query_vars['course_completion'] ) ) {
$headers = array();
$admins = array();
$survey_url = $this->survey_url;
$course_title = $post->post_title;
$student = get_user_by( 'ID', get_current_user_id() );
$send_to = $student->data->user_email;
$email_subject = "Thank you for completing \"{$course_title}\"";
$email_content = '';
$instructors = (array) CoursePress_Data_Course::get_setting( $post->ID, 'instructors', array() );
$assessment_link = add_query_arg(
array(
'post_type' => 'course',
'page' => 'coursepress_assessments',
'view' => 'profile',
'course_id' => $post->ID,
'student_id' => $student->data->ID,
),
admin_url( 'edit.php' )
);
// Enable HTML Support
add_filter( 'wp_mail_content_type', function(){ return "text/html"; } );
// Email body
$email_content = "Dear {$student->data->display_name},\n
Congratulations on successful completion of {$course_title}!\n
Your certificate of completion is being processed and will be delivered to you via email in the next 1-2 business days.\n
In the meantime, if you could provide us some feedback on your experience, that would be very helpful in the development of our e-training program.\n
Click the link the below and complete this brief, 2-minute survey to receive 10% off any future Geophysical Insights course purchase.";
// Email survey URL
if( $survey_url ){
$email_content .= "\n\n<a href='{$survey_url}'>{$survey_url}</a>";
}
// Check for admins, notify them and add them as Cc
if ( ! empty( $this->admins ) ){
foreach( $this->admins as $email ){
if( filter_var( $email, FILTER_VALIDATE_EMAIL ) ){
$headers[] = "Cc: {$email}";
wp_mail( $email, $email_subject, nl2br( $email_content ) );
// Add a delay if you experience sending issues
// sleep( 5 );
}
}
}
// Send notification to student, adding admins as Cc
if( wp_mail( $send_to, $email_subject, nl2br( $email_content ), $headers ) ){
add_post_meta( $post->ID, 'course_completed_email_' . get_current_user_id(), $student->data->user_email );
}
}
return $content;
}
}
if ( ! function_exists( 'WPMUDEV_CoursePress_Completion_Email' ) ) {
function WPMUDEV_CoursePress_Completion_Email() {
return WPMUDEV_CoursePress_Completion_Email::get_instance();
};
add_action( 'plugins_loaded', 'WPMUDEV_CoursePress_Completion_Email', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment