bb_recount_clean_favorites

The BuddyPress bb recount clean favorites function.

Description

bb_recount_clean_favorites(); 

Usage

  1. if ( !function_exists( 'bb_recount_clean_favorites' ) ) { 
  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_clean_favorites(); 
  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_clean_favorites() 
  2. global $bbdb; 
  3.  
  4. $statement = __( 'Removing deleted topics from user favorites… %s' ); 
  5. $result = __( 'Failed!' ); 
  6.  
  7. $meta_key = $bbdb->prefix . 'favorites'; 
  8.  
  9. $users = $bbdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';" ); 
  10. if ( is_wp_error( $users ) ) { 
  11. return array( 1, sprintf( $statement, $result ) ); 
  12.  
  13. $topics = $bbdb->get_col( "SELECT `topic_id` FROM `$bbdb->topics` WHERE `topic_status` = '0';" ); 
  14.  
  15. if ( is_wp_error( $topics ) ) { 
  16. return array( 2, sprintf( $statement, $result ) ); 
  17.  
  18. $values = array(); 
  19. foreach ( $users as $user ) { 
  20. if ( empty( $user->favorites ) || !is_string( $user->favorites ) ) { 
  21. continue; 
  22. $favorites = explode( ', ', $user->favorites ); 
  23. if ( empty( $favorites ) || !is_array( $favorites ) ) { 
  24. continue; 
  25. $favorites = join( ', ', array_intersect( $topics, $favorites ) ); 
  26. $values[] = "('$user->user_id', '$meta_key', '$favorites')"; 
  27.  
  28. if ( !count( $values ) ) { 
  29. $result = __( 'Nothing to remove!' ); 
  30. return array( 0, sprintf( $statement, $result ) ); 
  31.  
  32. $sql_delete = "DELETE FROM `$bbdb->usermeta` WHERE `meta_key` = '$meta_key';"; 
  33. if ( is_wp_error( $bbdb->query( $sql_delete ) ) ) { 
  34. return array( 4, sprintf( $statement, $result ) ); 
  35.  
  36. $values = array_chunk( $values, 10000 ); 
  37. foreach ( $values as $chunk ) { 
  38. $chunk = "\n" . join( ", \n", $chunk ); 
  39. $sql_insert = "INSERT INTO `$bbdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  40. if ( is_wp_error( $bbdb->query( $sql_insert ) ) ) { 
  41. return array( 5, sprintf( $statement, $result ) ); 
  42.  
  43. $result = __( 'Complete!' ); 
  44. return array( 0, sprintf( $statement, $result ) );