public function send_post_to_user_categories($post)
 {
     $user_cat = get_post_meta($post->ID, 'pnfw_user_cat', true);
     $category_id = pnfw_get_normalized_term_id((int) $this->get_category_id($post));
     $lang = pnfw_get_post_lang($post->ID);
     $total = 0;
     $page = 0;
     $tokens = $this->get_tokens($category_id, $lang, $page);
     if (empty($user_cat)) {
         // Send tokens to all categories (no filter)
         $total += $this->raw_send($tokens, $post->post_title, $post->ID);
     } else {
         // Send tokens only to selected category
         $out_tokens = array();
         foreach ($tokens as $token) {
             $user_id = $this->get_user_id($token);
             if ($this->user_should_be_notified($user_id, $user_cat)) {
                 $out_tokens[] = $token;
             }
         }
         $total += $this->raw_send($out_tokens, $post->post_title, $post->ID);
     }
     if (count($tokens) >= 1000) {
         pnfw_log($this->type, sprintf(__('This plugin allows you to send push notifications to up to %d tokens per platform. To send more notifications, please upgrade to <a href="http://www.delitestudio.com/wordpress/push-notifications-for-wordpress/">Push Notifications for Wordpress</a>.', 'pnfw'), 1000));
     }
     return $total;
 }
 public function __construct()
 {
     parent::__construct(site_url('pnfw/categories/'));
     // Optional
     $timestamp = $this->opt_parameter('timestamp', FILTER_SANITIZE_NUMBER_INT);
     if ($timestamp == $this->get_last_modification_timestamp()) {
         $this->header_error('304');
     }
     switch ($this->get_method()) {
         case 'GET':
             $object_taxonomies = get_option('pnfw_enabled_object_taxonomies', array());
             $raw_terms = get_terms($object_taxonomies, array('hide_empty' => false));
             $categories = array();
             foreach ($raw_terms as $raw_term) {
                 // Mandatory fields
                 $category = array('id' => (int) $raw_term->term_id, 'name' => $raw_term->name);
                 // Optional fields
                 $description = $raw_term->description;
                 if (!empty($description)) {
                     $category['description'] = $description;
                 }
                 $category['exclude'] = $this->isCategoryExcluded(pnfw_get_normalized_term_id((int) $raw_term->term_id));
                 $categories[] = $category;
             }
             header('Content-Type: application/json');
             echo json_encode(array('categories' => $categories, 'timestamp' => $this->get_last_modification_timestamp()));
             break;
         case 'POST':
             $this->category_id = pnfw_get_normalized_term_id((int) $this->get_parameter('id', FILTER_SANITIZE_NUMBER_INT));
             $excluded = $this->get_parameter('exclude', FILTER_VALIDATE_BOOLEAN);
             $this->setCategoryExcluded($excluded);
             break;
         default:
             $this->json_error('401', __('Invalid HTTP method', 'pnfw'));
     }
     exit;
 }
 function column_default($item, $column_name)
 {
     switch ($column_name) {
         case 'username':
             return $item->display_name;
         case 'email':
             return $item->user_email;
         case 'user_categories':
             $user_groups = wp_get_object_terms($item->ID, 'user_cat', array('fields' => 'names'));
             return implode(', ', $user_groups);
         case 'devices':
             global $wpdb;
             $push_tokens = $wpdb->get_blog_prefix() . 'push_tokens';
             $token_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$push_tokens} WHERE user_id=%s", $item->ID));
             return $token_count;
         case 'excluded_categories':
             $object_taxonomies = get_option('pnfw_enabled_object_taxonomies', array());
             if (empty($object_taxonomies)) {
                 return '';
             }
             $terms = get_terms($object_taxonomies, array('hide_empty' => false));
             $excluded_categories = array();
             foreach ($terms as $term) {
                 $is_category_excluded = $this->is_category_excluded($item->ID, pnfw_get_normalized_term_id((int) $term->term_id));
                 if ($is_category_excluded) {
                     $excluded_categories[] = $term->name;
                 }
             }
             return implode(", ", $excluded_categories);
         default:
             // custom parameters
             return '';
     }
 }