Exemplo n.º 1
0
/**
 * Get the forum statistics
 *
 * @since 2.0.0 bbPress (r2769)
 * @since 2.6.0 bbPress (r6055) Introduced the `count_pending_topics` and
 *                               `count_pending_replies` arguments.
 *
 * @param array $args Optional. The function supports these arguments (all
 *                     default to true):
 *  - count_users: Count users?
 *  - count_forums: Count forums?
 *  - count_topics: Count topics? If set to false, private, spammed and trashed
 *                   topics are also not counted.
 *  - count_pending_topics: Count pending topics? (only counted if the current
 *                           user has edit_others_topics cap)
 *  - count_private_topics: Count private topics? (only counted if the current
 *                           user has read_private_topics cap)
 *  - count_spammed_topics: Count spammed topics? (only counted if the current
 *                           user has edit_others_topics cap)
 *  - count_trashed_topics: Count trashed topics? (only counted if the current
 *                           user has view_trash cap)
 *  - count_replies: Count replies? If set to false, private, spammed and
 *                   trashed replies are also not counted.
 *  - count_pending_replies: Count pending replies? (only counted if the current
 *                           user has edit_others_replies cap)
 *  - count_private_replies: Count private replies? (only counted if the current
 *                           user has read_private_replies cap)
 *  - count_spammed_replies: Count spammed replies? (only counted if the current
 *                           user has edit_others_replies cap)
 *  - count_trashed_replies: Count trashed replies? (only counted if the current
 *                           user has view_trash cap)
 *  - count_tags: Count tags? If set to false, empty tags are also not counted
 *  - count_empty_tags: Count empty tags?
 * @uses bbp_get_total_users() To count the number of registered users
 * @uses bbp_get_forum_post_type() To get the forum post type
 * @uses bbp_get_topic_post_type() To get the topic post type
 * @uses bbp_get_reply_post_type() To get the reply post type
 * @uses wp_count_posts() To count the number of forums, topics and replies
 * @uses wp_count_terms() To count the number of topic tags
 * @uses current_user_can() To check if the user is capable of doing things
 * @uses number_format_i18n() To format the number
 * @uses apply_filters() Calls 'bbp_get_statistics' with the statistics and args
 * @return object Walked forum tree
 */
function bbp_get_statistics($args = array())
{
    // Parse arguments against default values
    $r = bbp_parse_args($args, array('count_users' => true, 'count_forums' => true, 'count_topics' => true, 'count_pending_topics' => true, 'count_private_topics' => true, 'count_spammed_topics' => true, 'count_trashed_topics' => true, 'count_replies' => true, 'count_pending_replies' => true, 'count_private_replies' => true, 'count_spammed_replies' => true, 'count_trashed_replies' => true, 'count_tags' => true, 'count_empty_tags' => true), 'get_statistics');
    // Defaults
    $user_count = 0;
    $forum_count = 0;
    $topic_count = 0;
    $topic_count_hidden = 0;
    $reply_count = 0;
    $reply_count_hidden = 0;
    $topic_tag_count = 0;
    $empty_topic_tag_count = 0;
    // Users
    if (!empty($r['count_users'])) {
        $user_count = bbp_get_total_users();
    }
    // Forums
    if (!empty($r['count_forums'])) {
        $forum_count = wp_count_posts(bbp_get_forum_post_type())->publish;
    }
    // Post statuses
    $pending = bbp_get_pending_status_id();
    $private = bbp_get_private_status_id();
    $spam = bbp_get_spam_status_id();
    $trash = bbp_get_trash_status_id();
    $closed = bbp_get_closed_status_id();
    // Topics
    if (!empty($r['count_topics'])) {
        $all_topics = wp_count_posts(bbp_get_topic_post_type());
        // Published (publish + closed)
        $topic_count = $all_topics->publish + $all_topics->{$closed};
        if (current_user_can('read_private_topics') || current_user_can('edit_others_topics') || current_user_can('view_trash')) {
            // Declare empty arrays
            $topics = $topic_titles = array();
            // Pending
            $topics['pending'] = !empty($r['count_pending_topics']) && current_user_can('edit_others_topics') ? (int) $all_topics->{$pending} : 0;
            // Private
            $topics['private'] = !empty($r['count_private_topics']) && current_user_can('read_private_topics') ? (int) $all_topics->{$private} : 0;
            // Spam
            $topics['spammed'] = !empty($r['count_spammed_topics']) && current_user_can('edit_others_topics') ? (int) $all_topics->{$spam} : 0;
            // Trash
            $topics['trashed'] = !empty($r['count_trashed_topics']) && current_user_can('view_trash') ? (int) $all_topics->{$trash} : 0;
            // Total hidden (private + spam + trash)
            $topic_count_hidden = $topics['pending'] + $topics['private'] + $topics['spammed'] + $topics['trashed'];
            // Generate the hidden topic count's title attribute
            $topic_titles[] = !empty($topics['pending']) ? sprintf(__('Pending: %s', 'bbpress'), number_format_i18n($topics['pending'])) : '';
            $topic_titles[] = !empty($topics['private']) ? sprintf(__('Private: %s', 'bbpress'), number_format_i18n($topics['private'])) : '';
            $topic_titles[] = !empty($topics['spammed']) ? sprintf(__('Spammed: %s', 'bbpress'), number_format_i18n($topics['spammed'])) : '';
            $topic_titles[] = !empty($topics['trashed']) ? sprintf(__('Trashed: %s', 'bbpress'), number_format_i18n($topics['trashed'])) : '';
            // Compile the hidden topic title
            $hidden_topic_title = implode(' | ', array_filter($topic_titles));
        }
    }
    // Replies
    if (!empty($r['count_replies'])) {
        $all_replies = wp_count_posts(bbp_get_reply_post_type());
        // Published
        $reply_count = $all_replies->publish;
        if (current_user_can('read_private_replies') || current_user_can('edit_others_replies') || current_user_can('view_trash')) {
            // Declare empty arrays
            $replies = $reply_titles = array();
            // Pending
            $replies['pending'] = !empty($r['count_pending_replies']) && current_user_can('edit_others_replies') ? (int) $all_replies->{$pending} : 0;
            // Private
            $replies['private'] = !empty($r['count_private_replies']) && current_user_can('read_private_replies') ? (int) $all_replies->{$private} : 0;
            // Spam
            $replies['spammed'] = !empty($r['count_spammed_replies']) && current_user_can('edit_others_replies') ? (int) $all_replies->{$spam} : 0;
            // Trash
            $replies['trashed'] = !empty($r['count_trashed_replies']) && current_user_can('view_trash') ? (int) $all_replies->{$trash} : 0;
            // Total hidden (private + spam + trash)
            $reply_count_hidden = $replies['pending'] + $replies['private'] + $replies['spammed'] + $replies['trashed'];
            // Generate the hidden topic count's title attribute
            $reply_titles[] = !empty($replies['pending']) ? sprintf(__('Pending: %s', 'bbpress'), number_format_i18n($replies['pending'])) : '';
            $reply_titles[] = !empty($replies['private']) ? sprintf(__('Private: %s', 'bbpress'), number_format_i18n($replies['private'])) : '';
            $reply_titles[] = !empty($replies['spammed']) ? sprintf(__('Spammed: %s', 'bbpress'), number_format_i18n($replies['spammed'])) : '';
            $reply_titles[] = !empty($replies['trashed']) ? sprintf(__('Trashed: %s', 'bbpress'), number_format_i18n($replies['trashed'])) : '';
            // Compile the hidden replies title
            $hidden_reply_title = implode(' | ', array_filter($reply_titles));
        }
    }
    // Topic Tags
    if (!empty($r['count_tags']) && bbp_allow_topic_tags()) {
        // Get the count
        $topic_tag_count = wp_count_terms(bbp_get_topic_tag_tax_id(), array('hide_empty' => true));
        // Empty tags
        if (!empty($r['count_empty_tags']) && current_user_can('edit_topic_tags')) {
            $empty_topic_tag_count = wp_count_terms(bbp_get_topic_tag_tax_id()) - $topic_tag_count;
        }
    }
    // Tally the tallies
    $statistics = array_map('number_format_i18n', array_map('absint', compact('user_count', 'forum_count', 'topic_count', 'topic_count_hidden', 'reply_count', 'reply_count_hidden', 'topic_tag_count', 'empty_topic_tag_count')));
    // Add the hidden (topic/reply) count title attribute strings because we
    // don't need to run the math functions on these (see above)
    $statistics['hidden_topic_title'] = isset($hidden_topic_title) ? $hidden_topic_title : '';
    $statistics['hidden_reply_title'] = isset($hidden_reply_title) ? $hidden_reply_title : '';
    return apply_filters('bbp_get_statistics', $statistics, $r);
}
Exemplo n.º 2
0
 /**
  * @covers ::bbp_get_total_users
  */
 public function test_bbp_get_total_users()
 {
     $this->factory->user->create_many(3);
     $users = (int) bbp_get_total_users();
     // 15 + 1, the + 1 is the default admin user
     $this->assertSame(4, $users);
 }