-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper-functions.php
111 lines (104 loc) · 3.4 KB
/
helper-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
*
*/
/**
* Helper functions for the plugin
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
/**
* Send a single email (no trigger required)
*
* @param WP_Post $email_post Email post object
* @param WP_User $user User object
* @param array $placeholders Any placeholders that need to be replaced
*/
function pce_send_email($email_post, $user, $placeholders = array())
{
$to = $user->user_email;
$subject = pce_process_placeholders(get_the_title($email_post), $user, $placeholders);
$body = pce_process_placeholders(get_the_content(null, false, $email_post), $user, $placeholders);
$headers = array('Content-Type: text/html; charset=UTF-8');
return wp_mail($to, $subject, $body, $headers);
}
/**
* Send emails for certain trigger
*
* @param int/string $trigger Trigger (tag) ID/slug
* @param int/WP_User $user User ID or User Object
* @param array $placeholders Any placeholders that need to be replaced
*/
function pce_send_emails($trigger = '', $user = 0, $placeholders = array())
{
if (!term_exists($trigger, 'trigger')) {
return new WP_Error('pie-custom-emails', 'No trigger found with ID/slug: ' . $trigger);
}
if (!is_a($user, 'WP_User')) {
$user = get_user_by('id', $user);
}
if (!$user) {
return new WP_Error('pie-custom-emails', 'No user found');
}
// Get an array of all emails with set trigger
$emails = pce_get_emails($trigger);
if (empty($emails)) {
return new WP_Error('pie-custom-emails', 'No emails found for trigger: ' . $trigger);
}
// loop through emails (post objects) and send
foreach ($emails as $email_post) {
pce_send_email($email_post, $user, $placeholders);
}
}
/**
* Retrieve email posts by trigger (tag)
*
* @param int/string $trigger Trigger (tag) ID/slug
*/
function pce_get_emails($trigger = '')
{
$field = is_int($trigger) ? 'term_id' : 'slug';
$args = array(
'posts_per_page' => -1,
'post_type' => 'pie_email',
'tax_query' => array(
array(
'taxonomy' => 'trigger',
'field' => $field,
'terms' => $trigger,
)
)
);
return get_posts($args);
}
/**
* Parse content to replace placeholders with User info
*
* @param string $content Email content
* @param WP_User $user User object
* @param array $placeholders Any placeholders that need to be replaced
*/
function pce_process_placeholders($content, $user, $placeholders = array())
{
$first_name = get_user_meta($user->ID, 'first_name', true) ? get_user_meta($user->ID, 'first_name', true) : '{{user.first_name}}';
$last_name = get_user_meta($user->ID, 'last_name', true) ? get_user_meta($user->ID, 'last_name', true) : '{{user.last_name}}';
$defaults = array(
'{{user.first_name}}' => $first_name,
'{{user.last_name}}' => $last_name,
'{{user.email}}' => $user->user_email,
);
$to_replace = wp_parse_args($placeholders, $defaults);
return str_replace(array_keys($to_replace), array_values($to_replace), $content);
}
/**
* Filter ACF date in format(Ymd) to F j, Y
*/
function pce_filter_acf_date($date)
{
if ($new_date = DateTime::createFromFormat('Ymd', $date)) {
return $new_date;
}
return $date;
}
add_filter('pce_filter_acf_date', 'pce_filter_acf_date');