bbp_admin_repair_user_forum_subscriptions()
Clean the user forum subscriptions
Description Description
Return Return
(array) An array of the status code and the message
Source Source
File: includes/admin/tools/repair.php
function bbp_admin_repair_user_forum_subscriptions() {
// Define variables
$bbp_db = bbp_db();
$statement = esc_html__( 'Removing trashed forums from user subscriptions… %s', 'bbpress' );
$result = esc_html__( 'Failed!', 'bbpress' );
$key = $bbp_db->prefix . '_bbp_forum_subscriptions';
$users = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
if ( is_wp_error( $users ) ) {
return array( 1, sprintf( $statement, $result ) );
}
$forums = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "'" );
if ( is_wp_error( $forums ) ) {
return array( 2, sprintf( $statement, $result ) );
}
$values = array();
foreach ( $users as $user ) {
if ( empty( $user->subscriptions ) || ! is_string( $user->subscriptions ) ) {
continue;
}
$subscriptions = array_intersect( $forums, explode( ',', $user->subscriptions ) );
if ( empty( $subscriptions ) || ! is_array( $subscriptions ) ) {
continue;
}
$subscriptions_joined = implode( ',', $subscriptions );
$values[] = "('{$user->user_id}', '{$key}', '{$subscriptions_joined}')";
// Cleanup
unset( $subscriptions, $subscriptions_joined );
}
if ( !count( $values ) ) {
$result = esc_html__( 'Nothing to remove!', 'bbpress' );
return array( 0, sprintf( $statement, $result ) );
}
$sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
return array( 4, sprintf( $statement, $result ) );
}
foreach ( array_chunk( $values, 10000 ) as $chunk ) {
$chunk = "\n" . implode( ",\n", $chunk );
$sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
return array( 5, sprintf( $statement, $result ) );
}
}
return array( 0, sprintf( $statement, esc_html__( 'Complete!', 'bbpress' ) ) );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |