Skip to:
Content

bbPress.org

Changeset 6529


Ignore:
Timestamp:
06/12/2017 05:41:49 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Engagements: Keep topic engagement counts up-to-date.

This changes the way the old 'voice count' works with the new engagements API in the following ways:

  • Introduce new function to update voice count meta when necessary
  • Modify recalculation function with a $force parameter, and only recalculate the counts when the engagements have changed by default
  • Hook the above functions in where appropriate, largely when deleting replies (this is a unique case where a fully deleted reply needs to check if the author of the current reply has a previously public reply in that same topic already, which should not trigger a recalculation.)
  • We are silently moving the bbp_delete_ actions onto the before_delete_post hook, so that all term & meta-data is available to them (which helps with race conditions mentioned in r6528)

Engagements are now hooked in and listening in the most efficient way possible, at least until a common & shared walker is introduced for the entire tree.

See: #3068.

Location:
trunk/src/includes
Files:
6 edited

Legend:

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

    r6497 r6529  
    7878
    7979/**
    80  * If current user can and is vewing all topics/replies
     80 * If current user can and is viewing all topics/replies
    8181 *
    8282 * @since 2.0.0 bbPress (r3325)
  • trunk/src/includes/core/actions.php

    r6369 r6529  
    155155
    156156// Before Delete/Trash/Untrash Forum
    157 add_action( 'wp_trash_post', 'bbp_trash_forum'   );
    158 add_action( 'trash_post',    'bbp_trash_forum'   );
    159 add_action( 'untrash_post',  'bbp_untrash_forum' );
    160 add_action( 'delete_post',  'bbp_delete_forum'  );
     157add_action( 'wp_trash_post',      'bbp_trash_forum'   );
     158add_action( 'trash_post',         'bbp_trash_forum'   );
     159add_action( 'untrash_post',       'bbp_untrash_forum' );
     160add_action( 'before_delete_post', 'bbp_delete_forum'  );
    161161
    162162// After Deleted/Trashed/Untrashed Forum
     
    184184
    185185// Before Delete/Trash/Untrash Reply
    186 add_action( 'wp_trash_post', 'bbp_trash_reply'   );
    187 add_action( 'trash_post',    'bbp_trash_reply'   );
    188 add_action( 'untrash_post',  'bbp_untrash_reply' );
    189 add_action( 'delete_post',  'bbp_delete_reply'  );
     186add_action( 'wp_trash_post',      'bbp_trash_reply'   );
     187add_action( 'trash_post',         'bbp_trash_reply'   );
     188add_action( 'untrash_post',       'bbp_untrash_reply' );
     189add_action( 'before_delete_post', 'bbp_delete_reply'  );
    190190
    191191// After Deleted/Trashed/Untrashed Reply
     
    206206
    207207// Before Delete/Trash/Untrash Topic
    208 add_action( 'wp_trash_post', 'bbp_trash_topic'   );
    209 add_action( 'trash_post',    'bbp_trash_topic'   );
    210 add_action( 'untrash_post',  'bbp_untrash_topic' );
    211 add_action( 'delete_post',  'bbp_delete_topic'  );
     208add_action( 'wp_trash_post',      'bbp_trash_topic'   );
     209add_action( 'trash_post',         'bbp_trash_topic'   );
     210add_action( 'untrash_post',       'bbp_untrash_topic' );
     211add_action( 'before_delete_post', 'bbp_delete_topic'  );
    212212
    213213// After Deleted/Trashed/Untrashed Topic
     
    293293add_action( 'bbp_unapproved_reply', 'bbp_decrease_topic_reply_count'        );
    294294add_action( 'bbp_unapproved_reply', 'bbp_increase_topic_reply_count_hidden' );
     295add_action( 'bbp_deleted_reply',    'bbp_decrease_topic_reply_count_hidden' );
    295296
    296297// Users topic & reply counts.
     
    310311add_action( 'bbp_insert_reply', 'bbp_insert_reply_update_counts', 10, 3 );
    311312
    312 // Update topic voice counts.
     313// Update engagements.
     314add_action( 'bbp_new_topic', 'bbp_update_topic_engagements' );
     315add_action( 'bbp_new_reply', 'bbp_update_topic_engagements' );
     316
     317// Recalculate engagements.
     318add_action( 'bbp_deleted_topic', 'bbp_recalculate_topic_engagements' );
     319add_action( 'bbp_deleted_reply', 'bbp_recalculate_topic_engagements' );
     320
     321// Update engagement counts.
    313322add_action( 'bbp_new_reply',        'bbp_update_topic_voice_count' );
    314323add_action( 'bbp_trashed_reply',    'bbp_update_topic_voice_count' );
     
    318327add_action( 'bbp_approved_reply',   'bbp_update_topic_voice_count' );
    319328add_action( 'bbp_unapproved_reply', 'bbp_update_topic_voice_count' );
     329add_action( 'bbp_deleted_reply',    'bbp_update_topic_voice_count' );
    320330
    321331// Insert reply voice counts.
  • trunk/src/includes/forums/functions.php

    r6499 r6529  
    19901990
    19911991    // Only update topic count if we're deleting a topic, or in the dashboard.
    1992     if ( in_array( current_filter(), array( 'bbp_deleted_topic', 'save_post' ), true ) ) {
     1992    if ( in_array( current_filter(), array( 'bbp_delete_topic', 'save_post' ), true ) ) {
    19931993        bbp_update_forum_reply_count(        $r['forum_id'] );
    19941994        bbp_update_forum_topic_count(        $r['forum_id'] );
     
    27712771 * Called after deleting a forum
    27722772 *
     2773 * Try not to use this action. All meta & taxonomy terms have already been
     2774 * deleted, making them impossible to use.
     2775 *
    27732776 * @since 2.1.0 bbPress (r3668)
     2777 * @since 2.6.0 bbPress (r6526) Not recommend for usage
    27742778 *
    27752779 * @uses bbp_get_forum_id() To get the forum id
  • trunk/src/includes/replies/functions.php

    r6526 r6529  
    964964        if ( empty( $topic_id ) ) {
    965965            $topic_id = bbp_get_reply_topic_id( $reply_id );
    966 
    967             // Make every effort to get topic id
    968             // https://bbpress.trac.wordpress.org/ticket/2529
    969             if ( empty( $topic_id ) && ( current_filter() === 'bbp_deleted_reply' ) ) {
    970                 $topic_id = get_post_field( 'post_parent', $reply_id );
    971             }
    972966        }
    973967
     
    20652059 * Called after deleting a reply
    20662060 *
     2061 * @since 2.0.0 bbPress (r2993)
     2062 *
    20672063 * @uses bbp_get_reply_id() To get the reply id
    20682064 * @uses bbp_is_reply() To check if the passed id is a reply
     
    20822078 * Called after trashing a reply
    20832079 *
     2080 * @since 2.0.0 bbPress (r2993)
     2081 *
    20842082 * @uses bbp_get_reply_id() To get the reply id
    20852083 * @uses bbp_is_reply() To check if the passed id is a reply
     
    20982096/**
    20992097 * Called after untrashing (restoring) a reply
     2098 *
     2099 * @since 2.0.0 bbPress (r2993)
    21002100 *
    21012101 * @uses bbp_get_reply_id() To get the reply id
  • trunk/src/includes/topics/functions.php

    r6516 r6529  
    944944        if ( empty( $forum_id )  ) {
    945945            $forum_id = bbp_get_topic_forum_id( $topic_id );
    946 
    947             // Make every effort to get forum id
    948             // https://bbpress.trac.wordpress.org/ticket/2529
    949             if ( empty( $forum_id ) && ( current_filter() === 'bbp_deleted_topic' ) ) {
    950                 $forum_id = get_post_field( 'post_parent', $topic_id );
    951             }
    952946        }
    953947
     
    29832977function bbp_update_topic_voice_count( $topic_id = 0 ) {
    29842978
    2985     // Get the old voices
     2979    // If it's a reply, then get the parent (topic id)
     2980    $topic_id = bbp_is_reply( $topic_id )
     2981        ? bbp_get_reply_topic_id( $topic_id )
     2982        : bbp_get_topic_id( $topic_id );
     2983
     2984    // Bail if no topic ID
     2985    if ( empty( $topic_id ) ) {
     2986        return;
     2987    }
     2988
     2989    // Count the engagements
    29862990    $count = count( bbp_get_topic_engagements( $topic_id ) );
    29872991
     
    38743878 * Called after deleting a topic
    38753879 *
     3880 * @since 2.0.0 bbPress (r2993)
     3881 *
    38763882 * @uses bbp_get_topic_id() To get the topic id
    38773883 * @uses bbp_is_topic() To check if the passed id is a topic
     
    38913897 * Called after trashing a topic
    38923898 *
     3899 * @since 2.0.0 bbPress (r2993)
     3900 *
    38933901 * @uses bbp_get_topic_id() To get the topic id
    38943902 * @uses bbp_is_topic() To check if the passed id is a topic
     
    39073915/**
    39083916 * Called after untrashing a topic
     3917 *
     3918 * @since 2.0.0 bbPress (r2993)
    39093919 *
    39103920 * @uses bbp_get_topic_id() To get the topic id
  • trunk/src/includes/users/engagements.php

    r6525 r6529  
    2222 * @param string $meta_key  The relationship key
    2323 * @param string $meta_type The relationship type (usually 'post')
    24  * @param bool   $unique    Whether metadata should be unique to the object
     24 * @param bool   $unique    Whether meta key should be unique to the object
    2525 *
    2626 * @uses add_metadata() To add the user to an object
     
    2828 * @return bool Returns true on success, false on failure
    2929 */
    30 function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = true ) {
     30function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) {
    3131    $retval = add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique );
    3232
     
    375375 * Recalculate all of the users who have engaged in a topic.
    376376 *
    377  * You may need to do this when deleting a reply
     377 * This happens when permanently deleting a reply, because that reply author may
     378 * have authored other replies to that same topic, or the topic itself.
     379 *
     380 * You may need to do this manually on heavily active forums where engagement
     381 * count accuracy is important.
    378382 *
    379383 * @since 2.6.0 bbPress (r6522)
    380384 *
    381  * @param int $topic_id
     385 * @param int  $topic_id
     386 * @param bool $force
    382387 *
    383388 * @return boolean True if any engagements are added, false otherwise
    384389 */
    385 function bbp_recalculate_topic_engagements( $topic_id = 0 ) {
     390function bbp_recalculate_topic_engagements( $topic_id = 0, $force = false ) {
    386391
    387392    // Default return value
    388393    $retval = false;
    389394
    390     // Bail if not enough info
    391     $topic_id = bbp_get_topic_id( $topic_id );
     395    // Check post type
     396    $topic_id = bbp_is_reply( $topic_id )
     397        ? bbp_get_reply_topic_id( $topic_id )
     398        : bbp_get_topic_id( $topic_id );
     399
     400    // Bail if no topic ID
    392401    if ( empty( $topic_id ) ) {
    393402        return $retval;
     
    395404
    396405    // Query for engagements
    397     $engagements = bbp_get_topic_engagements_raw( $topic_id );
    398 
    399     // Delete all engagements
    400     bbp_remove_all_users_from_object( $topic_id, '_bbp_engagement' );
    401 
    402     // Update the voice count for this topic id
    403     foreach ( $engagements as $user_id ) {
    404         $retval = bbp_add_user_engagement( $user_id, $topic_id );
     406    $old_engagements = bbp_get_topic_engagements( $topic_id );
     407    $new_engagements = bbp_get_topic_engagements_raw( $topic_id );
     408
     409    // Sort arrays
     410    sort( $old_engagements, SORT_NUMERIC );
     411    sort( $new_engagements, SORT_NUMERIC );
     412
     413    // Only recalculate on change
     414    if ( ( true === $force ) || ( $old_engagements !== $new_engagements ) ) {
     415
     416        // Delete all engagements
     417        bbp_remove_all_users_from_object( $topic_id, '_bbp_engagement' );
     418
     419        // Update the voice count for this topic id
     420        foreach ( $new_engagements as $user_id ) {
     421            $retval = bbp_add_user_engagement( $user_id, $topic_id );
     422        }
    405423    }
    406424
    407425    // Filter & return
    408426    return (bool) apply_filters( 'bbp_recalculate_user_engagements', $retval, $topic_id );
     427}
     428
     429/**
     430 * Update the engagements of a topic.
     431 *
     432 * Hooked to 'bbp_new_topic' and 'bbp_new_reply', this gets the post author and
     433 * if not anonymous, passes it into bbp_add_user_engagement().
     434 *
     435 * @since 2.6.0 bbPress (r6526)
     436 *
     437 * @param int $topic_id
     438 */
     439function bbp_update_topic_engagements( $topic_id = 0 ) {
     440
     441    // Check post type
     442    if ( bbp_is_reply( $topic_id ) ) {
     443        $author_id = bbp_get_reply_author_id( $topic_id );
     444        $topic_id  = bbp_get_reply_topic_id( $topic_id );
     445    } elseif ( bbp_is_topic( $topic_id ) ) {
     446        $author_id = bbp_get_topic_author_id( $topic_id );
     447        $topic_id  = bbp_get_topic_id( $topic_id );
     448    } else {
     449        return;
     450    }
     451
     452    // Return whether engagement was added
     453    return bbp_add_user_engagement( $author_id, $topic_id );
    409454}
    410455
Note: See TracChangeset for help on using the changeset viewer.