Skip to:
Content

bbPress.org

Ticket #1320: counting-123.diff

File counting-123.diff, 19.4 KB (added by GautamGupta, 13 years ago)

Works like a charm for me. TODO: Recount

  • bbp-admin/bbp-functions.php

     
    164164/**
    165165 * Recount forum topics
    166166 *
     167 * @todo Forum total topic recount
     168 *
    167169 * @since bbPress (r2613)
    168170 *
    169171 * @uses wpdb::query() To run our recount sql queries
     
    176178        $statement = __( 'Counting the number of topics in each forum… %s', 'bbpress' );
    177179        $result    = __( 'Failed!', 'bbpress' );
    178180
    179         $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_forum_topic_count';";
     181        $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_forum_topic_count', '_bbp_forum_total_topic_count' );";
    180182        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    181183                return array( 1, sprintf( $statement, $result ) );
    182184
    183         $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_forum_topic_count', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$bbp->topic_id}' AND `post_status` = 'publish' GROUP BY `post_parent`);";
     185        $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_forum_topic_count', COUNT(`post_status`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '{$bbp->topic_id}' AND `post_status` IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) GROUP BY `post_parent`);";
    184186        if ( is_wp_error( $wpdb->query( $sql ) ) )
    185187                return array( 2, sprintf( $statement, $result ) );
    186188
     
    191193/**
    192194 * Recount forum replies
    193195 *
     196 * @todo Make the recounts actually work
     197 *
    194198 * @since bbPress (r2613)
    195199 *
    196200 * @uses wpdb::query() To run our recount sql queries
     
    203207        $statement = __( 'Counting the number of replies in each forum… %s', 'bbpress' );
    204208        $result    = __( 'Failed!', 'bbpress' );
    205209
    206         $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_forum_reply_count';";
     210        $sql_delete = "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` IN ( '_bbp_forum_reply_count', '_bbp_forum_total_reply_count' );";
    207211        if ( is_wp_error( $wpdb->query( $sql_delete ) ) )
    208212                return array( 1, sprintf( $statement, $result ) );
    209213
  • bbp-includes/bbp-forum-functions.php

     
    236236 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
    237237 *                       is a topic or a forum. If it's a topic, its parent,
    238238 *                       i.e. the forum is automatically retrieved.
     239 * @param bool $total_count Optional. To return the total count or normal
     240 *                           count?
    239241 * @uses get_post_field() To check whether the supplied id is a topic
    240242 * @uses bbp_get_topic_forum_id() To get the topic's forum id
    241243 * @uses wpdb::prepare() To prepare the sql statement
    242244 * @uses wpdb::get_col() To execute the query and get the column back
     245 * @uses bbp_get_topic_status() To get the topic status
    243246 * @uses update_post_meta() To update the forum's topic count meta
    244247 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic
    245  *                        count and forum id
     248 *                        count, forum id and total count bool
    246249 * @return int Forum topic count
    247250 */
    248 function bbp_update_forum_topic_count( $forum_id = 0 ) {
     251function bbp_update_forum_topic_count( $forum_id = 0, $total_count = true ) {
    249252        global $wpdb, $bbp;
    250253
    251254        $forum_id = bbp_get_forum_id( $forum_id );
    252255
    253         // If it's a reply, then get the parent (topic id)
    254         if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) )
     256        // If it's a topic, then get the parent (forum id)
     257        if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) ) {
     258                $topic_id = $forum_id;
    255259                $forum_id = bbp_get_topic_forum_id( $forum_id );
     260        }
    256261
    257         // Get topics count
    258         $topics = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->topic_id . "';", $forum_id ) ) );
     262        $topics               = 0;
     263        $children_topic_count = 0;
     264        $children             = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) );
    259265
     266        foreach ( (array) $children as $child ) {
     267                $children_topic_count += (int) bbp_get_forum_topic_count( $child->ID );
     268        }
     269
     270        // Don't count topics if the forum is a category
     271        if ( !bbp_is_forum_category( $forum_id ) ) {
     272
     273                if ( empty( $topic_id ) || !$topics = (int) get_post_meta( $forum_id, '_bbp_forum_topic_count', true ) ) {
     274                        $topics = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( "', '", array( 'publish', $bbp->closed_status_id ) ) . "' ) AND post_type = '" . $bbp->topic_id . "';", $forum_id ) );
     275                } else {
     276                        if ( in_array( bbp_get_topic_status( $topic_id ), array( 'publish', $bbp->closed_status_id ) ) )
     277                                $topics++;
     278                        else
     279                                $topics--;
     280                }
     281
     282        }
     283
     284        $total_topics = $topics + $children_topic_count;
     285
    260286        // Update the count
    261         update_post_meta( $forum_id, '_bbp_forum_topic_count', (int) $topics );
     287        update_post_meta( $forum_id, '_bbp_forum_topic_count',       $topics       );
     288        update_post_meta( $forum_id, '_bbp_forum_total_topic_count', $total_topics );
    262289
    263         return apply_filters( 'bbp_update_forum_topic_count', (int) $topics, $forum_id );
     290        if ( $parent = bbp_get_forum_parent( $forum_id ) )
     291                bbp_update_forum_topic_count( $parent );
     292
     293        return apply_filters( 'bbp_update_forum_topic_count', empty( $total_count ) ? $topics : $total_topics, $forum_id, $total_count );
    264294}
    265295
    266296/**
     
    270300 *
    271301 * @since bbPress (r2464)
    272302 *
    273  * @param int $forum_id Optional. Forum id or reply id. It is checked whether it
    274  *                       is a reply or a forum. If it's a reply, its forum is
    275  *                       automatically retrieved.
     303 * @param int $forum_id Optional. Forum id or topic id reply id. It is checked
     304 *                       whether it is a reply or a topic or a forum and the
     305 *                       forum id is automatically retrieved.
     306 * @param bool $total_count Optional. To return the total count or normal
     307 *                           count?
    276308 * @uses get_post_field() To check whether the supplied id is a reply
    277  * @uses bbp_get_reply_topic_id() To get the reply's topic id
     309 * @uses bbp_get_reply_forum_id() To get the reply's forum id
    278310 * @uses bbp_get_topic_forum_id() To get the topic's forum id
    279311 * @uses wpdb::prepare() To prepare the sql statement
    280312 * @uses wpdb::get_col() To execute the query and get the column back
     313 * @uses wpdb::get_var() To execute the query and get the var back
     314 * @uses bbp_get_reply_status() To get the reply status
    281315 * @uses update_post_meta() To update the forum's reply count meta
    282316 * @uses apply_filters() Calls 'bbp_update_forum_reply_count' with the reply
    283  *                        count and forum id
     317 *                        count, forum id and total count bool
    284318 * @return int Forum reply count
    285319 */
    286 function bbp_update_forum_reply_count( $forum_id = 0 ) {
     320function bbp_update_forum_reply_count( $forum_id = 0, $total_count = true ) {
    287321        global $wpdb, $bbp;
    288322
    289323        $forum_id = bbp_get_forum_id( $forum_id );
    290324
    291         // If it's a reply, then get the parent (topic id)
     325        // If it's a reply, then get the grandparent (forum id)
    292326        if ( $bbp->reply_id == get_post_field( 'post_type', $forum_id ) ) {
    293                 $topic_id = bbp_get_reply_topic_id( $forum_id );
    294                 $forum_id = bbp_get_topic_forum_id( $topic_id );
     327                $reply_id = $forum_id;
     328                $forum_id = bbp_get_reply_forum_id( $forum_id );
    295329        }
    296330
    297         // There should always be at least 1 voice
    298         $replies = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';", $forum_id ) ) );
     331        // If it's a topic, then get the parent (forum id)
     332        if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) )
     333                $forum_id = bbp_get_topic_forum_id( $forum_id );
    299334
     335        $replies              = 0;
     336        $children_reply_count = 0;
     337        $children             = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) );
     338
     339        foreach ( (array) $children as $child ) {
     340                $children_reply_count += (int) bbp_get_forum_reply_count( $child->ID );
     341        }
     342
     343        // Don't count replies if the forum is a category
     344        if ( !bbp_is_forum_category( $forum_id ) ) {
     345
     346                if ( empty( $reply_id ) || !$replies = (int) get_post_meta( $forum_id, '_bbp_forum_reply_count', true ) ) {
     347                        $topics  = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->topic_id . "';", $forum_id ) );
     348                        $replies = (int) !empty( $topics ) ? $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topics ) . " ) AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';" ) : 0;
     349                } else {
     350                        if ( 'publish' == bbp_get_reply_status( $reply_id ) )
     351                                $replies++;
     352                        else
     353                                $replies--;
     354                }
     355
     356        }
     357
     358        $total_replies = $replies + $children_reply_count;
     359
    300360        // Update the count
    301         update_post_meta( $forum_id, '_bbp_forum_reply_count', (int) $replies );
     361        update_post_meta( $forum_id, '_bbp_forum_reply_count',       $replies       );
     362        update_post_meta( $forum_id, '_bbp_forum_total_reply_count', $total_replies );
    302363
    303         return apply_filters( 'bbp_update_forum_reply_count', (int) $replies, $forum_id );
     364        if ( $parent = bbp_get_forum_parent( $forum_id ) )
     365                bbp_update_forum_reply_count( $parent );
     366
     367        return apply_filters( 'bbp_update_forum_reply_count', empty( $total_count ) ? $replies : $total_replies, $forum_id, $total_count );
    304368}
    305369
    306370/**
  • bbp-includes/bbp-forum-template.php

     
    4040        $r = wp_parse_args( $args, $default );
    4141
    4242        // Don't show private forums to normal users
    43         if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) {
    44                 $r['meta_key']     = '_bbp_forum_visibility';
    45                 $r['meta_value']   = 'public';
    46                 $r['meta_compare'] = '==';
     43        if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) {
     44                $r['meta_key']   = '_bbp_forum_visibility';
     45                $r['meta_value'] = 'public';
    4746        }
    4847
    4948        $bbp->forum_query = new WP_Query( $r );
     
    397396        $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
    398397
    399398        // Don't show private forums to normal users
    400         if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) {
    401                 $r['meta_key']     = '_bbp_forum_visibility';
    402                 $r['meta_value']   = 'public';
    403                 $r['meta_compare'] = '==';
     399        if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) {
     400                $r['meta_key']   = '_bbp_forum_visibility';
     401                $r['meta_value'] = 'public';
    404402        }
    405403
    406404        // No forum passed
     
    467465                        if ( !empty( $show_topic_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
    468466                                $topic_count = ' (' . bbp_get_forum_topic_count( $sub_forum->ID ) . ')';
    469467
    470                         //if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
    471                         //      $reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')';
     468                        if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
     469                                $reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')';
    472470
    473471                        $output .= $link_before . '<a href="' . $permalink . '" class="bbp-forum-link">' . $title . $topic_count . $reply_count . '</a>' . $show_sep . $link_after;
    474472                }
     
    866864 * @since bbPress (r2464)
    867865 *
    868866 * @param int $forum_id Optional. Forum id
     867 * @param bool $total_count Optional. To get the total count or normal count?
    869868 * @uses bbp_get_forum_topic_count() To get the forum topic count
    870869 */
    871 function bbp_forum_topic_count( $forum_id = 0 ) {
     870function bbp_forum_topic_count( $forum_id = 0, $total_count = true ) {
    872871        echo bbp_get_forum_topic_count( $forum_id );
    873872}
    874873        /**
     
    877876         * @since bbPress (r2464)
    878877         *
    879878         * @param int $forum_id Optional. Forum id
     879         * @param bool $total_count Optional. To get the total count or normal
     880         *                           count?
    880881         * @uses bbp_get_forum_id() To get the forum id
    881882         * @uses get_post_meta() To get the forum topic count
    882883         * @uses bbp_update_forum_topic_count() To update the topic count if
     
    885886         *                        topic count and forum id
    886887         * @return int Forum topic count
    887888         */
    888         function bbp_get_forum_topic_count( $forum_id = 0 ) {
     889        function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true ) {
    889890                $forum_id = bbp_get_forum_id( $forum_id );
    890                 $topics   = get_post_meta( $forum_id, '_bbp_forum_topic_count', true );
     891                $topics  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_topic_count' : '_bbp_forum_total_topic_count', true );
    891892
    892893                if ( '' === $topics )
    893                         $topics = bbp_update_forum_topic_count( $forum_id );
     894                        $topics = bbp_update_forum_topic_count( $forum_id, $total_count );
    894895
    895896                return apply_filters( 'bbp_get_forum_topic_count', (int) $topics, $forum_id );
    896897        }
     
    901902 * @since bbPress (r2464)
    902903 *
    903904 * @param int $forum_id Optional. Forum id
     905 * @param bool $total_count Optional. To get the total count or normal count?
    904906 * @uses bbp_get_forum_reply_count() To get the forum reply count
    905907 */
    906 function bbp_forum_reply_count( $forum_id = 0 ) {
    907         echo bbp_get_forum_reply_count( $forum_id );
     908function bbp_forum_reply_count( $forum_id = 0, $total_count = true ) {
     909        echo bbp_get_forum_reply_count( $forum_id, $total_count );
    908910}
    909911        /**
    910912         * Return total post count of a forum
     
    912914         * @since bbPress (r2464)
    913915         *
    914916         * @param int $forum_id Optional. Forum id
     917         * @param bool $total_count Optional. To get the total count or normal
     918         *                           count?
    915919         * @uses bbp_get_forum_id() To get the forum id
    916920         * @uses get_post_meta() To get the forum reply count
    917921         * @uses bbp_update_forum_reply_count() To update the reply count if
     
    920924         *                        reply count and forum id
    921925         * @return int Forum reply count
    922926         */
    923         function bbp_get_forum_reply_count( $forum_id = 0 ) {
     927        function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true ) {
    924928                $forum_id = bbp_get_forum_id( $forum_id );
    925                 $replies  = get_post_meta( $forum_id, '_bbp_forum_reply_count', true );
     929                $replies  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_reply_count' : '_bbp_forum_total_reply_count', true );
    926930
    927931                if ( '' === $replies )
    928                         $replies = bbp_update_forum_reply_count( $forum_id );
     932                        $replies = bbp_update_forum_reply_count( $forum_id, $total_count );
    929933
    930934                return apply_filters( 'bbp_get_forum_reply_count', (int) $replies, $forum_id );
    931935        }
  • bbp-includes/bbp-hooks.php

     
    152152add_action( 'untrashed_post',      'bbp_update_forum_reply_count' );
    153153add_action( 'deleted_post',        'bbp_update_forum_reply_count' );
    154154add_action( 'bbp_new_reply',       'bbp_update_forum_reply_count' );
    155 add_action( 'bbp_edit_relpy',      'bbp_update_forum_reply_count' );
     155add_action( 'bbp_edit_topic',      'bbp_update_forum_reply_count' );
    156156add_action( 'bbp_move_topic',      'bbp_update_forum_reply_count' );
    157157add_action( 'bbp_spammed_reply',   'bbp_update_forum_reply_count' );
    158158add_action( 'bbp_unspammed_reply', 'bbp_update_forum_reply_count' );
  • bbp-includes/bbp-reply-template.php

     
    489489                        $author = bbp_get_reply_author_link( array( 'link_text' => bbp_get_reply_author( $revision->ID ), 'reply_id' => $revision->ID ) );
    490490                        $since  = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
    491491
    492                         $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item" class="bbp-reply-revision-log-item">' . "\n";
     492                        $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item-' . $revision->ID . '" class="bbp-reply-revision-log-item">' . "\n";
    493493                                $r .= "\t\t" . sprintf( __( empty( $reason ) ? 'This reply was modified %1$s ago by %2$s.' : 'This reply was modified %1$s ago by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n";
    494494                        $r .= "\t" . '</li>' . "\n";
    495495
  • bbp-includes/bbp-topic-functions.php

     
    589589
    590590        // Forum Topic Counts
    591591        bbp_update_forum_topic_count( $source_topic_forum_id );
    592         bbp_update_forum_topic_count( $destination_topic_id  );
     592        // bbp_update_forum_topic_count( $destination_topic_id  );
    593593
    594594        // Forum Reply Counts
    595595        bbp_update_forum_reply_count( $source_topic_forum_id );
    596         bbp_update_forum_reply_count( $destination_topic_id  );
     596        // bbp_update_forum_reply_count( $destination_topic_id  );
    597597
    598598        // Forum Voice Counts
    599599        bbp_update_forum_voice_count( $source_topic_forum_id );
     
    807807function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
    808808
    809809        // Forum Topic Counts
    810         bbp_update_forum_topic_count( $source_topic_id      );
     810        // bbp_update_forum_topic_count( $source_topic_id      );
    811811        bbp_update_forum_topic_count( $destination_topic_id );
    812812
    813813        // Forum Reply Counts
    814         bbp_update_forum_reply_count( $source_topic_id      );
     814        // bbp_update_forum_reply_count( $source_topic_id      );
    815815        bbp_update_forum_reply_count( $destination_topic_id );
    816816
    817817        // Forum Voice Counts
  • bbp-includes/bbp-topic-template.php

     
    525525                        $author = bbp_get_topic_author_link( array( 'link_text' => bbp_get_topic_author( $revision->ID ), 'topic_id' => $revision->ID ) );
    526526                        $since  = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
    527527
    528                         $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item" class="bbp-topic-revision-log-item">' . "\n";
     528                        $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item-' . $revision->ID . '" class="bbp-topic-revision-log-item">' . "\n";
    529529                        $r .= "\t\t" . sprintf( __( empty( $reason ) ? 'This topic was modified %1$s ago by %2$s.' : 'This topic was modified %1$s ago by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n";
    530530                        $r .= "\t" . '</li>' . "\n";
    531531
  • bbp-themes/bbp-twentyten/css/bbpress.css

     
    339339/* =Stickies
    340340-------------------------------------------------------------- */
    341341
    342 tr.super-sticky td,
     342.bbp-topics-front tr.super-sticky td,
    343343.bbp-forum-info tr.sticky td {
    344344        background-color: #ffffe0 !important;
    345345        font-size: 1.1em;