update to notification
Showing
1 changed file
with
49 additions
and
5 deletions
| ... | @@ -8,6 +8,7 @@ use Tz\Common; | ... | @@ -8,6 +8,7 @@ use Tz\Common; |
| 8 | const OPTION_NAME = "notif_options"; | 8 | const OPTION_NAME = "notif_options"; |
| 9 | 9 | ||
| 10 | call_user_func(function() { | 10 | call_user_func(function() { |
| 11 | Tools\add_actions(__NAMESPACE__ . '\Actions'); | ||
| 11 | Vars::$options = new Tools\WP_Option(OPTION_NAME); | 12 | Vars::$options = new Tools\WP_Option(OPTION_NAME); |
| 12 | if (is_admin()) { | 13 | if (is_admin()) { |
| 13 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php'); | 14 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php'); |
| ... | @@ -38,6 +39,39 @@ function get_notification_by_trigger($trigger) { | ... | @@ -38,6 +39,39 @@ function get_notification_by_trigger($trigger) { |
| 38 | 39 | ||
| 39 | } | 40 | } |
| 40 | 41 | ||
| 42 | /** | ||
| 43 | * GET NUMBER OF NEW NOTICES | ||
| 44 | @returns (Int) | ||
| 45 | */ | ||
| 46 | function get_num_notices() { | ||
| 47 | // get number of notices for this user that are new... | ||
| 48 | $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true); | ||
| 49 | $notifications = array(); | ||
| 50 | // return $num; | ||
| 51 | if(empty($notices)) { | ||
| 52 | return 0; | ||
| 53 | } else { | ||
| 54 | foreach($notices as $notice) { | ||
| 55 | if ($notice['status']=="show") { | ||
| 56 | $notifications[] = $notice; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | return count($notifications); | ||
| 61 | } | ||
| 62 | |||
| 63 | function remove_notice($notification_id = -1) { | ||
| 64 | $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true); | ||
| 65 | if (!empty($notices)) { | ||
| 66 | foreach($notices as $id => $notice) { | ||
| 67 | if ($id==$notification_id) { | ||
| 68 | $notices[$id]['status']="hide"; | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | update_user_meta(Tools\getCurrentUser()->ID,'notices',$notices); | ||
| 74 | } | ||
| 41 | 75 | ||
| 42 | /** | 76 | /** |
| 43 | Send Notifications | 77 | Send Notifications |
| ... | @@ -79,17 +113,20 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array()) | ... | @@ -79,17 +113,20 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array()) |
| 79 | 113 | ||
| 80 | // if is_system ========================================== | 114 | // if is_system ========================================== |
| 81 | if ($notification->is_system) { | 115 | if ($notification->is_system) { |
| 82 | $notices = get_user_meta($user->ID,'notices',false); | 116 | $notices = get_user_meta($user->ID,'notices',true); |
| 83 | if(!is_array($notices)) { | 117 | |
| 84 | $notices = array(); | ||
| 85 | } | ||
| 86 | $insert = array( | 118 | $insert = array( |
| 87 | 'notification_id' => $nid | 119 | 'notification_id' => $nid |
| 88 | , 'status' => 'show' | 120 | , 'status' => 'show' |
| 89 | , 'sent_date' => time() | 121 | , 'sent_date' => time() |
| 90 | , 'args' => $args | 122 | , 'args' => $args |
| 91 | ); | 123 | ); |
| 92 | $notices[] = $insert; | 124 | if(empty($notices)) { |
| 125 | $notices = array(); | ||
| 126 | } | ||
| 127 | $notices[] = $insert; | ||
| 128 | |||
| 129 | |||
| 93 | update_user_meta($user->ID,'notices',$notices); | 130 | update_user_meta($user->ID,'notices',$notices); |
| 94 | } | 131 | } |
| 95 | 132 | ||
| ... | @@ -141,6 +178,13 @@ function current_url() { | ... | @@ -141,6 +178,13 @@ function current_url() { |
| 141 | return $pageURL; | 178 | return $pageURL; |
| 142 | } | 179 | } |
| 143 | 180 | ||
| 181 | class Actions { | ||
| 182 | public static function wp_ajax_update_notification_count() { | ||
| 183 | $count = get_num_notices(); | ||
| 184 | $return = array('count'=>$count); | ||
| 185 | die(json_encode($return)); | ||
| 186 | } | ||
| 187 | } | ||
| 144 | 188 | ||
| 145 | class Vars { | 189 | class Vars { |
| 146 | public static $options; | 190 | public static $options; | ... | ... |
-
Please register or sign in to post a comment