Skip to:
Content

bbPress.org


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 *
Note: See TracChangeset for help on using the changeset viewer.