Skip to:
Content

bbPress.org


Ignore:
Timestamp:
02/26/2017 11:12:37 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Engagements: Updates existing functions & unit tests:

  • Correct tests so that post_author of 0 does not get included in the overall count
  • Add user IDs to all topics & replies where voice counts are being tested
  • Update voice-count update function to use the new user-relationships API
  • Clean-up topic merge code to more efficiently migrate favorites, subscriptions, and engagements

See #3068.

File:
1 edited

Legend:

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

    r6318 r6330  
    12821282    }
    12831283
     1284    /** Engagements ***********************************************************/
     1285
     1286    // Get engagements from source topic
     1287    $engagements = bbp_get_topic_engagements( $source_topic->ID );
     1288
     1289    // Maybe migrate engagements
     1290    if ( ! empty( $engagements ) ) {
     1291        foreach ( $engagements as $engager ) {
     1292            bbp_add_user_engagement( $engager, $destination_topic->ID );
     1293        }
     1294    }
     1295
    12841296    /** Subscriptions *********************************************************/
    12851297
     
    12871299    $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
    12881300
    1289     // Remove the topic from everybody's subscriptions
    1290     if ( ! empty( $subscribers ) ) {
    1291 
    1292         // Loop through each user
    1293         foreach ( (array) $subscribers as $subscriber ) {
    1294 
    1295             // Shift the subscriber if told to
    1296             if ( ! empty( $_POST['bbp_topic_subscribers'] ) && ( "1" === $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() ) {
    1297                 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
    1298             }
    1299 
    1300             // Remove old subscription
    1301             bbp_remove_user_subscription( $subscriber, $source_topic->ID );
     1301    // Maybe migrate subscriptions
     1302    if ( ! empty( $subscribers ) && ! empty( $_POST['bbp_topic_subscribers'] ) && ( '1' === $_POST['bbp_topic_subscribers'] ) ) {
     1303        foreach ( $subscribers as $subscriber ) {
     1304            bbp_add_user_subscription( $subscriber, $destination_topic->ID );
    13021305        }
    13031306    }
     
    13081311    $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
    13091312
    1310     // Remove the topic from everybody's favorites
    1311     if ( ! empty( $favoriters ) ) {
    1312 
    1313         // Loop through each user
    1314         foreach ( (array) $favoriters as $favoriter ) {
    1315 
    1316             // Shift the favoriter if told to
    1317             if ( ! empty( $_POST['bbp_topic_favoriters'] ) && "1" === $_POST['bbp_topic_favoriters'] ) {
    1318                 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
    1319             }
    1320 
    1321             // Remove old favorite
    1322             bbp_remove_user_favorite( $favoriter, $source_topic->ID );
     1313    // Maybe migrate favorites
     1314    if ( ! empty( $favoriters ) && ! empty( $_POST['bbp_topic_favoriters'] ) && ( '1' === $_POST['bbp_topic_favoriters'] ) ) {
     1315        foreach ( $favoriters as $favoriter ) {
     1316            bbp_add_user_favorite( $favoriter, $destination_topic->ID );
    13231317        }
    13241318    }
     
    13561350    delete_post_meta( $source_topic->ID, '_bbp_reply_count'        );
    13571351    delete_post_meta( $source_topic->ID, '_bbp_reply_count_hidden' );
     1352
     1353    // Delete source topics user relationships
     1354    delete_post_meta( $source_topic->ID, '_bbp_favorite'     );
     1355    delete_post_meta( $source_topic->ID, '_bbp_subscription' );
     1356    delete_post_meta( $source_topic->ID, '_bbp_engagement'   );
    13581357
    13591358    // Get the replies of the source topic
     
    29662965    }
    29672966
     2967    // Delete all engagements
     2968    delete_post_meta( $topic_id, '_bbp_engagement' );
     2969
    29682970    // Query the DB to get voices in this topic
    2969     $bbp_db = bbp_db();
    2970     $query  = $bbp_db->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
    2971     $voices = (int) $bbp_db->get_var( $query );
     2971    $bbp_db  = bbp_db();
     2972    $sql     = "SELECT DISTINCT post_author FROM {$bbp_db->posts} WHERE ( post_parent = %d AND post_status = %s AND post_type = %s ) OR ( ID = %d AND post_type = %s )";
     2973    $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() );
     2974    $results = $bbp_db->get_col( $query );
     2975
     2976    // Parse results into voices
     2977    $voices  = ! is_wp_error( $results )
     2978        ? wp_parse_id_list( array_filter( $results ) )
     2979        : array();
    29722980
    29732981    // Update the voice count for this topic id
    2974     update_post_meta( $topic_id, '_bbp_voice_count', $voices );
    2975 
    2976     return (int) apply_filters( 'bbp_update_topic_voice_count', $voices, $topic_id );
     2982    foreach ( $voices as $user_id ) {
     2983        bbp_add_user_engagement( $user_id, $topic_id );
     2984    }
     2985
     2986    // Get the count
     2987    $count = count( $voices );
     2988
     2989    // Update the voice count for this topic id
     2990    update_post_meta( $topic_id, '_bbp_voice_count', $count );
     2991
     2992    return (int) apply_filters( 'bbp_update_topic_voice_count', $count, $topic_id );
    29772993}
    29782994
Note: See TracChangeset for help on using the changeset viewer.