Changeset 6496 for trunk/src/includes/admin/tools/repair.php
- Timestamp:
- 06/08/2017 01:37:58 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/admin/tools/repair.php
r6495 r6496 311 311 312 312 /** 313 * Repair group forum ID mappings after a bbPress 1.1 to bbPress 2.2 conversion314 *315 * @since 2.2.0 bbPress (r4395)316 *317 * @uses bbp_get_forum_post_type() To get the forum post type318 * @return If a wp_error() occurs and no converted forums are found319 */320 function bbp_admin_repair_group_forum_relationship() {321 322 // Define variables323 $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 active330 $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 IDs335 $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 error344 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 results349 $results = $bbp_db->last_result;350 351 // Update each group forum352 foreach ( $results as $group_forums ) {353 354 // Only update if is a converted forum355 if ( empty( $group_forums->meta_value ) ) {356 continue;357 }358 359 // Attempt to update group meta360 $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 count363 if ( ! empty( $updated ) && ! is_wp_error( $updated ) ) {364 ++$g_count;365 }366 367 // Update group to forum relationship data368 $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 forums372 update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) );373 374 // Get the group status375 $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}'" );376 377 // Sync up forum visibility based on group status378 switch ( $group_status ) {379 380 // Public groups have public forums381 case 'public' :382 bbp_publicize_forum( $group_forums->ID );383 384 // Bump the count for output later385 ++$s_count;386 break;387 388 // Private/hidden groups have hidden forums389 case 'private' :390 case 'hidden' :391 bbp_hide_forum( $group_forums->ID );392 393 // Bump the count for output later394 ++$s_count;395 break;396 }397 398 // Bump the count for output later399 ++$f_count;400 }401 }402 403 // Make some logical guesses at the old group root forum404 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 forum413 $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' => 1419 ) );420 421 // Found the group root forum422 if ( ! empty( $posts ) ) {423 424 // Rename 'Default Forum' since it's now visible in sitewide forums425 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 metadata434 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 results445 $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 /**450 313 * Recount forum topics 451 314 *
Note: See TracChangeset
for help on using the changeset viewer.