Example #1
0
/**
 * Filter on the heartbeat recieved data and inject the new notifications data
 * 
 * @param type $response
 * @param type $data
 * @param type $screen_id
 * @return type
 */
function bpln_process_notification_request($response, $data, $screen_id)
{
    if (isset($data['bpln-data'])) {
        $notifications = array();
        $notification_ids = array();
        $request = $data['bpln-data'];
        $last_notified_id = absint($request['last_notified']);
        if (!empty($request)) {
            $notifications = bpln_get_new_notifications(get_current_user_id(), $last_notified_id);
            $notification_ids = wp_list_pluck($notifications, 'id');
            $notifications = bpln_get_notification_messages($notifications);
        }
        //include our last notified id to the list
        $notification_ids[] = $last_notified_id;
        //find the max id that we are sending with this request
        $last_notified_id = max($notification_ids);
        $response['bpln-data'] = array('messages' => $notifications, 'last_notified' => $last_notified_id);
    }
    return $response;
}
function bpln_check_notification()
{
    if (!is_user_logged_in()) {
        return;
    }
    if (!function_exists("bp_is_active")) {
        return;
    }
    //check_ajax_referer("bpln_check");
    $resp = array();
    //response array
    $ids = $_POST['notification_ids'];
    if (empty($ids)) {
        $notification_ids_old = array();
    } else {
        $notification_ids_old = explode(",", $ids);
    }
    $notification_ids_current = bpln_get_all_notification_ids(bp_loggedin_user_id());
    $notification_ids_diff = array_diff($notification_ids_current, $notification_ids_old);
    //difference between the current i
    if (!$notification_ids_diff) {
        $notification_ids_diff = array_diff($notification_ids_old, $notification_ids_current);
    }
    $count_new = count($notification_ids_diff);
    if ($count_new > 0) {
        //we have notifications
        $resp["change"] = "yes";
        $resp["current_ids"] = $notification_ids_current;
        //collect the specific new messages messages
        $resp['messages'] = bpln_get_notification_messages($notification_ids_diff);
        //ignored if change is no
        $resp['notification_all'] = bpln_get_all_notification();
        //replace all notification in admin bar as we don't have a way to differentiate using any dom technique
    } else {
        $resp["change"] = "no";
    }
    echo json_encode($resp);
    die(0);
    //for wp admin
}