bbp_admin_repair_group_forum_relationship

Repair group forum ID mappings after a bbPress 1.1 to bbPress 2.2 conversion.

Description

bbp_admin_repair_group_forum_relationship(); 

Usage

  1. if ( !function_exists( 'bbp_admin_repair_group_forum_relationship' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/admin/tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bbp_admin_repair_group_forum_relationship(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/admin/tools.php  
  1. function bbp_admin_repair_group_forum_relationship() { 
  2. global $wpdb; 
  3.  
  4. $statement = __( 'Repairing BuddyPress group-forum relationships… %s', bbpress ); 
  5. $g_count = 0; 
  6. $f_count = 0; 
  7. $s_count = 0; 
  8.  
  9. // Copy the BuddyPress filter here, incase BuddyPress is not active 
  10. $prefix = apply_filters( 'bp_core_get_table_prefix', $wpdb->base_prefix ); 
  11. $groups_table = $prefix . 'bp_groups'; 
  12. $groups_meta_table = $prefix . 'bp_groups_groupmeta'; 
  13.  
  14. // Get the converted forum IDs 
  15. $forum_ids = $wpdb->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value` 
  16. FROM `{$wpdb->posts}` AS `forum` 
  17. LEFT JOIN `{$wpdb->postmeta}` AS `forummeta` 
  18. ON `forum`.`ID` = `forummeta`.`post_id` 
  19. AND `forummeta`.`meta_key` = '_bbp_old_forum_id' 
  20. WHERE `forum`.`post_type` = 'forum' 
  21. GROUP BY `forum`.`ID`;" ); 
  22.  
  23. // Bail if forum IDs returned an error 
  24. if ( is_wp_error( $forum_ids ) || empty( $wpdb->last_result ) ) 
  25. return array( 2, sprintf( $statement, __( 'Failed!', bbpress ) ) ); 
  26.  
  27. // Stash the last results 
  28. $results = $wpdb->last_result; 
  29.  
  30. // Update each group forum 
  31. foreach ( $results as $group_forums ) { 
  32.  
  33. // Only update if is a converted forum 
  34. if ( ! isset( $group_forums->meta_value ) ) 
  35. continue; 
  36.  
  37. // Attempt to update group meta 
  38. $updated = $wpdb->query( "UPDATE `{$groups_meta_table}` SET `meta_value` = '{$group_forums->ID}' WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->meta_value}';" ); 
  39.  
  40. // Bump the count 
  41. if ( !empty( $updated ) && ! is_wp_error( $updated ) ) { 
  42. ++$g_count; 
  43.  
  44. // Update group to forum relationship data 
  45. $group_id = (int) $wpdb->get_var( "SELECT `group_id` FROM `{$groups_meta_table}` WHERE `meta_key` = 'forum_id' AND `meta_value` = '{$group_forums->ID}';" ); 
  46. if ( !empty( $group_id ) ) { 
  47.  
  48. // Update the group to forum meta connection in forums 
  49. update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) ); 
  50.  
  51. // Get the group status 
  52. $group_status = $wpdb->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}';" ); 
  53.  
  54. // Sync up forum visibility based on group status 
  55. switch ( $group_status ) { 
  56.  
  57. // Public groups have public forums 
  58. case 'public' : 
  59. bbp_publicize_forum( $group_forums->ID ); 
  60.  
  61. // Bump the count for output later 
  62. ++$s_count; 
  63. break; 
  64.  
  65. // Private/hidden groups have hidden forums 
  66. case 'private' : 
  67. case 'hidden' : 
  68. bbp_hide_forum( $group_forums->ID ); 
  69.  
  70. // Bump the count for output later 
  71. ++$s_count; 
  72. break; 
  73.  
  74. // Bump the count for output later 
  75. ++$f_count; 
  76.  
  77. // Make some logical guesses at the old group root forum 
  78. if ( function_exists( 'bp_forums_parent_forum_id' ) ) { 
  79. $old_default_forum_id = bp_forums_parent_forum_id(); 
  80. } elseif ( defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) { 
  81. $old_default_forum_id = (int) BP_FORUMS_PARENT_FORUM_ID
  82. } else { 
  83. $old_default_forum_id = 1; 
  84.  
  85. // Try to get the group root forum 
  86. $posts = get_posts( array( 
  87. 'post_type' => bbp_get_forum_post_type(),  
  88. 'meta_key' => '_bbp_old_forum_id',  
  89. 'meta_value' => $old_default_forum_id,  
  90. 'numberposts' => 1 
  91. ) ); 
  92.  
  93. // Found the group root forum 
  94. if ( ! empty( $posts ) ) { 
  95.  
  96. // Rename 'Default Forum' since it's now visible in sitewide forums 
  97. if ( 'Default Forum' === $posts[0]->post_title ) { 
  98. wp_update_post( array( 
  99. 'ID' => $posts[0]->ID,  
  100. 'post_title' => __( 'Group Forums', bbpress ),  
  101. 'post_name' => __( 'group-forums', bbpress ),  
  102. ) ); 
  103.  
  104. // Update the group forums root metadata 
  105. update_option( '_bbp_group_forums_root_id', $posts[0]->ID ); 
  106.  
  107. // Remove old bbPress 1.1 roles (BuddyPress) 
  108. remove_role( 'member' ); 
  109. remove_role( 'inactive' ); 
  110. remove_role( 'blocked' ); 
  111. remove_role( 'moderator' ); 
  112. remove_role( 'keymaster' ); 
  113.  
  114. // Complete results 
  115. $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 ) ); 
  116. return array( 0, sprintf( $statement, $result ) );