Skip to:
Content

bbPress.org

Ticket #1427: statistics.diff

File statistics.diff, 11.8 KB (added by GautamGupta, 14 years ago)
  • bbp-includes/bbp-functions.php

     
    9292        return call_user_func_array( array( &$walker, 'walk' ), $args );
    9393}
    9494
     95/**
     96 * Get the forum statistics
     97 *
     98 * @since bbPress (r2769)
     99 *
     100 * @param mixed $args Optional. The function supports these arguments (all
     101 *                     default to true):
     102 *  - count_users: Count users?
     103 *  - count_forums: Count forums?
     104 *  - count_topics: Count topics? If set to false, private, spammed and trashed
     105 *                   topics are also not counted.
     106 *  - count_private_topics: Count private topics? (only counted if the current
     107 *                           user has read_private_topics cap)
     108 *  - count_spammed_topics: Count spammed topics? (only counted if the current
     109 *                           user has edit_others_topics cap)
     110 *  - count_trashed_topics: Count trashed topics? (only counted if the current
     111 *                           user has view_trash cap)
     112 *  - count_replies: Count replies? If set to false, private, spammed and
     113 *                   trashed replies are also not counted.
     114 *  - count_private_replies: Count private replies? (only counted if the current
     115 *                           user has read_private_replies cap)
     116 *  - count_spammed_replies: Count spammed replies? (only counted if the current
     117 *                           user has edit_others_replies cap)
     118 *  - count_trashed_replies: Count trashed replies? (only counted if the current
     119 *                           user has view_trash cap)
     120 *  - count_tags: Count tags? If set to false, empty tags are also not counted
     121 *  - count_empty_tags: Count empty tags?
     122 * @uses bbp_count_users() To count the number of registered users
     123 * @uses wp_count_posts() To count the number of forums, topics and replies
     124 * @uses wp_count_terms() To count the number of topic tags
     125 * @uses current_user_can() To check if the user is capable of doing things
     126 * @uses number_format_i18n() To format the number
     127 * @uses apply_filters() Calls 'bbp_get_statistics' with the statistics and args
     128 * @return object Walked forum tree
     129 */
     130function bbp_get_statistics( $args = '' ) {
     131        global $bbp;
     132
     133        $defaults = array (
     134                'count_users'           => true,
     135                'count_forums'          => true,
     136                'count_topics'          => true,
     137                'count_private_topics'  => true,
     138                'count_spammed_topics'  => true,
     139                'count_trashed_topics'  => true,
     140                'count_replies'         => true,
     141                'count_private_replies' => true,
     142                'count_spammed_replies' => true,
     143                'count_trashed_replies' => true,
     144                'count_tags'            => true,
     145                'count_empty_tags'      => true
     146        );
     147
     148        $r = wp_parse_args( $args, $defaults );
     149        extract( $r );
     150
     151        // Users
     152        if ( !empty( $count_users ) )
     153                $user_count = bbp_count_users();
     154
     155        // Forums
     156        if ( !empty( $count_forums ) ) {
     157                $forum_count = wp_count_posts( $bbp->forum_id );
     158                $forum_count = $forum_count->publish;
     159        }
     160
     161        // Topics
     162        if ( !empty( $count_topics ) ) {
     163
     164                $all_topics     = wp_count_posts( $bbp->topic_id );
     165
     166                // Published (publish + closed)
     167                $topic_count    = $all_topics->publish + $all_topics->{$bbp->closed_status_id};
     168
     169                if ( current_user_can( 'read_private_topics' ) || current_user_can( 'edit_others_topics' ) || current_user_can( 'view_trash' ) ) {
     170
     171                        // Private
     172                        $private_topics = ( !empty( $count_private_topics ) && current_user_can( 'read_private_topics' ) ) ? (int) $all_topics->private                 : 0;
     173
     174                        // Spam
     175                        $spammed_topics = ( !empty( $count_spammed_topics ) && current_user_can( 'edit_others_topics'  ) ) ? (int) $all_topics->{$bbp->spam_status_id}  : 0;
     176
     177                        // Trash
     178                        $trashed_topics = ( !empty( $count_trashed_topics ) && current_user_can( 'view_trash'          ) ) ? (int) $all_topics->{$bbp->trash_status_id} : 0;
     179
     180                        // Total hidden (private + spam + trash)
     181                        $hidden_topic_count = $private_topics + $spammed_topics + $trashed_topics;
     182
     183                        // Generate the hidden topic count's title attribute
     184                        $hidden_topic_title  = !empty( $private_topics ) ? sprintf( __( 'Private: %s | ', 'bbpress' ), number_format_i18n( $private_topics ) ) : '';
     185                        $hidden_topic_title .= !empty( $spammed_topics ) ? sprintf( __( 'Spammed: %s | ', 'bbpress' ), number_format_i18n( $spammed_topics ) ) : '';
     186                        $hidden_topic_title .= !empty( $trashed_topics ) ? sprintf( __( 'Trashed: %s',    'bbpress' ), number_format_i18n( $trashed_topics ) ) : '';
     187
     188                }
     189
     190        }
     191
     192        // Replies
     193        if ( !empty( $count_replies ) ) {
     194
     195                $all_replies     = wp_count_posts( $bbp->reply_id );
     196
     197                // Published
     198                $reply_count     = $all_replies->publish;
     199
     200                if ( current_user_can( 'read_private_replies' ) || current_user_can( 'edit_others_replies' ) || current_user_can( 'view_trash' ) ) {
     201
     202                        // Private
     203                        $private_replies = ( !empty( $count_private_replies ) && current_user_can( 'read_private_replies' ) ) ? (int) $all_replies->private                 : 0;
     204
     205                        // Spam
     206                        $spammed_replies = ( !empty( $count_spammed_replies ) && current_user_can( 'edit_others_replies'  ) ) ? (int) $all_replies->{$bbp->spam_status_id}  : 0;
     207
     208                        // Trash
     209                        $trashed_replies = ( !empty( $count_trashed_replies ) && current_user_can( 'view_trash'           ) ) ? (int) $all_replies->{$bbp->trash_status_id} : 0;
     210
     211                        // Total hidden (private + spam + trash)
     212                        $hidden_reply_count = $private_replies + $spammed_replies + $trashed_replies;
     213
     214                        // Generate the hidden reply count's title attribute
     215                        $hidden_reply_title  = !empty( $private_replies ) ? sprintf( __( 'Private: %s | ', 'bbpress' ), number_format_i18n( $private_replies ) ) : '';
     216                        $hidden_reply_title .= !empty( $spammed_replies ) ? sprintf( __( 'Spammed: %s | ', 'bbpress' ), number_format_i18n( $spammed_replies ) ) : '';
     217                        $hidden_reply_title .= !empty( $trashed_replies ) ? sprintf( __( 'Trashed: %s',    'bbpress' ), number_format_i18n( $trashed_replies ) ) : '';
     218
     219                }
     220
     221        }
     222
     223        // Topic Tags
     224        if ( !empty( $count_tags ) ) {
     225                $topic_tag_count = wp_count_terms( $bbp->topic_tag_id, array( 'hide_empty' => true ) );
     226
     227                if ( !empty( $count_empty_tags ) && current_user_can( 'edit_topic_tags' ) )
     228                        $empty_topic_tag_count = wp_count_terms( $bbp->topic_tag_id ) - $topic_tag_count;
     229        }
     230
     231        $statistics = compact( 'user_count', 'forum_count', 'topic_count', 'hidden_topic_count', 'reply_count', 'hidden_reply_count', 'topic_tag_count', 'empty_topic_tag_count' );
     232        $statistics = array_map( 'absint',             $statistics );
     233        $statistics = array_map( 'number_format_i18n', $statistics );
     234
     235        // Add the hidden (topic/reply) count title attribute strings because we don't need to run the math functions on these (see above)
     236        if ( isset( $hidden_topic_title ) )
     237                $statistics['hidden_topic_title'] = $hidden_topic_title;
     238
     239        if ( isset( $hidden_reply_title ) )
     240                $statistics['hidden_reply_title'] = $hidden_reply_title;
     241
     242        return apply_filters( 'bbp_get_statistics', $statistics, $args );
     243}
     244
    95245/** Post Form Handlers ********************************************************/
    96246
    97247/**
  • bbp-includes/bbp-users.php

     
    517517        return false;
    518518}
    519519
     520/**
     521 * Get the total number of users on the forums
     522 *
     523 *  - Checks for a global $bbp_total_users, if it is set, then that is returned.
     524 *  - Runs the filter 'bbp_get_total_users', if we get anything other than false
     525 *     (strict check ===), then that is returned.
     526 *  - Runs its own query to count the users
     527 *
     528 * @since bbPress (r2769)
     529 *
     530 * @uses apply_filters() Calls 'bbp_get_total_users' with bool false
     531 * @uses wpdb::get_var() To execute our query and get the var back
     532 * @return int Total number of users
     533 */
     534function bbp_count_users() {
     535        global $wpdb, $bbp_total_users;
     536
     537        if ( isset( $bbp_total_users ) )
     538                return $bbp_total_users;
     539
     540        if ( false === $bbp_total_users = apply_filters( 'bbp_get_total_users', false ) )
     541                $bbp_total_users = $wpdb->get_var( "SELECT COUNT(ID) FROM $wpdb->users USE INDEX (PRIMARY);" );
     542
     543        return (int) $bbp_total_users;
     544}
     545
    520546?>
  • bbp-themes/bbp-twentyten/page-bbp_statistics.php

     
     1<?php
     2
     3/**
     4 * Template Name: bbPress - Statistics
     5 *
     6 * @package bbPress
     7 * @subpackage Themes
     8 */
     9
     10/** Get the statistics and extract them for later use in this template */
     11extract( bbp_get_statistics(), EXTR_SKIP );
     12
     13?>
     14
     15<?php get_header(); ?>
     16
     17                <div id="container">
     18                        <div id="content" role="main">
     19
     20                                <?php do_action( 'bbp_template_notices' ); ?>
     21
     22                                <?php while ( have_posts() ) : the_post(); ?>
     23
     24                                        <div id="bbp-statistics" class="bbp-statistics">
     25                                                <h1 class="entry-title"><?php bbp_title_breadcrumb(); ?></h1>
     26                                                <div class="entry-content">
     27
     28                                                        <?php get_the_content() ? the_content() : _e( '<p>Here are the statistics and popular topics of our forums.</p>', 'bbpress' ); ?>
     29
     30                                                        <dl role="main">
     31
     32                                                                <dt><?php _e( 'Registered Users', 'bbpress' ); ?></dt>
     33                                                                <dd>
     34                                                                        <strong><?php echo $user_count; ?></strong>
     35                                                                </dd>
     36
     37                                                                <dt><?php _e( 'Forums', 'bbpress' ); ?></dt>
     38                                                                <dd>
     39                                                                        <strong><?php echo $forum_count; ?></strong>
     40                                                                </dd>
     41
     42                                                                <dt><?php _e( 'Topics', 'bbpress' ); ?></dt>
     43                                                                <dd>
     44                                                                        <strong><?php echo $topic_count; ?></strong>
     45                                                                </dd>
     46
     47                                                                <?php if ( !empty( $hidden_topic_count ) ) : ?>
     48
     49                                                                        <dt><?php _e( 'Hidden Topics', 'bbpress' ); ?></dt>
     50                                                                        <dd>
     51                                                                                <strong>
     52                                                                                        <abbr title="<?php echo esc_attr( $hidden_topic_title ); ?>"><?php echo $hidden_topic_count; ?></abbr>
     53                                                                                </strong>
     54                                                                        </dd>
     55
     56                                                                <?php endif; ?>
     57
     58                                                                <dt><?php _e( 'Replies', 'bbpress' ); ?></dt>
     59                                                                <dd>
     60                                                                        <strong><?php echo $reply_count; ?></strong>
     61                                                                </dd>
     62
     63                                                                <?php if ( !empty( $hidden_reply_count ) ) : ?>
     64
     65                                                                        <dt><?php _e( 'Hidden Replies', 'bbpress' ); ?></dt>
     66                                                                        <dd>
     67                                                                                <strong>
     68                                                                                        <abbr title="<?php echo esc_attr( $hidden_reply_title ); ?>"><?php echo $hidden_reply_count; ?></abbr>
     69                                                                                </strong>
     70                                                                        </dd>
     71
     72                                                                <?php endif; ?>
     73
     74                                                                <dt><?php _e( 'Topic Tags', 'bbpress' ); ?></dt>
     75                                                                <dd>
     76                                                                        <strong><?php echo $topic_tag_count; ?></strong>
     77                                                                </dd>
     78
     79                                                                <?php if ( !empty( $empty_topic_tag_count ) ) : ?>
     80
     81                                                                        <dt><?php _e( 'Empty Topic Tags', 'bbpress' ); ?></dt>
     82                                                                        <dd>
     83                                                                                <strong><?php echo $empty_topic_tag_count; ?></strong>
     84                                                                        </dd>
     85
     86                                                                <?php endif; ?>
     87
     88                                                                <?php do_action( 'bbp_statistics' ); ?>
     89                                                        </dl>
     90
     91                                                        <?php bbp_set_query_name( 'bbp_popular_topics' ); ?>
     92
     93                                                        <?php if ( bbp_has_topics( array( 'meta_key' => '_bbp_topic_reply_count', 'nopaging' => true, 'ignore_sticky_topics' => true ) ) ) : ?>
     94
     95                                                                <h2 class="entry-title"><?php _e( 'Popular Topics', 'bbpress' ); ?></h2>
     96
     97                                                                <?php get_template_part( 'loop', 'bbp_topics' ); ?>
     98
     99                                                        <?php endif; ?>
     100
     101                                                        <?php bbp_reset_query_name(); ?>
     102
     103                                                </div>
     104                                        </div><!-- #bbp-statistics -->
     105
     106                                <?php endwhile; ?>
     107
     108                        </div><!-- #content -->
     109                </div><!-- #container -->
     110
     111<?php get_sidebar(); ?>
     112<?php get_footer(); ?>
  • bbpress.php

     
    6161        var $topic_tag_id;
    6262
    6363        /**
    64          * @var string Closed post status id. Used by forums and topics.
     64         * @var string Closed post status id. Used by topics.
    6565         */
    6666        var $closed_status_id;
    6767