bbp_update_topic_walker

Walks up the post_parent tree from the current topic_id, and updates the counts of forums above it.

Description

bbp_update_topic_walker( (int) $topic_id, (string) $last_active_time = '', (int) $forum_id = 0, (int) $reply_id = 0, (bool) $refresh = true ); 

This calls a few internal functions that all run manual queries against the database to get their results. As such, this function can be costly to run but is necessary to keep everything accurate.

Parameters (5)

0. $topic_id (int)
The topic id.
1. $last_active_time — Optional. (string) => ''
Last active time
2. $forum_id — Optional. (int)
Forum id
3. $reply_id — Optional. (int)
Reply id
4. $refresh — Optional. (bool) => true
Reset all the previous parameters? Defaults to true.

Usage

  1. if ( !function_exists( 'bbp_update_topic_walker' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/topics/functions.php'; 
  3.  
  4. // The topic id. 
  5. $topic_id = -1; 
  6.  
  7. // Optional. Last active time 
  8. $last_active_time = ''; 
  9.  
  10. // Optional. Forum id 
  11. $forum_id = -1; 
  12.  
  13. // Optional. Reply id 
  14. $reply_id = -1; 
  15.  
  16. // Reset all the previous parameters? Defaults to true. 
  17. $refresh = true; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = bbp_update_topic_walker($topic_id, $last_active_time, $forum_id, $reply_id, $refresh); 
  21.  

Defined (1)

The function is defined in the following location(s).

/includes/topics/functions.php  
  1. function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) { 
  2.  
  3. // Validate topic_id 
  4. $topic_id = bbp_get_topic_id( $topic_id ); 
  5.  
  6. // Define local variable(s) 
  7. $active_id = 0; 
  8.  
  9. // Topic was passed 
  10. if ( !empty( $topic_id ) ) { 
  11.  
  12. // Get the forum ID if none was passed 
  13. if ( empty( $forum_id ) ) { 
  14. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  15.  
  16. // Set the active_id based on topic_id/reply_id 
  17. $active_id = empty( $reply_id ) ? $topic_id : $reply_id; 
  18.  
  19. // Get topic ancestors 
  20. $ancestors = array_values( array_unique( array_merge( array( $forum_id ), (array) get_post_ancestors( $topic_id ) ) ) ); 
  21.  
  22. // Topic status 
  23. $topic_status = get_post_status( $topic_id ); 
  24.  
  25. // If we want a full refresh, unset any of the possibly passed variables 
  26. if ( true === $refresh ) { 
  27. $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0; 
  28. $topic_status = bbp_get_public_status_id(); 
  29.  
  30. // Loop through ancestors 
  31. if ( !empty( $ancestors ) ) { 
  32. foreach ( $ancestors as $ancestor ) { 
  33.  
  34. // If ancestor is a forum, update counts 
  35. if ( bbp_is_forum( $ancestor ) ) { 
  36.  
  37. // Update the forum 
  38. bbp_update_forum( array( 
  39. 'forum_id' => $ancestor,  
  40. 'last_topic_id' => $topic_id,  
  41. 'last_reply_id' => $reply_id,  
  42. 'last_active_id' => $active_id,  
  43. 'last_active_time' => 0,  
  44. 'last_active_status' => $topic_status 
  45. ) );