Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active March 27, 2024 18:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/4f4dddfdee99f310a4c35d4316083f6a to your computer and use it in GitHub Desktop.
Save xnau/4f4dddfdee99f310a4c35d4316083f6a to your computer and use it in GitHub Desktop.
Shows how to set up a Participants Database email that will automatically send when conditions are met
<?php
/**
* Plugin Name: PDB Email Cron
* Plugin URI: https://xnau.com/?p=7354
* Description: Provides a basic framework for setting up an automated Participants Database email send
* Version: 1.0
* Author: xnau webdesign
* Author URI: https://xnau.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/*
* to use this plugin, you must have the Participants Database Email Expansion Kit
* plugin installed and you must define an email template to use. The ID of that
* template must be typed in where the value of the $template_id property is set.
*/
class PDB_Email_Cron {
/**
* @var string name of the cron event
*/
const event = 'pdb_email_cron';
/**
* @var int ID of the Participants Database email template to use
*
* you must change the value here to the ID of the email template you want to use
*/
private $template_id = 9797;
/**
* @var string name of the option to use to keep the send logs
*/
const send_log = 'pdb_email_cron_send_log';
/**
* initializes the plugin
*/
public function __construct()
{
add_action( self::event, array($this, 'check_reminders') );
register_activation_hook( __FILE__, array(__CLASS__, 'activate') );
register_deactivation_hook( __FILE__, array(__CLASS__, 'deactivate') );
register_uninstall_hook( __FILE__, array(__CLASS__, 'uninstall') );
}
/**
* checks for records that need an email sent
*/
public function check_reminders()
{
// first, get a list of all accounts that have not checked in for the last 30 days
$late_accounts = $this->get_late_accounts();
// check each one to see if it needs to get an email sent
foreach ( $late_accounts as $record ) {
// check these against the send log so we don't keep sending notices
if ( $this->email_should_be_sent( $record['id'] ) ) {
$this->send_reminder( $record );
}
}
}
/**
* sends the email
*
* @param array $record array of record data for the template
*
*/
private function send_reminder( $record )
{
// we need an instance of the email template to send with the action
$template = new cpt_email_templates\template($this->template_id);
// this action triggers sending the email using the record data and the selected email template
// UNCOMMENT THE NEXT LINE WHEN YOU KNOW YOU ARE READY TO START SENDING EMAILS
//do_action( 'pdbcptet-action_send_email', $template, $record, 'PDB Email Cron' );
/*
* this is where you would put an error_log statement to see what email would be sent
* for example:
* error_log(__METHOD__.' sending email to record: ' . $record['id']);
*/
// log the send so we can prevent duplicate notices
$this->log_send( $record['id'] );
}
/**
* check if an email was sent to an account in the last 7 days
*
* @param int $record_id
* @return bool true if the email should be sent
*/
private function email_should_be_sent( $record_id )
{
$send_list = get_option( self::send_log );
if ( !$send_list ) {
$send_list = array();
}
if ( !array_key_exists( $record_id, $send_list ) ) {
// email to this account has never been sent
return true;
}
$last_send_date = $send_list[$record_id];
if ( $last_send_date < strtotime( '-7 days' ) ) {
// it's been over a week, send it again
return true;
}
// an email was sent to this account in the last week; don't send
return false;
}
/**
* logs the send
*
* @param int $record_id
*/
private function log_send( $record_id )
{
$send_list = get_option( self::send_log );
if ( ! $send_list ) {
$send_list = [];
}
$send_list[$record_id] = time(); // log the timestamp of the sent email
update_option( self::send_log, $send_list );
}
/**
* queries the db for late accounts
*
* @global wpdb $wpdb
*/
private function get_late_accounts()
{
global $wpdb;
// looking for records that have a last_accessed date of over 30 days ago
$late_date = date( 'Y-m-d G:i:s', strtotime( '-30 days' ) ); // 30 days ago
$sql = 'SELECT * FROM ' . Participants_Db::$participants_table . ' WHERE last_accessed < "' . $late_date . '" ';
$result = $wpdb->get_results( $sql, ARRAY_A );
return $result;
}
/**
* initializes the cron
*/
public static function activate()
{
wp_schedule_event( time(), 'daily', self::event );
}
/**
* clears the cron
*/
public static function deactivate()
{
wp_clear_scheduled_hook( self::event );
}
/**
* removes the plugin
*/
public static function uninstall()
{
delete_option( self::send_log );
}
}
new PDB_Email_Cron;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment