Skip to:
Content

bbPress.org

Changeset 2806


Ignore:
Timestamp:
01/14/2011 07:22:08 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Adjust topic and reply counts up the subforum tree. Fixes #1320. Props GautamGupta

Location:
branches/plugin/bbp-includes
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-forum-functions.php

    r2790 r2806  
    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 );
    256 
    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 ) ) );
     260    }
     261
     262    $topics   = $children_topic_count = 0;
     263    $children = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) );
     264
     265    // Loop through children and add together forum topic counts
     266    foreach ( (array) $children as $child )
     267        $children_topic_count += (int) bbp_get_forum_topic_count( $child->ID );
     268
     269    // Don't count topics if the forum is a category
     270    if ( !bbp_is_forum_category( $forum_id ) ) {
     271        if ( empty( $topic_id ) || !$topics = (int) get_post_meta( $forum_id, '_bbp_forum_topic_count', true ) ) {
     272            $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 ) );
     273        } else {
     274            if ( in_array( bbp_get_topic_status( $topic_id ), array( 'publish', $bbp->closed_status_id ) ) ) {
     275                $topics++;
     276            } else {
     277                $topics--;
     278            }
     279        }
     280    }
     281
     282    // Calculate total topics in this forum
     283    $total_topics = $topics + $children_topic_count;
    259284
    260285    // Update the count
    261     update_post_meta( $forum_id, '_bbp_forum_topic_count', (int) $topics );
    262 
    263     return apply_filters( 'bbp_update_forum_topic_count', (int) $topics, $forum_id );
     286    update_post_meta( $forum_id, '_bbp_forum_topic_count',       $topics       );
     287    update_post_meta( $forum_id, '_bbp_forum_total_topic_count', $total_topics );
     288
     289    // Walk up ancestors
     290    if ( $parent_id = bbp_get_forum_parent( $forum_id ) )
     291        bbp_update_forum_topic_count( $parent_id );
     292
     293    return apply_filters( 'bbp_update_forum_topic_count', empty( $total_count ) ? $topics : $total_topics, $forum_id, $total_count );
    264294}
    265295
     
    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 );
     334
     335    $replies  = $children_reply_count = 0;
     336    $children = get_posts( array( 'post_parent' => $forum_id, 'post_type' => $bbp->forum_id, 'meta_key' => '_bbp_forum_visibility', 'meta_value' => 'public' ) );
     337
     338    // Loop through children and add together forum reply counts
     339    foreach ( (array) $children as $child )
     340        $children_reply_count += (int) bbp_get_forum_reply_count( $child->ID );
     341
     342    // Don't count replies if the forum is a category
     343    if ( !bbp_is_forum_category( $forum_id ) ) {
     344        if ( empty( $reply_id ) || !$replies = (int) get_post_meta( $forum_id, '_bbp_forum_reply_count', true ) ) {
     345            $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 ) );
     346            $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;
     347        } else {
     348            if ( 'publish' == bbp_get_reply_status( $reply_id ) ) {
     349                $replies++;
     350            } else {
     351                $replies--;
     352            }
     353        }
     354    }
     355
     356    // Calculate total replies in this forum
     357    $total_replies = $replies + $children_reply_count;
    299358
    300359    // Update the count
    301     update_post_meta( $forum_id, '_bbp_forum_reply_count', (int) $replies );
    302 
    303     return apply_filters( 'bbp_update_forum_reply_count', (int) $replies, $forum_id );
     360    update_post_meta( $forum_id, '_bbp_forum_reply_count',       $replies       );
     361    update_post_meta( $forum_id, '_bbp_forum_total_reply_count', $total_replies );
     362
     363    // Walk up ancestors
     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
  • branches/plugin/bbp-includes/bbp-forum-template.php

    r2795 r2806  
    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
     
    435434
    436435    // Don't show private forums to normal users
    437     if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) && empty( $r['meta_compare'] ) ) {
    438         $r['meta_key']     = '_bbp_forum_visibility';
    439         $r['meta_value']   = 'public';
    440         $r['meta_compare'] = '==';
     436    if ( !current_user_can( 'read_private_forums' ) && empty( $r['meta_key'] ) && empty( $r['meta_value'] ) ) {
     437        $r['meta_key']   = '_bbp_forum_visibility';
     438        $r['meta_value'] = 'public';
    441439    }
    442440
     
    505503                $topic_count = ' (' . bbp_get_forum_topic_count( $sub_forum->ID ) . ')';
    506504
    507             //if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
    508             //  $reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')';
     505            if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
     506                $reply_count = ' (' . bbp_get_forum_reply_count( $sub_forum->ID ) . ')';
    509507
    510508            $output .= $link_before . '<a href="' . $permalink . '" class="bbp-forum-link">' . $title . $topic_count . $reply_count . '</a>' . $show_sep . $link_after;
     
    904902 *
    905903 * @param int $forum_id Optional. Forum id
     904 * @param bool $total_count Optional. To get the total count or normal count?
    906905 * @uses bbp_get_forum_topic_count() To get the forum topic count
    907906 */
    908 function bbp_forum_topic_count( $forum_id = 0 ) {
     907function bbp_forum_topic_count( $forum_id = 0, $total_count = true ) {
    909908    echo bbp_get_forum_topic_count( $forum_id );
    910909}
     
    915914     *
    916915     * @param int $forum_id Optional. Forum id
     916     * @param bool $total_count Optional. To get the total count or normal
     917     *                           count?
    917918     * @uses bbp_get_forum_id() To get the forum id
    918919     * @uses get_post_meta() To get the forum topic count
     
    923924     * @return int Forum topic count
    924925     */
    925     function bbp_get_forum_topic_count( $forum_id = 0 ) {
    926         $forum_id = bbp_get_forum_id( $forum_id );
    927         $topics   = get_post_meta( $forum_id, '_bbp_forum_topic_count', true );
     926    function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true ) {
     927        $forum_id = bbp_get_forum_id( $forum_id );
     928        $topics  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_topic_count' : '_bbp_forum_total_topic_count', true );
    928929
    929930        if ( '' === $topics )
    930             $topics = bbp_update_forum_topic_count( $forum_id );
     931            $topics = bbp_update_forum_topic_count( $forum_id, $total_count );
    931932
    932933        return apply_filters( 'bbp_get_forum_topic_count', (int) $topics, $forum_id );
     
    939940 *
    940941 * @param int $forum_id Optional. Forum id
     942 * @param bool $total_count Optional. To get the total count or normal count?
    941943 * @uses bbp_get_forum_reply_count() To get the forum reply count
    942944 */
    943 function bbp_forum_reply_count( $forum_id = 0 ) {
    944     echo bbp_get_forum_reply_count( $forum_id );
     945function bbp_forum_reply_count( $forum_id = 0, $total_count = true ) {
     946    echo bbp_get_forum_reply_count( $forum_id, $total_count );
    945947}
    946948    /**
     
    950952     *
    951953     * @param int $forum_id Optional. Forum id
     954     * @param bool $total_count Optional. To get the total count or normal
     955     *                           count?
    952956     * @uses bbp_get_forum_id() To get the forum id
    953957     * @uses get_post_meta() To get the forum reply count
     
    958962     * @return int Forum reply count
    959963     */
    960     function bbp_get_forum_reply_count( $forum_id = 0 ) {
    961         $forum_id = bbp_get_forum_id( $forum_id );
    962         $replies  = get_post_meta( $forum_id, '_bbp_forum_reply_count', true );
     964    function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true ) {
     965        $forum_id = bbp_get_forum_id( $forum_id );
     966        $replies  = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_reply_count' : '_bbp_forum_total_reply_count', true );
    963967
    964968        if ( '' === $replies )
    965             $replies = bbp_update_forum_reply_count( $forum_id );
     969            $replies = bbp_update_forum_reply_count( $forum_id, $total_count );
    966970
    967971        return apply_filters( 'bbp_get_forum_reply_count', (int) $replies, $forum_id );
  • branches/plugin/bbp-includes/bbp-hooks.php

    r2789 r2806  
    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' );
  • branches/plugin/bbp-includes/bbp-reply-template.php

    r2796 r2806  
    494494            $since  = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
    495495
    496             $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item" class="bbp-reply-revision-log-item">' . "\n";
     496            $r .= "\t" . '<li id="bbp-reply-revision-log-' . $reply_id . '-item-' . $revision->ID . '" class="bbp-reply-revision-log-item">' . "\n";
    497497                $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";
    498498            $r .= "\t" . '</li>' . "\n";
     
    871871
    872872        if ( empty( $link_text ) ) {
    873             if ( bbp_is_topic() || bbp_is_reply() )
     873            if ( bbp_is_topic() || bbp_is_reply() ) {
    874874                $link_text = bbp_get_reply_author_avatar( $reply_id, 80 );
    875             else
     875            } else {
    876876                $link_text = bbp_get_reply_author( $reply_id );
     877            }
    877878        }
    878879
  • branches/plugin/bbp-includes/bbp-topic-functions.php

    r2803 r2806  
    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
     
    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
  • branches/plugin/bbp-includes/bbp-topic-template.php

    r2796 r2806  
    530530            $since  = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
    531531
    532             $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item" class="bbp-topic-revision-log-item">' . "\n";
     532            $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item-' . $revision->ID . '" class="bbp-topic-revision-log-item">' . "\n";
    533533            $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";
    534534            $r .= "\t" . '</li>' . "\n";
Note: See TracChangeset for help on using the changeset viewer.