b799f85c by Kevin Burton

notifications

1 parent 4d94d9dc
......@@ -3,6 +3,7 @@ namespace Tz\WordPress\Tools\Notifications;
use Tz\Common;
use Tz\WordPress\Tools;
use Tz\WordPress\CBV;
use WP_User;
use Exception, StdClass;
......@@ -12,6 +13,7 @@ const OPTION_NAME = "notif_options";
call_user_func(function() {
Tools\add_actions(__NAMESPACE__ . '\Actions');
Vars::$options = new Tools\WP_Option(OPTION_NAME);
Vars::$notices = Array();
if (is_admin()) {
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php');
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Admin.php');
......@@ -19,6 +21,82 @@ const OPTION_NAME = "notif_options";
});
function get_user_notices($uid) {
$notices = get_user_meta($uid,'notices',true);
if(!empty($notices)) {
$notices = array_reverse($notices);
Vars::$notices = $notices;
}
}
function print_user_notices($show_more = true) {
get_user_notices(Tools\getCurrentUser()->ID);
if (CBV\on_page('/dashboard')) {
$limit = 5;
} else {
$limit = 10;
}
$notices = Vars::$notices;
$total_rows = count($notices);
$next_id = isset($_POST['last_id']) ? ($_POST['last_id']+1) : 0;
$end = (($next_id + $limit) < $total_rows) ? ($next_id + $limit) : $total_rows;
if (count($notices) < 1) {
print '<tr><td colspan="3">You have no new notices.</td></tr>';
}
$rows = "";
$last_id = 0;
for($i = $next_id; $i < $end; $i++) {
$the_notice = get_post($notices[$i]['notification_id']);
$system = get_post_meta($notices[$i]['notification_id'],'system',true);
$content = $system['message'];
$id = $the_notice->ID;
foreach($notices[$i]['args'] as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
$content = str_replace("{".$key."}","<a href='".$val."'>Click here</a>",$content);
} else {
$content = str_replace("{".$key."}",$val,$content);
}
}
$rows .= '<tr>';
if ($notices[$i]['status']=="unread") {
$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>';
} else {
$rows .= '<td width="12" style="padding:0;padding-left:10px;vertical-align:middle;"><img src="assets/images/blank.gif" width="12" height="12" /></td>';
}
if($system['system_message_type']=="none"):
$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>';
else:
$rows .= '<td width="80" style="vertical-align:middle;"><span>'.ucfirst(str_replace("_","&nbsp;",$system['system_message_type'])).'</span></td>';
$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>';
endif;
$rows .= '</tr>';
$last_id = $i;
}
if ($end < $total_rows && CBV\on_page('/notices')) {
$rows .= '<tr id="TzMoreRows"><td colspan="4"><a onclick="return false;" class="load_more_notices" id="'.$last_id.'" href="#">Show more...</a></td></tr>';
}
if(isset($_POST['ajax'])) {
die($rows);
} else {
print $rows;
}
}
function get_notification_by_trigger($trigger) {
$args = array(
'post_type' => 'notifications'
......@@ -47,14 +125,15 @@ function get_notification_by_trigger($trigger) {
*/
function get_num_notices() {
// get number of notices for this user that are new...
$notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
get_user_notices(Tools\getCurrentUser()->ID);
$notices = Vars::$notices;
$notifications = array();
// return $num;
if(empty($notices)) {
return 0;
} else {
foreach($notices as $notice) {
if ($notice['status']=="show") {
if ($notice['status']=="unread") {
$notifications[] = $notice;
}
}
......@@ -63,11 +142,12 @@ function get_num_notices() {
}
function remove_notice($notification_id = -1) {
$notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
get_user_notices(Tools\getCurrentUser()->ID);
$notices = Vars::$notices;
if (!empty($notices)) {
foreach($notices as $id => $notice) {
if ($id==$notification_id) {
$notices[$id]['status']="hide";
$notices[$id]['status']="read";
break;
}
}
......@@ -115,11 +195,12 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),
// if is_system ==========================================
if ($notification->is_system) {
$notices = get_user_meta($user->ID,'notices',true);
get_user_notices($user->ID);
$notices = Vars::$notices;
$insert = array(
'notification_id' => $nid
, 'status' => 'show'
, 'status' => 'unread'
, 'sent_date' => time()
, 'args' => $args
);
......@@ -229,9 +310,18 @@ class Actions {
$return = array('count'=>$count);
die(json_encode($return));
}
public static function wp_ajax_mark_read() {
remove_notice($_POST['index']);
}
public static function wp_ajax_print_user_notices() {
print_user_notices();
}
}
class Vars {
public static $notices;
public static $options;
public static $field_lookup = array(
'current_user' => "Current Logged-in User"
......