Skip to:
Content

bbPress.org


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.