b799f85c by Kevin Burton

notifications

1 parent 4d94d9dc
...@@ -3,6 +3,7 @@ namespace Tz\WordPress\Tools\Notifications; ...@@ -3,6 +3,7 @@ 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\CBV;
6 use WP_User; 7 use WP_User;
7 use Exception, StdClass; 8 use Exception, StdClass;
8 9
...@@ -12,6 +13,7 @@ const OPTION_NAME = "notif_options"; ...@@ -12,6 +13,7 @@ const OPTION_NAME = "notif_options";
12 call_user_func(function() { 13 call_user_func(function() {
13 Tools\add_actions(__NAMESPACE__ . '\Actions'); 14 Tools\add_actions(__NAMESPACE__ . '\Actions');
14 Vars::$options = new Tools\WP_Option(OPTION_NAME); 15 Vars::$options = new Tools\WP_Option(OPTION_NAME);
16 Vars::$notices = Array();
15 if (is_admin()) { 17 if (is_admin()) {
16 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php'); 18 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php');
17 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Admin.php'); 19 require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Admin.php');
...@@ -19,6 +21,82 @@ const OPTION_NAME = "notif_options"; ...@@ -19,6 +21,82 @@ const OPTION_NAME = "notif_options";
19 }); 21 });
20 22
21 23
24 function get_user_notices($uid) {
25 $notices = get_user_meta($uid,'notices',true);
26 if(!empty($notices)) {
27 $notices = array_reverse($notices);
28 Vars::$notices = $notices;
29 }
30 }
31
32 function print_user_notices($show_more = true) {
33
34 get_user_notices(Tools\getCurrentUser()->ID);
35 if (CBV\on_page('/dashboard')) {
36 $limit = 5;
37 } else {
38 $limit = 10;
39 }
40
41 $notices = Vars::$notices;
42 $total_rows = count($notices);
43 $next_id = isset($_POST['last_id']) ? ($_POST['last_id']+1) : 0;
44 $end = (($next_id + $limit) < $total_rows) ? ($next_id + $limit) : $total_rows;
45
46
47 if (count($notices) < 1) {
48 print '<tr><td colspan="3">You have no new notices.</td></tr>';
49 }
50
51 $rows = "";
52
53 $last_id = 0;
54 for($i = $next_id; $i < $end; $i++) {
55
56 $the_notice = get_post($notices[$i]['notification_id']);
57 $system = get_post_meta($notices[$i]['notification_id'],'system',true);
58 $content = $system['message'];
59 $id = $the_notice->ID;
60
61 foreach($notices[$i]['args'] as $key => $val) {
62 if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
63 $content = str_replace("{".$key."}","<a href='".$val."'>Click here</a>",$content);
64 } else {
65 $content = str_replace("{".$key."}",$val,$content);
66 }
67 }
68
69 $rows .= '<tr>';
70 if ($notices[$i]['status']=="unread") {
71 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><a id="'.$i.'" href="#" class="notice '.(($notices[$i]['status']=="read") ? 'read' : 'unread').'"><img src="assets/images/blank.gif" width="12" height="12" /></a></td>';
72 } else {
73 $rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><img src="assets/images/blank.gif" width="12" height="12" /></td>';
74 }
75 if($system['system_message_type']=="none"):
76 $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>';
77 else:
78 $rows .= '<td width="80" style="vertical-align:middle;"><span>'.ucfirst(str_replace("_","&nbsp;",$system['system_message_type'])).'</span></td>';
79 $rows .= '<td style="vertical-align:middle;">'.nl2br($content).'<br /><span class="legal">Sent: '.date("M j, Y @ h:ia",$notices[$i]['sent_date']).'</span></td>';
80 endif;
81 $rows .= '</tr>';
82 $last_id = $i;
83
84 }
85
86 if ($end < $total_rows && CBV\on_page('/notices')) {
87 $rows .= '<tr id="TzMoreRows"><td colspan="4"><a onclick="return false;" class="load_more_notices" id="'.$last_id.'" href="#">Show more...</a></td></tr>';
88 }
89
90 if(isset($_POST['ajax'])) {
91 die($rows);
92 } else {
93 print $rows;
94 }
95
96
97 }
98
99
22 function get_notification_by_trigger($trigger) { 100 function get_notification_by_trigger($trigger) {
23 $args = array( 101 $args = array(
24 'post_type' => 'notifications' 102 'post_type' => 'notifications'
...@@ -47,14 +125,15 @@ function get_notification_by_trigger($trigger) { ...@@ -47,14 +125,15 @@ function get_notification_by_trigger($trigger) {
47 */ 125 */
48 function get_num_notices() { 126 function get_num_notices() {
49 // get number of notices for this user that are new... 127 // get number of notices for this user that are new...
50 $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true); 128 get_user_notices(Tools\getCurrentUser()->ID);
129 $notices = Vars::$notices;
51 $notifications = array(); 130 $notifications = array();
52 // return $num; 131 // return $num;
53 if(empty($notices)) { 132 if(empty($notices)) {
54 return 0; 133 return 0;
55 } else { 134 } else {
56 foreach($notices as $notice) { 135 foreach($notices as $notice) {
57 if ($notice['status']=="show") { 136 if ($notice['status']=="unread") {
58 $notifications[] = $notice; 137 $notifications[] = $notice;
59 } 138 }
60 } 139 }
...@@ -63,11 +142,12 @@ function get_num_notices() { ...@@ -63,11 +142,12 @@ function get_num_notices() {
63 } 142 }
64 143
65 function remove_notice($notification_id = -1) { 144 function remove_notice($notification_id = -1) {
66 $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true); 145 get_user_notices(Tools\getCurrentUser()->ID);
146 $notices = Vars::$notices;
67 if (!empty($notices)) { 147 if (!empty($notices)) {
68 foreach($notices as $id => $notice) { 148 foreach($notices as $id => $notice) {
69 if ($id==$notification_id) { 149 if ($id==$notification_id) {
70 $notices[$id]['status']="hide"; 150 $notices[$id]['status']="read";
71 break; 151 break;
72 } 152 }
73 } 153 }
...@@ -115,11 +195,12 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(), ...@@ -115,11 +195,12 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),
115 195
116 // if is_system ========================================== 196 // if is_system ==========================================
117 if ($notification->is_system) { 197 if ($notification->is_system) {
118 $notices = get_user_meta($user->ID,'notices',true); 198 get_user_notices($user->ID);
199 $notices = Vars::$notices;
119 200
120 $insert = array( 201 $insert = array(
121 'notification_id' => $nid 202 'notification_id' => $nid
122 , 'status' => 'show' 203 , 'status' => 'unread'
123 , 'sent_date' => time() 204 , 'sent_date' => time()
124 , 'args' => $args 205 , 'args' => $args
125 ); 206 );
...@@ -229,9 +310,18 @@ class Actions { ...@@ -229,9 +310,18 @@ class Actions {
229 $return = array('count'=>$count); 310 $return = array('count'=>$count);
230 die(json_encode($return)); 311 die(json_encode($return));
231 } 312 }
313
314 public static function wp_ajax_mark_read() {
315 remove_notice($_POST['index']);
316 }
317
318 public static function wp_ajax_print_user_notices() {
319 print_user_notices();
320 }
232 } 321 }
233 322
234 class Vars { 323 class Vars {
324 public static $notices;
235 public static $options; 325 public static $options;
236 public static $field_lookup = array( 326 public static $field_lookup = array(
237 'current_user' => "Current Logged-in User" 327 'current_user' => "Current Logged-in User"
......