bp_forums_get_topic_extras

Fetch BP-specific details for an array of topics.

Description

(array) bp_forums_get_topic_extras( (array) $topics ); 

Done in one fell swoop to reduce query overhead. Currently determines the following: - details about the last poster - information about topic users that may have been deleted/spammed

Returns (array)

$topics Topics with BP details added.

Parameters (1)

0. $topics (array)
Array of topics.

Usage

  1. if ( !function_exists( 'bp_forums_get_topic_extras' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bp-forums-functions.php'; 
  3.  
  4. // Array of topics. 
  5. $topics = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_forums_get_topic_extras($topics); 
  9.  

Defined (1)

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

/bp-forums/bp-forums-functions.php  
  1. function bp_forums_get_topic_extras( $topics ) { 
  2. global $wpdb, $bbdb; 
  3.  
  4. if ( empty( $topics ) ) 
  5. return $topics; 
  6.  
  7. $bp =buddypress); 
  8.  
  9. // Get the topic ids. 
  10. foreach ( (array) $topics as $topic ) $topic_ids[] = $topic->topic_id; 
  11. $topic_ids = implode( ', ', wp_parse_id_list( $topic_ids ) ); 
  12.  
  13. // Fetch the topic's last poster details. 
  14. $poster_details = $wpdb->get_results( "SELECT t.topic_id, t.topic_last_poster, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u, {$bbdb->topics} t WHERE u.ID = t.topic_last_poster AND t.topic_id IN ( {$topic_ids} )" ); 
  15. for ( $i = 0, $count = count( $topics ); $i < $count; ++$i ) { 
  16. foreach ( (array) $poster_details as $poster ) { 
  17. if ( $poster->topic_id == $topics[$i]->topic_id ) { 
  18. $topics[$i]->topic_last_poster_email = $poster->user_email; 
  19. $topics[$i]->topic_last_poster_nicename = $poster->user_nicename; 
  20. $topics[$i]->topic_last_poster_login = $poster->user_login; 
  21. $topics[$i]->topic_last_poster_displayname = $poster->display_name; 
  22.  
  23. // Fetch fullname for the topic's last poster. 
  24. if ( bp_is_active( 'xprofile' ) ) { 
  25. $poster_names = $wpdb->get_results( "SELECT t.topic_id, pd.value FROM {$bp->profile->table_name_data} pd, {$bbdb->topics} t WHERE pd.user_id = t.topic_last_poster AND pd.field_id = 1 AND t.topic_id IN ( {$topic_ids} )" ); 
  26. for ( $i = 0, $count = count( $topics ); $i < $count; ++$i ) { 
  27. foreach ( (array) $poster_names as $name ) { 
  28. if ( $name->topic_id == $topics[$i]->topic_id ) 
  29. $topics[$i]->topic_last_poster_displayname = $name->value; 
  30.  
  31. // Loop through to make sure that each topic has the proper values set. This covers the 
  32. // case of deleted users. 
  33. foreach ( (array) $topics as $key => $topic ) { 
  34. if ( !isset( $topic->topic_last_poster_email ) ) 
  35. $topics[$key]->topic_last_poster_email = ''; 
  36.  
  37. if ( !isset( $topic->topic_last_poster_nicename ) ) 
  38. $topics[$key]->topic_last_poster_nicename = ''; 
  39.  
  40. if ( !isset( $topic->topic_last_poster_login ) ) 
  41. $topics[$key]->topic_last_poster_login = ''; 
  42.  
  43. if ( !isset( $topic->topic_last_poster_displayname ) ) 
  44. $topics[$key]->topic_last_poster_displayname = ''; 
  45.  
  46. return $topics;