bbp_admin_repair_user_reply_count

Recount topic replied by the users.

Description

(array) bbp_admin_repair_user_reply_count(); 

Returns (array)

An array of the status code and the message


Usage

  1. if ( !function_exists( 'bbp_admin_repair_user_reply_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/admin/tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_admin_repair_user_reply_count(); 
  7.  

Defined (1)

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

/includes/admin/tools.php  
  1. function bbp_admin_repair_user_reply_count() { 
  2. global $wpdb; 
  3.  
  4. $statement = __( 'Counting the number of topics to which each user has replied… %s', bbpress ); 
  5. $result = __( 'Failed!', bbpress ); 
  6. $sql_select = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`;"; 
  7. $insert_rows = $wpdb->get_results( $sql_select ); 
  8.  
  9. if ( is_wp_error( $insert_rows ) ) 
  10. return array( 1, sprintf( $statement, $result ) ); 
  11.  
  12. $key = $wpdb->prefix . '_bbp_reply_count'; 
  13. $insert_values = array(); 
  14. foreach ( $insert_rows as $insert_row ) 
  15. $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')"; 
  16.  
  17. if ( !count( $insert_values ) ) 
  18. return array( 2, sprintf( $statement, $result ) ); 
  19.  
  20. $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';"; 
  21. if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) 
  22. return array( 3, sprintf( $statement, $result ) ); 
  23.  
  24. foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) { 
  25. $chunk = "\n" . implode( ", \n", $chunk ); 
  26. $sql_insert = "INSERT INTO `{$wpdb->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  27.  
  28. if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) { 
  29. return array( 4, sprintf( $statement, $result ) ); 
  30.  
  31. return array( 0, sprintf( $statement, __( 'Complete!', bbpress ) ) );