Skip to:
Content

bbPress.org

Changeset 6496


Ignore:
Timestamp:
06/08/2017 01:37:58 AM (8 years ago)
Author:
johnjamesjacoby
Message:

Tools: Change BuddyPress group forum repair into an upgrade routine.

This is only intended as a migration path from bbPress 1.x to 2.x, not to be rerun multiple times to force-recount the meta values & private/hidden options.

See #2829.

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

Legend:

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

    r6438 r6496  
    171171    ) );
    172172
    173     // Sync all BuddyPress group forum relationships
    174     bbp_register_repair_tool( array(
    175         'id'          => 'bbp-group-forums',
    176         'type'        => 'repair',
    177         'description' => __( 'Repair BuddyPress Group Forum relationships', 'bbpress' ),
    178         'callback'    => 'bbp_admin_repair_group_forum_relationship',
    179         'priority'    => 35,
    180         'overhead'    => 'low',
    181         'components'  => array( bbp_get_forum_post_type() )
    182     ) );
    183 
    184173    // Update closed topic counts
    185174    bbp_register_repair_tool( array(
     
    188177        'description' => __( 'Repair closed topics', 'bbpress' ),
    189178        'callback'    => 'bbp_admin_repair_closed_topics',
    190         'priority'    => 40,
     179        'priority'    => 35,
    191180        'overhead'    => 'medium',
    192181        'components'  => array( bbp_get_topic_post_type() )
     
    199188        'description' => __( 'Recount topics in each forum', 'bbpress' ),
    200189        'callback'    => 'bbp_admin_repair_forum_topic_count',
    201         'priority'    => 45,
     190        'priority'    => 40,
    202191        'overhead'    => 'medium',
    203192        'components'  => array( bbp_get_forum_post_type(), bbp_get_topic_post_type() )
     
    210199        'description' => __( 'Recount topics in each topic-tag', 'bbpress' ),
    211200        'callback'    => 'bbp_admin_repair_topic_tag_count',
    212         'priority'    => 50,
     201        'priority'    => 45,
    213202        'overhead'    => 'medium',
    214203        'components'  => array( bbp_get_topic_post_type(), bbp_get_topic_tag_tax_id() )
     
    221210        'description' => __( 'Recount replies in each forum', 'bbpress' ),
    222211        'callback'    => 'bbp_admin_repair_forum_reply_count',
    223         'priority'    => 55,
     212        'priority'    => 50,
    224213        'overhead'    => 'high',
    225214        'components'  => array( bbp_get_forum_post_type(), bbp_get_reply_post_type() )
     
    232221        'description' => __( 'Recount replies in each topic', 'bbpress' ),
    233222        'callback'    => 'bbp_admin_repair_topic_reply_count',
     223        'priority'    => 55,
     224        'overhead'    => 'high',
     225        'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     226    ) );
     227
     228    // Count topic engagements
     229    bbp_register_repair_tool( array(
     230        'id'          => 'bbp-topic-engagements',
     231        'type'        => 'repair',
     232        'description' => __( 'Recount engagements in each topic', 'bbpress' ),
     233        'callback'    => 'bbp_admin_repair_topic_voice_count',
    234234        'priority'    => 60,
    235         'overhead'    => 'high',
    236         'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
    237     ) );
    238 
    239     // Count topic voices
    240     bbp_register_repair_tool( array(
    241         'id'          => 'bbp-topic-voices',
    242         'type'        => 'repair',
    243         'description' => __( 'Recount voices in each topic', 'bbpress' ),
    244         'callback'    => 'bbp_admin_repair_topic_voice_count',
    245         'priority'    => 65,
    246235        'overhead'    => 'high',
    247236        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     
    254243        'description' => __( 'Recount pending, spammed, and trashed replies in each topic', 'bbpress' ),
    255244        'callback'    => 'bbp_admin_repair_topic_hidden_reply_count',
    256         'priority'    => 70,
     245        'priority'    => 65,
    257246        'overhead'    => 'high',
    258247        'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     
    265254        'description' => __( 'Recount topics for each user', 'bbpress' ),
    266255        'callback'    => 'bbp_admin_repair_user_topic_count',
    267         'priority'    => 75,
     256        'priority'    => 70,
    268257        'overhead'    => 'medium',
    269258        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     
    276265        'description' => __( 'Recount replies for each user', 'bbpress' ),
    277266        'callback'    => 'bbp_admin_repair_user_reply_count',
    278         'priority'    => 80,
     267        'priority'    => 75,
    279268        'overhead'    => 'medium',
    280269        'components'  => array( bbp_get_reply_post_type(), bbp_get_user_rewrite_id() )
     
    287276        'description' => __( 'Remove unpublished topics from user favorites', 'bbpress' ),
    288277        'callback'    => 'bbp_admin_repair_user_favorites',
    289         'priority'    => 85,
     278        'priority'    => 80,
    290279        'overhead'    => 'medium',
    291280        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     
    298287        'description' => __( 'Remove unpublished topics from user subscriptions', 'bbpress' ),
    299288        'callback'    => 'bbp_admin_repair_user_topic_subscriptions',
    300         'priority'    => 90,
     289        'priority'    => 85,
    301290        'overhead'    => 'medium',
    302291        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     
    309298        'description' => __( 'Remove unpublished forums from user subscriptions', 'bbpress' ),
    310299        'callback'    => 'bbp_admin_repair_user_forum_subscriptions',
    311         'priority'    => 95,
     300        'priority'    => 90,
    312301        'overhead'    => 'medium',
    313302        'components'  => array( bbp_get_forum_post_type(), bbp_get_user_rewrite_id() )
     
    320309        'description' => __( 'Remap existing users to default forum roles', 'bbpress' ),
    321310        'callback'    => 'bbp_admin_repair_user_roles',
    322         'priority'    => 100,
     311        'priority'    => 95,
    323312        'overhead'    => 'low',
    324313        'components'  => array( bbp_get_user_rewrite_id() )
     
    331320        'description' => __( 'Upgrade user favorites', 'bbpress' ),
    332321        'callback'    => 'bbp_admin_upgrade_user_favorites',
    333         'priority'    => 105,
     322        'priority'    => 100,
    334323        'overhead'    => 'high',
    335324        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_favorites_rewrite_id() )
     
    342331        'description' => __( 'Upgrade user topic subscriptions', 'bbpress' ),
    343332        'callback'    => 'bbp_admin_upgrade_user_topic_subscriptions',
    344         'priority'    => 110,
     333        'priority'    => 105,
    345334        'overhead'    => 'high',
    346335        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
     
    353342        'description' => __( 'Upgrade user forum subscriptions', 'bbpress' ),
    354343        'callback'    => 'bbp_admin_upgrade_user_forum_subscriptions',
    355         'priority'    => 115,
     344        'priority'    => 110,
    356345        'overhead'    => 'high',
    357346        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_subscriptions_rewrite_id() )
     
    364353        'description' => __( 'Upgrade topic engagements', 'bbpress' ),
    365354        'callback'    => 'bbp_admin_upgrade_user_engagements',
     355        'priority'    => 115,
     356        'overhead'    => 'medium',
     357        'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_engagements_rewrite_id() )
     358    ) );
     359
     360    // Sync all BuddyPress group forum relationships
     361    bbp_register_repair_tool( array(
     362        'id'          => 'bbp-group-forums',
     363        'type'        => 'upgrade',
     364        'description' => __( 'Upgrade BuddyPress Group Forum relationships', 'bbpress' ),
     365        'callback'    => 'bbp_admin_upgrade_group_forum_relationship',
    366366        'priority'    => 120,
    367         'overhead'    => 'medium',
    368         'components'  => array( bbp_get_user_rewrite_id(), bbp_get_user_engagements_rewrite_id() )
     367        'overhead'    => 'low',
     368        'components'  => array( bbp_get_forum_post_type() )
    369369    ) );
    370370
  • trunk/src/includes/admin/tools/repair.php

    r6495 r6496  
    311311
    312312/**
    313  * Repair group forum ID mappings after a bbPress 1.1 to bbPress 2.2 conversion
    314  *
    315  * @since 2.2.0 bbPress (r4395)
    316  *
    317  * @uses bbp_get_forum_post_type() To get the forum post type
    318  * @return If a wp_error() occurs and no converted forums are found
    319  */
    320 function bbp_admin_repair_group_forum_relationship() {
    321 
    322     // Define variables
    323     $bbp_db    = bbp_db();
    324     $statement = __( 'Repairing BuddyPress group-forum relationships… %s', 'bbpress' );
    325     $g_count   = 0;
    326     $f_count   = 0;
    327     $s_count   = 0;
    328 
    329     // Copy the BuddyPress filter here, incase BuddyPress is not active
    330     $prefix            = apply_filters( 'bp_core_get_table_prefix', $bbp_db->base_prefix );
    331     $groups_table      = $prefix . 'bp_groups';
    332     $groups_meta_table = $prefix . 'bp_groups_groupmeta';
    333 
    334     // Get the converted forum IDs
    335     $forum_ids = $bbp_db->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
    336                                 FROM `{$bbp_db->posts}` AS `forum`
    337                                     LEFT JOIN `{$bbp_db->postmeta}` AS `forummeta`
    338                                         ON `forum`.`ID` = `forummeta`.`post_id`
    339                                         AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
    340                                 WHERE `forum`.`post_type` = '" . bbp_get_forum_post_type() . "'
    341                                 GROUP BY `forum`.`ID`" );
    342 
    343     // Bail if forum IDs returned an error
    344     if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
    345         return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
    346     }
    347 
    348     // Stash the last results
    349     $results = $bbp_db->last_result;
    350 
    351     // Update each group forum
    352     foreach ( $results as $group_forums ) {
    353 
    354         // Only update if is a converted forum
    355         if ( empty( $group_forums->meta_value ) ) {
    356             continue;
    357         }
    358 
    359         // Attempt to update group meta
    360         $updated = $bbp_db->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}'" );
    361 
    362         // Bump the count
    363         if ( ! empty( $updated ) && ! is_wp_error( $updated ) ) {
    364             ++$g_count;
    365         }
    366 
    367         // Update group to forum relationship data
    368         $group_id = (int) $bbp_db->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}'" );
    369         if ( ! empty( $group_id ) ) {
    370 
    371             // Update the group to forum meta connection in forums
    372             update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) );
    373 
    374             // Get the group status
    375             $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}'" );
    376 
    377             // Sync up forum visibility based on group status
    378             switch ( $group_status ) {
    379 
    380                 // Public groups have public forums
    381                 case 'public' :
    382                     bbp_publicize_forum( $group_forums->ID );
    383 
    384                     // Bump the count for output later
    385                     ++$s_count;
    386                     break;
    387 
    388                 // Private/hidden groups have hidden forums
    389                 case 'private' :
    390                 case 'hidden'  :
    391                     bbp_hide_forum( $group_forums->ID );
    392 
    393                     // Bump the count for output later
    394                     ++$s_count;
    395                     break;
    396             }
    397 
    398             // Bump the count for output later
    399             ++$f_count;
    400         }
    401     }
    402 
    403     // Make some logical guesses at the old group root forum
    404     if ( function_exists( 'bp_forums_parent_forum_id' ) ) {
    405         $old_default_forum_id = bp_forums_parent_forum_id();
    406     } elseif ( defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) {
    407         $old_default_forum_id = (int) BP_FORUMS_PARENT_FORUM_ID;
    408     } else {
    409         $old_default_forum_id = 1;
    410     }
    411 
    412     // Try to get the group root forum
    413     $posts = get_posts( array(
    414         'post_type'   => bbp_get_forum_post_type(),
    415         'meta_key'    => '_bbp_old_forum_id',
    416         'meta_type'   => 'NUMERIC',
    417         'meta_value'  => $old_default_forum_id,
    418         'numberposts' => 1
    419     ) );
    420 
    421     // Found the group root forum
    422     if ( ! empty( $posts ) ) {
    423 
    424         // Rename 'Default Forum'  since it's now visible in sitewide forums
    425         if ( 'Default Forum' === $posts[0]->post_title ) {
    426             wp_update_post( array(
    427                 'ID'         => $posts[0]->ID,
    428                 'post_title' => __( 'Group Forums', 'bbpress' ),
    429                 'post_name'  => __( 'group-forums', 'bbpress' ),
    430             ) );
    431         }
    432 
    433         // Update the group forums root metadata
    434         update_option( '_bbp_group_forums_root_id', $posts[0]->ID );
    435     }
    436 
    437     // Remove old bbPress 1.1 roles (BuddyPress)
    438     remove_role( 'member'    );
    439     remove_role( 'inactive'  );
    440     remove_role( 'blocked'   );
    441     remove_role( 'moderator' );
    442     remove_role( 'keymaster' );
    443 
    444     // Complete results
    445     $result = sprintf( __( 'Complete! %s groups updated; %s forums updated; %s forum statuses synced.', 'bbpress' ), bbp_number_format( $g_count ), bbp_number_format( $f_count ), bbp_number_format( $s_count ) );
    446     return array( 0, sprintf( $statement, $result ) );
    447 }
    448 
    449 /**
    450313 * Recount forum topics
    451314 *
  • trunk/src/includes/admin/tools/upgrades.php

    r6340 r6496  
    204204
    205205/**
     206 * Upgrade group forum ID mappings after a bbPress 1.x to bbPress 2.x conversion
     207 *
     208 * Previously named: bbp_admin_repair_group_forum_relationships()
     209 *
     210 * @since 2.6.0 bbPress (r4395)
     211 *
     212 * @uses bbp_get_forum_post_type() To get the forum post type
     213 * @return If a wp_error() occurs and no converted forums are found
     214 */
     215function bbp_admin_upgrade_group_forum_relationships() {
     216
     217    // Define variables
     218    $bbp_db    = bbp_db();
     219    $statement = __( 'Upgrading BuddyPress group-forum relationships… %s', 'bbpress' );
     220    $g_count   = 0;
     221    $f_count   = 0;
     222    $s_count   = 0;
     223
     224    // Copy the BuddyPress filter here, incase BuddyPress is not active
     225    $prefix            = apply_filters( 'bp_core_get_table_prefix', $bbp_db->base_prefix );
     226    $groups_table      = $prefix . 'bp_groups';
     227    $groups_meta_table = $prefix . 'bp_groups_groupmeta';
     228
     229    // Get the converted forum IDs
     230    $forum_ids = $bbp_db->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
     231                                FROM `{$bbp_db->posts}` AS `forum`
     232                                    LEFT JOIN `{$bbp_db->postmeta}` AS `forummeta`
     233                                        ON `forum`.`ID` = `forummeta`.`post_id`
     234                                        AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
     235                                WHERE `forum`.`post_type` = '" . bbp_get_forum_post_type() . "'
     236                                GROUP BY `forum`.`ID`" );
     237
     238    // Bail if forum IDs returned an error
     239    if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
     240        return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
     241    }
     242
     243    // Stash the last results
     244    $results = $bbp_db->last_result;
     245
     246    // Update each group forum
     247    foreach ( $results as $group_forums ) {
     248
     249        // Only update if is a converted forum
     250        if ( empty( $group_forums->meta_value ) ) {
     251            continue;
     252        }
     253
     254        // Attempt to update group meta
     255        $updated = $bbp_db->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}'" );
     256
     257        // Bump the count
     258        if ( ! empty( $updated ) && ! is_wp_error( $updated ) ) {
     259            ++$g_count;
     260        }
     261
     262        // Update group to forum relationship data
     263        $group_id = (int) $bbp_db->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}'" );
     264        if ( ! empty( $group_id ) ) {
     265
     266            // Update the group to forum meta connection in forums
     267            update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) );
     268
     269            // Get the group status
     270            $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}'" );
     271
     272            // Sync up forum visibility based on group status
     273            switch ( $group_status ) {
     274
     275                // Public groups have public forums
     276                case 'public' :
     277                    bbp_publicize_forum( $group_forums->ID );
     278
     279                    // Bump the count for output later
     280                    ++$s_count;
     281                    break;
     282
     283                // Private/hidden groups have hidden forums
     284                case 'private' :
     285                case 'hidden'  :
     286                    bbp_hide_forum( $group_forums->ID );
     287
     288                    // Bump the count for output later
     289                    ++$s_count;
     290                    break;
     291            }
     292
     293            // Bump the count for output later
     294            ++$f_count;
     295        }
     296    }
     297
     298    // Make some logical guesses at the old group root forum
     299    if ( function_exists( 'bp_forums_parent_forum_id' ) ) {
     300        $old_default_forum_id = bp_forums_parent_forum_id();
     301    } elseif ( defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) {
     302        $old_default_forum_id = (int) BP_FORUMS_PARENT_FORUM_ID;
     303    } else {
     304        $old_default_forum_id = 1;
     305    }
     306
     307    // Try to get the group root forum
     308    $posts = get_posts( array(
     309        'post_type'   => bbp_get_forum_post_type(),
     310        'meta_key'    => '_bbp_old_forum_id',
     311        'meta_type'   => 'NUMERIC',
     312        'meta_value'  => $old_default_forum_id,
     313        'numberposts' => 1
     314    ) );
     315
     316    // Found the group root forum
     317    if ( ! empty( $posts ) ) {
     318
     319        // Rename 'Default Forum'  since it's now visible in sitewide forums
     320        if ( 'Default Forum' === $posts[0]->post_title ) {
     321            wp_update_post( array(
     322                'ID'         => $posts[0]->ID,
     323                'post_title' => __( 'Group Forums', 'bbpress' ),
     324                'post_name'  => __( 'group-forums', 'bbpress' ),
     325            ) );
     326        }
     327
     328        // Update the group forums root metadata
     329        update_option( '_bbp_group_forums_root_id', $posts[0]->ID );
     330    }
     331
     332    // Remove old bbPress 1.1 roles (BuddyPress)
     333    remove_role( 'member'    );
     334    remove_role( 'inactive'  );
     335    remove_role( 'blocked'   );
     336    remove_role( 'moderator' );
     337    remove_role( 'keymaster' );
     338
     339    // Complete results
     340    $result = sprintf( __( 'Complete! %s groups updated; %s forums updated; %s forum statuses synced.', 'bbpress' ), bbp_number_format( $g_count ), bbp_number_format( $f_count ), bbp_number_format( $s_count ) );
     341    return array( 0, sprintf( $statement, $result ) );
     342}
     343
     344/**
    206345 * Upgrade user favorites for bbPress 2.6 and higher
    207346 *
Note: See TracChangeset for help on using the changeset viewer.