Skip to:
Content

bbPress.org

Changeset 2831


Ignore:
Timestamp:
02/04/2011 04:27:36 AM (15 years ago)
Author:
johnjamesjacoby
Message:

When topics are deleted, recalculate the associated forum last active time. This includes possible subforums and walks up through parent forums setting the correct time.

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

Legend:

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

    r2818 r2831  
    193193 * @param int $forum_id Optional. Forum id
    194194 * @param string $new_time Optional. New time in mysql format
    195  * @uses bbp_get_forum_id() To get the forum id
    196  * @uses current_time() To get the current time
    197  * @uses update_post_meta() To update the forum's last active meta
     195 * @uses bbp_forum_has_subforums() Get sub forums
     196 * @uses bbp_get_topic_forum_id() Get forum_id from possible topic_id
     197 * @uses get_posts() Get topics from the forum_id
     198 * @uses bbp_get_forum_id() Get the forum id
     199 * @uses current_time() Get the current time
     200 * @uses get_post_meta() Get the last active times of topics and forums
     201 * @uses update_post_meta() Update the forum's last active meta
     202 * @uses delete_post_meta() Delete last active meta if no topics exist
    198203 * @return bool True on success, false on failure
    199204 */
    200205function bbp_update_forum_last_active( $forum_id = 0, $new_time = '' ) {
    201     $forum_id = bbp_get_forum_id( $forum_id );
    202 
    203     // Check time and use current if empty
    204     if ( empty( $new_time ) )
    205         $new_time = current_time( 'mysql' );
    206 
    207     // Update last active
    208     if ( !empty( $forum_id ) )
    209         return update_post_meta( $forum_id, '_bbp_forum_last_active', $new_time );
    210 
    211     return false;
     206    global $wpdb, $bbp;
     207
     208    $forum_id = bbp_get_forum_id( $forum_id );
     209    $sub_forum_time = $topic_time = $calculated_time = '';
     210
     211    // If it's a topic, then get the parent (forum id)
     212    if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) ) {
     213        $topic_id = $forum_id;
     214        $forum_id = bbp_get_topic_forum_id( $forum_id );
     215    }
     216
     217    // No time was passed, so we need to do some calculating
     218    if ( empty( $new_time ) ) {
     219
     220        // If forum has sub forums, loop through them and get the last active time
     221        if ( $sub_forums = bbp_forum_has_subforums( $forum_id ) ) {
     222
     223            // Loop through sub forums
     224            foreach( $sub_forums as $sub_forum ) {
     225
     226                // Get the sub forum last active time
     227                $sub_forum_temp_time = get_post_meta( $sub_forum->ID, '_bbp_forum_last_active', true );
     228
     229                // Compare this sub forum time to the most recent, and assign to
     230                // $sub_forum_time if it's more recent than the last
     231                if ( strtotime( $sub_forum_temp_time ) > strtotime( $sub_forum_time ) ) {
     232                    $sub_forum_time = $sub_forum_temp_time;
     233                }
     234            }
     235        }
     236
     237        // Load the most recent topic in this forum_id based on
     238        // the '_bbp_topic_last_active' post_meta value
     239        if ( $topics = get_posts( array( 'numberposts' => 1, 'post_parent' => $forum_id, 'post_type' => $bbp->topic_id, 'meta_key' => '_bbp_topic_last_active', 'orderby' => 'meta_value' ) ) )
     240            $topic_time = get_post_meta( $topics[0]->ID, '_bbp_topic_last_active', true );
     241
     242        // Calculate a new time
     243        if ( strtotime( $topic_time ) > strtotime( $sub_forum_time ) )
     244            $calculated_time = $topic_time;
     245        else
     246            $calculated_time = $sub_forum_time;
     247
     248    // Specific time was passed, so skip calculations
     249    } else {
     250        $calculated_time = $new_time;
     251    }
     252
     253    // No forums or topics in this forum_id, so delete the meta entries
     254    if ( empty( $calculated_time ) ) {
     255        delete_post_meta( $forum_id, '_bbp_forum_last_active'   );
     256        delete_post_meta( $forum_id, '_bbp_forum_last_topic_id' );
     257
     258    // Update the forum last active time
     259    } else
     260        update_post_meta( $forum_id, '_bbp_forum_last_active', $calculated_time );
     261
     262    // Walk up ancestors
     263    if ( $parent_id = bbp_get_forum_parent( $forum_id ) )
     264        bbp_update_forum_last_active( $parent_id );
     265
     266    return apply_filters( 'bbp_update_forum_last_active', $calculated_time, $forum_id );
    212267}
    213268
  • branches/plugin/bbp-includes/bbp-forum-template.php

    r2818 r2831  
    918918     * @param int $forum_id Optional. Forum id
    919919     * @param bool $total_count Optional. To get the total count or normal
    920      *                           count?
     920     *                           count? Defaults to total.
    921921     * @uses bbp_get_forum_id() To get the forum id
    922922     * @uses get_post_meta() To get the forum topic count
  • branches/plugin/bbp-includes/bbp-hooks.php

    r2827 r2831  
    138138add_action( 'trash_post',  'bbp_unstick_topic' );
    139139add_action( 'delete_post', 'bbp_unstick_topic' );
     140
     141// Update forum last active
     142add_action( 'trashed_post',        'bbp_update_forum_last_active' );
     143add_action( 'untrashed_post',      'bbp_update_forum_last_active' );
     144add_action( 'deleted_post',        'bbp_update_forum_last_active' );
    140145
    141146// Update forum topic counts
Note: See TracChangeset for help on using the changeset viewer.