Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active September 7, 2023 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xnau/f23785e84e6d6797c744caff8d853176 to your computer and use it in GitHub Desktop.
Save xnau/f23785e84e6d6797c744caff8d853176 to your computer and use it in GitHub Desktop.
Shows how to add an additional conditional to a Participants Database email template
<?php
/**
* Plugin Name: PDb Email Conditionals
* Description: shows how to add additional conditional checks to an email template
*/
class PDb_Email_Template_Conditionals {
/**
* @var int template ID
*/
const template_id = 1000; // set this to the ID of the email template you want this to apply to
/**
* @var array the record data
*/
private $record_data;
/**
* sets up the conditional filter
*/
public function __construct()
{
add_filter( 'pdbcptet-record_data', [$this, 'get_record_data'], 50 );
add_filter( 'pdbcptet-conditionals_for_template_' . self::template_id, [$this, 'add_conditional'] );
}
/**
* grabs the record data for later use
*
* @param array $record_data
* @return array record data
*/
public function get_record_data( $record_data )
{
$this->record_data = $record_data;
return $record_data;
}
/**
* adds a conditional to the conditionals for the email template
*
* each conditional in the list is a configuration array with these elements:
* "data_value" => record value to use in the comparison
* "operator" => the operator used in the comparison
* "comparison_value" => the fixed value to compare the record against
* "logic" => this must be "OR" or "AND" controls how successive comparisons will be applied
*
* @param array $conditional_list
* @return array conditional list
*/
public function add_conditional( $conditional_list )
{
$blog_post_conditional = [
'data_value' => $this->record_data['subscription_preference'], // set this to the name of the value in the record you want to use for the comparison 'subscription_preference'
'operator' => '~', // this means try to find the data value in the comparison value
'comparison_value' => $this->record_data['post_categories'], // set this to whatever value you need to compare to
'logic' => 'AND',
];
// add the conditional to the list of conditionals
$conditional_list[] = $blog_post_conditional;
return $conditional_list;
}
}
new PDb_Email_Template_Conditionals();
@xnau
Copy link
Author

xnau commented Sep 7, 2023

This demonstrates how to add additional conditionals to a conditional send email template for Participants Database Email Expansion Kit.

It works with a single template. Set the variable on line 13 to the ID of the template you want to add the conditional to.

To set the record value to check, set the key of the record_data array to the name of the field in the record. In the example, that is 'subscription_preference' you would change that to the name of the field you want to check in your conditional.

Additionally, this is made to work with the "send on blog post publish" email trigger: the email will only go out if the value in the "subscription_preference" field matches the post category. So if the record has a subscription preference of "knitting" then the email will be sent to the email address in the record if the blog post is in the "knitting" category.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment