Habari\Comments::count_total PHP Method

count_total() public static method

returns the number of comments based on the specified status and type
public static count_total ( integer | string $status = 'approved', integer | string $type = 'comment' ) : integer
$status integer | string A comment status value, or false to not filter on status (default: 'approved')
$type integer | string A comment type value, or false to not filter on type (default: 'comment')
return integer a count of the comments based on the specified status and type
    public static function count_total($status = 'approved', $type = 'comment')
    {
        $params = array('count' => 1, 'status' => $status, 'type' => $type);
        return self::get($params);
    }

Usage Example

示例#1
0
 /**
  * Output an Atom collection of comments based on the supplied parameters.
  *
  * @param array $params An array of parameters passed to Comments::get() to retrieve comments
  */
 function get_comments($params = array())
 {
     $comments = null;
     $comments_count = null;
     // Assign self link.
     $self = '';
     // Assign alternate link.
     $alternate = '';
     $updated = DateTime::create();
     // Check if this is a feed for a single post
     if (isset($params['slug']) || isset($params['id'])) {
         if (isset($params['slug'])) {
             $post = Post::get(array('slug' => $params['slug']));
         } elseif (isset($params['id'])) {
             $post = Post::get(array('id' => $params['id']));
         }
         // If the post doesn't exist, send a 404
         if (!$post instanceof Post) {
             header('HTTP/1.1 404 Not Found', true, 404);
             die('The post could not be found');
         }
         $comments = $post->comments->approved;
         $comments_count = count($comments);
         $content_type = Post::type_name($post->content_type);
         $self = URL::get("atom_feed_{$content_type}_comments", $post, false);
         $alternate = URL::get("display_{$content_type}", $post, false);
         if ($comments_count) {
             $updated = $comments[$comments_count - 1]->date;
         }
     } else {
         $self = URL::get('atom_feed_comments');
         $alternate = URL::get('display_home');
         $params['status'] = 'approved';
         $comments = Comments::get($params);
         $comments_count = Comments::count_total(Comment::status('approved'));
         if ($comments_count) {
             $updated = $comments[0]->date;
         }
     }
     $id = isset($params['slug']) ? $params['slug'] : 'atom_comments';
     $xml = $this->create_atom_wrapper($alternate, $self, $id, $updated);
     $xml = $this->add_pagination_links($xml, $comments_count);
     $xml = $this->add_comments($xml, $comments);
     Plugins::act('atom_get_comments', $xml, $params, $this->handler_vars);
     $xml = $xml->asXML();
     ob_clean();
     header('Content-Type: application/atom+xml');
     print $xml;
 }