bbp_admin_upgrade_user_topic_subscriptions()
Upgrade user topic subscriptions for bbPress 2.6 and higher
Description Description
Return Return
(array) An array of the status code and the message
Source Source
File: includes/admin/tools/upgrade.php
function bbp_admin_upgrade_user_topic_subscriptions() {
// Define variables
$bbp_db = bbp_db();
$statement = esc_html__( 'Upgrading user topic subscriptions… %s', 'bbpress' );
$result = esc_html__( 'No topic subscriptions to upgrade.', 'bbpress' );
$total = 0;
$old_key = $bbp_db->prefix . '_bbp_subscriptions';
$new_key = '_bbp_subscription';
// Results
$query = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id";
$prepare = $bbp_db->prepare( $query, $old_key );
$subs = $bbp_db->get_results( $prepare );
// Bail if no topic subscriptions found
if ( empty( $subs ) || is_wp_error( $subs ) ) {
return array( 1, sprintf( $statement, $result ) );
}
// Loop through each user's topic subscriptions
foreach ( $subs as $meta ) {
// Get post IDs
$post_ids = explode( ',', $meta->meta_value );
// Add user ID to all subscribed topics
foreach ( $post_ids as $post_id ) {
// Skip if already exists
if ( $bbp_db->get_var( $bbp_db->prepare( "SELECT COUNT(*) FROM {$bbp_db->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_value = %d", $post_id, $new_key, $meta->user_id ) ) ) {
continue;
}
// Add the post meta
$added = add_post_meta( $post_id, $new_key, $meta->user_id, false );
// Bump counts if successfully added
if ( ! empty( $added ) ) {
++$total;
}
}
}
// Cleanup
unset( $subs, $added, $post_ids );
// Complete results
$result = sprintf( _n( 'Complete! %d topic subscription upgraded.', 'Complete! %d topic subscriptions upgraded.', $total, 'bbpress' ), bbp_number_format( $total ) );
return array( 0, sprintf( $statement, $result ) );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |