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/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
Note: See TracChangeset for help on using the changeset viewer.