Skip to:
Content

bbPress.org

Changeset 3501


Ignore:
Timestamp:
09/09/2011 09:43:33 PM (14 years ago)
Author:
johnjamesjacoby
Message:

Fix issue when topics and replies are flagged as spam/trash where they would still update last active data for ancestors. Fixes #1631.
Fix issue when trashing and deleting a topic, replies may not be properly queried and updated. Fixes #1629.

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

Legend:

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

    r3445 r3501  
    387387    $forum_id = bbp_get_forum_id( $forum_id );
    388388
     389    // Define local variable(s)
     390    $children_last_topic = 0;
     391
    389392    // Do some calculation if not manually set
    390393    if ( empty( $topic_id ) ) {
    391394
    392395        // Loop through children and add together forum reply counts
    393         if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
    394             foreach ( (array) $children as $child )
    395                 $children_last_topic = bbp_update_forum_last_topic_id ( $child );
     396        if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) {
     397            foreach ( (array) $children as $child ) {
     398                $children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive
     399            }
     400        }
    396401
    397402        // Setup recent topic query vars
     
    405410
    406411        // Get the most recent topic in this forum_id
    407         if ( $recent_topic = get_posts( $post_vars ) )
     412        if ( $recent_topic = get_posts( $post_vars ) ) {
    408413            $topic_id = $recent_topic[0]->ID;
    409     }
     414        }
     415    }
     416
     417    // Cast as integer in case of empty or string
     418    $topic_id            = (int) $topic_id;
     419    $children_last_topic = (int) $children_last_topic;
    410420
    411421    // If child forums have higher id, use that instead
     
    413423        $topic_id = $children_last_topic;
    414424
    415     // Update the last topic id
    416     update_post_meta( $forum_id, '_bbp_last_topic_id', (int) $topic_id );
    417 
    418     return apply_filters( 'bbp_update_forum_last_topic_id', (int) $topic_id, $forum_id );
     425    // Update the last public topic ID
     426    if ( bbp_is_topic_published( $topic_id ) )
     427        update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id );
     428
     429    return apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id );
    419430}
    420431
     
    432443 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
    433444 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
     445 * @uses bbp_is_reply_published() To make sure the reply is published
    434446 * @uses update_post_meta() To update the forum's last active id meta
    435447 * @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last
     
    440452    $forum_id = bbp_get_forum_id( $forum_id );
    441453
     454    // Define local variable(s)
     455    $children_last_reply = 0;
     456
    442457    // Do some calculation if not manually set
    443458    if ( empty( $reply_id ) ) {
    444459
    445460        // Loop through children and get the most recent reply id
    446         if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
    447             foreach ( (array) $children as $child )
    448                 $children_last_reply = bbp_update_forum_last_reply_id ( $child );
     461        if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) {
     462            foreach ( (array) $children as $child ) {
     463                $children_last_reply = bbp_update_forum_last_reply_id( $child ); // Recursive
     464            }
     465        }
    449466
    450467        // If this forum has topics...
     
    459476    }
    460477
     478    // Cast as integer in case of empty or string
     479    $reply_id            = (int) $reply_id;
     480    $children_last_reply = (int) $children_last_reply;
     481   
    461482    // If child forums have higher ID, check for newer reply id
    462     if ( !empty( $children ) && ( (int) $children_last_reply > (int) $reply_id ) )
     483    if ( !empty( $children ) && ( $children_last_reply > $reply_id ) )
    463484        $reply_id = $children_last_reply;
    464485
    465     // Update the last reply id with what was passed
    466     update_post_meta( $forum_id, '_bbp_last_reply_id', (int) $reply_id );
    467 
    468     return apply_filters( 'bbp_update_forum_last_reply_id', (int) $reply_id, $forum_id );
     486    // Update the last public reply ID
     487    if ( bbp_is_reply_published( $reply_id ) )
     488        update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id );
     489
     490    return apply_filters( 'bbp_update_forum_last_reply_id', $reply_id, $forum_id );
    469491}
    470492
     
    482504 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
    483505 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
     506 * @uses get_post_status() To make sure the reply is published
    484507 * @uses update_post_meta() To update the forum's last active id meta
    485508 * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last
     
    490513    $forum_id = bbp_get_forum_id( $forum_id );
    491514
     515    // Define local variable(s)
     516    $children_last_active = 0;
     517
    492518    // Do some calculation if not manually set
    493519    if ( empty( $active_id ) ) {
     
    509535    }
    510536
     537    // Cast as integer in case of empty or string
     538    $active_id            = (int) $active_id;
     539    $children_last_active = (int) $children_last_active;
     540
    511541    // If child forums have higher id, use that instead
    512542    if ( !empty( $children ) && ( $children_last_active > $active_id ) )
    513543        $active_id = $children_last_active;
    514544
    515     update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id );
     545    // Update only if published
     546    if ( 'publish' == get_post_status( $active_id ) )
     547        update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id );
    516548
    517549    return apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id );
     
    540572        $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) );
    541573
    542     update_post_meta( $forum_id, '_bbp_last_active_time', $new_time );
     574    // Update only if there is a time
     575    if ( !empty( $new_time ) )
     576        update_post_meta( $forum_id, '_bbp_last_active_time', $new_time );
    543577
    544578    return apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id );
     
    589623
    590624    // Loop through subforums and add together forum topic counts
    591     if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
    592         foreach ( (array) $children as $child )
    593             $children_topic_count += bbp_update_forum_topic_count( $child );
     625    if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) {
     626        foreach ( (array) $children as $child ) {
     627            $children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive
     628        }
     629    }
    594630
    595631    // Get total topics for this forum
     
    726762function bbp_update_forum( $args = '' ) {
    727763    $defaults = array(
    728         'forum_id'         => 0,
    729         'post_parent'      => 0,
    730         'last_topic_id'    => 0,
    731         'last_reply_id'    => 0,
    732         'last_active_id'   => 0,
    733         'last_active_time' => 0,
     764        'forum_id'           => 0,
     765        'post_parent'        => 0,
     766        'last_topic_id'      => 0,
     767        'last_reply_id'      => 0,
     768        'last_active_id'     => 0,
     769        'last_active_time'   => 0,
     770        'last_active_status' => 'publish'
    734771    );
    735772
     
    748785        $last_active_time = get_post_field( 'post_date', $last_active_id );
    749786
    750     bbp_update_forum_last_active_time( $forum_id, $last_active_time );
     787    if ( 'publish' == $last_active_status ) {
     788        bbp_update_forum_last_active_time( $forum_id, $last_active_time );
     789    }
    751790
    752791    // Counts
  • branches/plugin/bbp-includes/bbp-reply-functions.php

    r3468 r3501  
    610610
    611611        // Set transient for throttle check (only on new, not edit)
    612         if ( empty( $is_edit ) )
     612        if ( empty( $is_edit ) ) {
    613613            set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
     614        }
    614615
    615616        // Website is optional
    616         if ( !empty( $bbp_anonymous_website ) )
     617        if ( !empty( $bbp_anonymous_website ) ) {
    617618            update_post_meta( $reply_id, '_bbp_anonymous_website', $bbp_anonymous_website, false );
     619        }
    618620
    619621    } else {
    620         if ( empty( $is_edit ) && !current_user_can( 'throttle' ) )
     622        if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
    621623            update_user_meta( $author_id, '_bbp_last_posted', time() );
     624        }
    622625    }
    623626
     
    628631
    629632        // Subscribed and unsubscribing
    630         if ( true == $subscribed && false == $subscheck )
     633        if ( true == $subscribed && false == $subscheck ) {
    631634            bbp_remove_user_subscription( $author_id, $topic_id );
    632635
    633636        // Subscribing
    634         elseif ( false == $subscribed && true == $subscheck )
     637        } elseif ( false == $subscribed && true == $subscheck ) {
    635638            bbp_add_user_subscription( $author_id, $topic_id );
     639        }
    636640    }
    637641
     
    728732
    729733            // Last reply and active ID's
    730             bbp_update_topic_last_reply_id  ( $ancestor, $reply_id  );
    731             bbp_update_topic_last_active_id ( $ancestor, $active_id );
     734            bbp_update_topic_last_reply_id ( $ancestor, $reply_id  );
     735            bbp_update_topic_last_active_id( $ancestor, $active_id );
    732736
    733737            // Get the last active time if none was passed
    734             if ( empty( $last_active_time ) )
     738            $topic_last_active_time = $last_active_time;
     739            if ( empty( $last_active_time ) ) {
    735740                $topic_last_active_time = get_post_field( 'post_date', bbp_get_topic_last_active_id( $ancestor ) );
    736             else
    737                 $topic_last_active_time = $last_active_time;
    738 
    739             bbp_update_topic_last_active_time  ( $ancestor, $topic_last_active_time );
     741            }
     742
     743            // Only update if reply is published
     744            if ( bbp_is_reply_published( $reply_id ) ) {
     745                bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
     746            }
    740747
    741748            // Counts
     
    754761            bbp_update_forum_last_active_id( $ancestor, $active_id );
    755762
    756             if ( empty( $last_active_time ) )
     763            // Get the last active time if none was passed
     764            $forum_last_active_time = $last_active_time;
     765            if ( empty( $last_active_time ) ) {
    757766                $forum_last_active_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $ancestor ) );
    758             else
    759                 $forum_last_active_time = $last_active_time;
    760 
    761             bbp_update_forum_last_active_time( $ancestor, $forum_last_active_time );
     767            }
     768
     769            // Only update if reply is published
     770            if ( bbp_is_reply_published( $reply_id ) ) {
     771                bbp_update_forum_last_active_time( $ancestor, $forum_last_active_time );
     772            }
    762773
    763774            // Counts
  • branches/plugin/bbp-includes/bbp-reply-template.php

    r3492 r3501  
    697697
    698698/**
     699 * Is the reply not spam or deleted?
     700 *
     701 * @since bbPress (r3496)
     702 *
     703 * @param int $reply_id Optional. Topic id
     704 * @uses bbp_get_reply_id() To get the reply id
     705 * @uses bbp_get_reply_status() To get the reply status
     706 * @return bool True if published, false if not.
     707 */
     708function bbp_is_reply_published( $reply_id = 0 ) {
     709    global $bbp;
     710
     711    $reply_status = bbp_get_reply_status( bbp_get_reply_id( $reply_id ) );
     712    return apply_filters( 'bbp_is_reply_published', 'publish' == $reply_status, $reply_id );
     713}
     714
     715/**
    699716 * Is the reply marked as spam?
    700717 *
  • branches/plugin/bbp-includes/bbp-topic-functions.php

    r3489 r3501  
    7474    $forum_id = bbp_get_topic_forum_id( $topic_id );
    7575    if ( !empty( $forum_id ) )
    76         bbp_update_forum( $forum_id );
     76        bbp_update_forum( array( 'forum_id' => $forum_id ) );
    7777
    7878    // Return new topic ID
     
    339339
    340340            // Get the topic URL
    341             $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
     341            $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
    342342
    343343            // Add view all?
    344             if ( bbp_get_view_all() || ( current_user_can( 'moderate' ) && !empty( $view_all ) ) )
    345                 $topic_url = bbp_add_view_all( $topic_url );
     344            if ( bbp_get_view_all() || !empty( $view_all ) ) {
     345
     346                // User can moderate, so redirect to topic with view all set
     347                if ( current_user_can( 'moderate' ) ) {
     348                    $redirect_url = bbp_add_view_all( $redirect_url );
     349
     350                // User cannot moderate, so redirect to forum
     351                } else {
     352                    $redirect_url = bbp_get_forum_permalink( $forum_id );
     353                }
     354            }
    346355
    347356            // Allow to be filtered
    348             $topic_url = apply_filters( 'bbp_new_topic_redirect_to', $topic_url, $redirect_to );
     357            $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to );
    349358
    350359            /** Successful Save ***********************************************/
    351360
    352361            // Redirect back to new topic
    353             wp_safe_redirect( $topic_url );
     362            wp_safe_redirect( $redirect_url );
    354363
    355364            // For good measure
     
    709718
    710719        // Set transient for throttle check (only on new, not edit)
    711         if ( empty( $is_edit ) )
     720        if ( empty( $is_edit ) ) {
    712721            set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
     722        }
    713723
    714724        // Website is optional
    715         if ( !empty( $bbp_anonymous_website ) )
     725        if ( !empty( $bbp_anonymous_website ) ) {
    716726            update_post_meta( $topic_id, '_bbp_anonymous_website', $bbp_anonymous_website, false );
     727        }
    717728    } else {
    718         if ( empty( $is_edit ) && !current_user_can( 'throttle' ) )
     729        if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
    719730            update_user_meta( $author_id, '_bbp_last_posted', time() );
     731        }
    720732    }
    721733
     
    726738
    727739        // Subscribed and unsubscribing
    728         if ( true == $subscribed && false == $subscheck )
     740        if ( true == $subscribed && false == $subscheck ) {
    729741            bbp_remove_user_subscription( $author_id, $topic_id );
    730742
    731743        // Subscribing
    732         elseif ( false == $subscribed && true == $subscheck )
     744        } elseif ( false == $subscribed && true == $subscheck ) {
    733745            bbp_add_user_subscription( $author_id, $topic_id );
     746        }
    734747    }
    735748
     
    798811    $ancestors = array_values( array_unique( array_merge( array( $forum_id ), get_post_ancestors( $topic_id ) ) ) );
    799812
     813    // Topic status
     814    $topic_status = get_post_status( $topic_id );
     815
    800816    // If we want a full refresh, unset any of the possibly passed variables
    801     if ( true == $refresh )
     817    if ( true == $refresh ) {
    802818        $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
     819        $topic_status = 'publish';
     820    }
    803821
    804822    // Loop through ancestors
     
    810828            // Update the forum
    811829            bbp_update_forum( array(
    812                 'forum_id'         => $ancestor,
    813                 'last_topic_id'    => $topic_id,
    814                 'last_reply_id'    => $reply_id,
    815                 'last_active_id'   => $active_id,
    816                 'last_active_time' => 0,
     830                'forum_id'           => $ancestor,
     831                'last_topic_id'      => $topic_id,
     832                'last_reply_id'      => $reply_id,
     833                'last_active_id'     => $active_id,
     834                'last_active_time'   => 0,
     835                'last_active_status' => $topic_status
    817836            ) );
    818837        }
     
    11501169        /** Successful Merge **************************************************/
    11511170
     1171        // Update topic's last meta data
     1172        bbp_update_topic_last_reply_id   ( $destination_topic->ID );
     1173        bbp_update_topic_last_active_id  ( $destination_topic->ID );
     1174        bbp_update_topic_last_active_time( $destination_topic->ID );
     1175
    11521176        // Send the post parent of the source topic as it has been shifted
    11531177        // (possibly to a new forum) so we need to update the counts of the
     
    21482172        $active_id = $topic_id;
    21492173
    2150     update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
     2174    // Update only if published
     2175    if ( 'publish' == get_post_status( $active_id ) )
     2176        update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
    21512177
    21522178    return apply_filters( 'bbp_update_topic_last_active_id', (int) $active_id, $topic_id );
     
    21782204        $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
    21792205
    2180     update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
     2206    // Update only if published
     2207    if ( !empty( $new_time ) )
     2208        update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
    21812209
    21822210    return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
     
    22192247        $reply_id = 0;
    22202248
    2221     update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
     2249    // Update if reply is published
     2250    if ( bbp_is_reply_published( $reply_id ) )
     2251        update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
    22222252
    22232253    return apply_filters( 'bbp_update_topic_last_reply_id', (int) $reply_id, $topic_id );
     
    26672697
    26682698/**
    2669  * Called before deleting a topic
     2699 * Called before deleting a topic.
     2700 *
     2701 * This function is supplemental to the actual topic deletion which is
     2702 * handled by WordPress core API functions. It is used to clean up after
     2703 * a topic that is being deleted.
    26702704 *
    26712705 * @uses bbp_get_topic_id() To get the topic id
     
    26792713 */
    26802714function bbp_delete_topic( $topic_id = 0 ) {
     2715    global $bbp;
     2716
     2717    // Validate topic ID
    26812718    $topic_id = bbp_get_topic_id( $topic_id );
    26822719
     
    26862723    do_action( 'bbp_delete_topic', $topic_id );
    26872724
     2725    // Valid topic/reply statuses
     2726    $post_stati = join( ',', array( 'publish', $bbp->spam_status_id, 'trash' ) );
     2727
    26882728    // Topic is being permanently deleted, so its replies gotta go too
    2689     if ( bbp_has_replies( array( 'post_parent' => $topic_id, 'post_status' => 'publish', 'posts_per_page' => -1 ) ) ) {
     2729    if ( bbp_has_replies( array(
     2730        'post_type'      => bbp_get_reply_post_type(),
     2731        'post_status'    => $post_stati,
     2732        'posts_per_page' => -1,
     2733        'meta_query'     => array( array(
     2734            'key'        => '_bbp_topic_id',
     2735            'value'      => $topic_id,
     2736            'compare'    => '='
     2737        ) )
     2738    ) ) ) {
    26902739        while ( bbp_replies() ) {
    26912740            bbp_the_reply();
     
    26982747 * Called before trashing a topic
    26992748 *
     2749 * This function is supplemental to the actual topic being trashed which is
     2750 * handled by WordPress core API functions. It is used to clean up after
     2751 * a topic that is being trashed.
     2752 *
    27002753 * @uses bbp_get_topic_id() To get the topic id
    27012754 * @uses bbp_is_topic() To check if the passed id is a topic
     
    27092762 */
    27102763function bbp_trash_topic( $topic_id = 0 ) {
     2764    global $bbp;
     2765
    27112766    $topic_id = bbp_get_topic_id( $topic_id );
    27122767
     
    27172772
    27182773    // Topic is being trashed, so its replies are trashed too
    2719     if ( bbp_has_replies( array( 'post_parent' => $topic_id, 'post_status' => 'publish', 'posts_per_page' => -1 ) ) ) {
    2720         global $bbp;
     2774    if ( bbp_has_replies( array(
     2775        'post_type'      => bbp_get_reply_post_type(),
     2776        'post_status'    => 'publish',
     2777        'posts_per_page' => -1,
     2778        'meta_query'     => array( array(
     2779            'key'        => '_bbp_topic_id',
     2780            'value'      => $topic_id,
     2781            'compare'    => '='
     2782        ) )
     2783    ) ) ) {
    27212784
    27222785        // Prevent debug notices
  • branches/plugin/bbp-includes/bbp-topic-template.php

    r3496 r3501  
    956956
    957957/**
     958 * Is the topic not spam or deleted?
     959 *
     960 * @since bbPress (r3496)
     961 *
     962 * @param int $topic_id Optional. Topic id
     963 * @uses bbp_get_topic_id() To get the topic id
     964 * @uses bbp_get_topic_status() To get the topic status
     965 * @return bool True if published, false if not.
     966 */
     967function bbp_is_topic_published( $topic_id = 0 ) {
     968    global $bbp;
     969
     970    $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) );
     971    return 'publish' == $topic_status;
     972}
     973
     974/**
    958975 * Is the topic marked as spam?
    959976 *
Note: See TracChangeset for help on using the changeset viewer.