-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.php
111 lines (86 loc) · 3.54 KB
/
function.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
// A function to be called for $message argument in wp_mail(), it returns events in question
function alertsEmailBody() {
$tomorrow = date("Ymd", strtotime('tomorrow'));
$body .= 'The following events start tomorrow, ' . date("d.m.Y.", strtotime('tomorrow')) . '<br/>';
// ACF resources: To successfully query sub field values, we need to remember that the row number is not known (there may be 1,2 or even 3 rows of repeater field data). Therefore, we need to use a LIKE clause in our SQL query to allow for a WILDCARD in the meta_key search. To do this, we create a custom filter to replace the standard ‘=’ with ‘LIKE’. (https://www.advancedcustomfields.com/resources/query-posts-custom-fields/)
// WP codex: posts_where filter applies to the posts where clause and allows you to restrict which posts will show up in various areas of the site
function alertsQueryWildcard($where) {
$where = str_replace("meta_key = 'dates_$", "meta_key LIKE 'dates_%", $where);
return $where;
}
add_filter('posts_where', 'alertsQueryWildcard');
// And now querying posts whose events start tomorrow...
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array (
'key' => 'dates_$_date_start',
'compare' => '=',
'value' => $tomorrow,
)
)
);
$the_query = new WP_Query($args);
// Displaying events, linked and one per line
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
$body .= '<a href="'. get_permalink() .'" target="_blank" style="color:#3295ae;text-decoration:none;">' . get_the_title() . '</a><br>';
}
}
wp_reset_query();
}
// Scheduling and running sendingAlerts() on a daily basis
if (!wp_next_scheduled('schedulingAlerts')) {
wp_schedule_event(strtotime('00:00:00'), 'daily', 'schedulingAlerts');
}
add_action('schedulingAlerts', 'sendingAlerts');
// Checking if there are tomorrow's events and sending an alert
function sendingAlerts() {
// This is for HTML in $message
add_filter ("wp_mail_content_type", "alertsContentType");
function alertsContentType() {
return "text/html";
}
// Setting wp_mail() args
$to = '[email protected]';
$subject = 'ICO events starting tomorrow';
$message = alertsEmailBody();
global $wpdb;
// Querying active subscribers and passing them as a string into a BCC header
$recipients = $wpdb->get_results("SELECT DISTINCT wp_users.user_email AS email FROM wp_users INNER JOIN wp_usermeta ON wp_users.ID=wp_usermeta.user_id WHERE wp_usermeta.meta_value = 'Subscribed' ");
foreach($recipients as $recepient) {
$headers[] = 'Bcc: '.$recepient->email;
}
$headers[] = 'Content-Type: text/html; charset=UTF-8';
// Again, querying tomorrow's events
function alertsWildcard($where) {
$where = str_replace("meta_key = 'dates_$", "meta_key LIKE 'dates_%", $where);
return $where;
}
add_filter('posts_where', 'alertsWildcard');
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'dates_$_date_start',
'compare' => '=',
'value' => $tomorrow,
)
)
);
$the_query = new WP_Query($args);
// And actually sending the message (if there are any events queried)
if ($the_query->have_posts()) {
wp_mail($to, $subject, $message, $headers);
}
wp_reset_query();
}
?>