bbp_admin_upgrade_user_favorites()
Upgrade user favorites 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_favorites() {
// Define variables
$bbp_db = bbp_db();
$statement = esc_html__( 'Upgrading user favorites… %s', 'bbpress' );
$result = esc_html__( 'No favorites to upgrade.', 'bbpress' );
$total = 0;
$old_key = $bbp_db->prefix . '_bbp_favorites';
$new_key = '_bbp_favorite';
// Results
$query = "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s";
$prepare = $bbp_db->prepare( $query, $old_key );
$favs = $bbp_db->get_results( $prepare );
// Bail if no closed topics found
if ( empty( $favs ) || is_wp_error( $favs ) ) {
return array( 1, sprintf( $statement, $result ) );
}
// Loop through each user's favorites
foreach ( $favs as $meta ) {
// Get post IDs
$post_ids = explode( ',', $meta->meta_value );
// Add user ID to all favorited posts
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( $favs, $added, $post_ids );
// Complete results
$result = sprintf( _n( 'Complete! %d favorite upgraded.', 'Complete! %d favorites upgraded.', $total, 'bbpress' ), bbp_number_format( $total ) );
return array( 0, sprintf( $statement, $result ) );
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |