Skip to:
Content

bbPress.org


Ignore:
Timestamp:
02/03/2017 06:37:37 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Tools: Separate wp_usermeta clean-up from database upgrade routines.

Clean-up should be run after upgrades, not during. This way all results can be verified by the admin.

See #3052.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/upgrades.php

    r6279 r6281  
    156156<?php
    157157}
     158
     159/**
     160 * Upgrade user favorites for bbPress 2.6 and higher
     161 *
     162 * @since 2.6.0 bbPress (r6174)
     163 *
     164 * @return array An array of the status code and the message
     165 */
     166function bbp_admin_upgrade_user_favorites() {
     167
     168    // Define variables
     169    $bbp_db    = bbp_db();
     170    $statement = __( 'Upgrading user favorites &hellip; %s', 'bbpress' );
     171    $result    = __( 'No favorites to upgrade.',             'bbpress' );
     172    $total     = 0;
     173    $key       = $bbp_db->prefix . '_bbp_favorites';
     174    $favorites = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s", $key ) );
     175
     176    // Bail if no closed topics found
     177    if ( empty( $favorites ) || is_wp_error( $favorites ) ) {
     178        return array( 1, sprintf( $statement, $result ) );
     179    }
     180
     181    // Loop through each user's favorites
     182    foreach ( $favorites as $meta ) {
     183
     184        // Get post IDs
     185        $post_ids = explode( ',', $meta->meta_value );
     186
     187        // Add user ID to all favorited posts
     188        foreach ( $post_ids as $post_id ) {
     189
     190            // Skip if already exists
     191            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, '_bbp_favorite', $meta->user_id ) ) ) {
     192                continue;
     193            }
     194
     195            // Add the post meta
     196            $added = add_post_meta( $post_id, '_bbp_favorite', $meta->user_id, false );
     197
     198            // Bump counts if successfully added
     199            if ( ! empty( $added ) ) {
     200                ++$total;
     201            }
     202        }
     203    }
     204
     205    // Cleanup
     206    unset( $favorites, $added, $post_ids );
     207
     208    // Complete results
     209    $result = sprintf( _n( 'Complete! %d favorite upgraded.', 'Complete! %d favorites upgraded.', $total, 'bbpress' ), $total );
     210
     211    return array( 0, sprintf( $statement, $result ) );
     212}
     213
     214/**
     215 * Upgrade user topic subscriptions for bbPress 2.6 and higher
     216 *
     217 * @since 2.6.0 bbPress (r6174)
     218 *
     219 * @return array An array of the status code and the message
     220 */
     221function bbp_admin_upgrade_user_topic_subscriptions() {
     222
     223    // Define variables
     224    $bbp_db        = bbp_db();
     225    $statement     = __( 'Upgrading user topic subscriptions &hellip; %s', 'bbpress' );
     226    $result        = __( 'No topic subscriptions to upgrade.',             'bbpress' );
     227    $total         = 0;
     228    $key           = $bbp_db->prefix . '_bbp_subscriptions';
     229    $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     230
     231    // Bail if no topic subscriptions found
     232    if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
     233        return array( 1, sprintf( $statement, $result ) );
     234    }
     235
     236    // Loop through each user's topic subscriptions
     237    foreach ( $subscriptions as $meta ) {
     238
     239        // Get post IDs
     240        $post_ids = explode( ',', $meta->meta_value );
     241
     242        // Add user ID to all subscribed topics
     243        foreach ( $post_ids as $post_id ) {
     244
     245            // Skip if already exists
     246            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, '_bbp_subscription', $meta->user_id ) ) ) {
     247                continue;
     248            }
     249
     250            // Add the post meta
     251            $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
     252
     253            // Bump counts if successfully added
     254            if ( ! empty( $added ) ) {
     255                ++$total;
     256            }
     257        }
     258    }
     259
     260    // Cleanup
     261    unset( $subscriptions, $added, $post_ids );
     262
     263    // Complete results
     264    $result = sprintf( _n( 'Complete! %d topic subscription upgraded.', 'Complete! %d topic subscriptions upgraded.', $total, 'bbpress' ), $total );
     265
     266    return array( 0, sprintf( $statement, $result ) );
     267}
     268
     269/**
     270 * Upgrade user forum subscriptions for bbPress 2.6 and higher
     271 *
     272 * @since 2.6.0 bbPress (r6193)
     273 *
     274 * @return array An array of the status code and the message
     275 */
     276function bbp_admin_upgrade_user_forum_subscriptions() {
     277
     278    // Define variables
     279    $bbp_db        = bbp_db();
     280    $statement     = __( 'Upgrading user forum subscriptions &hellip; %s', 'bbpress' );
     281    $result        = __( 'No forum subscriptions to upgrade.',             'bbpress' );
     282    $total         = 0;
     283    $key           = $bbp_db->prefix . '_bbp_forum_subscriptions';
     284    $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     285
     286    // Bail if no forum subscriptions found
     287    if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
     288        return array( 1, sprintf( $statement, $result ) );
     289    }
     290
     291    // Loop through each user's forum subscriptions
     292    foreach ( $subscriptions as $meta ) {
     293
     294        // Get post IDs
     295        $post_ids = explode( ',', $meta->meta_value );
     296
     297        // Add user ID to all subscribed forums
     298        foreach ( $post_ids as $post_id ) {
     299
     300            // Skip if already exists
     301            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, '_bbp_forum_subscription', $meta->user_id ) ) ) {
     302                continue;
     303            }
     304
     305            // Add the post meta
     306            $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
     307
     308            // Bump counts if successfully added
     309            if ( ! empty( $added ) ) {
     310                ++$total;
     311            }
     312        }
     313    }
     314
     315    // Cleanup
     316    unset( $subscriptions, $added, $post_ids );
     317
     318    // Complete results
     319    $result = sprintf( _n( 'Complete! %d forum subscription upgraded.', 'Complete! %d forum subscriptions upgraded.', $total, 'bbpress' ), $total );
     320
     321    return array( 0, sprintf( $statement, $result ) );
     322}
     323
     324/**
     325 * Remove favorites data from wp_usermeta for bbPress 2.6 and higher
     326 *
     327 * @since 2.6.0 bbPress (r6281)
     328 *
     329 * @return array An array of the status code and the message
     330 */
     331function bbp_admin_upgrade_remove_favorites_from_usermeta() {
     332
     333    // Define variables
     334    $bbp_db    = bbp_db();
     335    $statement = __( 'Remove favorites from usermeta &hellip; %s', 'bbpress' );
     336    $result    = __( 'No favorites to remove.',                    'bbpress' );
     337    $total     = 0;
     338    $key       = $bbp_db->prefix . '_bbp_favorites';
     339    $favs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     340
     341    // Bail if no favorites found
     342    if ( empty( $favs ) || is_wp_error( $favs ) ) {
     343        return array( 1, sprintf( $statement, $result ) );
     344    }
     345
     346    // Loop through each user's favorites
     347    foreach ( $favorites as $meta ) {
     348
     349        // Get post IDs
     350        $post_ids  = explode( ',', $meta->meta_value );
     351        $total     = $total + count( $post_ids );
     352
     353        delete_metadata_by_mid( 'user', $meta->umeta_id );
     354    }
     355
     356    // Cleanup
     357    unset( $favs, $post_ids );
     358
     359    // Complete results
     360    $result = sprintf( _n( 'Complete! %d favorites upgraded.', 'Complete! %d favorites upgraded.', $total, 'bbpress' ), $total );
     361
     362    return array( 0, sprintf( $statement, $result ) );
     363}
     364
     365/**
     366 * Remove topic subscriptions data from wp_usermeta for bbPress 2.6 and higher
     367 *
     368 * @since 2.6.0 bbPress (r6281)
     369 *
     370 * @return array An array of the status code and the message
     371 */
     372function bbp_admin_upgrade_remove_topic_subscriptions_from_usermeta() {
     373
     374    // Define variables
     375    $bbp_db    = bbp_db();
     376    $statement = __( 'Remove topic subscriptions from usermeta &hellip; %s', 'bbpress' );
     377    $result    = __( 'No topic subscriptions to remove.',                    'bbpress' );
     378    $total     = 0;
     379    $key       = $bbp_db->prefix . '_bbp_subscriptions';
     380    $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     381
     382    // Bail if no forum favorites found
     383    if ( empty( $subs ) || is_wp_error( $subs ) ) {
     384        return array( 1, sprintf( $statement, $result ) );
     385    }
     386
     387    // Loop through each user's favorites
     388    foreach ( $subs as $meta ) {
     389
     390        // Get post IDs
     391        $post_ids  = explode( ',', $meta->meta_value );
     392        $total     = $total + count( $post_ids );
     393
     394        delete_metadata_by_mid( 'user', $meta->umeta_id );
     395    }
     396
     397    // Cleanup
     398    unset( $subs, $post_ids );
     399
     400    // Complete results
     401    $result = sprintf( _n( 'Complete! %d topic subscription upgraded.', 'Complete! %d topic subscriptions upgraded.', $total, 'bbpress' ), $total );
     402
     403    return array( 0, sprintf( $statement, $result ) );
     404}
     405
     406/**
     407 * Remove topic subscriptions data from wp_usermeta for bbPress 2.6 and higher
     408 *
     409 * @since 2.6.0 bbPress (r6281)
     410 *
     411 * @return array An array of the status code and the message
     412 */
     413function bbp_admin_upgrade_remove_forum_subscriptions_from_usermeta() {
     414
     415    // Define variables
     416    $bbp_db    = bbp_db();
     417    $statement = __( 'Remove forum subscriptions from usermeta &hellip; %s', 'bbpress' );
     418    $result    = __( 'No forum subscriptions to remove.',                    'bbpress' );
     419    $total     = 0;
     420    $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
     421    $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     422
     423    // Bail if no forum favorites found
     424    if ( empty( $subs ) || is_wp_error( $subs ) ) {
     425        return array( 1, sprintf( $statement, $result ) );
     426    }
     427
     428    // Loop through each user's favorites
     429    foreach ( $subs as $meta ) {
     430
     431        // Get post IDs
     432        $post_ids  = explode( ',', $meta->meta_value );
     433        $total     = $total + count( $post_ids );
     434
     435        delete_metadata_by_mid( 'user', $meta->umeta_id );
     436    }
     437
     438    // Cleanup
     439    unset( $subs, $post_ids );
     440
     441    // Complete results
     442    $result = sprintf( _n( 'Complete! %d forum subscription upgraded.', 'Complete! %d forum subscriptions upgraded.', $total, 'bbpress' ), $total );
     443
     444    return array( 0, sprintf( $statement, $result ) );
     445}
Note: See TracChangeset for help on using the changeset viewer.