Skip to:
Content

bbPress.org


Ignore:
Timestamp:
02/13/2011 11:31:27 AM (15 years ago)
Author:
johnjamesjacoby
Message:

First pass at forum, topic, and reply count routines. In this first iteration, counts are purposely overly sensitive resulting in more hits to the database than ultimately will be necessary in the final iteration. The process of recounting and realigning last_active_id and last_active_time is made difficult by being restricted to using the post_parent column for post relationships. Future versions may use syncopated taxonomies, mptt, or some other method to lighten this load. This first pass also relies heavily on the WordPress API wherever possible, and only uses custom queries in an attempt to limit memory usage. There me additional benefits from sub-queries and/or self joins. Would love to have more eyes on this specifically.

File:
1 edited

Legend:

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

    r2858 r2895  
    2525
    2626        // Update the last reply ID
    27         if ( !empty( $reply_id ) )
    28                 return update_post_meta( $reply_id, '_bbp_reply_forum_id', $forum_id );
    29 
    30         return false;
     27        return update_post_meta( $reply_id, '_bbp_reply_forum_id', (int) $forum_id );
     28
     29        return apply_filters( 'bbp_update_reply_forum_id', (int) $forum_id, $reply_id );
    3130}
    3231
     
    4847
    4948        // Update the last reply ID
    50         if ( !empty( $reply_id ) )
    51                 return update_post_meta( $reply_id, '_bbp_reply_topic_id', $topic_id );
    52 
    53         return false;
     49        update_post_meta( $reply_id, '_bbp_reply_topic_id', (int) $topic_id );
     50
     51        return apply_filters( 'bbp_update_reply_topic_id', (int) $topic_id, $reply_id );
    5452}
    5553
     
    286284                        if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
    287285
     286                                // Get reply parent ID's
     287                                $topic_id = bbp_get_reply_topic_id( $reply_id );
     288                                $forum_id = bbp_get_topic_forum_id( $topic_id );
     289
    288290                                // Update counts, etc...
    289                                 do_action( 'bbp_edit_reply', $reply_id, $reply->post_parent, bbp_get_topic_forum_id( $reply->post_parent ), $anonymous_data, $reply->post_author , true /* Is edit */ );
     291                                do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply->post_author , true /* Is edit */ );
    290292
    291293                                // Redirect back to new reply
     
    326328 * @uses bbp_remove_user_subscription() To remove the user's subscription
    327329 * @uses bbp_add_user_subscription() To add the user's subscription
    328  * @uses bbp_update_topic_last_active() To update the last active topic meta
    329  * @uses bbp_update_forum_last_active() To update the last active forum meta
    330  * @uses bbp_update_topic_last_reply_id() To update the last reply id topic meta
    331  * @uses bbp_update_forum_last_topic_id() To update the last topic id forum meta
    332  * @uses bbp_update_forum_last_reply_id() To update the last reply id forum meta
    333  */
    334 function bbp_reply_updater( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
     330 */
     331function bbp_update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
    335332        // Validate the ID's passed from 'bbp_new_reply' action
    336333        $reply_id = bbp_get_reply_id( $reply_id );
     
    381378        if ( empty( $is_edit ) ) {
    382379                // Last active time
    383                 $last_active = current_time( 'mysql' );
     380                $last_active_time = current_time( 'mysql' );
    384381
    385382                // Reply meta relating to reply position in tree
    386                 bbp_update_reply_forum_id   ( $reply_id, $forum_id );
    387                 bbp_update_reply_topic_id   ( $reply_id, $topic_id );
    388 
    389                 global $bbp;
    390 
    391                 foreach ( get_post_ancestors( $reply_id ) as $ancestor ) {
    392                         // Topic meta relating to most recent reply
    393                         if ( bbp_get_topic_post_type() == get_post_field( 'post_type', $ancestor ) ) {
    394                                 bbp_update_topic_last_reply_id( $ancestor, $reply_id    );
    395                                 bbp_update_topic_last_active  ( $ancestor, $last_active );
    396                                 bbp_update_topic_reply_count  ( $ancestor               );
    397                                 bbp_update_topic_voice_count  ( $ancestor               );
    398 
    399                         // Forum meta relating to most recent topic
    400                         } elseif ( bbp_get_forum_post_type() == get_post_field( 'post_type', $ancestor ) ) {
    401                                 bbp_update_forum_last_topic_id( $ancestor, $topic_id    );
    402                                 bbp_update_forum_last_reply_id( $ancestor, $reply_id    );
    403                                 bbp_update_forum_last_active  ( $ancestor, $last_active );
    404                                 bbp_update_forum_reply_count  ( $ancestor               );
    405                                 bbp_update_forum_voice_count  ( $ancestor               );
    406                         }
     383                bbp_update_reply_forum_id( $reply_id, $forum_id );
     384                bbp_update_reply_topic_id( $reply_id, $topic_id );
     385
     386                // Walk up ancestors and do the dirty work
     387                bbp_update_reply_walker( $reply_id, $last_active_time, $forum_id, $topic_id, false );
     388        }
     389}
     390
     391/**
     392 * Walk up the ancestor tree from the current reply, and update all the counts
     393 *
     394 * @since bbPress (r2884)
     395 *
     396 * @param int $reply_id
     397 *
     398 * @uses bbp_update_topic_last_active_time() To update the last active topic meta
     399 * @uses bbp_update_forum_last_active_time() To update the last active forum meta
     400 * @uses bbp_update_topic_last_reply_id() To update the last reply id topic meta
     401 * @uses bbp_update_forum_last_topic_id() To update the last topic id forum meta
     402 * @uses bbp_update_forum_last_reply_id() To update the last reply id forum meta
     403 */
     404function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 0, $topic_id = 0, $refresh = true ) {
     405
     406        // Verifiy the reply ID
     407        if ( $reply_id = bbp_get_reply_id ( $reply_id ) ) {
     408
     409                // Get the topic ID if none was passed
     410                if ( empty( $topic_id ) )
     411                        $topic_id = bbp_get_reply_topic_id ( $reply_id );
     412
     413                // Get the forum ID if none was passed
     414                if ( empty( $forum_id ) )
     415                        $forum_id = bbp_get_reply_forum_id ( $reply_id );
     416        }
     417
     418        // Set the active_id based on topic_id/reply_id
     419        $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
     420
     421        // Setup ancestors array to walk up
     422        $ancestors = array_values( array_unique( array_merge( array( $topic_id, $forum_id ), get_post_ancestors( $topic_id ) ) ) );
     423
     424        // If we want a full refresh, unset any of the possibly passed variables
     425        if ( true == $refresh )
     426                $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
     427
     428        // Walk up ancestors
     429        foreach ( $ancestors as $ancestor ) {
     430
     431                // Reply meta relating to most recent reply
     432                if ( bbp_get_reply_id( $ancestor ) ) {
     433                        // @todo - hierarchical replies
     434
     435                // Topic meta relating to most recent reply
     436                } elseif ( bbp_get_topic_id( $ancestor ) ) {
     437
     438                        // Last reply and active ID's
     439                        bbp_update_topic_last_reply_id( $ancestor, $reply_id );
     440                        bbp_update_topic_last_active_id ( $ancestor, $active_id );
     441
     442                        // Get the last active time if none was passed
     443                        if ( empty( $last_active_time ) )
     444                                $topic_last_active_time = get_post_field( 'post_date', bbp_get_topic_last_active_id( $ancestor ) );
     445                        else
     446                                $topic_last_active_time = $last_active_time;
     447
     448                        bbp_update_topic_last_active_time   ( $ancestor, $topic_last_active_time );
     449
     450                        // Counts
     451                        bbp_update_topic_voice_count        ( $ancestor );
     452                        bbp_update_topic_reply_count        ( $ancestor );
     453                        bbp_update_topic_hidden_reply_count ( $ancestor );
     454
     455                // Forum meta relating to most recent topic
     456                } elseif ( bbp_get_forum_id( $ancestor ) ) {
     457
     458                        // Last topic and reply ID's
     459                        bbp_update_forum_last_topic_id ( $ancestor, $topic_id );
     460                        bbp_update_forum_last_reply_id ( $ancestor, $reply_id );
     461
     462                        // Last Active
     463                        bbp_update_forum_last_active_id ( $ancestor, $active_id );
     464
     465                        if ( empty( $last_active_time ) )
     466                                $forum_last_active_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $ancestor ) );
     467                        else
     468                                $forum_last_active_time = $last_active_time;
     469
     470                        bbp_update_forum_last_active_time ( $ancestor, $forum_last_active_time );
     471
     472                        // Counts
     473                        bbp_update_forum_reply_count ( $ancestor );
    407474                }
    408475        }
     
    506573
    507574                // Check for errors
    508                 if ( false != $success && !is_wp_error( $success ) ) {
     575                if ( ( false != $success ) && !is_wp_error( $success ) ) {
    509576
    510577                        // Redirect back to the reply
     
    599666}
    600667
     668/** Before Delete/Trash/Untrash ***********************************************/
     669
     670function bbp_delete_reply( $reply_id = 0 ) {
     671        $reply_id = bbp_get_reply_id( $reply_id );
     672
     673        if ( empty( $reply_id ) )
     674                return false;
     675
     676        do_action( 'bbp_delete_reply', $reply_id );
     677}
     678
     679function bbp_trash_reply( $reply_id = 0 ) {
     680        $reply_id = bbp_get_reply_id( $reply_id );
     681
     682        if ( empty( $reply_id ) )
     683                return false;
     684
     685        do_action( 'bbp_trash_reply', $reply_id );
     686}
     687
     688function bbp_untrash_reply( $reply_id = 0 ) {
     689        $reply_id = bbp_get_reply_id( $reply_id );
     690
     691        if ( empty( $reply_id ) )
     692                return false;
     693
     694        do_action( 'bbp_untrash_reply', $reply_id );
     695}
     696
     697/** After Delete/Trash/Untrash ************************************************/
     698
     699function bbp_deleted_reply( $reply_id = 0 ) {
     700        $reply_id = bbp_get_reply_id( $reply_id );
     701
     702        if ( empty( $reply_id ) )
     703                return false;
     704
     705        do_action( 'bbp_deleted_reply', $reply_id );
     706}
     707
     708function bbp_trashed_reply( $reply_id = 0 ) {
     709        $reply_id = bbp_get_reply_id( $reply_id );
     710
     711        if ( empty( $reply_id ) )
     712                return false;
     713
     714        do_action( 'bbp_trashed_reply', $reply_id );
     715}
     716
     717function bbp_untrashed_reply( $reply_id = 0 ) {
     718        $reply_id = bbp_get_reply_id( $reply_id );
     719
     720        if ( empty( $reply_id ) )
     721                return false;
     722
     723        do_action( 'bbp_untrashed_reply', $reply_id );
     724}
     725
     726
    601727?>
Note: See TracChangeset for help on using the changeset viewer.