bbp_admin_repair_user_favorites

Clean the users' favorites.

Description

(array) bbp_admin_repair_user_favorites(); 

Returns (array)

An array of the status code and the message


Usage

  1. if ( !function_exists( 'bbp_admin_repair_user_favorites' ) ) { 
  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_favorites(); 
  7.  

Defined (1)

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

/includes/admin/tools.php  
  1. function bbp_admin_repair_user_favorites() { 
  2. global $wpdb; 
  3.  
  4. $statement = __( 'Removing trashed topics from user favorites… %s', bbpress ); 
  5. $result = __( 'Failed!', bbpress ); 
  6. $key = $wpdb->prefix . '_bbp_favorites'; 
  7. $users = $wpdb->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';" ); 
  8.  
  9. if ( is_wp_error( $users ) ) 
  10. return array( 1, sprintf( $statement, $result ) ); 
  11.  
  12. $topics = $wpdb->get_col( "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "';" ); 
  13.  
  14. if ( is_wp_error( $topics ) ) 
  15. return array( 2, sprintf( $statement, $result ) ); 
  16.  
  17. $values = array(); 
  18. foreach ( $users as $user ) { 
  19. if ( empty( $user->favorites ) || !is_string( $user->favorites ) ) 
  20. continue; 
  21.  
  22. $favorites = array_intersect( $topics, explode( ', ', $user->favorites ) ); 
  23. if ( empty( $favorites ) || !is_array( $favorites ) ) 
  24. continue; 
  25.  
  26. $favorites_joined = implode( ', ', $favorites ); 
  27. $values[] = "('{$user->user_id}', '{$key}, '{$favorites_joined}')"; 
  28.  
  29. // Cleanup 
  30. unset( $favorites, $favorites_joined ); 
  31.  
  32. if ( !count( $values ) ) { 
  33. $result = __( 'Nothing to remove!', bbpress ); 
  34. return array( 0, sprintf( $statement, $result ) ); 
  35.  
  36. $sql_delete = "DELETE FROM `{$wpdb->usermeta}` WHERE `meta_key` = '{$key}';"; 
  37. if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) 
  38. return array( 4, sprintf( $statement, $result ) ); 
  39.  
  40. foreach ( array_chunk( $values, 10000 ) as $chunk ) { 
  41. $chunk = "\n" . implode( ", \n", $chunk ); 
  42. $sql_insert = "INSERT INTO `$wpdb->usermeta` (`user_id`, `meta_key`, `meta_value`) VALUES $chunk;"; 
  43. if ( is_wp_error( $wpdb->query( $sql_insert ) ) ) { 
  44. return array( 5, sprintf( $statement, $result ) ); 
  45.  
  46. return array( 0, sprintf( $statement, __( 'Complete!', bbpress ) ) );