bb_recount_user_topics_replied

The BuddyPress bb recount user topics replied function.

Description

bb_recount_user_topics_replied(); 

Usage

  1. if ( !function_exists( 'bb_recount_user_topics_replied' ) ) { 
  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_user_topics_replied(); 
  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_user_topics_replied() 
  2. global $bbdb; 
  3.  
  4. $statement = __( 'Counting the number of topics to which each user has replied… %s' ); 
  5. $result = __( 'Failed!' ); 
  6.  
  7. $sql_select = "SELECT `poster_id`, COUNT(DISTINCT `topic_id`) as `_count` FROM `$bbdb->posts` WHERE `post_status` = '0' GROUP BY `poster_id`;"; 
  8. $insert_rows = $bbdb->get_results( $sql_select ); 
  9.  
  10. if ( is_wp_error( $insert_rows ) ) { 
  11. return array( 1, sprintf( $statement, $result ) ); 
  12.  
  13. $meta_key = $bbdb->prefix . 'topics_replied'; 
  14.  
  15. $insert_values = array(); 
  16. foreach ( $insert_rows as $insert_row ) { 
  17. $insert_values[] = "('$insert_row->poster_id', '$meta_key', '$insert_row->_count')"; 
  18.  
  19. if ( !count( $insert_values ) ) { 
  20. return array( 2, sprintf( $statement, $result ) ); 
  21.  
  22. $sql_delete = "DELETE FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';"; 
  23. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  24. return array( 3, sprintf( $statement, $result ) ); 
  25.  
  26. $insert_values = array_chunk( $insert_values, 10000 ); 
  27. foreach ( $insert_values as $chunk ) { 
  28. $chunk = "\n" . join( ", \n", $chunk ); 
  29. $sql_insert = "INSERT INTO `$bbdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  30.  
  31. if ( is_wp_error( $bbdb->query( $sql_insert ) ) ) { 
  32. return array( 4, sprintf( $statement, $result ) ); 
  33.  
  34. $result = __( 'Complete!' ); 
  35. return array( 0, sprintf( $statement, $result ) );