Changeset 6529 for trunk/src/includes/users/engagements.php
- Timestamp:
- 06/12/2017 05:41:49 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/users/engagements.php
r6525 r6529 22 22 * @param string $meta_key The relationship key 23 23 * @param string $meta_type The relationship type (usually 'post') 24 * @param bool $unique Whether meta datashould be unique to the object24 * @param bool $unique Whether meta key should be unique to the object 25 25 * 26 26 * @uses add_metadata() To add the user to an object … … 28 28 * @return bool Returns true on success, false on failure 29 29 */ 30 function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = true ) {30 function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post', $unique = false ) { 31 31 $retval = add_metadata( $meta_type, $object_id, $meta_key, $user_id, $unique ); 32 32 … … 375 375 * Recalculate all of the users who have engaged in a topic. 376 376 * 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. 378 382 * 379 383 * @since 2.6.0 bbPress (r6522) 380 384 * 381 * @param int $topic_id 385 * @param int $topic_id 386 * @param bool $force 382 387 * 383 388 * @return boolean True if any engagements are added, false otherwise 384 389 */ 385 function bbp_recalculate_topic_engagements( $topic_id = 0 ) {390 function bbp_recalculate_topic_engagements( $topic_id = 0, $force = false ) { 386 391 387 392 // Default return value 388 393 $retval = false; 389 394 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 392 401 if ( empty( $topic_id ) ) { 393 402 return $retval; … … 395 404 396 405 // 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 } 405 423 } 406 424 407 425 // Filter & return 408 426 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 */ 439 function 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 ); 409 454 } 410 455
Note: See TracChangeset
for help on using the changeset viewer.