bb_recount_topic_tags

The BuddyPress bb recount topic tags function.

Description

bb_recount_topic_tags(); 

Usage

  1. if ( !function_exists( 'bb_recount_topic_tags' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-recount.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bb_recount_topic_tags(); 
  7.  

Defined (1)

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

/bp-forums/bbpress/bb-admin/includes/functions.bb-recount.php  
  1. function bb_recount_topic_tags() 
  2. global $bbdb; 
  3.  
  4. $statement = __( 'Counting the number of topic tags in each topic… %s' ); 
  5. $result = __( 'Failed!' ); 
  6.  
  7. // Delete empty tags 
  8. if ( $delete[0] > 0 ) { 
  9. $result = __( 'Could not delete empty tags.' ); 
  10. return array( 1, sprintf( $statement, $result ) ); 
  11.  
  12. // Get all tags 
  13. $sql_terms = "SELECT 
  14. `$bbdb->term_relationships`.`object_id`,  
  15. `$bbdb->term_taxonomy`.`term_id` 
  16. FROM `$bbdb->term_relationships` 
  17. JOIN `$bbdb->term_taxonomy` 
  18. ON `$bbdb->term_taxonomy`.`term_taxonomy_id` = `$bbdb->term_relationships`.`term_taxonomy_id` 
  19. WHERE 
  20. `$bbdb->term_taxonomy`.`taxonomy` = 'bb_topic_tag' 
  21. ORDER BY 
  22. `$bbdb->term_relationships`.`object_id`,  
  23. `$bbdb->term_taxonomy`.`term_id`;"; 
  24.  
  25. $terms = $bbdb->get_results( $sql_terms ); 
  26. if ( is_wp_error( $terms ) || !is_array( $terms ) ) { 
  27. return array( 2, sprintf( $statement, $result ) ); 
  28. if ( empty( $terms ) ) { 
  29. $result = __( 'No topic tags found.' ); 
  30. return array( 3, sprintf( $statement, $result ) ); 
  31.  
  32. // Count the tags in each topic 
  33. $topics = array(); 
  34. foreach ( $terms as $term ) { 
  35. if ( !isset( $topics[$term->object_id] ) ) { 
  36. $topics[$term->object_id] = 1; 
  37. } else { 
  38. $topics[$term->object_id]++; 
  39. if ( empty( $topics ) ) { 
  40. return array( 4, sprintf( $statement, $result ) ); 
  41.  
  42. // Build the values to insert into the SQL statement 
  43. $values = array(); 
  44. foreach ($topics as $topic_id => $tag_count) { 
  45. $values[] = '(' . $topic_id . ', ' . $tag_count . ')'; 
  46. if ( empty( $values ) ) { 
  47. return array( 5, sprintf( $statement, $result ) ); 
  48.  
  49. // Update the topics with the new tag counts 
  50. $values = array_chunk( $values, 10000 ); 
  51. foreach ($values as $chunk) { 
  52. $sql = "INSERT INTO `$bbdb->topics` (`topic_id`, `tag_count`) VALUES " . implode(", ", $chunk) . " ON DUPLICATE KEY UPDATE `tag_count` = VALUES(`tag_count`);"; 
  53. if ( is_wp_error( $bbdb->query( $sql ) ) ) { 
  54. return array( 6, sprintf( $statement, $result ) ); 
  55.  
  56. $result = __( 'Complete!' ); 
  57. return array( 0, sprintf( $statement, $result ) );