4cd50c72 by Kevin Burton

update to notification

1 parent 33f056a9
......@@ -8,6 +8,7 @@ use Tz\Common;
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');
......@@ -38,6 +39,39 @@ function get_notification_by_trigger($trigger) {
}
/**
* 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
......@@ -79,17 +113,20 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array())
// if is_system ==========================================
if ($notification->is_system) {
$notices = get_user_meta($user->ID,'notices',false);
if(!is_array($notices)) {
$notices = array();
}
$notices = get_user_meta($user->ID,'notices',true);
$insert = array(
'notification_id' => $nid
, 'status' => 'show'
, 'sent_date' => time()
, 'args' => $args
);
$notices[] = $insert;
if(empty($notices)) {
$notices = array();
}
$notices[] = $insert;
update_user_meta($user->ID,'notices',$notices);
}
......@@ -141,6 +178,13 @@ function current_url() {
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;
......