Skip to content

Instantly share code, notes, and snippets.

@mtndev
Created February 19, 2020 23:59
Show Gist options
  • Save mtndev/15f1d68381dfa97b9d4e64b418e9d374 to your computer and use it in GitHub Desktop.
Save mtndev/15f1d68381dfa97b9d4e64b418e9d374 to your computer and use it in GitHub Desktop.
Send an email with PDF attachments to each checked-in Attendee at the close of an Event
<?php
/*
* Plugin Name: Tickera - Certificates of Attendance
* Description: A plugin created to demonstrate how to build a PDF document from WordPress posts.
* Version: 1.0
* Author: Luke Robinson
*/
/*
Notes for future implementation: this file in addition to the my-events.php file in myaccount template files for wp are the main files. You will have to add in the tab in the events
tab in the myaccount. For the editing, you have to create a new post category for the pdf templates.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$tcac = new TC_Attendance_Certificates();
/* metadata_exists('post', $post_id, 'meta_key_to_check'); */
require_once('tc-checkin-email.php');
require_once(ABSPATH . 'wp-content/plugins/tickera/includes/tcpdf/examples/tcpdf_include.php');
require_once('libraries/action-scheduler/action-scheduler.php');
/*function tcac_email_stuff() {
//for($i = 0; $i < 2; $i++) {
if(count(as_get_scheduled_actions(array('hook' => 'tcac_mail_cert'))) == 0) {
as_schedule_recurring_action(time(), 10, 'tcac_mail_cert', array('i' => 23));
}
//as_unschedule_action('tcac_mail_cert');
//}
} */
//add_action('init', 'tcac_email_stuff');
//add_action('tcac_mail_cert_', array('TC_Attendance_Certificates', 'email_certs'));
/*function tcac_email_stuff() {
global $tcac;
if ( false === as_next_scheduled_action( 'tcac_mail_cert_' ) ) {
as_schedule_single_action(time(), 'tcac_mail_cert_');
//add_action('tcac_mail_cert_', array('TC_Attendance_Certificates', 'email_certs'));
}
} */
class TC_Attendance_Certificates {
public $current_cert_id;
public $auto_generate_pdfs = true;
public function __construct() {
add_action('tc_check_in_notification', array(&$this, 'set_current_cert_id'));
if($this->auto_generate_pdfs) {
add_action('tc_after_checkin_array_update', array(&$this, 'tcac_auto_generate_pdf'));
}
//$timestamp = strtotime(get_post_meta(26190, 'event_end_date_time', true));
}
public static function email_certs($event_id = 32203) {
//as_schedule_single_action(time() + 10, 'tcac_email_cert');
$args = array(
//'numberposts' => 1,
'post_type' => 'tc_tickets_instances',
'meta_query' => array(
array(
'key' => 'event_id',
'value' => $event_id,
//'compare' => '=',
)
),
);
$tickets = get_posts($args);
$event_name = get_the_title($event_id);
$email_message = get_post_meta($event_id, 'email_message', true);
$emails = array();
foreach($tickets as $ticket) {
$email = get_post_meta($ticket->ID, 'owner_email', true);
$first_name = get_post_meta($ticket->ID, 'first_name', true);
$last_name = get_post_meta($ticket->ID, 'last_name', true);
$emails[] = array(
'ticket' => $ticket,
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
);
}
foreach($emails as $email) {
$placeholders = array('$$event_name', '$$name');
$values = array($event_name, $email['first_name'] . ' ' . $email['last_name']);
$email_message_with_vals = str_replace($placeholders, $values, $email_message);
TC_Attendance_Certificates::tcac_generate_general_pdf($email['ticket']->ID, 'F');
TC_Attendance_Certificates::tcac_generate_sessions_pdf($email['ticket']->ID, 'F');
$tcac_file_name = 'attendence-certificate-' . $email['first_name'] . '-' . $email['last_name'] . '-' . $event_name;
$tcac_sess_filepath = 'attendence-certificate-' . $email['first_name'] . '-' . $email['last_name'] . '-' . $event_name . '-sessions';
$tcac_cert_filepath = ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $tcac_file_name . '.pdf';
$tcac_sess_cert_filepath = ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $tcac_sess_filepath . '.pdf';
//add_action('tcac_email_cert'/* . $event_name*/, function() /*use($email, $event_name, $email_message_with_vals, $tcac_cert_filepath, $tcac_sess_cert_filepath) */ {
//for($i = 0; $i < 400; $i++){
wp_mail($email['email'], 'Attendence Certificate for ' . $event_name, $email_message_with_vals, '', array($tcac_cert_filepath, $tcac_sess_cert_filepath));
//wp_mail('val@robidev.com', 'Test', 'This is a test!');
//});
//}
}
//add_action( 'tcac_email_cert', array(&$tcac, 'tcac_test'));
}
public function set_current_cert_id($ticket_id) {
$this->current_cert_id = $ticket_id;
}
public function get_current_cert_id() {
return $this->current_cert_id;
}
public static function tcac_generate_pdf($ticket_id) {
ob_start();
$latest_checkin_meta = end(get_post_meta($ticket_id, 'tc_checkins', true));
$checkin_date = $latest_checkin_meta['date_checked'];
$checkin_status = $latest_checkin_meta['status'];
$checkin_api_key_id = $latest_checkin_meta['api_key_id'];
if($checkin_status != 'Pass') {
return;
}
$pdf_url = $ticket_id . '-' . $checkin_api_key_id . '-' . $checkin_date . '.pdf';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
<h1>Congrats, you went to ' . $checkin_api_key_id . ' </h1>
</body>
</html>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
ob_end_clean();
$pdf->Output(ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $pdf_url, 'D');
}
public function tcac_auto_generate_pdf() {
ob_start();
$ticket_id = $this->current_cert_id;
$latest_checkin_meta = end(get_post_meta($ticket_id, 'tc_checkins', true));
$checkin_date = $latest_checkin_meta['date_checked'];
$checkin_status = $latest_checkin_meta['status'];
$checkin_api_key_id = $latest_checkin_meta['api_key_id'];
if($checkin_status != 'Pass') {
return;
}
$pdf_url = $ticket_id . '-' . $checkin_api_key_id . '-' . $checkin_date . '.pdf';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
<h1>Congrats, you went to ' . $checkin_api_key_id . ' </h1>
</body>
</html>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
ob_end_clean();
$pdf->Output(ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $pdf_url, 'F');
}
public static function tcac_generate_general_pdf($ticket_id, $output = 'I') {
ob_start();
$checkins = get_post_meta($ticket_id, 'tc_checkins', true);
$event_id = get_post_meta($ticket_id, 'event_id', true);
$event_name = get_the_title($event_id);
$event_pdf_temp = get_post_meta($event_id, 'pdf_template', true);
$event_end_date = get_post_meta($event_id, 'event_end_date_time', true);
$attendee_first_name = get_post_meta($ticket_id, 'first_name', true);
$attendee_last_name = get_post_meta($ticket_id, 'last_name', true);
$attendee_name = $attendee_first_name . ' ' . $attendee_last_name;
$attendee_first_name = get_post_meta($ticket_id, 'first_name', true);
$attendee_last_name = get_post_meta($ticket_id, 'last_name', true);
$credit_hour_total = 0;
foreach($checkins as $checkin) {
$api_key_id = $checkin['api_key_id'];
$api_credit_hours = get_post_meta($api_key_id, 'api_credit_hours', true);
$credit_hour_total = $credit_hour_total + $api_credit_hours;
}
$placeholders = array('$$event_name', '$$name', '$$date',);
$values = array($api_key_name, $attendee_name, $event_end_date);
$cert_html = str_replace($placeholders, $values, get_post_field('post_content', $event_pdf_temp));
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->SetFont('helvetica', '', 9);
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
' . $cert_html . '
</body>
</html>';
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->lastPage();
ob_end_clean();
$tcac_file_name = 'attendence-certificate-' . $attendee_first_name . '-' . $attendee_last_name . '-' . $event_name;
$pdf->Output(ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $tcac_file_name . '.pdf', $output);
}
public static function tcac_generate_sessions_pdf($ticket_id, $output = 'I') {
ob_start();
$checkins = self::parse_checkins(get_post_meta($ticket_id, 'tc_checkins', true));
$event_id = get_post_meta($ticket_id, 'event_id', true);
$event_name = get_the_title($event_id);
$event_pdf_temp = get_post_meta($event_id, 'pdf_template', true);
$event_end_date = get_post_meta($event_id, 'event_end_date_time', true);
$attendee_first_name = get_post_meta($ticket_id, 'first_name', true);
$attendee_last_name = get_post_meta($ticket_id, 'last_name', true);
$attendee_name = $attendee_first_name . ' ' . $attendee_last_name;
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
require_once(dirname(__FILE__).'/lang/eng.php');
$pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 9);
foreach($checkins as $api_key_id) {
//$api_key_id = $checkin['api_key_id'];
$api_credit_hours = get_post_meta($api_key_id, 'api_credit_hours', true);
$api_key_name = get_post_meta($api_key_id, 'api_key_name', true);
$api_key_pdf_temp = get_post_meta($api_key_id, 'pdf_template', true);
$api_key_aia_num = get_post_meta($api_key_id, 'aia_course_num', true);
$placeholders = array('$$event_name', '$$credit_hour_total', '$$name', '$$date', '$$course_num');
$values = array($api_key_name, $api_credit_hours, $attendee_name, $event_end_date, $api_key_aia_num);
$cert_html = str_replace($placeholders, $values, get_post_field('post_content', $api_key_pdf_temp));
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
' . $cert_html . '
</body>
</html>';
$pdf->writeHTML($html, true, 0, true, 0);
}
$pdf->lastPage();
ob_end_clean();
$tcac_file_name = 'attendence-certificate-' . $attendee_first_name . '-' . $attendee_last_name . '-' . $event_name . '-sessions';
$pdf->Output(ABSPATH . 'wp-content/plugins/tickera-attendence-certificates/pdfs/' . $tcac_file_name . '.pdf', $output);
}
private function parse_checkins($checkins) {
$api_ids = array('');
foreach($checkins as $checkin) {
$api_ids[] = $checkin['api_key_id'];
}
return array_unique($api_ids);
}
}
class TCAC_Add_Email_Queue {
public function __construct() {
add_filter('events_metas', array(&$this, 'add_email_to_queue'), 10, 1);
//add_action('tcac_email_cert_' /* . $post->ID, */, array('TC_Attendance_Certificates', 'email_certs'));
}
public $tcac_value = '';
public function add_email_to_queue($metas) {
global $post;
global $tcac;
//$event_id = (int) $_POST['post_id'];
//$utc_to_mst = 25200;
foreach($metas as $key => $value) {
if ($key == 'event_end_date_time' and empty(as_get_scheduled_actions(array('hook' => 'tcac_email_cert_' . $post->ID))) ) {
//$this->tcac_value = $value;
//add_action('init', array(&$this, 'schedule_as_event'));
//$this->tcac_value = $value;
//$val = 'Yep';
//if (!class_exists('TC_Attendance_Certificates')) {
//$val = 'nope';
//}
as_schedule_single_action($value, 'tcac_email_cert_' . $post->ID , array('event_id' => $post->ID));
add_action('tcac_email_cert_' . $post->ID, array('TC_Attendance_Certificates', 'email_certs'));
}
}
return $metas;
}
}
new TCAC_Add_Email_Queue();
// some TMS custom stuff
add_filter('tc_api_keys_fields', 'add_credit_hours_field');
function add_credit_hours_field($default_fields) {
$default_fields[] = array(
'field_name' => 'offers_ce_credt',
'field_title' => __('Offers Continuing Education Credit', 'tc'),
'field_type' => 'function',
'function' => 'tc_true_false_dropdown',
'field_description' => '',
'table_visibility' => true,
'post_field_type' => 'post_meta',
);
$default_fields[] = array(
'field_name' => 'api_credit_hours',
'field_title' => __('API / Session Credit Hours', 'tc'),
'field_type' => 'text',
'field_description' => __('The amount of credit hours awarded for attendance to this session', 'tc'),
'table_visibility' => true,
'post_field_type' => 'post_meta',
);
$default_fields[] = array(
'field_name' => 'aia_course_num',
'field_title' => __('AIA Course Number', 'tc'),
'field_type' => 'text',
'field_description' => __('This number is the sessions AIA course number', 'tc'),
'table_visibility' => true,
'post_field_type' => 'post_meta',
);
return $default_fields;
}
function tc_true_false_dropdown($field_name = '', $post_id = '') {
if ($post_id !== '') {
$currently_selected = get_post_meta($post_id, $field_name, true);
} else {
$currently_selected = '';
}
?>
<select name="offers_ce_credt_post_meta">
<option value="yes" <?php selected($currently_selected, 'yes', true); ?>>Yes</option>
<option value="no" <?php selected($currently_selected, 'no', true); ?>>No</option>
</select> <?php
}
add_filter('tc_event_fields', 'add_pdf_temp_field');
add_filter('tc_api_keys_fields', 'add_pdf_temp_field');
function add_pdf_temp_field($default_fields) {
$default_fields[] = array(
'field_name' => 'pdf_template',
'field_title' => __('PDF Template', 'tc'),
'field_type' => 'function',
'function' => 'tc_choose_pdf_template',
'field_description' => '',
'table_visibility' => true,
'post_field_type' => 'post_meta',
);
$default_fields[] = array(
'field_name' => 'email_message',
'field_title' => __('CE PDF Email Message', 'tc'),
'field_type' => 'textarea_editor',
'field_description' => 'Enter the body text of the email that will have PDF Certificates attached. <br>Variables you can use: $$name and $$event_name',
'table_visibility' => false,
'post_field_type' => 'post_meta',
);
return $default_fields;
}
function tc_choose_pdf_template($field_name = '', $post_id = ''){
if ($post_id !== '') {
$currently_selected = get_post_meta($post_id, $field_name, true);
} else {
$currently_selected = '';
}
$args = array(
'numberposts' => 100,
'category' => 2803
);
$options = get_posts($args);
?>
<select name="pdf_template_post_meta">
<option value="none">None</option>
<?php foreach($options as $opt) { ?>
<option value="<?php echo (int) $opt->ID; ?>" <?php selected($currently_selected, $opt->ID, true); ?>><?php echo get_post_field('post_title', $opt); ?></option>
<?php }?>
</select>
<?php
}
add_action("wp_ajax_tcac_general_pdf", "general_pdf_gen");
add_action("wp_ajax_nopriv_tcac_general_pdf", "my_must_login");
function general_pdf_gen() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "tcac_general_pdf_nonce")) {
exit("No naughty business please");
}
TC_Attendance_Certificates::tcac_generate_general_pdf($_REQUEST['ticket_id']);
}
add_action("wp_ajax_tcac_sessions_pdf", "sessions_pdf_gen");
add_action("wp_ajax_nopriv_tcac_sessions_pdf", "my_must_login");
function sessions_pdf_gen() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "tcac_sessions_pdf_nonce")) {
exit("No naughty business please");
}
TC_Attendance_Certificates::tcac_generate_sessions_pdf($_REQUEST['ticket_id']);
}
add_action("wp_ajax_tcac_last_year_pdf", "last_year_gen");
add_action("wp_ajax_nopriv_tcac_last_year_pdf", "my_must_login");
function last_year_gen() {
if ( !wp_verify_nonce( $_REQUEST['nonce'], "tcac_last_year_pdf_nonce")) {
exit("No naughty business please");
}
$user_id = $_REQUEST['user_id'];
$customer_orders = get_posts( apply_filters( 'tms_woocommerce_my_account_my_orders_query', array(
'meta_query' => array (
array('key' => '_customer_user', 'value' => $user_id,
array('key' => '_tc_paid_date', 'compare' => 'EXISTS'),
'relation' => 'AND'
),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() ),
) ) ));
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$item_count = $order->get_item_count();
$order_tickets_ids = TC_Orders::get_tickets_ids($order->get_id());
foreach ($order_tickets_ids as $ticket_id) {
$event_id = get_post_meta($ticket_id, 'event_id', true);
if(get_post_meta($event_id, 'offers_ce_credt', true) == 'yes') {
TC_Attendance_Certificates::tcac_generate_sessions_pdf($ticket_id);
}
}
}
}
function my_must_login() {
echo "You must log in to vote";
die();
}
function add_as_action_to_event_completion($ID, $post) {
//if ( ( $new == 'publish' ) && ( $old != 'publish' ) && ( $post->post_type == 'tc_events' ) ) {
//$event = new TC_Event($post->ID);
$event_end_time = strtotime(get_post_meta($ID, 'event_end_date_time', true));
as_schedule_single_action($event_end_time, 'tcac_email_cert_' . get_the_title($post), array('event_id' => $post->ID, 'time' => get_post_meta($ID, 'event_end_date_time', false)));
//add_action('tcac_init_cert_' . get_the_title($post), array(&$tcac, 'email_certs'));
}
//add_action('publish_tc_events', 'add_as_action_to_event_completion', 100, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment