a1b952c1 by Marty

Fix line endings in 2 files

1 parent f11d1bad
1 <?php 1 <?php
2 namespace Tz\WordPress\Tools\Notifications\Settings; 2 namespace Tz\WordPress\Tools\Notifications\Settings;
3 //error_reporting(E_ALL^E_DEPRECATED); 3 //error_reporting(E_ALL^E_DEPRECATED);
4 4
5 use Tz\WordPress\Tools; 5 use Tz\WordPress\Tools;
6 use Tz\Common; 6 use Tz\Common;
7 use Tz\WordPress\Tools\Notifications; 7 use Tz\WordPress\Tools\Notifications;
8 use Tz\WordPress\Tools\Notifications\Validation; 8 use Tz\WordPress\Tools\Notifications\Validation;
9 9
10 const CAPABILITY = "manage_notifications"; 10 const CAPABILITY = "manage_notifications";
11 const MANAGE_SYSTEM_NOTIFICATIONS = "create_system_notifications"; 11 const MANAGE_SYSTEM_NOTIFICATIONS = "create_system_notifications";
12 const SETTING_NS = 'canspam_settings'; 12 const SETTING_NS = 'canspam_settings';
13 const ADMIN_PAGE = "canspam_admin"; 13 const ADMIN_PAGE = "canspam_admin";
14 14
15 call_user_func(function() { 15 call_user_func(function() {
16 $role = get_role('administrator'); 16 $role = get_role('administrator');
17 $role->add_cap(CAPABILITY); 17 $role->add_cap(CAPABILITY);
18 $role->add_cap(MANAGE_SYSTEM_NOTIFICATIONS); 18 $role->add_cap(MANAGE_SYSTEM_NOTIFICATIONS);
19 //$role->remove_cap(SUB_ADMIN_CAPABILITY); 19 //$role->remove_cap(SUB_ADMIN_CAPABILITY);
20 20
21 Vars::$settings = new Tools\WP_Option(SETTING_NS); 21 Vars::$settings = new Tools\WP_Option(SETTING_NS);
22 22
23 Tools\add_actions(__NAMESPACE__ . '\Actions'); 23 Tools\add_actions(__NAMESPACE__ . '\Actions');
24 24
25 25
26 26
27 }); 27 });
28 28
29 function display_page() { 29 function display_page() {
30 30
31 31
32 if (isset($_GET['action']) && $_GET['action']=="edit") { 32 if (isset($_GET['action']) && $_GET['action']=="edit") {
33 global $wpdb; 33 global $wpdb;
34 34
35 $entry = get_post($_GET['page_id']); 35 $entry = get_post($_GET['page_id']);
36 36
37 $id = $entry->ID; 37 $id = $entry->ID;
38 38
39 $details = get_post_meta($id,'details',true); 39 $details = get_post_meta($id,'details',true);
40 $email = get_post_meta($id,'email',true); 40 $email = get_post_meta($id,'email',true);
41 $system = get_post_meta($id,'system',true); 41 $system = get_post_meta($id,'system',true);
42 42
43 $entry->details = $details; 43 $entry->details = $details;
44 $entry->email = $email; 44 $entry->email = $email;
45 $entry->system = $system; 45 $entry->system = $system;
46 46
47 // here 47 // here
48 $validation = new Notifications\Validation; 48 $validation = new Notifications\Validation;
49 49
50 $validation->set_rules('type','Notification Type','required'); 50 $validation->set_rules('type','Notification Type','required');
51 $validation->set_rules('title','Description','trim|required|min_length[4]'); 51 $validation->set_rules('title','Description','trim|required|min_length[4]');
52 $validation->set_rules('type','Notification Type','required'); 52 $validation->set_rules('type','Notification Type','required');
53 $validation->set_rules('sendto','Send To','required'); 53 $validation->set_rules('sendto','Send To','required');
54 54
55 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : ''; 55 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : '';
56 $validation->set_rules('execute_date','Execute Date', $type_val); 56 $validation->set_rules('execute_date','Execute Date', $type_val);
57 57
58 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : ''; 58 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : '';
59 $validation->set_rules('trigger','Trigger',$trigger_val); 59 $validation->set_rules('trigger','Trigger',$trigger_val);
60 60
61 $validation->set_rules('subject','Subject','trim'); 61 $validation->set_rules('subject','Subject','trim');
62 $validation->set_rules('text','Text Version','trim|min_length[16]'); 62 $validation->set_rules('text','Text Version','trim|min_length[16]');
63 $validation->set_rules('html','HTML Version','trim|min_length[16]'); 63 $validation->set_rules('html','HTML Version','trim|min_length[16]');
64 64
65 $validation->set_rules('system_message_type','System Message Type','trim'); 65 $validation->set_rules('system_message_type','System Message Type','trim');
66 $validation->set_rules('system','System Message','trim|min_length[16]'); 66 $validation->set_rules('system','System Message','trim|min_length[16]');
67 67
68 68
69 //details 69 //details
70 if ($validation->run() == TRUE) { 70 if ($validation->run() == TRUE) {
71 71
72 // Clean up data before saving 72 // Clean up data before saving
73 Tools\tzClean($_POST); 73 Tools\tzClean($_POST);
74 74
75 $type = $_POST['type']; 75 $type = $_POST['type'];
76 $title = $_POST['title']; 76 $title = $_POST['title'];
77 $sendto = $_POST['sendto']; 77 $sendto = $_POST['sendto'];
78 $execute_date = ($type=="scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00"; 78 $execute_date = ($type=="scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00";
79 $trigger = ($type=="scheduled") ? "scheduled-cron-job" : $_POST['trigger']; 79 $trigger = ($type=="scheduled") ? "scheduled-cron-job" : $_POST['trigger'];
80 80
81 // email 81 // email
82 $subject = $_POST['subject']; 82 $subject = $_POST['subject'];
83 $text = $_POST['text']; 83 $text = $_POST['text'];
84 $html = $_POST['html']; 84 $html = $_POST['html'];
85 $attachments = array(); 85 $attachments = array();
86 $upload_dir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'notifications' . DIRECTORY_SEPARATOR; 86 $upload_dir = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'notifications' . DIRECTORY_SEPARATOR;
87 87
88 fixFilesArray($_FILES['attachment']); 88 fixFilesArray($_FILES['attachment']);
89 89
90 foreach ($_FILES['attachment'] as $position => $file) { 90 foreach ($_FILES['attachment'] as $position => $file) {
91 // should output array with indices name, type, tmp_name, error, size 91 // should output array with indices name, type, tmp_name, error, size
92 if($file['name'] != "") { 92 if($file['name'] != "") {
93 move_uploaded_file($file['tmp_name'], $upload_dir . $file['name']); 93 move_uploaded_file($file['tmp_name'], $upload_dir . $file['name']);
94 $attachments[] = $file['name']; 94 $attachments[] = $file['name'];
95 } 95 }
96 } 96 }
97 97
98 // system 98 // system
99 $system_message_type = $_POST['system_message_type']; 99 $system_message_type = $_POST['system_message_type'];
100 $system = $_POST['system']; 100 $system = $_POST['system'];
101 101
102 update_post_meta($id, 'notif_type', $type); 102 update_post_meta($id, 'notif_type', $type);
103 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date)); 103 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date));
104 update_post_meta($id, 'trigger', $trigger); 104 update_post_meta($id, 'trigger', $trigger);
105 105
106 update_post_meta($id, "details", array( 106 update_post_meta($id, "details", array(
107 'type' => $type 107 'type' => $type
108 , 'sendto' => $sendto 108 , 'sendto' => $sendto
109 , 'status' => $entry->details['status'] 109 , 'status' => $entry->details['status']
110 , 'trigger' => $trigger 110 , 'trigger' => $trigger
111 , 'execute_date' => $execute_date 111 , 'execute_date' => $execute_date
112 )); 112 ));
113 113
114 update_post_meta($id,'send_status',$entry->details['status']); 114 update_post_meta($id,'send_status',$entry->details['status']);
115 115
116 if ( count( $entry->email['attachments'] ) > 0 ) { 116 if ( count( $entry->email['attachments'] ) > 0 ) {
117 $attachments = array_merge($entry->email['attachments'], $attachments); 117 $attachments = array_merge($entry->email['attachments'], $attachments);
118 } 118 }
119 119
120 update_post_meta($id, "email", array( 120 update_post_meta($id, "email", array(
121 'subject' => $subject 121 'subject' => $subject
122 , 'text' => $text 122 , 'text' => $text
123 , 'html' => $html 123 , 'html' => $html
124 , 'attachments' => $attachments 124 , 'attachments' => $attachments
125 )); 125 ));
126 update_post_meta($id, "system", array( 126 update_post_meta($id, "system", array(
127 'system_message_type' => $system_message_type 127 'system_message_type' => $system_message_type
128 , 'message' => $system 128 , 'message' => $system
129 )); 129 ));
130 130
131 131
132 $update = array(); 132 $update = array();
133 $update['ID'] = $id; 133 $update['ID'] = $id;
134 $update['post_title'] = $title; 134 $update['post_title'] = $title;
135 wp_update_post($update); 135 wp_update_post($update);
136 136
137 $id = $entry->ID; 137 $id = $entry->ID;
138 138
139 $details = get_post_meta($id,'details',true); 139 $details = get_post_meta($id,'details',true);
140 $email = get_post_meta($id,'email',true); 140 $email = get_post_meta($id,'email',true);
141 $system = get_post_meta($id,'system',true); 141 $system = get_post_meta($id,'system',true);
142 142
143 143
144 $entry->details = $details; 144 $entry->details = $details;
145 $entry->email = $email; 145 $entry->email = $email;
146 $entry->system = $system; 146 $entry->system = $system;
147 147
148 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>"; 148 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
149 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); 149 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
150 } else { 150 } else {
151 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php'); 151 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'form.php');
152 } 152 }
153 } else { 153 } else {
154 154
155 if (isset($_GET['action']) && $_GET['action']=="delete") { 155 if (isset($_GET['action']) && $_GET['action']=="delete") {
156 wp_delete_post($_GET['page_id'],true); 156 wp_delete_post($_GET['page_id'],true);
157 157
158 } elseif (isset($_GET['action']) && $_GET['action']=="archive") { 158 } elseif (isset($_GET['action']) && $_GET['action']=="archive") {
159 159
160 $id = $_GET['page_id']; 160 $id = $_GET['page_id'];
161 161
162 $postdata = get_post_meta($id,'details',true); 162 $postdata = get_post_meta($id,'details',true);
163 $postdata['status'] = "archived"; 163 $postdata['status'] = "archived";
164 164
165 update_post_meta($id,'details',$postdata); 165 update_post_meta($id,'details',$postdata);
166 update_post_meta($id,'send_status',$postdata['status']); 166 update_post_meta($id,'send_status',$postdata['status']);
167 } 167 }
168 168
169 169
170 // get all the notifications that status != "archived"; 170 // get all the notifications that status != "archived";
171 $notifications = array(); 171 $notifications = array();
172 $notifications['triggered'] = array(); 172 $notifications['triggered'] = array();
173 $notifications['scheduled'] = array(); 173 $notifications['scheduled'] = array();
174 174
175 $args = array( 175 $args = array(
176 'post_type' => 'notifications' 176 'post_type' => 'notifications'
177 , 'numberposts' => -1 177 , 'numberposts' => -1
178 , 'orderby' => 'modified' 178 , 'orderby' => 'modified'
179 , 'order' => 'desc' 179 , 'order' => 'desc'
180 , 'meta_key' => 'send_status' 180 , 'meta_key' => 'send_status'
181 , 'meta_compare' => '=' 181 , 'meta_compare' => '='
182 , 'meta_value' => 'pending' 182 , 'meta_value' => 'pending'
183 ); 183 );
184 184
185 $entries = get_posts($args); 185 $entries = get_posts($args);
186 186
187 187
188 foreach($entries as $entry) { 188 foreach($entries as $entry) {
189 $id = $entry->ID; 189 $id = $entry->ID;
190 190
191 $details = get_post_meta($id,'details',true); 191 $details = get_post_meta($id,'details',true);
192 $email = get_post_meta($id,'email',true); 192 $email = get_post_meta($id,'email',true);
193 $system = get_post_meta($id,'system',true); 193 $system = get_post_meta($id,'system',true);
194 194
195 195
196 $entry->trigger = $details['trigger']; 196 $entry->trigger = $details['trigger'];
197 $entry->status = isset($details['status']) ? $details['status'] : "active"; 197 $entry->status = isset($details['status']) ? $details['status'] : "active";
198 $entry->type = $details['type']; 198 $entry->type = $details['type'];
199 $entry->sendto = $details['sendto']; 199 $entry->sendto = $details['sendto'];
200 200
201 $entry->is_email = (($email['text'] != "" || $email['html'] != "")) ? true : false; 201 $entry->is_email = (($email['text'] != "" || $email['html'] != "")) ? true : false;
202 $entry->is_system = (isset($system['message']) && $system['message'] != "") ? true : false; 202 $entry->is_system = (isset($system['message']) && $system['message'] != "") ? true : false;
203 203
204 204
205 $entry->execute_date = $details['execute_date']; 205 $entry->execute_date = $details['execute_date'];
206 206
207 if ($entry->status != "archived") { 207 if ($entry->status != "archived") {
208 $notifications[$entry->type][] = $entry; 208 $notifications[$entry->type][] = $entry;
209 } 209 }
210 210
211 211
212 } 212 }
213 213
214 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'admin.php'); 214 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'admin.php');
215 } 215 }
216 } 216 }
217 217
218 function mysqldatetime_to_timestamp($datetime = "") 218 function mysqldatetime_to_timestamp($datetime = "")
219 { 219 {
220 // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters) 220 // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
221 $l = strlen($datetime); 221 $l = strlen($datetime);
222 if(!($l == 10 || $l == 19)) 222 if(!($l == 10 || $l == 19))
223 return 0; 223 return 0;
224 224
225 // 225 //
226 $date = $datetime; 226 $date = $datetime;
227 $hours = 0; 227 $hours = 0;
228 $minutes = 0; 228 $minutes = 0;
229 $seconds = 0; 229 $seconds = 0;
230 230
231 // DATETIME only 231 // DATETIME only
232 if($l == 19) 232 if($l == 19)
233 { 233 {
234 list($date, $time) = explode(" ", $datetime); 234 list($date, $time) = explode(" ", $datetime);
235 list($hours, $minutes, $seconds) = explode(":", $time); 235 list($hours, $minutes, $seconds) = explode(":", $time);
236 } 236 }
237 237
238 list($year, $month, $day) = explode("-", $date); 238 list($year, $month, $day) = explode("-", $date);
239 239
240 return mktime($hours, $minutes, $seconds, $month, $day, $year); 240 return mktime($hours, $minutes, $seconds, $month, $day, $year);
241 } 241 }
242 242
243 function fixFilesArray(&$files) 243 function fixFilesArray(&$files)
244 { 244 {
245 $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1); 245 $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);
246 246
247 foreach ($files as $key => $part) { 247 foreach ($files as $key => $part) {
248 // only deal with valid keys and multiple files 248 // only deal with valid keys and multiple files
249 $key = (string) $key; 249 $key = (string) $key;
250 if (isset($names[$key]) && is_array($part)) { 250 if (isset($names[$key]) && is_array($part)) {
251 foreach ($part as $position => $value) { 251 foreach ($part as $position => $value) {
252 $files[$position][$key] = $value; 252 $files[$position][$key] = $value;
253 } 253 }
254 // remove old key reference 254 // remove old key reference
255 unset($files[$key]); 255 unset($files[$key]);
256 } 256 }
257 } 257 }
258 } 258 }
259 259
260 260
261 function notification_settings() { 261 function notification_settings() {
262 262
263 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php'); 263 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'settings.php');
264 264
265 265
266 266
267 } 267 }
268 268
269 function create_notification() { 269 function create_notification() {
270 270
271 global $wpdb; 271 global $wpdb;
272 272
273 273
274 // here 274 // here
275 $validation = new Notifications\Validation; 275 $validation = new Notifications\Validation;
276 276
277 $validation->set_rules('type','Notification Type','required'); 277 $validation->set_rules('type','Notification Type','required');
278 $validation->set_rules('title','Description','trim|required|min_length[16]'); 278 $validation->set_rules('title','Description','trim|required|min_length[16]');
279 $validation->set_rules('type','Notification Type','required'); 279 $validation->set_rules('type','Notification Type','required');
280 $validation->set_rules('sendto','Send To','required'); 280 $validation->set_rules('sendto','Send To','required');
281 281
282 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : ''; 282 $type_val = ($_POST && $_POST['type'] == "scheduled") ? 'required' : '';
283 $validation->set_rules('execute_date','Execute Date', $type_val); 283 $validation->set_rules('execute_date','Execute Date', $type_val);
284 284
285 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : ''; 285 $trigger_val = ($_POST && $_POST['type'] == "triggered") ? 'required' : '';
286 $validation->set_rules('trigger','Trigger',$trigger_val); 286 $validation->set_rules('trigger','Trigger',$trigger_val);
287 287
288 $validation->set_rules('subject','Subject','trim'); 288 $validation->set_rules('subject','Subject','trim');
289 $validation->set_rules('text','Text Version','trim|min_length[16]'); 289 $validation->set_rules('text','Text Version','trim|min_length[16]');
290 $validation->set_rules('html','HTML Version','trim|min_length[16]'); 290 $validation->set_rules('html','HTML Version','trim|min_length[16]');
291 291
292 $validation->set_rules('system','System Message','trim|min_length[16]'); 292 $validation->set_rules('system','System Message','trim|min_length[16]');
293 293
294 294
295 if ($_POST && ($_POST['subject']=="" && $_POST['system']=="")) { 295 if ($_POST && ($_POST['subject']=="" && $_POST['system']=="")) {
296 $form_error = true; 296 $form_error = true;
297 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php'); 297 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
298 } else { 298 } else {
299 if ($validation->run() == TRUE) { 299 if ($validation->run() == TRUE) {
300 300
301 // Clean up the data before saving 301 // Clean up the data before saving
302 Tools\tzClean($_POST); 302 Tools\tzClean($_POST);
303 303
304 // ok, so now we need to create the notification. 304 // ok, so now we need to create the notification.
305 class postTemplate { 305 class postTemplate {
306 var $post_title = ''; 306 var $post_title = '';
307 var $post_content = ''; 307 var $post_content = '';
308 var $post_status = 'publish'; 308 var $post_status = 'publish';
309 var $post_type = 'notifications'; 309 var $post_type = 'notifications';
310 var $comment_status = 'closed'; 310 var $comment_status = 'closed';
311 } 311 }
312 312
313 //details 313 //details
314 $type = $_POST['type']; 314 $type = $_POST['type'];
315 $title = $_POST['title']; 315 $title = $_POST['title'];
316 $sendto = $_POST['sendto']; 316 $sendto = $_POST['sendto'];
317 $execute_date = ($type=="scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00"; 317 $execute_date = ($type=="scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00";
318 $trigger = ($type=="scheduled") ? "scheduled-cron-job" : $_POST['trigger']; 318 $trigger = ($type=="scheduled") ? "scheduled-cron-job" : $_POST['trigger'];
319 319
320 // email 320 // email
321 $subject = $_POST['subject']; 321 $subject = $_POST['subject'];
322 $text = $_POST['text']; 322 $text = $_POST['text'];
323 $html = $_POST['html']; 323 $html = $_POST['html'];
324 $attachments = array(); 324 $attachments = array();
325 $upload_dir = __DIR__ . "/uploads/"; 325 $upload_dir = __DIR__ . "/uploads/";
326 fixFilesArray($_FILES['attachment']); 326 fixFilesArray($_FILES['attachment']);
327 foreach ($_FILES['attachment'] as $position => $file) { 327 foreach ($_FILES['attachment'] as $position => $file) {
328 // should output array with indices name, type, tmp_name, error, size 328 // should output array with indices name, type, tmp_name, error, size
329 if($file['name'] != "") { 329 if($file['name'] != "") {
330 move_uploaded_file($file['tmp_name'],$upload_dir . $file['name']); 330 move_uploaded_file($file['tmp_name'],$upload_dir . $file['name']);
331 $attachments[] = $file['name']; 331 $attachments[] = $file['name'];
332 } 332 }
333 } 333 }
334 334
335 // system 335 // system
336 $system_message_type = $_POST['system_message_type']; 336 $system_message_type = $_POST['system_message_type'];
337 $system = $_POST['system']; 337 $system = $_POST['system'];
338 338
339 339
340 340
341 341
342 // make post... 342 // make post...
343 343
344 $notification = new postTemplate(); 344 $notification = new postTemplate();
345 $notification->post_title = $title; 345 $notification->post_title = $title;
346 $notification->post_content = "Notification created ".date('Y-m-d H:i:s')." --- to be sent on $execute_date"; 346 $notification->post_content = "Notification created ".date('Y-m-d H:i:s')." --- to be sent on $execute_date";
347 $notification->post_date_gmt = date("Y-m-d H:i:s",time()); 347 $notification->post_date_gmt = date("Y-m-d H:i:s",time());
348 348
349 $id = wp_insert_post($notification); 349 $id = wp_insert_post($notification);
350 350
351 update_post_meta($id, 'notif_type', $type); 351 update_post_meta($id, 'notif_type', $type);
352 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date)); 352 update_post_meta($id, 'execute_date', mysqldatetime_to_timestamp($execute_date));
353 update_post_meta($id, 'trigger', $trigger); 353 update_post_meta($id, 'trigger', $trigger);
354 354
355 add_post_meta($id, "details", array( 355 add_post_meta($id, "details", array(
356 'type' => $type 356 'type' => $type
357 , 'sendto' => $sendto 357 , 'sendto' => $sendto
358 , 'status' => 'pending' 358 , 'status' => 'pending'
359 , 'trigger' => $trigger 359 , 'trigger' => $trigger
360 , 'execute_date' => $execute_date 360 , 'execute_date' => $execute_date
361 )); 361 ));
362 362
363 update_post_meta($id,'send_status','pending'); 363 update_post_meta($id,'send_status','pending');
364 364
365 add_post_meta($id, "email", array( 365 add_post_meta($id, "email", array(
366 'subject' => $subject 366 'subject' => $subject
367 , 'text' => $text 367 , 'text' => $text
368 , 'html' => $html 368 , 'html' => $html
369 , 'attachments' => $attachments 369 , 'attachments' => $attachments
370 )); 370 ));
371 update_post_meta($id, "system", array( 371 update_post_meta($id, "system", array(
372 'system_message_type' => $system_message_type 372 'system_message_type' => $system_message_type
373 , 'message' => $system 373 , 'message' => $system
374 )); 374 ));
375 375
376 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>"; 376 $flash = "<strong>Notification Saved Successfully!</strong><br /><a href='/wp-admin/admin.php?page=notifications'>Click here</a> to view all notifications.</a>";
377 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php'); 377 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
378 378
379 } else { 379 } else {
380 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php'); 380 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
381 } 381 }
382 } 382 }
383 383
384 384
385 } 385 }
386 386
387 class Actions { 387 class Actions {
388 388
389 public static function init() { 389 public static function init() {
390 global $wpdb; 390 global $wpdb;
391 $wpdb->show_errors(); 391 $wpdb->show_errors();
392 392
393 register_post_type( 'notifications', array( 393 register_post_type( 'notifications', array(
394 'label' => __('Notifs') 394 'label' => __('Notifs')
395 , 'public' => true 395 , 'public' => true
396 , 'show_ui' => false 396 , 'show_ui' => false
397 , 'hierarchical' => false 397 , 'hierarchical' => false
398 , 'exclude_from_search' => true 398 , 'exclude_from_search' => true
399 )); 399 ));
400 400
401 } 401 }
402 402
403 public static function admin_menu() { 403 public static function admin_menu() {
404 add_menu_page('Notifications','Notifications',CAPABILITY,'notifications',__NAMESPACE__ . '\display_page' ); 404 add_menu_page('Notifications','Notifications',CAPABILITY,'notifications',__NAMESPACE__ . '\display_page' );
405 add_submenu_page('notifications','New Notification', 'New Notification',CAPABILITY,'notifications-create-new',__NAMESPACE__ . '\create_notification'); 405 add_submenu_page('notifications','New Notification', 'New Notification',CAPABILITY,'notifications-create-new',__NAMESPACE__ . '\create_notification');
406 //add_options_page('CAN-SPAM Settings', 'CAN-SPAM Settings', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\notification_settings'); 406 //add_options_page('CAN-SPAM Settings', 'CAN-SPAM Settings', CAPABILITY, ADMIN_PAGE, __NAMESPACE__ . '\notification_settings');
407 //add_submenu_page('notifications','CAN-SPAM Settings', 'CAN-SPAM Settings',CAPABILITY,'notifications-settings',__NAMESPACE__ . '\notification_settings'); 407 //add_submenu_page('notifications','CAN-SPAM Settings', 'CAN-SPAM Settings',CAPABILITY,'notifications-settings',__NAMESPACE__ . '\notification_settings');
408 } 408 }
409 409
410 public function admin_init() { 410 public function admin_init() {
411 // register_setting(Notifications\OPTION_NAME, Notifications\OPTION_NAME); 411 // register_setting(Notifications\OPTION_NAME, Notifications\OPTION_NAME);
412 register_setting(SETTING_NS, SETTING_NS); 412 register_setting(SETTING_NS, SETTING_NS);
413 } 413 }
414 414
415 } 415 }
416 416
417 class Vars { 417 class Vars {
418 public static $settings; 418 public static $settings;
419 } 419 }
420 420
421 421
422 ?> 422 ?>
...\ No newline at end of file ...\ No newline at end of file
......
1 <?php 1 <?php
2 namespace Tz\WordPress\Tools\Notifications; 2 namespace Tz\WordPress\Tools\Notifications;
3 3
4 use Tz\Common; 4 use Tz\Common;
5 use Tz\WordPress\Tools; 5 use Tz\WordPress\Tools;
6 use Tz\WordPress\Tools\Sequencer; 6 use Tz\WordPress\Tools\Sequencer;
7 use Tz\WordPress\CBV; 7 use Tz\WordPress\CBV;
8 use Tz\WordPress\CBV\User; 8 use Tz\WordPress\CBV\User;
9 use WP_User; 9 use WP_User;
10 use Exception, StdClass; 10 use Exception, StdClass;
11 11
12 const OPTION_NAME = "notif_options"; 12 const OPTION_NAME = "notif_options";
13 13
14 call_user_func( 14 call_user_func(
15 function() 15 function()
16 { 16 {
17 Tools\add_actions(__NAMESPACE__.'\Actions'); 17 Tools\add_actions(__NAMESPACE__.'\Actions');
18 Vars::$options = new Tools\WP_Option(OPTION_NAME); 18 Vars::$options = new Tools\WP_Option(OPTION_NAME);
19 Vars::$notices = Array(); 19 Vars::$notices = Array();
20 if (is_admin()) { 20 if (is_admin()) {
21 require_once(__DIR__.DIRECTORY_SEPARATOR.'Validation.php'); 21 require_once(__DIR__.DIRECTORY_SEPARATOR.'Validation.php');
22 require_once(__DIR__.DIRECTORY_SEPARATOR.'Admin.php'); 22 require_once(__DIR__.DIRECTORY_SEPARATOR.'Admin.php');
23 } 23 }
24 }); 24 });
25 25
26 function subval_sort($a, $subkey, $sort) 26 function subval_sort($a, $subkey, $sort)
27 { 27 {
28 foreach ($a as $k=> $v) { 28 foreach ($a as $k=> $v) {
29 $b[$k] = strtolower($v[$subkey]); 29 $b[$k] = strtolower($v[$subkey]);
30 } 30 }
31 $sort($b); 31 $sort($b);
32 foreach ($b as $key=> $val) { 32 foreach ($b as $key=> $val) {
33 $c[] = $a[$key]; 33 $c[] = $a[$key];
34 } 34 }
35 return $c; 35 return $c;
36 } 36 }
37 37
38 function get_user_notices($uid) 38 function get_user_notices($uid)
39 { 39 {
40 $notices = get_user_meta($uid, 'notices', true); 40 $notices = get_user_meta($uid, 'notices', true);
41 if (!empty($notices)) { 41 if (!empty($notices)) {
42 $notices = subval_sort($notices, 'sent_date', 'arsort'); 42 $notices = subval_sort($notices, 'sent_date', 'arsort');
43 Vars::$notices = $notices; 43 Vars::$notices = $notices;
44 } 44 }
45 } 45 }
46 46
47 function print_user_notices($show_more = true) 47 function print_user_notices($show_more = true)
48 { 48 {
49 $user = new User\CurrentAccount(); 49 $user = new User\CurrentAccount();
50 50
51 get_user_notices($user->ID); 51 get_user_notices($user->ID);
52 52
53 if (CBV\on_page('/dashboard')) { 53 if (CBV\on_page('/dashboard')) {
54 $limit = 5; 54 $limit = 5;
55 } else { 55 } else {
56 $limit = 10; 56 $limit = 10;
57 } 57 }
58 58
59 $notices = Vars::$notices; 59 $notices = Vars::$notices;
60 $total_rows = count($notices); 60 $total_rows = count($notices);
61 $next_id = isset($_POST['last_id']) ? ($_POST['last_id'] + 1) : 0; 61 $next_id = isset($_POST['last_id']) ? ($_POST['last_id'] + 1) : 0;
62 $end = (($next_id + $limit) < $total_rows) ? ($next_id + $limit) : $total_rows; 62 $end = (($next_id + $limit) < $total_rows) ? ($next_id + $limit) : $total_rows;
63 63
64 if (count($notices) < 1) { 64 if (count($notices) < 1) {
65 //print '<tr><td colspan="3">You have no new notices.</td></tr>'; 65 //print '<tr><td colspan="3">You have no new notices.</td></tr>';
66 } 66 }
67 67
68 $rows = ""; 68 $rows = "";
69 69
70 $last_id = 0; 70 $last_id = 0;
71 for ($i = 0; $i < $total_rows; $i++) { 71 for ($i = 0; $i < $total_rows; $i++) {
72 72
73 $the_notice = get_post($notices[$i]['notification_id']); 73 $the_notice = get_post($notices[$i]['notification_id']);
74 $system = get_post_meta($notices[$i]['notification_id'], 'system', true); 74 $system = get_post_meta($notices[$i]['notification_id'], 'system', true);
75 $content = $system['message']; 75 $content = $system['message'];
76 $id = $the_notice->ID; 76 $id = $the_notice->ID;
77 77
78 if (isset($notices[$i]['args']) && count($notices[$i]['args']) > 0): 78 if (isset($notices[$i]['args']) && count($notices[$i]['args']) > 0):
79 foreach ($notices[$i]['args'] as $key => $val) { 79 foreach ($notices[$i]['args'] as $key => $val) {
80 if (filter_var($val, FILTER_VALIDATE_URL) !== false) { 80 if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
81 $content = str_replace("{".$key."}", "<a href='".$val."'>Click here</a>", $content); 81 $content = str_replace("{".$key."}", "<a href='".$val."'>Click here</a>", $content);
82 } else { 82 } else {
83 $content = str_replace("{".$key."}", $val, $content); 83 $content = str_replace("{".$key."}", $val, $content);
84 } 84 }
85 } 85 }
86 endif; 86 endif;
87 87
88 $rows .= '<tr class="notice-row" id="'.$i.'">'; 88 $rows .= '<tr class="notice-row" id="'.$i.'">';
89 if ($notices[$i]['status'] == "unread") { 89 if ($notices[$i]['status'] == "unread") {
90 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><a href="#" class="notice '.(($notices[$i]['status'] == "read") ? 'read' : 'unread').'"><img src="assets/images/blank.gif" width="12" height="12" /></a></td>'; 90 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><a href="#" class="notice '.(($notices[$i]['status'] == "read") ? 'read' : 'unread').'"><img src="assets/images/blank.gif" width="12" height="12" /></a></td>';
91 } else { 91 } else {
92 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><img src="assets/images/blank.gif" width="12" height="12" /></td>'; 92 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><img src="assets/images/blank.gif" width="12" height="12" /></td>';
93 } 93 }
94 /* 94 /*
95 if($system['system_message_type']=="none"): 95 if($system['system_message_type']=="none"):
96 $rows .= '<td colspan="2" style="vertical-align:middle;">'.nl2br($content).'<br /><span class="legal">Sent: '.date("M j, Y @ h:ia",$notices[$i]['sent_date']).'</span></td>'; 96 $rows .= '<td colspan="2" style="vertical-align:middle;">'.nl2br($content).'<br /><span class="legal">Sent: '.date("M j, Y @ h:ia",$notices[$i]['sent_date']).'</span></td>';
97 else: 97 else:
98 */ 98 */
99 if ($system['system_message_type'] == "none") { 99 if ($system['system_message_type'] == "none") {
100 $system['system_message_type'] = "notice"; 100 $system['system_message_type'] = "notice";
101 } 101 }
102 102
103 $rows .= '<td width="80" style="vertical-align:middle;"><span>'.ucfirst( 103 $rows .= '<td width="80" style="vertical-align:middle;"><span>'.ucfirst(
104 str_replace("_", "&nbsp;", $system['system_message_type'])).'</span></td>'; 104 str_replace("_", "&nbsp;", $system['system_message_type'])).'</span></td>';
105 $rows .= '<td style="vertical-align:middle;">'.nl2br($content).'<br /><span class="legal">Sent: '.date( 105 $rows .= '<td style="vertical-align:middle;">'.nl2br($content).'<br /><span class="legal">Sent: '.date(
106 "M j, Y @ h:ia", $notices[$i]['sent_date']).'</span></td>'; 106 "M j, Y @ h:ia", $notices[$i]['sent_date']).'</span></td>';
107 /*endif;*/ 107 /*endif;*/
108 $rows .= '</tr>'; 108 $rows .= '</tr>';
109 $last_id = $i; 109 $last_id = $i;
110 } 110 }
111 111
112 if ($end < $total_rows && CBV\on_page('/notices')) { 112 if ($end < $total_rows && CBV\on_page('/notices')) {
113 //$rows .= '<tr id="TzMoreRows"><td colspan="4"><a onclick="return false;" class="load_more_notices" id="'.$last_id.'" href="#">Show more...</a></td></tr>'; 113 //$rows .= '<tr id="TzMoreRows"><td colspan="4"><a onclick="return false;" class="load_more_notices" id="'.$last_id.'" href="#">Show more...</a></td></tr>';
114 } 114 }
115 115
116 if (isset($_POST['ajax'])) { 116 if (isset($_POST['ajax'])) {
117 die($rows); 117 die($rows);
118 } else { 118 } else {
119 print $rows; 119 print $rows;
120 } 120 }
121 } 121 }
122 122
123 function get_notification_by_trigger($trigger) 123 function get_notification_by_trigger($trigger)
124 { 124 {
125 $args = array( 125 $args = array(
126 'post_type' => 'notifications', 126 'post_type' => 'notifications',
127 'numberposts' => -1, 127 'numberposts' => -1,
128 'orderby' => 'modified', 128 'orderby' => 'modified',
129 'order' => 'desc' 129 'order' => 'desc'
130 ); 130 );
131 131
132 $my_notif = false; 132 $my_notif = false;
133 133
134 foreach (get_posts($args) as $entry) { 134 foreach (get_posts($args) as $entry) {
135 $details = get_post_meta($entry->ID, 'details', true); 135 $details = get_post_meta($entry->ID, 'details', true);
136 if ($details['type'] == "triggered" && $details['trigger'] == $trigger) { 136 if ($details['type'] == "triggered" && $details['trigger'] == $trigger) {
137 $my_notif = $entry; 137 $my_notif = $entry;
138 break; 138 break;
139 } 139 }
140 } 140 }
141 141
142 return $my_notif; 142 return $my_notif;
143 } 143 }
144 144
145 /** 145 /**
146 * GET NUMBER OF NEW NOTICES 146 * GET NUMBER OF NEW NOTICES
147 @returns (Int) 147 @returns (Int)
148 */ 148 */
149 function get_num_notices() 149 function get_num_notices()
150 { 150 {
151 // get number of notices for this user that are new... 151 // get number of notices for this user that are new...
152 $user = new User\CurrentAccount(); 152 $user = new User\CurrentAccount();
153 153
154 get_user_notices($user->ID); 154 get_user_notices($user->ID);
155 $notices = Vars::$notices; 155 $notices = Vars::$notices;
156 $notifications = array(); 156 $notifications = array();
157 // return $num; 157 // return $num;
158 if (empty($notices)) { 158 if (empty($notices)) {
159 return 0; 159 return 0;
160 } else { 160 } else {
161 foreach ($notices as $notice) { 161 foreach ($notices as $notice) {
162 if ($notice['status'] == "unread") { 162 if ($notice['status'] == "unread") {
163 $notifications[] = $notice; 163 $notifications[] = $notice;
164 } 164 }
165 } 165 }
166 } 166 }
167 return count($notifications); 167 return count($notifications);
168 } 168 }
169 169
170 function remove_notice($notification_id = -1) 170 function remove_notice($notification_id = -1)
171 { 171 {
172 get_user_notices(Tools\getCurrentUser()->ID); 172 get_user_notices(Tools\getCurrentUser()->ID);
173 $notices = Vars::$notices; 173 $notices = Vars::$notices;
174 if (!empty($notices)) { 174 if (!empty($notices)) {
175 foreach ($notices as $id => $notice) { 175 foreach ($notices as $id => $notice) {
176 if ($id == $notification_id) { 176 if ($id == $notification_id) {
177 $notices[$id]['status'] = "read"; 177 $notices[$id]['status'] = "read";
178 break; 178 break;
179 } 179 }
180 } 180 }
181 } 181 }
182 update_user_meta(Tools\getCurrentUser()->ID, 'notices', $notices); 182 update_user_meta(Tools\getCurrentUser()->ID, 'notices', $notices);
183 } 183 }
184 184
185 /** 185 /**
186 Send Notifications 186 Send Notifications
187 @trigger = notification unique slug name 187 @trigger = notification unique slug name
188 */ 188 */
189 function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = array(), $send_override = false) 189 function send_triggered_notification($uid = 0, $trigger = "NO_TRIGGER", $args = array(), $send_override = false)
190 { 190 {
191 global $wpdb; 191 global $wpdb;
192 $notification = get_notification_by_trigger($trigger); 192 $notification = get_notification_by_trigger($trigger);
193 193
194 if ($notification) { 194 if ($notification) {
195 // get the notification and notificatio details.... 195 // get the notification and notificatio details....
196 $nid = $notification->ID; 196 $nid = $notification->ID;
197 $details = get_post_meta($nid, 'details', true); 197 $details = get_post_meta($nid, 'details', true);
198 $email = get_post_meta($nid, 'email', true); 198 $email = get_post_meta($nid, 'email', true);
199 $system = get_post_meta($nid, 'system', true); 199 $system = get_post_meta($nid, 'system', true);
200 200
201 $notification->trigger = $details['trigger']; 201 $notification->trigger = $details['trigger'];
202 $notification->status = isset($details['status']) ? $details['status'] : "active"; 202 $notification->status = isset($details['status']) ? $details['status'] : "active";
203 $notification->type = $details['type']; 203 $notification->type = $details['type'];
204 $notification->sendto = $details['sendto']; 204 $notification->sendto = $details['sendto'];
205 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false; 205 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false;
206 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false; 206 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false;
207 207
208 // if is_system ========================================== 208 // if is_system ==========================================
209 if ($notification->is_system && $uid != 0) { 209 if ($notification->is_system && $uid != 0) {
210 get_user_notices($uid); 210 get_user_notices($uid);
211 $notices = Vars::$notices; 211 $notices = Vars::$notices;
212 212
213 $insert = array( 213 $insert = array(
214 'notification_id' => $nid, 214 'notification_id' => $nid,
215 'status' => 'unread', 215 'status' => 'unread',
216 'sent_date' => current_time('timestamp'), 216 'sent_date' => current_time('timestamp'),
217 'args' => $args 217 'args' => $args
218 ); 218 );
219 if (empty($notices)) { 219 if (empty($notices)) {
220 $notices = array(); 220 $notices = array();
221 } 221 }
222 222
223 $sequence = Sequencer\generate('NTC'); 223 $sequence = Sequencer\generate('NTC');
224 224
225 $notices[$sequence] = $insert; 225 $notices[$sequence] = $insert;
226 226
227 update_user_meta($uid, 'notices', $notices); 227 update_user_meta($uid, 'notices', $notices);
228 } 228 }
229 229
230 // if is_email =========================================== 230 // if is_email ===========================================
231 if ($notification->is_email) { 231 if ($notification->is_email) {
232 232
233 if ($uid == 0 && !isset($args['email'])) { 233 if ($uid == 0 && !isset($args['email'])) {
234 return; 234 return;
235 } elseif ($uid == 0 && isset($args['email'])) { 235 } elseif ($uid == 0 && isset($args['email'])) {
236 $to_email = $args['email']; 236 $to_email = $args['email'];
237 } else { 237 } else {
238 238
239 $user = new User\Account($uid); 239 $user = new User\Account($uid);
240 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true); 240 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
241 if (empty($email_address_preference)) { 241 if (empty($email_address_preference)) {
242 $to_email = $user->user_email; 242 $to_email = $user->user_email;
243 } else { 243 } else {
244 244
245 $pp = strtolower($email_address_preference)."_"; 245 $pp = strtolower($email_address_preference)."_";
246 $to_email = get_user_meta($user->ID, $pp.'email', true); 246 $to_email = get_user_meta($user->ID, $pp.'email', true);
247 247
248 if (empty($to_email)) { 248 if (empty($to_email)) {
249 $to_email = $user->user_email; 249 $to_email = $user->user_email;
250 } 250 }
251 } 251 }
252 } 252 }
253 253
254 $contents = $email; 254 $contents = $email;
255 255
256 $from_email = get_bloginfo('admin_email'); 256 $from_email = get_bloginfo('admin_email');
257 $subject = strip_tags($contents['subject']); 257 $subject = strip_tags($contents['subject']);
258 $html = $contents['html']; 258 $html = $contents['html'];
259 $alttext = strip_tags($contents['text']); 259 $alttext = strip_tags($contents['text']);
260 260
261 foreach ($args as $key => $val) { 261 foreach ($args as $key => $val) {
262 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) { 262 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
263 $html = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $html); 263 $html = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $html);
264 } else { 264 } else {
265 $html = str_replace("{".$key."}", $val, $html); 265 $html = str_replace("{".$key."}", $val, $html);
266 $alttext = str_replace("{".$key."}", $val, $alttext); 266 $alttext = str_replace("{".$key."}", $val, $alttext);
267 $subject = str_replace("{".$key."}", $val, $subject); 267 $subject = str_replace("{".$key."}", $val, $subject);
268 } 268 }
269 } 269 }
270 270
271 $attachments = array(); 271 $attachments = array();
272 if (isset($contents['attachments'])) { 272 if (isset($contents['attachments'])) {
273 $attachments = $contents['attachments']; 273 $attachments = $contents['attachments'];
274 } 274 }
275 275
276 $att1 = isset($attachments[0]) ? $attachments[0] : ''; 276 $att1 = isset($attachments[0]) ? $attachments[0] : '';
277 $att2 = isset($attachments[1]) ? $attachments[1] : ''; 277 $att2 = isset($attachments[1]) ? $attachments[1] : '';
278 $att3 = isset($attachments[2]) ? $attachments[2] : ''; 278 $att3 = isset($attachments[2]) ? $attachments[2] : '';
279 279
280 $wpdb->query( 280 $wpdb->query(
281 "INSERT INTO wp_mail_daemon (notification_id,from_email,to_email,subject,text,html,attachment1,attachment2,attachment3,sent,sent_date) VALUES ($nid,'$from_email','$to_email','$subject','$alttext','$html','$att1','$att2','$att3',0,'')"); 281 "INSERT INTO wp_mail_daemon (notification_id,from_email,to_email,subject,text,html,attachment1,attachment2,attachment3,sent,sent_date) VALUES ($nid,'$from_email','$to_email','$subject','$alttext','$html','$att1','$att2','$att3',0,'')");
282 //send_email($uid,$email,$args, true); 282 //send_email($uid,$email,$args, true);
283 } 283 }
284 } 284 }
285 // if the system notification has set current user than get current user otherwise loop through the users needed. 285 // if the system notification has set current user than get current user otherwise loop through the users needed.
286 } 286 }
287 287
288 function send_email($uid = 0, $contents, $args, $override = false) 288 function send_email($uid = 0, $contents, $args, $override = false)
289 { 289 {
290 if ($uid == 0 && !isset($args['email'])) { 290 if ($uid == 0 && !isset($args['email'])) {
291 return; 291 return;
292 } 292 }
293 293
294 if (isset($args['email'])) { 294 if (isset($args['email'])) {
295 $to_email = $args['email']; 295 $to_email = $args['email'];
296 } else { 296 } else {
297 $user = new WP_User($uid); 297 $user = new WP_User($uid);
298 298
299 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true); 299 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
300 if (empty($email_address_preference)) { 300 if (empty($email_address_preference)) {
301 $to_email = $user->user_email; 301 $to_email = $user->user_email;
302 } else { 302 } else {
303 303
304 $pp = strtolower($email_address_preference)."_"; 304 $pp = strtolower($email_address_preference)."_";
305 $to_email = get_user_meta($user->ID, $pp.'email', true); 305 $to_email = get_user_meta($user->ID, $pp.'email', true);
306 306
307 if (empty($to_email)) { 307 if (empty($to_email)) {
308 $to_email = $user->user_email; 308 $to_email = $user->user_email;
309 } 309 }
310 } 310 }
311 } 311 }
312 312
313 $from_address = get_bloginfo('admin_email'); 313 $from_address = get_bloginfo('admin_email');
314 314
315 $subject = strip_tags($contents['subject']); 315 $subject = strip_tags($contents['subject']);
316 $html = $contents['html']; 316 $html = $contents['html'];
317 $alttext = $contents['text']; 317 $alttext = $contents['text'];
318 318
319 $headers = ""; 319 $headers = "";
320 320
321 if (empty($html)) { 321 if (empty($html)) {
322 $message = $alttext; 322 $message = $alttext;
323 $message = strip_tags($message); 323 $message = strip_tags($message);
324 // it's text based only... no need to check format preference. 324 // it's text based only... no need to check format preference.
325 } else { 325 } else {
326 $message = $html; 326 $message = $html;
327 $headers .= 'MIME-Version: 1.0'."\r\n"; 327 $headers .= 'MIME-Version: 1.0'."\r\n";
328 $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n"; 328 $headers .= 'Content-type: text/html; charset=iso-8859-1'."\r\n";
329 // it's HTML formatted. 329 // it's HTML formatted.
330 // Check user preference if they want Text ONly?? 330 // Check user preference if they want Text ONly??
331 } 331 }
332 332
333 foreach ($args as $key => $val) { 333 foreach ($args as $key => $val) {
334 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) { 334 if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
335 $message = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $message); 335 $message = str_replace("{".$key."}", "<a href='".$val."'>".$val."</a>", $message);
336 } else { 336 } else {
337 $message = str_replace("{".$key."}", $val, $message); 337 $message = str_replace("{".$key."}", $val, $message);
338 $subject = str_replace("{".$key."}", $val, $subject); 338 $subject = str_replace("{".$key."}", $val, $subject);
339 } 339 }
340 } 340 }
341 341
342 // Additional headers 342 // Additional headers
343 //$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n"; 343 //$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n";
344 $headers .= 'From: CICBV <'.$from_address.'>'."\r\n"; 344 $headers .= 'From: CICBV <'.$from_address.'>'."\r\n";
345 345
346 //die("To: $to_email, Subject: $subject, Message: $message, Header: $headers"); 346 //die("To: $to_email, Subject: $subject, Message: $message, Header: $headers");
347 347
348 mail($to_email, $subject, $message, $headers); 348 mail($to_email, $subject, $message, $headers);
349 } 349 }
350 350
351 function getGroups($grpID = 0) 351 function getGroups($grpID = 0)
352 { 352 {
353 global $userAccessManager; 353 global $userAccessManager;
354 354
355 $groups = array(); 355 $groups = array();
356 if ($grpID > 0) { 356 if ($grpID > 0) {
357 $userGroups = $userAccessManager 357 $userGroups = $userAccessManager
358 ->getAccessHandler() 358 ->getAccessHandler()
359 ->getUserGroups($grpID); 359 ->getUserGroups($grpID);
360 return $userGroups->getGroupName(); 360 return $userGroups->getGroupName();
361 } else { 361 } else {
362 $userGroups = $userAccessManager 362 $userGroups = $userAccessManager
363 ->getAccessHandler() 363 ->getAccessHandler()
364 ->getUserGroups(); 364 ->getUserGroups();
365 } 365 }
366 foreach ($userGroups as $group) { 366 foreach ($userGroups as $group) {
367 $groups[$group->getId()] = $group->getGroupName(); 367 $groups[$group->getId()] = $group->getGroupName();
368 } 368 }
369 return $groups; 369 return $groups;
370 } 370 }
371 371
372 function get_field_lookup($var) 372 function get_field_lookup($var)
373 { 373 {
374 return isset(Vars::$field_lookup[$var]) ? Vars::$field_lookup[$var] : $var; 374 return isset(Vars::$field_lookup[$var]) ? Vars::$field_lookup[$var] : $var;
375 } 375 }
376 376
377 function current_url() 377 function current_url()
378 { 378 {
379 $pageURL = 'http'; 379 $pageURL = 'http';
380 if ($_SERVER["HTTPS"] == "on") { 380 if ($_SERVER["HTTPS"] == "on") {
381 $pageURL .= "s"; 381 $pageURL .= "s";
382 } 382 }
383 $pageURL .= "://"; 383 $pageURL .= "://";
384 if ($_SERVER["SERVER_PORT"] != "80") { 384 if ($_SERVER["SERVER_PORT"] != "80") {
385 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; 385 $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
386 } else { 386 } else {
387 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; 387 $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
388 } 388 }
389 return $pageURL; 389 return $pageURL;
390 } 390 }
391 391
392 class Actions 392 class Actions
393 { 393 {
394 public static function wp_ajax_update_notification_count() 394 public static function wp_ajax_update_notification_count()
395 { 395 {
396 $count = get_num_notices(); 396 $count = get_num_notices();
397 $return = array('count'=> $count); 397 $return = array('count'=> $count);
398 die(json_encode($return)); 398 die(json_encode($return));
399 } 399 }
400 400
401 public static function wp_ajax_mark_read() 401 public static function wp_ajax_mark_read()
402 { 402 {
403 remove_notice($_POST['index']); 403 remove_notice($_POST['index']);
404 } 404 }
405 405
406 public static function wp_ajax_print_user_notices() 406 public static function wp_ajax_print_user_notices()
407 { 407 {
408 print_user_notices(); 408 print_user_notices();
409 } 409 }
410 410
411 public static function wp_ajax_remove_attachment() 411 public static function wp_ajax_remove_attachment()
412 { 412 {
413 $id = $_POST['id']; 413 $id = $_POST['id'];
414 $email = get_post_meta($id, 'email', true); 414 $email = get_post_meta($id, 'email', true);
415 $attachments = $email['attachments']; 415 $attachments = $email['attachments'];
416 foreach ($attachments as $key => $name) { 416 foreach ($attachments as $key => $name) {
417 if ($name == $_POST['file']) { 417 if ($name == $_POST['file']) {
418 unset($attachments[$key]); 418 unset($attachments[$key]);
419 } 419 }
420 } 420 }
421 421
422 $email['attachments'] = $attachments; 422 $email['attachments'] = $attachments;
423 423
424 update_post_meta($id, 'email', $email); 424 update_post_meta($id, 'email', $email);
425 425
426 $return = array('success' => 'true'); 426 $return = array('success' => 'true');
427 die(json_encode($return)); 427 die(json_encode($return));
428 } 428 }
429 } 429 }
430 430
431 class Vars 431 class Vars
432 { 432 {
433 public static $notices; 433 public static $notices;
434 public static $options; 434 public static $options;
435 public static $field_lookup = array( 435 public static $field_lookup = array(
436 'allusers' => "All Users", 436 'allusers' => "All Users",
437 'report' => "Report" 437 'report' => "Report"
438 ); 438 );
439 } 439 }
440 440
441 ?> 441 ?>
...\ No newline at end of file ...\ No newline at end of file
......