Notifications.php
7.76 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
namespace Tz\WordPress\Tools\Notifications;
use Tz\Common;
use Tz\WordPress\Tools;
use WP_User;
use Exception, StdClass;
const OPTION_NAME = "notif_options";
call_user_func(function() {
Tools\add_actions(__NAMESPACE__ . '\Actions');
Vars::$options = new Tools\WP_Option(OPTION_NAME);
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Admin.php');
}
});
function get_notification_by_trigger($trigger) {
$args = array(
'post_type' => 'notifications'
, 'numberposts' => -1
, 'orderby' => 'modified'
, 'order' => 'desc'
);
$my_notif = false;
foreach(get_posts($args) as $entry) {
$details = get_post_meta($entry->ID,'details',true);
if ($details['type']=="triggered" && $details['trigger'] == $trigger) {
$my_notif = $entry;
break;
}
}
return $my_notif;
}
/**
* GET NUMBER OF NEW NOTICES
@returns (Int)
*/
function get_num_notices() {
// get number of notices for this user that are new...
$notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
$notifications = array();
// return $num;
if(empty($notices)) {
return 0;
} else {
foreach($notices as $notice) {
if ($notice['status']=="show") {
$notifications[] = $notice;
}
}
}
return count($notifications);
}
function remove_notice($notification_id = -1) {
$notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
if (!empty($notices)) {
foreach($notices as $id => $notice) {
if ($id==$notification_id) {
$notices[$id]['status']="hide";
break;
}
}
}
update_user_meta(Tools\getCurrentUser()->ID,'notices',$notices);
}
/**
Send Notifications
@trigger = notification unique slug name
*/
function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),$send_override = false) {
$notification = get_notification_by_trigger($trigger);
if($notification) {
// get the user and user details....
$user = new WP_User($uid);//Tools\getCurrentUser();
$notify_me = get_user_meta($user->ID,'notify_me',true);
$notify_format = get_user_meta($user->ID,'notify_format',true);
$email_preference = get_user_meta($user->ID,'email_address_preference',true);
$cell = get_user_meta($user->ID,'home_mobile',true);
// get the notification and notificatio details....
$nid = $notification->ID;
$details = get_post_meta($nid,'details',true);
$email = get_post_meta($nid,'email',true);
$system = get_post_meta($nid,'system',true);
$sms = get_post_meta($nid,'sms',true);
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : "active";
$notification->type = $details['type'];
$notification->sendto = $details['sendto'];
$notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false;
$notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false;
$notification->is_sms = (isset($sms['message']) && $sms['message'] != "") ? true : false;
// if is_sms =============================================
if ($notification->is_sms && $notify_me && !empty($cell)) {
}
// if is_system ==========================================
if ($notification->is_system) {
$notices = get_user_meta($user->ID,'notices',true);
$insert = array(
'notification_id' => $nid
, 'status' => 'show'
, 'sent_date' => time()
, 'args' => $args
);
if(empty($notices)) {
$notices = array();
}
$notices[] = $insert;
update_user_meta($user->ID,'notices',$notices);
}
// if is_email ===========================================
if ($notification->is_email && ($notify_me || $send_override)) {
send_email($user->ID,$email,$args,$send_override);
}
} else {
die('no notification');
}
// if the system notification has set current user than get current user otherwise loop through the users needed.
}
function send_email($uid, $contents,$args) {
$from_address = get_bloginfo('admin_email');
$user = new WP_User($uid);
$to_email = $user->user_email;
$subject = $contents['subject'];
$html = $contents['html'];
$alttext = $contents['text'];
$headers = "";
if (empty($html)) {
$message = $alttext;
$message = strip_tags($message);
// it's text based only... no need to check format preference.
} else {
$message = $html;
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// it's HTML formatted.
// Check user preference if they want Text ONly??
}
foreach($args as $key => $val) {
if ( (filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$message = str_replace("{".$key."}","<a href='".$val."'>".$val."</a>",$message);
} else {
$message = str_replace("{".$key."}",$val,$message);
}
}
// Additional headers
//$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n";
$headers .= 'From: CICBV <'.$from_address.'>' . "\r\n";
mail($to_email, $subject, $message, $headers);
}
function getGroups($grpID=0) {
global $userAccessManager;
$groups = array();
if($grpID > 0) {
$userGroups = $userAccessManager->getAccessHandler()->getUserGroups($grpID);
return $userGroups->getGroupName();
} else {
$userGroups = $userAccessManager->getAccessHandler()->getUserGroups();
}
foreach($userGroups as $group) {
$groups[$group->getId()] = $group->getGroupName();
}
return $groups;
}
function get_field_lookup($var) {
return isset(Vars::$field_lookup[$var]) ? Vars::$field_lookup[$var] : $var;
}
function current_url() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
class Actions {
public static function wp_ajax_update_notification_count() {
$count = get_num_notices();
$return = array('count'=>$count);
die(json_encode($return));
}
}
class Vars {
public static $options;
public static $field_lookup = array(
'current_user' => "Current Logged-in User"
, 'allusers' => "All Users"
);
}
?>