Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/10/2017 07:25:42 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Engagements: Update bbp_update_topic_voice_count() to only count the number of existing engagements.

Recalculating the entire engagement tree for each topic is costly, should be done in its own dedicated function, and then only hooked in or executed when absolutely necessary.

This is easier to do with the engagements API that's new in 2.6. Recalculation function imminent.

See #3068.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/topics/functions.php

    r6504 r6516  
    29652965 *
    29662966 * @since 2.0.0 bbPress (r2567)
     2967 * @since 2.6.0 bbPress (r6515) This must be called after any engagement changes
    29672968 *
    29682969 * @param int $topic_id Optional. Topic id to update
     
    29822983function bbp_update_topic_voice_count( $topic_id = 0 ) {
    29832984
    2984     // If it's a reply, then get the parent (topic id)
    2985     if ( bbp_is_reply( $topic_id ) ) {
    2986         $topic_id = bbp_get_reply_topic_id( $topic_id );
    2987     } elseif ( bbp_is_topic( $topic_id ) ) {
    2988         $topic_id = bbp_get_topic_id( $topic_id );
    2989     } else {
    2990         return;
    2991     }
    2992 
    2993     // Query the DB to get voices in this topic
    2994     // See: https://bbpress.trac.wordpress.org/ticket/3083
    2995     $bbp_db  = bbp_db();
    2996     $sql     = "SELECT DISTINCT( post_author ) FROM (
    2997                     SELECT post_author FROM {$bbp_db->posts}
    2998                         WHERE ( ID = %d AND post_type = %s )
    2999                     UNION
    3000                     SELECT post_author FROM {$bbp_db->posts}
    3001                         WHERE ( post_parent = %d AND post_status = %s AND post_type = %s )
    3002                 ) as u1";
    3003     $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_topic_post_type(), $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type() );
    3004     $results = $bbp_db->get_col( $query );
    3005 
    3006     // Parse results into voices
    3007     $new_voices  = ! is_wp_error( $results )
    3008         ? wp_parse_id_list( array_filter( $results ) )
    3009         : array();
    3010 
    30112985    // Get the old voices
    3012     $old_voices = bbp_get_topic_engagements( $topic_id );
    3013 
    3014     // Get the count
    3015     $new_count = count( $new_voices );
    3016     $old_count = count( $old_voices );
    3017 
    3018     // Only recalculate if count is different
    3019     if ( $new_count !== $old_count ) {
    3020 
    3021         // Delete all engagements
    3022         delete_post_meta( $topic_id, '_bbp_engagement' );
    3023 
    3024         // Update the voice count for this topic id
    3025         foreach ( $new_voices as $user_id ) {
    3026             bbp_add_user_engagement( $user_id, $topic_id );
    3027         }
    3028     }
     2986    $count = count( bbp_get_topic_engagements( $topic_id ) );
    30292987
    30302988    // Update the voice count for this topic id
    3031     update_post_meta( $topic_id, '_bbp_voice_count', $new_count );
     2989    update_post_meta( $topic_id, '_bbp_voice_count', $count );
    30322990
    30332991    // Filter & return
    3034     return (int) apply_filters( 'bbp_update_topic_voice_count', $new_count, $topic_id );
     2992    return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
    30352993}
    30362994
Note: See TracChangeset for help on using the changeset viewer.