Skip to:
Content

bbPress.org

Changeset 6522


Ignore:
Timestamp:
06/10/2017 11:08:57 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Engagements: Introduce 2 new functions for recalculating engagements.

  • bbp_get_topic_engagements_raw() to efficiently query for users who have engaged in a topic
  • bbp_recalculate_topic_engagements() to update all of the related meta-data

See #3068.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/engagements.php

    r6521 r6522  
    178178    // Filter & return
    179179    return (array) apply_filters( 'bbp_get_topic_engagements', $users, $topic_id );
     180}
     181
     182/**
     183 * Return the users who have engaged in a topic, directly with a database query
     184 *
     185 * See: https://bbpress.trac.wordpress.org/ticket/3083
     186 *
     187 * @since 2.6.0 bbPress (r6522)
     188 *
     189 * @param int $topic_id
     190 *
     191 * @return array
     192 */
     193function bbp_get_topic_engagements_raw( $topic_id = 0 ) {
     194
     195    // Default variables
     196    $topic_id = bbp_get_topic_id( $topic_id );
     197    $bbp_db   = bbp_db();
     198    $statii   = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'";
     199
     200    // A cool UNION query!
     201    $sql = "
     202SELECT DISTINCT( post_author ) FROM (
     203    SELECT post_author FROM {$bbp_db->posts}
     204        WHERE ( ID = %d AND post_status IN ({$statii}) AND post_type = %s )
     205UNION
     206    SELECT post_author FROM {$bbp_db->posts}
     207        WHERE ( post_parent = %d AND post_status = %s AND post_type = %s )
     208) as u1";
     209
     210    // Prepare & get results
     211    $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_topic_post_type(), $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type() );
     212    $results = $bbp_db->get_col( $query );
     213
     214    // Parse results into voices
     215    $engagements = ! is_wp_error( $results )
     216        ? wp_parse_id_list( array_filter( $results ) )
     217        : array();
     218
     219    // Filter & return
     220    return (array) apply_filters( 'bbp_get_topic_engagements_raw', $engagements, $topic_id );
    180221}
    181222
     
    217258 * @uses apply_filters() Calls 'bbp_get_user_engaged_topic_ids' with
    218259 *                        the engaged topics and user id
    219  * @return array|bool Results if user has engaged, otherwise null
     260 * @return array Topic ids if user has engaged, otherwise empty array
    220261 */
    221262function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
     
    310351 *               false
    311352 */
    312 function bbp_remove_user_engagement( $user_id, $topic_id ) {
     353function bbp_remove_user_engagement( $user_id = 0, $topic_id = 0 ) {
    313354
    314355    // Bail if not enough info
     
    330371
    331372    return true;
     373}
     374
     375/**
     376 * Recalculate all of the users who have engaged in a topic.
     377 *
     378 * You may need to do this when deleting a reply
     379 *
     380 * @since 2.6.0 bbPress (r6522)
     381 *
     382 * @param int $topic_id
     383 *
     384 * @return boolean True if any engagements are added, false otherwise
     385 */
     386function bbp_recalculate_topic_engagements( $topic_id = 0 ) {
     387
     388    // Default return value
     389    $retval = false;
     390
     391    // Bail if not enough info
     392    $topic_id = bbp_get_topic_id( $topic_id );
     393    if ( empty( $topic_id ) ) {
     394        return $retval;
     395    }
     396
     397    // Query for engagements
     398    $engagements = bbp_get_topic_engagements_raw( $topic_id );
     399
     400    // Delete all engagements
     401    bbp_remove_all_users_from_object( $topic_id, '_bbp_engagement' );
     402
     403    // Update the voice count for this topic id
     404    foreach ( $engagements as $user_id ) {
     405        $retval = bbp_add_user_engagement( $user_id, $topic_id );
     406    }
     407
     408    // Filter & return
     409    return (bool) apply_filters( 'bbp_recalculate_user_engagements', $retval, $topic_id );
    332410}
    333411
Note: See TracChangeset for help on using the changeset viewer.