Skip to:
Content

bbPress.org

Changeset 6281


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.

Location:
trunk/src/includes/admin
Files:
2 edited

Legend:

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

    r6279 r6281  
    755755        'priority'    => 105,
    756756        'overhead'    => 'high',
    757         'components'  => array( bbp_get_user_rewrite_id() )
     757        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_favorites_rewrite_id() )
    758758    ) );
    759759
     
    766766        'priority'    => 110,
    767767        'overhead'    => 'high',
    768         'components'  => array( bbp_get_user_rewrite_id() )
     768        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
    769769    ) );
    770770
     
    777777        'priority'    => 115,
    778778        'overhead'    => 'high',
    779         'components'  => array( bbp_get_user_rewrite_id() )
     779        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
     780    ) );
     781
     782    // Remove favorites from user-meta
     783    bbp_register_repair_tool( array(
     784        'id'          => 'bbp-user-favorites-delete',
     785        'type'        => 'upgrade',
     786        'description' => __( 'Remove favorites from user-meta', 'bbpress' ),
     787        'callback'    => 'bbp_admin_upgrade_remove_favorites_from_usermeta',
     788        'priority'    => 120,
     789        'overhead'    => 'medium',
     790        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_favorites_rewrite_id() )
     791    ) );
     792
     793    // Remove topic subscriptions from user-meta
     794    bbp_register_repair_tool( array(
     795        'id'          => 'bbp-user-topic-subscriptions-delete',
     796        'type'        => 'upgrade',
     797        'description' => __( 'Remove topic subscriptions from user-meta', 'bbpress' ),
     798        'callback'    => 'bbp_admin_upgrade_remove_topic_subscriptions_from_usermeta',
     799        'priority'    => 125,
     800        'overhead'    => 'medium',
     801        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
     802    ) );
     803
     804    // Remove forum subscriptions from user-meta
     805    bbp_register_repair_tool( array(
     806        'id'          => 'bbp-user-forum-subscriptions-delete',
     807        'type'        => 'upgrade',
     808        'description' => __( 'Remove forum subscriptions from user-meta', 'bbpress' ),
     809        'callback'    => 'bbp_admin_upgrade_remove_forum_subscriptions_from_usermeta',
     810        'priority'    => 130,
     811        'overhead'    => 'medium',
     812        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
    780813    ) );
    781814}
     
    932965            $name = esc_html__( 'Topic Tags', 'bbpress' );
    933966            break;
     967        case bbp_get_user_rewrite_id() :
     968            $name = esc_html__( 'Users', 'bbpress' );
     969            break;
     970        case bbp_get_user_favorites_rewrite_id() :
     971            $name = esc_html__( 'Favorites', 'bbpress' );
     972            break;
     973        case bbp_get_user_subscriptions_rewrite_id() :
     974            $name = esc_html__( 'Subscriptions', 'bbpress' );
     975            break;
    934976        default :
    935977            $name = ucwords( $component );
     
    23702412
    23712413    return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2372 }
    2373 
    2374 /**
    2375  * Upgrade user favorites for bbPress 2.6 and higher
    2376  *
    2377  * @since 2.6.0 bbPress (r6174)
    2378  *
    2379  * @return array An array of the status code and the message
    2380  */
    2381 function bbp_admin_upgrade_user_favorites() {
    2382 
    2383     // Define variables
    2384     $bbp_db    = bbp_db();
    2385     $statement = __( 'Upgrading user favorites … %s', 'bbpress' );
    2386     $result    = __( 'No favorites to upgrade.',             'bbpress' );
    2387     $changed   = $total = 0;
    2388     $key       = $bbp_db->prefix . '_bbp_favorites';
    2389     $favorites = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s", $key ) );
    2390 
    2391     // Bail if no closed topics found
    2392     if ( empty( $favorites ) || is_wp_error( $favorites ) ) {
    2393         return array( 1, sprintf( $statement, $result ) );
    2394     }
    2395 
    2396     // Loop through each user's favorites
    2397     foreach ( $favorites as $meta ) {
    2398 
    2399         // Get post IDs
    2400         $post_ids  = explode( ',', $meta->meta_value );
    2401         $to_change = count( $post_ids );
    2402         $changed   = 0;
    2403 
    2404         // Add user ID to all favorited posts
    2405         foreach ( $post_ids as $post_id ) {
    2406 
    2407             // Skip if already exists
    2408             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 ) ) ) {
    2409                 continue;
    2410             }
    2411 
    2412             // Add the post meta
    2413             $added = add_post_meta( $post_id, '_bbp_favorite', $meta->user_id, false );
    2414 
    2415             // Bump counts if successfully added
    2416             if ( ! empty( $added ) ) {
    2417                 ++$changed;
    2418                 ++$total;
    2419             }
    2420         }
    2421 
    2422         // Delete user meta if everything was copied successfully
    2423         if ( $changed === $to_change ) {
    2424             //delete_metadata_by_mid( 'user', $meta->umeta_id );
    2425         }
    2426     }
    2427 
    2428     // Cleanup
    2429     unset( $favorites, $added, $post_ids );
    2430 
    2431     // Complete results
    2432     $result = sprintf( _n( 'Complete! %d favorite upgraded.', 'Complete! %d favorites upgraded.', $total, 'bbpress' ), $total );
    2433 
    2434     return array( 0, sprintf( $statement, $result ) );
    2435 }
    2436 
    2437 /**
    2438  * Upgrade user topic subscriptions for bbPress 2.6 and higher
    2439  *
    2440  * @since 2.6.0 bbPress (r6174)
    2441  *
    2442  * @return array An array of the status code and the message
    2443  */
    2444 function bbp_admin_upgrade_user_topic_subscriptions() {
    2445 
    2446     // Define variables
    2447     $bbp_db        = bbp_db();
    2448     $statement     = __( 'Upgrading user topic subscriptions … %s', 'bbpress' );
    2449     $result        = __( 'No topic subscriptions to upgrade.',             'bbpress' );
    2450     $changed       = $total = 0;
    2451     $key           = $bbp_db->prefix . '_bbp_subscriptions';
    2452     $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
    2453 
    2454     // Bail if no topic subscriptions found
    2455     if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
    2456         return array( 1, sprintf( $statement, $result ) );
    2457     }
    2458 
    2459     // Loop through each user's topic subscriptions
    2460     foreach ( $subscriptions as $meta ) {
    2461 
    2462         // Get post IDs
    2463         $post_ids  = explode( ',', $meta->meta_value );
    2464         $to_change = count( $post_ids );
    2465         $changed   = 0;
    2466 
    2467         // Add user ID to all subscribed topics
    2468         foreach ( $post_ids as $post_id ) {
    2469 
    2470             // Skip if already exists
    2471             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 ) ) ) {
    2472                 continue;
    2473             }
    2474 
    2475             // Add the post meta
    2476             $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
    2477 
    2478             // Bump counts if successfully added
    2479             if ( ! empty( $added ) ) {
    2480                 ++$changed;
    2481                 ++$total;
    2482             }
    2483         }
    2484 
    2485         // Delete user meta if everything was copied successfully
    2486         if ( $changed === $to_change ) {
    2487             //delete_metadata_by_mid( 'user', $meta->umeta_id );
    2488         }
    2489     }
    2490 
    2491     // Cleanup
    2492     unset( $subscriptions, $added, $post_ids );
    2493 
    2494     // Complete results
    2495     $result = sprintf( _n( 'Complete! %d topic subscription upgraded.', 'Complete! %d topic subscriptions upgraded.', $total, 'bbpress' ), $total );
    2496 
    2497     return array( 0, sprintf( $statement, $result ) );
    2498 }
    2499 
    2500 /**
    2501  * Upgrade user forum subscriptions for bbPress 2.6 and higher
    2502  *
    2503  * @since 2.6.0 bbPress (r6193)
    2504  *
    2505  * @return array An array of the status code and the message
    2506  */
    2507 function bbp_admin_upgrade_user_forum_subscriptions() {
    2508 
    2509     // Define variables
    2510     $bbp_db        = bbp_db();
    2511     $statement     = __( 'Upgrading user forum subscriptions … %s', 'bbpress' );
    2512     $result        = __( 'No forum subscriptions to upgrade.',             'bbpress' );
    2513     $changed       = $total = 0;
    2514     $key           = $bbp_db->prefix . '_bbp_forum_subscriptions';
    2515     $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
    2516 
    2517     // Bail if no forum subscriptions found
    2518     if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
    2519         return array( 1, sprintf( $statement, $result ) );
    2520     }
    2521 
    2522     // Loop through each user's forum subscriptions
    2523     foreach ( $subscriptions as $meta ) {
    2524 
    2525         // Get post IDs
    2526         $post_ids  = explode( ',', $meta->meta_value );
    2527         $to_change = count( $post_ids );
    2528         $changed   = 0;
    2529 
    2530         // Add user ID to all subscribed forums
    2531         foreach ( $post_ids as $post_id ) {
    2532 
    2533             // Skip if already exists
    2534             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 ) ) ) {
    2535                 continue;
    2536             }
    2537 
    2538             // Add the post meta
    2539             $added = add_post_meta( $post_id, '_bbp_subscription', $meta->user_id, false );
    2540 
    2541             // Bump counts if successfully added
    2542             if ( ! empty( $added ) ) {
    2543                 ++$changed;
    2544                 ++$total;
    2545             }
    2546         }
    2547 
    2548         // Delete user meta if everything was copied successfully
    2549         if ( $changed === $to_change ) {
    2550             //delete_metadata_by_mid( 'user', $meta->umeta_id );
    2551         }
    2552     }
    2553 
    2554     // Cleanup
    2555     unset( $subscriptions, $added, $post_ids );
    2556 
    2557     // Complete results
    2558     $result = sprintf( _n( 'Complete! %d forum subscription upgraded.', 'Complete! %d forum subscriptions upgraded.', $total, 'bbpress' ), $total );
    2559 
    2560     return array( 0, sprintf( $statement, $result ) );
    25612414}
    25622415
  • 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.