bbp_admin_repair_user_topic_subscriptions

Clean the users' topic subscriptions.

Description

(array) bbp_admin_repair_user_topic_subscriptions(); 

Returns (array)

An array of the status code and the message


Usage

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

Defined (1)

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

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