Forked from wpmudev-sls/wpmudev-coursepress-completion-email.php
Last active
September 6, 2019 06:07
-
-
Save bappi-d-great/835f698dbeebda8f57eb17213ebc3cc0 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
* 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 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() { | |
add_filter( 'the_content', function( $content ){ | |
global $wp, $post; | |
// Check if user has already got an email | |
if( metadata_exists( 'post', $post->ID, 'course_completed_email_' . get_current_user_id() ) ){ | |
return $content; | |
} | |
if ( ! empty( $wp->query_vars['course_completion'] ) ) { | |
$survey_url = 'http://www.example.com'; | |
$send_to = array(); | |
$email_subject = 'A student has completed a course!'; | |
$email_content = ''; | |
$course_title = $post->post_title; | |
$student = get_user_by( 'ID', get_current_user_id() ); | |
$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' ) | |
); | |
$send_to[] = '[email protected]'; | |
$send_to[] = '[email protected]'; | |
// Enable HTML Support | |
add_filter( 'wp_mail_content_type', function(){ return "text/html"; } ); | |
$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."; | |
if($survey_url){ | |
$email_content .= "\n<a href='{$survey_url}'>{$survey_url}</a>"; | |
} | |
if( wp_mail( $send_to, $email_subject, nl2br( $email_content ) ) ){ | |
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