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/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.