Skip to:
Content

bbPress.org

Changeset 6282


Ignore:
Timestamp:
02/03/2017 08:28:47 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Tools: Break tools.php up into separate files. See #3052.

Location:
trunk/src/includes/admin
Files:
5 added
2 edited
1 moved

Legend:

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

    r6280 r6282  
    118118     */
    119119    private function includes() {
    120         require( $this->admin_dir . 'tools.php'     );
    121         require( $this->admin_dir . 'upgrades.php'  );
    122         require( $this->admin_dir . 'converter.php' );
    123         require( $this->admin_dir . 'settings.php'  );
    124         require( $this->admin_dir . 'functions.php' );
    125         require( $this->admin_dir . 'metaboxes.php' );
    126         require( $this->admin_dir . 'forums.php'    );
    127         require( $this->admin_dir . 'topics.php'    );
    128         require( $this->admin_dir . 'replies.php'   );
    129         require( $this->admin_dir . 'users.php'     );
     120
     121        // Tools
     122        require $this->admin_dir . 'tools.php';
     123        require $this->admin_dir . 'tools/common.php';
     124        require $this->admin_dir . 'tools/repair.php';
     125        require $this->admin_dir . 'tools/upgrades.php';
     126        require $this->admin_dir . 'tools/reset.php';
     127        require $this->admin_dir . 'tools/help.php';
     128
     129        // Components
     130        require $this->admin_dir . 'converter.php';
     131        require $this->admin_dir . 'settings.php';
     132        require $this->admin_dir . 'functions.php';
     133        require $this->admin_dir . 'metaboxes.php';
     134        require $this->admin_dir . 'forums.php';
     135        require $this->admin_dir . 'topics.php';
     136        require $this->admin_dir . 'replies.php';
     137        require $this->admin_dir . 'users.php';
    130138    }
    131139
  • trunk/src/includes/admin/tools.php

    r6281 r6282  
    5151
    5252<?php
    53 }
    54 
    55 /** Repair ********************************************************************/
    56 
    57 /**
    58  * Admin repair page
    59  *
    60  * @since 2.0.0 bbPress (r2613) Converted from bbPress 1.2
    61  * @since 2.6.0 bbPress (r5885) Upgraded to list-table UI
    62  *
    63  * @todo Use a real list table
    64  *
    65  * @uses bbp_admin_repair_list() To get the recount list
    66  * @uses check_admin_referer() To verify the nonce and the referer
    67  * @uses wp_cache_flush() To flush the cache
    68  * @uses do_action() Calls 'admin_notices' to display the notices
    69  * @uses wp_nonce_field() To add a hidden nonce field
    70  */
    71 function bbp_admin_repair_page() {
    72 
    73     // Get the registered repair tools
    74     $tools = bbp_admin_repair_list(); ?>
    75 
    76     <div class="wrap">
    77         <h1><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
    78         <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( __( 'Repair Forums', 'bbpress' ) ); ?></h2>
    79 
    80         <p><?php esc_html_e( 'bbPress keeps track of relationships between forums, topics, replies, topic-tags, favorites, subscriptions, and users. Occasionally these relationships become out of sync, most often after an import or migration. Use the tools below to manually recalculate these relationships.', 'bbpress' ); ?></p>
    81         <p class="description"><?php esc_html_e( 'Some of these tools create substantial database overhead. Use caution when running more than 1 repair at a time.', 'bbpress' ); ?></p>
    82 
    83         <?php bbp_admin_repair_tool_overhead_filters(); ?>
    84 
    85         <form class="settings" method="get" action="">
    86 
    87             <?php bbp_admin_repair_list_search_form(); ?>
    88 
    89             <input type="hidden" name="page" value="bbp-repair" />
    90             <?php wp_nonce_field( 'bbpress-do-counts' ); ?>
    91 
    92             <div class="tablenav top">
    93                 <div class="alignleft actions bulkactions">
    94                     <label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
    95                     <select name="action" id="bulk-action-selector-top">
    96                         <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
    97                         <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
    98                     </select>
    99                     <input type="submit" id="doaction" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
    100                 </div>
    101                 <div class="alignleft actions">
    102 
    103                     <?php bbp_admin_repair_list_components_filter(); ?>
    104 
    105                 </div>
    106                 <br class="clear">
    107             </div>
    108             <table class="wp-list-table widefat striped posts">
    109                 <thead>
    110                     <tr>
    111                         <td id="cb" class="manage-column column-cb check-column">
    112                             <label class="screen-reader-text" for="cb-select-all-1">
    113                                 <?php esc_html_e( 'Select All', 'bbpress' ); ?>
    114                             </label>
    115                             <input id="cb-select-all-1" type="checkbox">
    116                         </td>
    117                         <th scope="col" id="description" class="manage-column column-primary column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
    118                         <th scope="col" id="components" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
    119                         <th scope="col" id="overhead" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
    120                     </tr>
    121                 </thead>
    122 
    123                 <tbody id="the-list">
    124 
    125                     <?php if ( ! empty( $tools ) ) : ?>
    126 
    127                         <?php foreach ( $tools as $item ) : ?>
    128 
    129                             <tr id="bbp-repair-tools" class="inactive">
    130                                 <th scope="row" class="check-column">
    131                                     <label class="screen-reader-text" for="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>"></label>
    132                                     <input type="checkbox" name="checked[]" value="<?php echo esc_attr( $item['id'] ); ?>" id="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>">
    133                                 </th>
    134                                 <td class="bbp-tool-title column-primary column-description" data-colname="<?php esc_html_e( 'Description', 'bbpress' ); ?>">
    135                                     <strong><?php echo esc_html( $item['description'] ); ?></strong>
    136                                     <div class="row-actions hide-if-no-js">
    137                                         <span class="run">
    138                                             <a href="<?php bbp_admin_repair_tool_run_url( $item ); ?>" aria-label="<?php printf( esc_html__( 'Run %s', 'bbpress' ), $item['description'] ); ?>" id="<?php echo esc_attr( $item['id'] ); ?>" ><?php esc_html_e( 'Run', 'bbpress' ); ?></a>
    139                                         </span>
    140                                     </div>
    141                                     <button type="button" class="toggle-row">
    142                                         <span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'bbpress' ); ?></span>
    143                                     </button>
    144                                 </td>
    145                                 <td class="column-components desc" data-colname="<?php esc_html_e( 'Components', 'bbpress' ); ?>">
    146                                     <div class="bbp-tool-overhead">
    147 
    148                                         <?php echo implode( ', ', bbp_get_admin_repair_tool_components( $item ) ); ?>
    149 
    150                                     </div>
    151                                 </td>
    152                                 <td class="column-overhead desc" data-colname="<?php esc_html_e( 'Overhead', 'bbpress' ); ?>">
    153                                     <div class="bbp-tool-overhead">
    154 
    155                                         <?php echo implode( ', ', bbp_get_admin_repair_tool_overhead( $item ) ); ?>
    156 
    157                                     </div>
    158                                 </td>
    159                             </tr>
    160 
    161                         <?php endforeach; ?>
    162 
    163                     <?php else : ?>
    164 
    165                         <tr>
    166                             <td colspan="4">
    167                                 <?php esc_html_e( 'No repair tools match this criteria.', 'bbpress' ); ?>
    168                             </td>
    169                         </tr>
    170 
    171                     <?php endif; ?>
    172 
    173                 </tbody>
    174                 <tfoot>
    175                     <tr>
    176                         <td class="manage-column column-cb check-column">
    177                             <label class="screen-reader-text" for="cb-select-all-2">
    178                                 <?php esc_html_e( 'Select All', 'bbpress' ); ?>
    179                             </label>
    180                             <input id="cb-select-all-2" type="checkbox">
    181                         </td>
    182                         <th scope="col" class="manage-column column-primary column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
    183                         <th scope="col" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
    184                         <th scope="col" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
    185                     </tr>
    186                 </tfoot>
    187             </table>
    188             <div class="tablenav bottom">
    189                 <div class="alignleft actions bulkactions">
    190                     <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
    191                     <select name="action2" id="bulk-action-selector-bottom">
    192                         <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
    193                         <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
    194                     </select>
    195                     <input type="submit" id="doaction2" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
    196                 </div>
    197             </div>
    198         </form>
    199     </div>
    200 
    201 <?php
    202 }
    203 
    204 /**
    205  * Handle the processing and feedback of the admin tools page
    206  *
    207  * @since 2.0.0 bbPress (r2613)
    208  *
    209  * @uses bbp_admin_repair_list() To get the recount list
    210  * @uses check_admin_referer() To verify the nonce and the referer
    211  * @uses wp_cache_flush() To flush the cache
    212  * @uses do_action() Calls 'admin_notices' to display the notices
    213  */
    214 function bbp_admin_repair_handler() {
    215 
    216     if ( ! bbp_is_get_request() ) {
    217         return;
    218     }
    219 
    220     // Get the current action or bail
    221     if ( ! empty( $_GET['action'] ) ) {
    222         $action = sanitize_key( $_GET['action'] );
    223     } elseif ( ! empty( $_GET['action2'] ) ) {
    224         $action = sanitize_key( $_GET['action2'] );
    225     } else {
    226         return;
    227     }
    228 
    229     // Bail if not running an action
    230     if ( 'run' !== $action ) {
    231         return;
    232     }
    233 
    234     check_admin_referer( 'bbpress-do-counts' );
    235 
    236     // Stores messages
    237     $messages = array();
    238 
    239     // Kill all the caches, because we don't know what's where anymore
    240     wp_cache_flush();
    241 
    242     // Get the list
    243     $list = bbp_get_admin_repair_tools();
    244 
    245     // Run through checked repair tools
    246     if ( ! empty( $_GET['checked'] ) ) {
    247         foreach ( $_GET['checked'] as $item_id ) {
    248             if ( isset( $list[ $item_id ] ) && is_callable( $list[ $item_id ]['callback'] ) ) {
    249                 $messages[] = call_user_func( $list[ $item_id ]['callback'] );
    250             }
    251         }
    252     }
    253 
    254     // Feedback
    255     if ( count( $messages ) ) {
    256         foreach ( $messages as $message ) {
    257             bbp_admin_tools_feedback( $message[1] );
    258         }
    259     }
    260 
    261     // @todo Redirect away from here
    262 }
    263 
    264 /**
    265  * Output the URL to run a specific repair tool
    266  *
    267  * @since 2.6.0 bbPress (r5885)
    268  *
    269  * @param string $component
    270  */
    271 function bbp_admin_repair_tool_run_url( $component = array() ) {
    272     echo esc_url( bbp_get_admin_repair_tool_run_url( $component ) );
    273 }
    274 
    275 /**
    276  * Return the URL to run a specific repair tool
    277  *
    278  * @since 2.6.0 bbPress (r5885)
    279  *
    280  * @param string $component
    281  */
    282 function bbp_get_admin_repair_tool_run_url( $component = array() ) {
    283     $tools  = admin_url( 'tools.php' );
    284     $page   = ( 'repair' === $component['type'] ) ? 'bbp-repair' : 'bbp-upgrade';
    285     $args   = array( 'page' => $page, 'action' => 'run', 'checked' => array( $component['id'] ) );
    286     $url    = add_query_arg( $args, $tools );
    287     $nonced = wp_nonce_url( $url, 'bbpress-do-counts' );
    288 
    289     return apply_filters( 'bbp_get_admin_repair_tool_run_url', $nonced, $component );
    290 }
    291 
    292 /**
    293  * Contextual help for Repair Forums tools page
    294  *
    295  * @since 2.6.0 bbPress (r5314)
    296  *
    297  * @uses get_current_screen()
    298  */
    299 
    300 function bbp_admin_tools_repair_help() {
    301 
    302     $current_screen = get_current_screen();
    303 
    304     // Bail if current screen could not be found
    305     if ( empty( $current_screen ) ) {
    306         return;
    307     }
    308 
    309     // Repair Forums
    310     $current_screen->add_help_tab( array(
    311         'id'      => 'repair_forums',
    312         'title'   => __( 'Repair Forums', 'bbpress' ),
    313         'content' => '<p>' . __( 'There is more detailed information available on the bbPress and BuddyPress codex for the following:', 'bbpress' ) . '</p>' .
    314                      '<p>' .
    315                         '<ul>' .
    316                             '<li>' . __( 'BuddyPress Group Forums: <a href="https://codex.buddypress.org/getting-started/installing-group-and-sitewide-forums/">Installing Group and Sitewide Forums</a> and <a href="https://codex.buddypress.org/getting-started/guides/migrating-from-old-forums-to-bbpress-2/">Migrating from old forums to bbPress 2.2+</a>.', 'bbpress' ) . '</li>' .
    317                             '<li>' . __( 'bbPress roles: <a href="https://codex.bbpress.org/bbpress-user-roles-and-capabilities/" target="_blank">bbPress User Roles and Capabilities</a>',                                                                                                                                                                        'bbpress' ) . '</li>' .
    318                         '</ul>' .
    319                     '</p>' .
    320                     '<p>' . __( 'Also see <a href="https://codex.bbpress.org/repair-forums/">bbPress: Repair Forums</a>.', 'bbpress' ) . '</p>'
    321     ) );
    322 
    323     // Help Sidebar
    324     $current_screen->set_help_sidebar(
    325         '<p><strong>' . __( 'For more information:', 'bbpress' ) . '</strong></p>' .
    326         '<p>' . __( '<a href="https://codex.bbpress.org" target="_blank">bbPress Documentation</a>',    'bbpress' ) . '</p>' .
    327         '<p>' . __( '<a href="https://bbpress.org/forums/" target="_blank">bbPress Support Forums</a>', 'bbpress' ) . '</p>'
    328     );
    329 }
    330 
    331 /**
    332  * Contextual help for Reset Forums tools page
    333  *
    334  * @since 2.6.0 bbPress (r5314)
    335  *
    336  * @uses get_current_screen()
    337  */
    338 
    339 function bbp_admin_tools_reset_help() {
    340 
    341     $current_screen = get_current_screen();
    342 
    343     // Bail if current screen could not be found
    344     if ( empty( $current_screen ) ) {
    345         return;
    346     }
    347 
    348     // Reset Forums
    349     $current_screen->add_help_tab( array(
    350         'id'      => 'reset_forums',
    351         'title'   => __( 'Reset Forums', 'bbpress' ),
    352         'content' => '<p>' . __( 'Also see <a href="https://codex.bbpress.org/reset-forums/">bbPress: Reset Forums</a>.', 'bbpress' ) . '</p>'
    353     ) );
    354 
    355     // Help Sidebar
    356     $current_screen->set_help_sidebar(
    357         '<p><strong>' . __( 'For more information:', 'bbpress' ) . '</strong></p>' .
    358         '<p>' . __( '<a href="https://codex.bbpress.org" target="_blank">bbPress Documentation</a>',    'bbpress' ) . '</p>' .
    359         '<p>' . __( '<a href="https://bbpress.org/forums/" target="_blank">bbPress Support Forums</a>', 'bbpress' ) . '</p>'
    360     );
    361 }
    362 
    363 /**
    364  * Contextual help for Import Forums tools page
    365  *
    366  * @since 2.6.0 bbPress (r5314)
    367  *
    368  * @uses get_current_screen()
    369  */
    370 
    371 function bbp_admin_tools_converter_help() {
    372 
    373     $current_screen = get_current_screen();
    374 
    375     // Bail if current screen could not be found
    376     if ( empty( $current_screen ) ) {
    377         return;
    378     }
    379 
    380     // Overview
    381     $current_screen->add_help_tab( array(
    382         'id'      => 'overview',
    383         'title'   => __( 'Overview', 'bbpress' ),
    384         'content' => '<p>' . __( 'This screen provides access to all of the bbPress Import Forums settings and resources.',                                      'bbpress' ) . '</p>' .
    385                      '<p>' . __( 'Please see the additional help tabs for more information on each individual section.',                                         'bbpress' ) . '</p>' .
    386                      '<p>' . __( 'Also see the main article on the bbPress codex <a href="https://codex.bbpress.org/import-forums/">bbPress: Import Forums</a>.', 'bbpress' ) . '</p>'
    387     ) );
    388 
    389     // Database Settings
    390     $current_screen->add_help_tab( array(
    391         'id'      => 'database_settings',
    392         'title'   => __( 'Database Settings', 'bbpress' ),
    393         'content' => '<p>' . __( 'In the Database Settings you have a number of options:', 'bbpress' ) . '</p>' .
    394                      '<p>' .
    395                         '<ul>' .
    396                             '<li>' . __( 'The settings in this section refer to the database connection strings used by your old forum software. The best way to determine the exact settings you need is to copy them from your legacy forums configuration file or contact your web hosting provider.', 'bbpress' ) . '</li>' .
    397                         '</ul>' .
    398                     '</p>'
    399     ) );
    400 
    401     // Importer Options
    402     $current_screen->add_help_tab( array(
    403         'id'      => 'importer_options',
    404         'title'   => __( 'Importer Options', 'bbpress' ),
    405         'content' => '<p>' . __( 'In the Options you have a number of options:', 'bbpress' ) . '</p>' .
    406                      '<p>' .
    407                         '<ul>' .
    408                             '<li>' . __( 'Depending on your MySQL configuration you can tweak the "Rows Limit" and "Delay Time" that may help to improve the overall time it takes to perform a complete forum import.', 'bbpress' ) . '</li>' .
    409                             '<li>' . __( '"Convert Users" will import your legacy forum members as WordPress Users.',                                                                                                    'bbpress' ) . '</li>' .
    410                             '<li>' . __( '"Start Over" will start the importer fresh, if your import failed for any reason leaving this setting unchecked the importer will begin from where it left off.',              'bbpress' ) . '</li>' .
    411                             '<li>' . __( '"Purge Previous Import" will remove data imported from a failed import without removing your existing forum data.',                                                            'bbpress' ) . '</li>' .
    412                         '</ul>' .
    413                     '</p>'
    414     ) );
    415     // Help Sidebar
    416     $current_screen->set_help_sidebar(
    417         '<p><strong>' . __( 'For more information:', 'bbpress' ) . '</strong></p>' .
    418         '<p>' . __( '<a href="https://codex.bbpress.org" target="_blank">bbPress Documentation</a>',    'bbpress' ) . '</p>' .
    419         '<p>' . __( '<a href="https://bbpress.org/forums/" target="_blank">bbPress Support Forums</a>', 'bbpress' ) . '</p>'
    420     );
    421 }
    422 
    423 /**
    424  * Assemble the admin notices
    425  *
    426  * @since 2.0.0 bbPress (r2613)
    427  *
    428  * @param string|WP_Error $message A message to be displayed or {@link WP_Error}
    429  * @param string $class Optional. A class to be added to the message div
    430  * @uses WP_Error::get_error_messages() To get the error messages of $message
    431  * @uses add_action() Adds the admin notice action with the message HTML
    432  * @return string The message HTML
    433  */
    434 function bbp_admin_tools_feedback( $message, $class = false ) {
    435 
    436     // Dismiss button
    437     $dismiss = '<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice.', 'bbpress' ) . '</span></button>';
    438 
    439     // One message as string
    440     if ( is_string( $message ) ) {
    441         $message = '<p>' . $message . '</p>';
    442         $class   = $class ? $class : 'updated';
    443 
    444     // Messages as objects
    445     } elseif ( is_wp_error( $message ) ) {
    446         $errors  = $message->get_error_messages();
    447 
    448         switch ( count( $errors ) ) {
    449             case 0:
    450                 return false;
    451 
    452             case 1:
    453                 $message = '<p>' . $errors[0] . '</p>';
    454                 break;
    455 
    456             default:
    457                 $message = '<ul>' . "\n\t" . '<li>' . implode( '</li>' . "\n\t" . '<li>', $errors ) . '</li>' . "\n" . '</ul>';
    458                 break;
    459         }
    460 
    461         $class = $class ? $class : 'is-error';
    462     } else {
    463         return false;
    464     }
    465 
    466     // Assemble the message
    467     $message = '<div id="message" class="is-dismissible notice ' . esc_attr( $class ) . '">' . $message . $dismiss . '</div>';
    468     $message = str_replace( "'", "\'", $message );
    469 
    470     // Ugh
    471     $lambda  = create_function( '', "echo '$message';" );
    472     add_action( 'admin_notices', $lambda );
    473 
    474     return $lambda;
    47553}
    47654
     
    813391    ) );
    814392}
    815 
    816 /**
    817  * Get the array of available repair tools
    818  *
    819  * @since 2.6.0 bbPress (r5885)
    820  *
    821  * @param string $type repair|upgrade The type of tools to get. Default to 'repair'
    822  * @return array
    823  */
    824 function bbp_get_admin_repair_tools( $type = '' ) {
    825 
    826     // Get tools array
    827     $tools = ! empty( bbpress()->admin->tools )
    828         ? bbpress()->admin->tools
    829         : array();
    830 
    831     // Maybe limit to type (otherwise return all tools)
    832     if ( ! empty( $type ) ) {
    833         $tools = wp_list_filter( bbpress()->admin->tools, array( 'type' => $type ) );
    834     }
    835 
    836     return apply_filters( 'bbp_get_admin_repair_tools', $tools, $type );
    837 }
    838 
    839 /**
    840  * Return array of components from the array of registered tools
    841  *
    842  * @since 2.5.0 bbPress (r5885)
    843  *
    844  * @return array
    845  */
    846 function bbp_get_admin_repair_tool_registered_components() {
    847     $tools   = bbp_get_admin_repair_tools( str_replace( 'bbp-', '', sanitize_key( $_GET['page'] ) ) );
    848     $plucked = wp_list_pluck( $tools, 'components' );
    849     $retval  = array();
    850 
    851     foreach ( $plucked as $components ) {
    852         foreach ( $components as $component ) {
    853             if ( in_array( $component, $retval ) ) {
    854                 continue;
    855             }
    856             $retval[] = $component;
    857         }
    858     }
    859 
    860     return apply_filters( 'bbp_get_admin_repair_tool_registered_components', $retval );
    861 }
    862 
    863 /**
    864  * Output the repair list search form
    865  *
    866  * @since 2.6.0 bbPress (r5885)
    867  */
    868 function bbp_admin_repair_list_search_form() {
    869     ?>
    870 
    871     <p class="search-box">
    872         <label class="screen-reader-text" for="bbp-repair-search-input"><?php esc_html_e( 'Search Tools:', 'bbpress' ); ?></label>
    873         <input type="search" id="bbp-repair-search-input" name="s" value="<?php _admin_search_query(); ?>">
    874         <input type="submit" id="search-submit" class="button" value="<?php esc_html_e( 'Search Tools', 'bbpress' ); ?>">
    875     </p>
    876 
    877     <?php
    878 }
    879 
    880 /**
    881  * Output a select drop-down of components to filter by
    882  *
    883  * @since 2.5.0 bbPress (r5885)
    884  */
    885 function bbp_admin_repair_list_components_filter() {
    886 
    887     // Sanitize component value, if exists
    888     $selected = ! empty( $_GET['components'] )
    889         ? sanitize_key( $_GET['components'] )
    890         : '';
    891 
    892     // Get registered components
    893     $components = bbp_get_admin_repair_tool_registered_components(); ?>
    894 
    895     <label class="screen-reader-text" for="cat"><?php esc_html_e( 'Filter by Component', 'bbpress' ); ?></label>
    896     <select name="components" id="components" class="postform">
    897         <option value="" <?php selected( $selected, false ); ?>><?php esc_html_e( 'All Components', 'bbpress' ); ?></option>
    898 
    899         <?php foreach ( $components as $component ) : ?>
    900 
    901             <option class="level-0" value="<?php echo esc_attr( $component ); ?>" <?php selected( $selected, $component ); ?>><?php echo esc_html( bbp_admin_repair_tool_translate_component( $component ) ); ?></option>
    902 
    903         <?php endforeach; ?>
    904 
    905     </select>
    906     <input type="submit" name="filter_action" id="components-submit" class="button" value="<?php esc_html_e( 'Filter', 'bbpress' ); ?>">
    907 
    908     <?php
    909 }
    910 
    911 /**
    912  * Maybe translate a repair tool overhead name
    913  *
    914  * @since 2.6.0 bbPress (r6177)
    915  *
    916  * @param string $overhead
    917  * @return string
    918  */
    919 function bbp_admin_repair_tool_translate_overhead( $overhead = '' ) {
    920 
    921     // Get the name of the component
    922     switch ( $overhead ) {
    923         case 'low' :
    924             $name = esc_html__( 'Low', 'bbpress' );
    925             break;
    926         case 'medium' :
    927             $name = esc_html__( 'Medium', 'bbpress' );
    928             break;
    929         case 'high' :
    930             $name = esc_html__( 'High', 'bbpress' );
    931             break;
    932         default :
    933             $name = ucwords( $overhead );
    934             break;
    935     }
    936 
    937     return $name;
    938 }
    939 
    940 /**
    941  * Maybe translate a repair tool component name
    942  *
    943  * @since 2.6.0 bbPress (r5885)
    944  *
    945  * @param string $component
    946  * @return string
    947  */
    948 function bbp_admin_repair_tool_translate_component( $component = '' ) {
    949 
    950     // Get the name of the component
    951     switch ( $component ) {
    952         case 'bbp_user' :
    953             $name = esc_html__( 'Users', 'bbpress' );
    954             break;
    955         case bbp_get_forum_post_type() :
    956             $name = esc_html__( 'Forums', 'bbpress' );
    957             break;
    958         case bbp_get_topic_post_type() :
    959             $name = esc_html__( 'Topics', 'bbpress' );
    960             break;
    961         case bbp_get_reply_post_type() :
    962             $name = esc_html__( 'Replies', 'bbpress' );
    963             break;
    964         case bbp_get_topic_tag_tax_id() :
    965             $name = esc_html__( 'Topic Tags', 'bbpress' );
    966             break;
    967         case bbp_get_user_rewrite_id() :
    968             $name = esc_html__( 'Users', 'bbpress' );
    969             break;
    970         case bbp_get_user_favorites_rewrite_id() :
    971             $name = esc_html__( 'Favorites', 'bbpress' );
    972             break;
    973         case bbp_get_user_subscriptions_rewrite_id() :
    974             $name = esc_html__( 'Subscriptions', 'bbpress' );
    975             break;
    976         default :
    977             $name = ucwords( $component );
    978             break;
    979     }
    980 
    981     return $name;
    982 }
    983 
    984 /**
    985  * Get the array of the repair list
    986  *
    987  * @since 2.0.0 bbPress (r2613)
    988  *
    989  * @uses apply_filters() Calls 'bbp_repair_list' with the list array
    990  * @return array Repair list of options
    991  */
    992 function bbp_admin_repair_list( $type = 'repair' ) {
    993 
    994     // Define empty array
    995     $repair_list = array();
    996 
    997     // Get the available tools
    998     $list      = bbp_get_admin_repair_tools( $type );
    999     $search    = ! empty( $_GET['s']          ) ? stripslashes( $_GET['s']          ) : '';
    1000     $overhead  = ! empty( $_GET['overhead']   ) ? sanitize_key( $_GET['overhead']   ) : '';
    1001     $component = ! empty( $_GET['components'] ) ? sanitize_key( $_GET['components'] ) : '';
    1002 
    1003     // Overhead filter
    1004     if ( ! empty( $overhead ) ) {
    1005         $list = wp_list_filter( $list, array( 'overhead' => $overhead ) );
    1006     }
    1007 
    1008     // Loop through and key by priority for sorting
    1009     foreach ( $list as $id => $tool ) {
    1010 
    1011         // Component filter
    1012         if ( ! empty( $component ) ) {
    1013             if ( ! in_array( $component, $tool['components'] ) ) {
    1014                 continue;
    1015             }
    1016         }
    1017 
    1018         // Search
    1019         if ( ! empty( $search ) ) {
    1020             if ( ! strstr( strtolower( $tool['description'] ), strtolower( $search ) ) ) {
    1021                 continue;
    1022             }
    1023         }
    1024 
    1025         // Add to repair list
    1026         $repair_list[ $tool['priority'] ] = array(
    1027             'id'          => sanitize_key( $id ),
    1028             'type'        => $tool['type'],
    1029             'description' => $tool['description'],
    1030             'callback'    => $tool['callback'],
    1031             'overhead'    => $tool['overhead'],
    1032             'components'  => $tool['components'],
    1033         );
    1034     }
    1035 
    1036     // Sort
    1037     ksort( $repair_list );
    1038 
    1039     return (array) apply_filters( 'bbp_repair_list', $repair_list );
    1040 }
    1041 
    1042 /**
    1043  * Get filter links for components for a specific admin repair tool
    1044  *
    1045  * @since 2.6.0 bbPress (r5885)
    1046  *
    1047  * @param array $item
    1048  * @return array
    1049  */
    1050 function bbp_get_admin_repair_tool_components( $item = array() ) {
    1051 
    1052     // Get the tools URL
    1053     $tools_url = add_query_arg( array( 'page' => sanitize_key( $_GET['page'] ) ), admin_url( 'tools.php' ) );
    1054 
    1055     // Define links array
    1056     $links = array();
    1057 
    1058     // Loop through tool components and build links
    1059     foreach ( $item['components'] as $component ) {
    1060         $args       = array( 'components' => $component );
    1061         $filter_url = add_query_arg( $args, $tools_url );
    1062         $name       = bbp_admin_repair_tool_translate_component( $component );
    1063         $links[]    = '<a href="' . esc_url( $filter_url ) . '">' . esc_html( $name ) . '</a>';
    1064     }
    1065 
    1066     // Filter & return
    1067     return apply_filters( 'bbp_get_admin_repair_tool_components', $links, $item );
    1068 }
    1069 
    1070 /**
    1071  * Output filter links for components for a specific admin repair tool
    1072  *
    1073  * @since 2.6.0 bbPress (r5885)
    1074  *
    1075  * @param type array
    1076  */
    1077 function bbp_admin_repair_tool_overhead_filters( $args = array() ) {
    1078     echo bbp_get_admin_repair_tool_overhead_filters( $args );
    1079 }
    1080 
    1081 /**
    1082  * Get filter links for components for a specific admin repair tool
    1083  *
    1084  * @since 2.6.0 bbPress (r5885)
    1085  *
    1086  * @param array $args
    1087  * @return array
    1088  */
    1089 function bbp_get_admin_repair_tool_overhead_filters( $args = array() ) {
    1090 
    1091     // Parse args
    1092     $r = bbp_parse_args( $args, array(
    1093         'before'            => '<ul class="subsubsub">',
    1094         'after'             => '</ul>',
    1095         'link_before'       => '<li>',
    1096         'link_after'        => '</li>',
    1097         'count_before'      => ' <span class="count">(',
    1098         'count_after'       => ')</span>',
    1099         'separator'         => ' | ',
    1100     ), 'get_admin_repair_tool_overhead_filters' );
    1101 
    1102     // Get page
    1103     $page = sanitize_key( $_GET['page'] );
    1104 
    1105     // Count the tools
    1106     $tools = bbp_get_admin_repair_tools( str_replace( 'bbp-', '', $page ) );
    1107 
    1108     // Get the tools URL
    1109     $tools_url = add_query_arg( array( 'page' => $page ), admin_url( 'tools.php' ) );
    1110 
    1111     // Define arrays
    1112     $overheads = $links = array();
    1113 
    1114     // Loop through tools and count overheads
    1115     foreach ( $tools as $id => $tool ) {
    1116 
    1117         // Get the overhead level
    1118         $overhead = $tool['overhead'];
    1119 
    1120         // Set an empty count
    1121         if ( empty( $overheads[ $overhead ] ) ) {
    1122             $overheads[ $overhead ] = 0;
    1123         }
    1124 
    1125         // Bump the overhead count
    1126         $overheads[ $overhead ]++;
    1127     }
    1128 
    1129     // Create the "All" link
    1130     $current = empty( $_GET['overhead'] ) ? 'current' : '';
    1131     $links[] = $r['link_before']. '<a href="' . esc_url( $tools_url ) . '" class="' . esc_attr( $current ) . '">' . sprintf( esc_html__( 'All %s', 'bbpress' ), $r['count_before'] . count( $tools ) . $r['count_after'] ) . '</a>' . $r['link_after'];
    1132 
    1133     // Default ticker
    1134     $i = 0;
    1135 
    1136     // Loop through overheads and build filter
    1137     foreach ( $overheads as $overhead => $count ) {
    1138 
    1139         // Separator count
    1140         $i++;
    1141 
    1142         // Build the filter URL
    1143         $key        = sanitize_key( $overhead );
    1144         $args       = array( 'overhead' => $key );
    1145         $filter_url = add_query_arg( $args, $tools_url );
    1146 
    1147         // Figure out separator and active class
    1148         $current  = ! empty( $_GET['overhead'] ) && ( sanitize_key( $_GET['overhead'] ) === $key ) ? 'current' : '';
    1149 
    1150         // Counts to show
    1151         if ( ! empty( $count ) ) {
    1152             $overhead_count = $r['count_before'] . $count . $r['count_after'];
    1153         }
    1154 
    1155         // Build the link
    1156         $links[] = $r['link_before'] . '<a href="' . esc_url( $filter_url ) . '" class="' . esc_attr( $current ) . '">' . bbp_admin_repair_tool_translate_overhead( $overhead ) . $overhead_count . '</a>' . $r['link_after'];
    1157     }
    1158 
    1159     // Surround output with before & after strings
    1160     $output = $r['before'] . implode( $r['separator'], $links ) . $r['after'];
    1161 
    1162     // Filter & return
    1163     return apply_filters( 'bbp_get_admin_repair_tool_components', $output, $r, $args );
    1164 }
    1165 
    1166 /**
    1167  * Get filter links for overhead for a specific admin repair tool
    1168  *
    1169  * @since 2.6.0 bbPress (r5885)
    1170  *
    1171  * @param array $item
    1172  * @return array
    1173  */
    1174 function bbp_get_admin_repair_tool_overhead( $item = array() ) {
    1175 
    1176     // Get the tools URL
    1177     $tools_url = add_query_arg( array( 'page' => sanitize_key( $_GET['page'] ) ), admin_url( 'tools.php' ) );
    1178 
    1179     // Define links array
    1180     $links     = array();
    1181     $overheads = array( $item['overhead'] );
    1182 
    1183     // Loop through tool overhead and build links
    1184     foreach ( $overheads as $overhead ) {
    1185         $args       = array( 'overhead' => $overhead );
    1186         $filter_url = add_query_arg( $args, $tools_url );
    1187         $name       = bbp_admin_repair_tool_translate_overhead( $overhead );
    1188         $links[]    = '<a href="' . esc_url( $filter_url ) . '">' . esc_html( $name ) . '</a>';
    1189     }
    1190 
    1191     // Filter & return
    1192     return apply_filters( 'bbp_get_admin_repair_tool_overhead', $links, $item );
    1193 }
    1194 
    1195 /**
    1196  * Recount topic replies
    1197  *
    1198  * @since 2.0.0 bbPress (r2613)
    1199  *
    1200  * @uses wpdb::query() To run our recount sql queries
    1201  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1202  * @uses bbp_get_topic_post_type() To get the topic post type
    1203  * @uses bbp_get_reply_post_type() To get the reply post type
    1204  * @uses bbp_get_public_status_id() To get the public status id
    1205  * @uses bbp_get_closed_status_id() To get the closed status id
    1206  * @return array An array of the status code and the message
    1207  */
    1208 function bbp_admin_repair_topic_reply_count() {
    1209 
    1210     // Define variables
    1211     $bbp_db    = bbp_db();
    1212     $statement = __( 'Counting the number of replies in each topic&hellip; %s', 'bbpress' );
    1213     $result    = __( 'Failed!', 'bbpress' );
    1214 
    1215     // Post types and status
    1216     $tpt = bbp_get_topic_post_type();
    1217     $rpt = bbp_get_reply_post_type();
    1218     $pps = bbp_get_public_status_id();
    1219     $cps = bbp_get_closed_status_id();
    1220 
    1221     // Delete the meta key _bbp_reply_count for each topic
    1222     $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
    1223                         LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    1224                         WHERE `posts`.`post_type` = '{$tpt}'
    1225                         AND `postmeta`.`meta_key` = '_bbp_reply_count'";
    1226 
    1227     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1228         return array( 1, sprintf( $statement, $result ) );
    1229     }
    1230 
    1231     // Recalculate the meta key _bbp_reply_count for each topic
    1232     $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    1233             SELECT `topics`.`ID` AS `post_id`, '_bbp_reply_count' AS `meta_key`, COUNT(`replies`.`ID`) As `meta_value`
    1234                 FROM `{$bbp_db->posts}` AS `topics`
    1235                     LEFT JOIN `{$bbp_db->posts}` as `replies`
    1236                         ON  `replies`.`post_parent` = `topics`.`ID`
    1237                         AND `replies`.`post_status` = '{$pps}'
    1238                         AND `replies`.`post_type`   = '{$rpt}'
    1239                 WHERE `topics`.`post_type` = '{$tpt}'
    1240                     AND `topics`.`post_status` IN ( '{$pps}', '{$cps}' )
    1241                 GROUP BY `topics`.`ID`)";
    1242 
    1243     if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    1244         return array( 2, sprintf( $statement, $result ) );
    1245     }
    1246 
    1247     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1248 }
    1249 
    1250 /**
    1251  * Recount topic voices
    1252  *
    1253  * @since 2.0.0 bbPress (r2613)
    1254  *
    1255  * @uses wpdb::query() To run our recount sql queries
    1256  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1257  * @uses bbp_get_topic_post_type() To get the topic post type
    1258  * @uses bbp_get_reply_post_type() To get the reply post type
    1259  * @uses bbp_get_public_status_id() To get the public status id
    1260  * @uses bbp_get_closed_status_id() To get the closed status id
    1261  * @return array An array of the status code and the message
    1262  */
    1263 function bbp_admin_repair_topic_voice_count() {
    1264 
    1265     // Define variables
    1266     $bbp_db    = bbp_db();
    1267     $statement = __( 'Counting the number of voices in each topic&hellip; %s', 'bbpress' );
    1268     $result    = __( 'Failed!', 'bbpress' );
    1269 
    1270     $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_voice_count'";
    1271     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1272         return array( 1, sprintf( $statement, $result ) );
    1273     }
    1274 
    1275     // Post types and status
    1276     $tpt = bbp_get_topic_post_type();
    1277     $rpt = bbp_get_reply_post_type();
    1278     $pps = bbp_get_public_status_id();
    1279     $cps = bbp_get_closed_status_id();
    1280 
    1281     $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (
    1282             SELECT `postmeta`.`meta_value`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value`
    1283                 FROM `{$bbp_db->posts}` AS `posts`
    1284                 LEFT JOIN `{$bbp_db->postmeta}` AS `postmeta`
    1285                     ON `posts`.`ID` = `postmeta`.`post_id`
    1286                     AND `postmeta`.`meta_key` = '_bbp_topic_id'
    1287                 WHERE `posts`.`post_type` IN ( '{$tpt}', '{$rpt}' )
    1288                     AND `posts`.`post_status` IN ( '{$pps}', '{$cps}' )
    1289                     AND `posts`.`post_author` != '0'
    1290                 GROUP BY `postmeta`.`meta_value`)";
    1291 
    1292     if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    1293         return array( 2, sprintf( $statement, $result ) );
    1294     }
    1295 
    1296     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1297 }
    1298 
    1299 /**
    1300  * Recount topic hidden replies (spammed/trashed)
    1301  *
    1302  * @since 2.0.0 bbPress (r2747)
    1303  *
    1304  * @uses wpdb::query() To run our recount sql queries
    1305  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1306  * @uses bbp_get_reply_post_type() To get the reply post type
    1307  * @uses bbp_get_trash_status_id() To get the trash status id
    1308  * @uses bbp_get_spam_status_id() To get the spam status id
    1309  * @uses bbp_get_pending_status_id() To get the pending status id
    1310  * @return array An array of the status code and the message
    1311  */
    1312 function bbp_admin_repair_topic_hidden_reply_count() {
    1313 
    1314     // Define variables
    1315     $bbp_db    = bbp_db();
    1316     $statement = __( 'Counting the number of pending, spammed, and trashed replies in each topic&hellip; %s', 'bbpress' );
    1317     $result    = __( 'Failed!', 'bbpress' );
    1318 
    1319     $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_count_hidden'";
    1320     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1321         return array( 1, sprintf( $statement, $result ) );
    1322     }
    1323 
    1324     // Post types and status
    1325     $rpt = bbp_get_reply_post_type();
    1326     $tps = bbp_get_trash_status_id();
    1327     $sps = bbp_get_spam_status_id();
    1328     $pps = bbp_get_pending_status_id();
    1329 
    1330     $sql = "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_reply_count_hidden', COUNT(`post_status`) as `meta_value` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$rpt}' AND `post_status` IN ( '{$tps}', '{$sps}', '{$pps}' ) GROUP BY `post_parent`)";
    1331     if ( is_wp_error( $bbp_db->query( $sql ) ) ) {
    1332         return array( 2, sprintf( $statement, $result ) );
    1333     }
    1334 
    1335     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1336 }
    1337 
    1338 /**
    1339  * Repair group forum ID mappings after a bbPress 1.1 to bbPress 2.2 conversion
    1340  *
    1341  * @since 2.2.0 bbPress (r4395)
    1342  *
    1343  * @uses bbp_get_forum_post_type() To get the forum post type
    1344  * @return If a wp_error() occurs and no converted forums are found
    1345  */
    1346 function bbp_admin_repair_group_forum_relationship() {
    1347 
    1348     // Define variables
    1349     $bbp_db    = bbp_db();
    1350     $statement = __( 'Repairing BuddyPress group-forum relationships&hellip; %s', 'bbpress' );
    1351     $g_count   = 0;
    1352     $f_count   = 0;
    1353     $s_count   = 0;
    1354 
    1355     // Copy the BuddyPress filter here, incase BuddyPress is not active
    1356     $prefix            = apply_filters( 'bp_core_get_table_prefix', $bbp_db->base_prefix );
    1357     $groups_table      = $prefix . 'bp_groups';
    1358     $groups_meta_table = $prefix . 'bp_groups_groupmeta';
    1359 
    1360     // Get the converted forum IDs
    1361     $forum_ids = $bbp_db->query( "SELECT `forum`.`ID`, `forummeta`.`meta_value`
    1362                                 FROM `{$bbp_db->posts}` AS `forum`
    1363                                     LEFT JOIN `{$bbp_db->postmeta}` AS `forummeta`
    1364                                         ON `forum`.`ID` = `forummeta`.`post_id`
    1365                                         AND `forummeta`.`meta_key` = '_bbp_old_forum_id'
    1366                                 WHERE `forum`.`post_type` = '" . bbp_get_forum_post_type() . "'
    1367                                 GROUP BY `forum`.`ID`" );
    1368 
    1369     // Bail if forum IDs returned an error
    1370     if ( is_wp_error( $forum_ids ) || empty( $bbp_db->last_result ) ) {
    1371         return array( 2, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
    1372     }
    1373 
    1374     // Stash the last results
    1375     $results = $bbp_db->last_result;
    1376 
    1377     // Update each group forum
    1378     foreach ( $results as $group_forums ) {
    1379 
    1380         // Only update if is a converted forum
    1381         if ( ! isset( $group_forums->meta_value ) ) {
    1382             continue;
    1383         }
    1384 
    1385         // Attempt to update group meta
    1386         $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}'" );
    1387 
    1388         // Bump the count
    1389         if ( ! empty( $updated ) && ! is_wp_error( $updated ) ) {
    1390             ++$g_count;
    1391         }
    1392 
    1393         // Update group to forum relationship data
    1394         $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}'" );
    1395         if ( ! empty( $group_id ) ) {
    1396 
    1397             // Update the group to forum meta connection in forums
    1398             update_post_meta( $group_forums->ID, '_bbp_group_ids', array( $group_id ) );
    1399 
    1400             // Get the group status
    1401             $group_status = $bbp_db->get_var( "SELECT `status` FROM `{$groups_table}` WHERE `id` = '{$group_id}'" );
    1402 
    1403             // Sync up forum visibility based on group status
    1404             switch ( $group_status ) {
    1405 
    1406                 // Public groups have public forums
    1407                 case 'public' :
    1408                     bbp_publicize_forum( $group_forums->ID );
    1409 
    1410                     // Bump the count for output later
    1411                     ++$s_count;
    1412                     break;
    1413 
    1414                 // Private/hidden groups have hidden forums
    1415                 case 'private' :
    1416                 case 'hidden'  :
    1417                     bbp_hide_forum( $group_forums->ID );
    1418 
    1419                     // Bump the count for output later
    1420                     ++$s_count;
    1421                     break;
    1422             }
    1423 
    1424             // Bump the count for output later
    1425             ++$f_count;
    1426         }
    1427     }
    1428 
    1429     // Make some logical guesses at the old group root forum
    1430     if ( function_exists( 'bp_forums_parent_forum_id' ) ) {
    1431         $old_default_forum_id = bp_forums_parent_forum_id();
    1432     } elseif ( defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) {
    1433         $old_default_forum_id = (int) BP_FORUMS_PARENT_FORUM_ID;
    1434     } else {
    1435         $old_default_forum_id = 1;
    1436     }
    1437 
    1438     // Try to get the group root forum
    1439     $posts = get_posts( array(
    1440         'post_type'   => bbp_get_forum_post_type(),
    1441         'meta_key'    => '_bbp_old_forum_id',
    1442         'meta_type'   => 'NUMERIC',
    1443         'meta_value'  => $old_default_forum_id,
    1444         'numberposts' => 1
    1445     ) );
    1446 
    1447     // Found the group root forum
    1448     if ( ! empty( $posts ) ) {
    1449 
    1450         // Rename 'Default Forum'  since it's now visible in sitewide forums
    1451         if ( 'Default Forum' === $posts[0]->post_title ) {
    1452             wp_update_post( array(
    1453                 'ID'         => $posts[0]->ID,
    1454                 'post_title' => __( 'Group Forums', 'bbpress' ),
    1455                 'post_name'  => __( 'group-forums', 'bbpress' ),
    1456             ) );
    1457         }
    1458 
    1459         // Update the group forums root metadata
    1460         update_option( '_bbp_group_forums_root_id', $posts[0]->ID );
    1461     }
    1462 
    1463     // Remove old bbPress 1.1 roles (BuddyPress)
    1464     remove_role( 'member'    );
    1465     remove_role( 'inactive'  );
    1466     remove_role( 'blocked'   );
    1467     remove_role( 'moderator' );
    1468     remove_role( 'keymaster' );
    1469 
    1470     // Complete results
    1471     $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 ) );
    1472     return array( 0, sprintf( $statement, $result ) );
    1473 }
    1474 
    1475 /**
    1476  * Recount forum topics
    1477  *
    1478  * @since 2.0.0 bbPress (r2613)
    1479  *
    1480  * @uses wpdb::query() To run our recount sql queries
    1481  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1482  * @uses bbp_get_forum_post_type() To get the forum post type
    1483  * @uses get_posts() To get the forums
    1484  * @uses bbp_update_forum_topic_count() To update the forum topic count
    1485  * @return array An array of the status code and the message
    1486  */
    1487 function bbp_admin_repair_forum_topic_count() {
    1488 
    1489     // Define variables
    1490     $bbp_db    = bbp_db();
    1491     $statement = __( 'Counting the number of topics in each forum&hellip; %s', 'bbpress' );
    1492     $result    = __( 'Failed!', 'bbpress' );
    1493 
    1494     $sql_delete = "DELETE FROM {$bbp_db->postmeta} WHERE meta_key IN ( '_bbp_topic_count', '_bbp_total_topic_count', '_bbp_topic_count_hidden' )";
    1495     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1496         return array( 1, sprintf( $statement, $result ) );
    1497     }
    1498 
    1499     $forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
    1500     if ( ! empty( $forums ) ) {
    1501         foreach ( $forums as $forum ) {
    1502             bbp_update_forum_topic_count( $forum->ID );
    1503             bbp_update_forum_topic_count_hidden( $forum->ID );
    1504         }
    1505     } else {
    1506         return array( 2, sprintf( $statement, $result ) );
    1507     }
    1508 
    1509     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1510 }
    1511 
    1512 /**
    1513  * Recount topic in each topic-tag
    1514  *
    1515  * @since 2.6.0 bbPress (r6256)
    1516  *
    1517  * @uses bbp_get_topic_tag_tax_id() To get the topic-tag taxonomy
    1518  * @uses get_terms() To get the terms
    1519  * @uses wp_list_pluck() To get term taxonomy IDs
    1520  * @uses get_taxonomy() To get term taxonomy object
    1521  * @uses _update_post_term_count() To update generic counts
    1522  * @uses bbp_update_topic_tag_count() To update topic-tag counts
    1523  * @uses clean_term_cache() To bust the terms cache
    1524  * @return array An array of the status code and the message
    1525  */
    1526 function bbp_admin_repair_topic_tag_count() {
    1527 
    1528     // Define variables
    1529     $statement = __( 'Counting the number of topics in each topic-tag&hellip; %s', 'bbpress' );
    1530     $result    = __( 'Failed!', 'bbpress' );
    1531     $tax_id    = bbp_get_topic_tag_tax_id();
    1532     $terms     = get_terms( $tax_id, array( 'hide_empty' => false ) );
    1533     $tt_ids    = wp_list_pluck( $terms, 'term_taxonomy_id' );
    1534     $ints      = array_map( 'intval', $tt_ids );
    1535     $taxonomy  = get_taxonomy( $tax_id );
    1536 
    1537     // Bail if taxonomy does not exist
    1538     if ( empty( $taxonomy ) ) {
    1539         return array( 1, sprintf( $statement, $result ) );
    1540     }
    1541 
    1542     // Custom callback
    1543     if ( ! empty( $taxonomy->update_count_callback ) ) {
    1544 
    1545         // Bail if callback is not callable
    1546         if ( ! is_callable( $taxonomy->update_count_callback ) ) {
    1547             return array( 1, sprintf( $statement, $result ) );
    1548         }
    1549 
    1550         call_user_func( $taxonomy->update_count_callback, $ints, $taxonomy );
    1551 
    1552     // Generic callback fallback
    1553     } else {
    1554         _update_post_term_count( $ints, $taxonomy );
    1555     }
    1556 
    1557     // Bust the cache
    1558     clean_term_cache( $ints, '', false );
    1559 
    1560     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1561 }
    1562 
    1563 /**
    1564  * Recount forum replies
    1565  *
    1566  * @since 2.0.0 bbPress (r2613)
    1567  *
    1568  * @uses wpdb::query() To run our recount sql queries
    1569  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1570  * @uses bbp_get_forum_post_type() To get the forum post type
    1571  * @uses get_posts() To get the forums
    1572  * @uses bbp_update_forum_reply_count() To update the forum reply count
    1573  * @return array An array of the status code and the message
    1574  */
    1575 function bbp_admin_repair_forum_reply_count() {
    1576 
    1577     // Define variables
    1578     $bbp_db    = bbp_db();
    1579     $statement = __( 'Counting the number of replies in each forum&hellip; %s', 'bbpress' );
    1580     $result    = __( 'Failed!', 'bbpress' );
    1581 
    1582     // Post type
    1583     $fpt = bbp_get_forum_post_type();
    1584 
    1585     // Delete the meta keys _bbp_reply_count and _bbp_total_reply_count for each forum
    1586     $sql_delete = "DELETE `postmeta` FROM `{$bbp_db->postmeta}` AS `postmeta`
    1587                         LEFT JOIN `{$bbp_db->posts}` AS `posts` ON `posts`.`ID` = `postmeta`.`post_id`
    1588                         WHERE `posts`.`post_type` = '{$fpt}'
    1589                         AND `postmeta`.`meta_key` = '_bbp_reply_count'
    1590                         OR `postmeta`.`meta_key` = '_bbp_total_reply_count'";
    1591 
    1592     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1593         return array( 1, sprintf( $statement, $result ) );
    1594     }
    1595 
    1596     // Recalculate the metas key _bbp_reply_count and _bbp_total_reply_count for each forum
    1597     $forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
    1598     if ( ! empty( $forums ) ) {
    1599         foreach ( $forums as $forum ) {
    1600             bbp_update_forum_reply_count( $forum->ID );
    1601         }
    1602     } else {
    1603         return array( 2, sprintf( $statement, $result ) );
    1604     }
    1605 
    1606     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1607 }
    1608 
    1609 /**
    1610  * Recount topics by the users
    1611  *
    1612  * @since 2.1.0 bbPress (r3889)
    1613  *
    1614  * @uses bbp_get_topic_post_type() To get the topic post type
    1615  * @uses bbp_get_public_status_id() To get the public status id
    1616  * @uses wpdb::query() To run our recount sql queries
    1617  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1618  * @return array An array of the status code and the message
    1619  */
    1620 function bbp_admin_repair_user_topic_count() {
    1621 
    1622     // Define variables
    1623     $bbp_db      = bbp_db();
    1624     $statement   = __( 'Counting the number of topics each user has created&hellip; %s', 'bbpress' );
    1625     $result      = __( 'Failed!', 'bbpress' );
    1626 
    1627     $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`";
    1628     $insert_rows = $bbp_db->get_results( $sql_select );
    1629 
    1630     if ( is_wp_error( $insert_rows ) ) {
    1631         return array( 1, sprintf( $statement, $result ) );
    1632     }
    1633 
    1634     $key           = $bbp_db->prefix . '_bbp_topic_count';
    1635     $insert_values = array();
    1636     foreach ( $insert_rows as $insert_row ) {
    1637         $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    1638     }
    1639 
    1640     if ( !count( $insert_values ) ) {
    1641         return array( 2, sprintf( $statement, $result ) );
    1642     }
    1643 
    1644     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    1645     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1646         return array( 3, sprintf( $statement, $result ) );
    1647     }
    1648 
    1649     foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    1650         $chunk = "\n" . implode( ",\n", $chunk );
    1651         $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    1652 
    1653         if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    1654             return array( 4, sprintf( $statement, $result ) );
    1655         }
    1656     }
    1657 
    1658     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1659 }
    1660 
    1661 /**
    1662  * Recount topic replied by the users
    1663  *
    1664  * @since 2.0.0 bbPress (r2613)
    1665  *
    1666  * @uses bbp_get_reply_post_type() To get the reply post type
    1667  * @uses bbp_get_public_status_id() To get the public status id
    1668  * @uses wpdb::query() To run our recount sql queries
    1669  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1670  * @return array An array of the status code and the message
    1671  */
    1672 function bbp_admin_repair_user_reply_count() {
    1673 
    1674     // Define variables
    1675     $bbp_db    = bbp_db();
    1676     $statement   = __( 'Counting the number of topics to which each user has replied&hellip; %s', 'bbpress' );
    1677     $result      = __( 'Failed!', 'bbpress' );
    1678 
    1679     $sql_select  = "SELECT `post_author`, COUNT(DISTINCT `ID`) as `_count` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "' GROUP BY `post_author`";
    1680     $insert_rows = $bbp_db->get_results( $sql_select );
    1681 
    1682     if ( is_wp_error( $insert_rows ) ) {
    1683         return array( 1, sprintf( $statement, $result ) );
    1684     }
    1685 
    1686     $key           = $bbp_db->prefix . '_bbp_reply_count';
    1687     $insert_values = array();
    1688     foreach ( $insert_rows as $insert_row ) {
    1689         $insert_values[] = "('{$insert_row->post_author}', '{$key}', '{$insert_row->_count}')";
    1690     }
    1691 
    1692     if ( !count( $insert_values ) ) {
    1693         return array( 2, sprintf( $statement, $result ) );
    1694     }
    1695 
    1696     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    1697     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1698         return array( 3, sprintf( $statement, $result ) );
    1699     }
    1700 
    1701     foreach ( array_chunk( $insert_values, 10000 ) as $chunk ) {
    1702         $chunk = "\n" . implode( ",\n", $chunk );
    1703         $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    1704 
    1705         if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    1706             return array( 4, sprintf( $statement, $result ) );
    1707         }
    1708     }
    1709 
    1710     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1711 }
    1712 
    1713 /**
    1714  * Repair user favorites
    1715  *
    1716  * @since 2.0.0 bbPress (r2613)
    1717  *
    1718  * @uses wpdb::query() To run our recount sql queries
    1719  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1720  * @uses bbp_get_topic_post_type() To get the topic post type
    1721  * @uses bbp_get_public_status_id() To get the public status id
    1722  * @return array An array of the status code and the message
    1723  */
    1724 function bbp_admin_repair_user_favorites() {
    1725 
    1726     // Define variables
    1727     $bbp_db    = bbp_db();
    1728     $statement = __( 'Removing unpublished topics from user favorites&hellip; %s', 'bbpress' );
    1729     $result    = __( 'Failed!', 'bbpress' );
    1730 
    1731     // Query for users with favorites
    1732     $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `favorites` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    1733 
    1734     if ( is_wp_error( $users ) ) {
    1735         return array( 1, sprintf( $statement, $result ) );
    1736     }
    1737 
    1738     $topics = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "'" );
    1739 
    1740     if ( is_wp_error( $topics ) ) {
    1741         return array( 2, sprintf( $statement, $result ) );
    1742     }
    1743 
    1744     $values = array();
    1745     foreach ( $users as $user ) {
    1746         if ( empty( $user->favorites ) || ! is_string( $user->favorites ) ) {
    1747             continue;
    1748         }
    1749 
    1750         $favorites = array_intersect( $topics, explode( ',', $user->favorites ) );
    1751         if ( empty( $favorites ) || ! is_array( $favorites ) ) {
    1752             continue;
    1753         }
    1754 
    1755         $favorites_joined = implode( ',', $favorites );
    1756         $values[]         = "('{$user->user_id}', '{$key}, '{$favorites_joined}')";
    1757 
    1758         // Cleanup
    1759         unset( $favorites, $favorites_joined );
    1760     }
    1761 
    1762     if ( !count( $values ) ) {
    1763         $result = __( 'Nothing to remove!', 'bbpress' );
    1764         return array( 0, sprintf( $statement, $result ) );
    1765     }
    1766 
    1767     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    1768     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1769         return array( 4, sprintf( $statement, $result ) );
    1770     }
    1771 
    1772     foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    1773         $chunk = "\n" . implode( ",\n", $chunk );
    1774         $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    1775         if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    1776             return array( 5, sprintf( $statement, $result ) );
    1777         }
    1778     }
    1779 
    1780     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1781 }
    1782 
    1783 /**
    1784  * Clean the users' topic subscriptions
    1785  *
    1786  * @since 2.0.0 bbPress (r2668)
    1787  *
    1788  * @uses wpdb::query() To run our recount sql queries
    1789  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1790  * @uses bbp_get_topic_post_type() To get the topic post type
    1791  * @uses bbp_get_public_status_id() To get the public status id
    1792  * @return array An array of the status code and the message
    1793  */
    1794 function bbp_admin_repair_user_topic_subscriptions() {
    1795 
    1796     // Define variables
    1797     $bbp_db    = bbp_db();
    1798     $statement = __( 'Removing trashed topics from user subscriptions&hellip; %s', 'bbpress' );
    1799     $result    = __( 'Failed!', 'bbpress' );
    1800 
    1801     $key       = $bbp_db->prefix . '_bbp_subscriptions';
    1802     $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    1803 
    1804     if ( is_wp_error( $users ) ) {
    1805         return array( 1, sprintf( $statement, $result ) );
    1806     }
    1807 
    1808     $topics = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "'" );
    1809     if ( is_wp_error( $topics ) ) {
    1810         return array( 2, sprintf( $statement, $result ) );
    1811     }
    1812 
    1813     $values = array();
    1814     foreach ( $users as $user ) {
    1815         if ( empty( $user->subscriptions ) || ! is_string( $user->subscriptions ) ) {
    1816             continue;
    1817         }
    1818 
    1819         $subscriptions = array_intersect( $topics, explode( ',', $user->subscriptions ) );
    1820         if ( empty( $subscriptions ) || ! is_array( $subscriptions ) ) {
    1821             continue;
    1822         }
    1823 
    1824         $subscriptions_joined = implode( ',', $subscriptions );
    1825         $values[]             = "('{$user->user_id}', '{$key}', '{$subscriptions_joined}')";
    1826 
    1827         // Cleanup
    1828         unset( $subscriptions, $subscriptions_joined );
    1829     }
    1830 
    1831     if ( !count( $values ) ) {
    1832         $result = __( 'Nothing to remove!', 'bbpress' );
    1833         return array( 0, sprintf( $statement, $result ) );
    1834     }
    1835 
    1836     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    1837     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1838         return array( 4, sprintf( $statement, $result ) );
    1839     }
    1840 
    1841     foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    1842         $chunk = "\n" . implode( ",\n", $chunk );
    1843         $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    1844         if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    1845             return array( 5, sprintf( $statement, $result ) );
    1846         }
    1847     }
    1848 
    1849     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1850 }
    1851 
    1852 /**
    1853  * Clean the users' forum subscriptions
    1854  *
    1855  * @since 2.5.0 bbPress (r5155)
    1856  *
    1857  * @uses wpdb::query() To run our recount sql queries
    1858  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1859  * @uses bbp_get_forum_post_type() To get the forum post type
    1860  * @uses bbp_get_public_status_id() To get the public status id
    1861  * @return array An array of the status code and the message
    1862  */
    1863 function bbp_admin_repair_user_forum_subscriptions() {
    1864 
    1865     // Define variables
    1866     $bbp_db    = bbp_db();
    1867     $statement = __( 'Removing trashed forums from user subscriptions&hellip; %s', 'bbpress' );
    1868     $result    = __( 'Failed!', 'bbpress' );
    1869 
    1870     $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
    1871     $users     = $bbp_db->get_results( "SELECT `user_id`, `meta_value` AS `subscriptions` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'" );
    1872 
    1873     if ( is_wp_error( $users ) ) {
    1874         return array( 1, sprintf( $statement, $result ) );
    1875     }
    1876 
    1877     $forums = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "' AND `post_status` = '" . bbp_get_public_status_id() . "'" );
    1878     if ( is_wp_error( $forums ) ) {
    1879         return array( 2, sprintf( $statement, $result ) );
    1880     }
    1881 
    1882     $values = array();
    1883     foreach ( $users as $user ) {
    1884         if ( empty( $user->subscriptions ) || ! is_string( $user->subscriptions ) ) {
    1885             continue;
    1886         }
    1887 
    1888         $subscriptions = array_intersect( $forums, explode( ',', $user->subscriptions ) );
    1889         if ( empty( $subscriptions ) || ! is_array( $subscriptions ) ) {
    1890             continue;
    1891         }
    1892 
    1893         $subscriptions_joined = implode( ',', $subscriptions );
    1894         $values[]             = "('{$user->user_id}', '{$key}', '{$subscriptions_joined}')";
    1895 
    1896         // Cleanup
    1897         unset( $subscriptions, $subscriptions_joined );
    1898     }
    1899 
    1900     if ( !count( $values ) ) {
    1901         $result = __( 'Nothing to remove!', 'bbpress' );
    1902         return array( 0, sprintf( $statement, $result ) );
    1903     }
    1904 
    1905     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '{$key}'";
    1906     if ( is_wp_error( $bbp_db->query( $sql_delete ) ) ) {
    1907         return array( 4, sprintf( $statement, $result ) );
    1908     }
    1909 
    1910     foreach ( array_chunk( $values, 10000 ) as $chunk ) {
    1911         $chunk = "\n" . implode( ",\n", $chunk );
    1912         $sql_insert = "INSERT INTO `{$bbp_db->usermeta}` (`user_id`, `meta_key`, `meta_value`) VALUES {$chunk}";
    1913         if ( is_wp_error( $bbp_db->query( $sql_insert ) ) ) {
    1914             return array( 5, sprintf( $statement, $result ) );
    1915         }
    1916     }
    1917 
    1918     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    1919 }
    1920 
    1921 /**
    1922  * This repair tool will map each user of the current site to their respective
    1923  * forums role. By default, Admins will be Key Masters, and every other role
    1924  * will be the default role defined in Settings > Forums (Participant).
    1925  *
    1926  * @since 2.2.0 bbPress (r4340)
    1927  *
    1928  * @uses bbp_get_user_role_map() To get the map of user roles
    1929  * @uses bbp_get_default_role() To get the default bbPress user role
    1930  * @uses bbp_get_blog_roles() To get the current WordPress roles
    1931  * @uses get_users() To get the users of each role (limited to ID field)
    1932  * @uses bbp_set_user_role() To set each user's forums role
    1933  */
    1934 function bbp_admin_repair_user_roles() {
    1935 
    1936     $statement    = __( 'Remapping forum role for each user on this site&hellip; %s', 'bbpress' );
    1937     $changed      = 0;
    1938     $role_map     = bbp_get_user_role_map();
    1939     $default_role = bbp_get_default_role();
    1940 
    1941     // Bail if no role map exists
    1942     if ( empty( $role_map ) ) {
    1943         return array( 1, sprintf( $statement, __( 'Failed!', 'bbpress' ) ) );
    1944     }
    1945 
    1946     // Iterate through each role...
    1947     foreach ( array_keys( bbp_get_blog_roles() ) as $role ) {
    1948 
    1949         // Reset the offset
    1950         $offset = 0;
    1951 
    1952         // If no role map exists, give the default forum role (bbp-participant)
    1953         $new_role = isset( $role_map[ $role ] ) ? $role_map[ $role ] : $default_role;
    1954 
    1955         // Get users of this site, limited to 1000
    1956         while ( $users = get_users( array(
    1957             'role'   => $role,
    1958             'fields' => 'ID',
    1959             'number' => 1000,
    1960             'offset' => $offset
    1961         ) ) ) {
    1962 
    1963             // Iterate through each user of $role and try to set it
    1964             foreach ( (array) $users as $user_id ) {
    1965                 if ( bbp_set_user_role( $user_id, $new_role ) ) {
    1966                     ++$changed; // Keep a count to display at the end
    1967                 }
    1968             }
    1969 
    1970             // Bump the offset for the next query iteration
    1971             $offset = $offset + 1000;
    1972         }
    1973     }
    1974 
    1975     $result = sprintf( __( 'Complete! %s users updated.', 'bbpress' ), bbp_number_format( $changed ) );
    1976 
    1977     return array( 0, sprintf( $statement, $result ) );
    1978 }
    1979 
    1980 /**
    1981  * Recaches the last post in every topic and forum
    1982  *
    1983  * @since 2.0.0 bbPress (r3040)
    1984  *
    1985  * @uses wpdb::query() To run our recount sql queries
    1986  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    1987  * @uses bbp_get_forum_post_type() To get the forum post type
    1988  * @uses bbp_get_topic_post_type() To get the topic post type
    1989  * @uses bbp_get_reply_post_type() To get the reply post type
    1990  * @uses bbp_get_public_status_id() To get the public status id
    1991  * @uses bbp_is_forum_category() To check if the forum is a ategory
    1992  * @uses bbp_update_forum() To update the forums forum id
    1993  * @return array An array of the status code and the message
    1994  */
    1995 function bbp_admin_repair_freshness() {
    1996 
    1997     // Define variables
    1998     $bbp_db    = bbp_db();
    1999     $statement = __( 'Recomputing latest post in every topic and forum&hellip; %s', 'bbpress' );
    2000     $result    = __( 'Failed!', 'bbpress' );
    2001 
    2002     // First, delete everything.
    2003     if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id', '_bbp_last_active_time' )" ) ) ) {
    2004         return array( 1, sprintf( $statement, $result ) );
    2005     }
    2006 
    2007     // Post types and status
    2008     $fpt = bbp_get_forum_post_type();
    2009     $tpt = bbp_get_topic_post_type();
    2010     $rpt = bbp_get_reply_post_type();
    2011     $pps = bbp_get_public_status_id();
    2012 
    2013     // Next, give all the topics with replies the ID their last reply.
    2014     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2015             ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
    2016             FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    2017             WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    2018             GROUP BY `topic`.`ID` )" ) ) ) {
    2019         return array( 2, sprintf( $statement, $result ) );
    2020     }
    2021 
    2022     // For any remaining topics, give a reply ID of 0.
    2023     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2024             ( SELECT `ID`, '_bbp_last_reply_id', 0
    2025             FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    2026             ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id'
    2027             WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' )" ) ) ) {
    2028         return array( 3, sprintf( $statement, $result ) );
    2029     }
    2030 
    2031     // Now we give all the forums with topics the ID their last topic.
    2032     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2033             ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
    2034             FROM `{$bbp_db->posts}` AS `forum` INNER JOIN `{$bbp_db->posts}` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
    2035             WHERE `topic`.`post_status` = '{$pps}' AND `forum`.`post_type` = '{$fpt}' AND `topic`.`post_type` = '{$tpt}'
    2036             GROUP BY `forum`.`ID` )" ) ) ) {
    2037         return array( 4, sprintf( $statement, $result ) );
    2038     }
    2039 
    2040     // For any remaining forums, give a topic ID of 0.
    2041     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2042             ( SELECT `ID`, '_bbp_last_topic_id', 0
    2043             FROM `{$bbp_db->posts}` AS `forum` LEFT JOIN `{$bbp_db->postmeta}` AS `topic`
    2044             ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id'
    2045             WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = '{$fpt}' )" ) ) ) {
    2046         return array( 5, sprintf( $statement, $result ) );
    2047     }
    2048 
    2049     // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
    2050     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2051             ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
    2052             FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    2053             WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    2054             GROUP BY `topic`.`ID` )" ) ) ) {
    2055         return array( 6, sprintf( $statement, $result ) );
    2056     }
    2057 
    2058     // For any remaining topics, give a reply ID of themself.
    2059     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2060             ( SELECT `ID`, '_bbp_last_active_id', `ID`
    2061             FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    2062             ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
    2063             WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' )" ) ) ) {
    2064         return array( 7, sprintf( $statement, $result ) );
    2065     }
    2066 
    2067     // Give topics with replies their last update time.
    2068     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2069             ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
    2070             FROM `{$bbp_db->posts}` AS `topic` INNER JOIN `{$bbp_db->posts}` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    2071             WHERE `reply`.`post_status` = '{$pps}' AND `topic`.`post_type` = '{$tpt}' AND `reply`.`post_type` = '{$rpt}'
    2072             GROUP BY `topic`.`ID` )" ) ) ) {
    2073         return array( 8, sprintf( $statement, $result ) );
    2074     }
    2075 
    2076     // Give topics without replies their last update time.
    2077     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2078             ( SELECT `ID`, '_bbp_last_active_time', `post_date`
    2079             FROM `{$bbp_db->posts}` AS `topic` LEFT JOIN `{$bbp_db->postmeta}` AS `reply`
    2080             ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
    2081             WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = '{$tpt}' )" ) ) ) {
    2082         return array( 9, sprintf( $statement, $result ) );
    2083     }
    2084 
    2085     // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
    2086     $forums = $bbp_db->get_col( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` = '{$fpt}' and `post_status` != 'auto-draft'" );
    2087     if ( is_wp_error( $forums ) ) {
    2088         return array( 10, sprintf( $statement, $result ) );
    2089     }
    2090 
    2091     // Loop through forums
    2092     foreach ( $forums as $forum_id ) {
    2093         if ( ! bbp_is_forum_category( $forum_id ) ) {
    2094             bbp_update_forum( array( 'forum_id' => $forum_id ) );
    2095         }
    2096     }
    2097 
    2098     // Loop through categories when forums are done
    2099     foreach ( $forums as $forum_id ) {
    2100         if ( bbp_is_forum_category( $forum_id ) ) {
    2101             bbp_update_forum( array( 'forum_id' => $forum_id ) );
    2102         }
    2103     }
    2104 
    2105     // Complete results
    2106     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2107 }
    2108 
    2109 /**
    2110  * Repairs the relationship of sticky topics to the actual parent forum
    2111  *
    2112  * @since 2.3.0 bbPress (r4695)
    2113  *
    2114  * @uses wpdb::get_col() To run our recount sql queries
    2115  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    2116  * @uses bbp_get_forum_post_type() To get the forum post type
    2117  * @uses get_post_meta() To get the sticky topics
    2118  * @uses bbp_is_topic_super_sticky() To check if the topic is super sticky
    2119  * @uses bbp_get_topic_forum_id() To get the topics forum id
    2120  * @uses update_post_meta To update the topics sticky post meta
    2121  * @return array An array of the status code and the message
    2122  */
    2123 function bbp_admin_repair_sticky() {
    2124 
    2125     // Define variables
    2126     $bbp_db    = bbp_db();
    2127     $statement = __( 'Repairing the sticky topic to the parent forum relationships&hellip; %s', 'bbpress' );
    2128     $result    = __( 'Failed!', 'bbpress' );
    2129 
    2130     $forums    = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_forum_post_type() . "'" );
    2131 
    2132     // Bail if no forums found
    2133     if ( empty( $forums ) || is_wp_error( $forums ) ) {
    2134         return array( 1, sprintf( $statement, $result ) );
    2135     }
    2136 
    2137     // Loop through forums and get their sticky topics
    2138     foreach ( $forums as $forum ) {
    2139         $forum_stickies[ $forum ] = get_post_meta( $forum, '_bbp_sticky_topics', true );
    2140     }
    2141 
    2142     // Cleanup
    2143     unset( $forums, $forum );
    2144 
    2145     // Loop through each forum with sticky topics
    2146     foreach ( $forum_stickies as $forum_id => $stickies ) {
    2147 
    2148         // Skip if no stickies
    2149         if ( empty( $stickies ) ) {
    2150             continue;
    2151         }
    2152 
    2153         // Loop through each sticky topic
    2154         foreach ( $stickies as $id => $topic_id ) {
    2155 
    2156             // If the topic is not a super sticky, and the forum ID does not
    2157             // match the topic's forum ID, unset the forum's sticky meta.
    2158             if ( ! bbp_is_topic_super_sticky( $topic_id ) && $forum_id !== bbp_get_topic_forum_id( $topic_id ) ) {
    2159                 unset( $forum_stickies[ $forum_id ][ $id ] );
    2160             }
    2161         }
    2162 
    2163         // Get sticky topic ID's, or use empty string
    2164         $stickers = empty( $forum_stickies[ $forum_id ] ) ? '' : array_values( $forum_stickies[ $forum_id ] );
    2165 
    2166         // Update the forum's sticky topics meta
    2167         update_post_meta( $forum_id, '_bbp_sticky_topics', $stickers );
    2168     }
    2169 
    2170     // Complete results
    2171     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2172 }
    2173 
    2174 /**
    2175  * Repair closed topics
    2176  *
    2177  * Closed topics that are missing the postmeta "_bbp_status" key value "publish"
    2178  * result in unexpected behaviour, primarily this would have only occured if you
    2179  * had imported forums from another forum package previous to bbPress v2.6,
    2180  * https://bbpress.trac.wordpress.org/ticket/2577
    2181  *
    2182  * @since 2.6.0 bbPress (r5668)
    2183  *
    2184  * @uses wpdb::get_col() To run our recount sql queries
    2185  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    2186  * @uses bbp_get_topic_post_type() To get the topic post type
    2187  * @uses get_post_meta() To get the closed topic status meta
    2188  * @uses update_post_meta To update the topics closed status post meta
    2189  * @return array An array of the status code and the message
    2190  */
    2191 function bbp_admin_repair_closed_topics() {
    2192 
    2193     // Define variables
    2194     $bbp_db        = bbp_db();
    2195     $statement     = __( 'Repairing closed topics&hellip; %s', 'bbpress' );
    2196     $result        = __( 'No closed topics to repair.', 'bbpress' );
    2197     $changed       = 0;
    2198 
    2199     $closed_topics = $bbp_db->get_col( "SELECT ID FROM `{$bbp_db->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = 'closed'" );
    2200 
    2201     // Bail if no closed topics found
    2202     if ( empty( $closed_topics ) || is_wp_error( $closed_topics ) ) {
    2203         return array( 1, sprintf( $statement, $result ) );
    2204     }
    2205 
    2206     // Loop through each closed topic
    2207     foreach ( $closed_topics as $closed_topic ) {
    2208 
    2209         // Check if the closed topic already has a postmeta _bbp_status value
    2210         $topic_status = get_post_meta( $closed_topic, '_bbp_status', true );
    2211 
    2212         // If we don't have a postmeta _bbp_status value
    2213         if( empty( $topic_status ) ) {
    2214             update_post_meta( $closed_topic, '_bbp_status', 'publish' );
    2215             ++$changed; // Keep a count to display at the end
    2216         }
    2217     }
    2218 
    2219     // Cleanup
    2220     unset( $closed_topics, $closed_topic, $topic_status );
    2221 
    2222     // Complete results
    2223     $result = sprintf( _n( 'Complete! %d closed topic repaired.', 'Complete! %d closed topics repaired.', $changed, 'bbpress' ), $changed );
    2224 
    2225     return array( 0, sprintf( $statement, $result ) );
    2226 }
    2227 
    2228 /**
    2229  * Recaches the private and hidden forums
    2230  *
    2231  * @since 2.2.0 bbPress (r4104)
    2232  *
    2233  * @uses bbp_repair_forum_visibility() To update private and hidden forum ids
    2234  * @return array An array of the status code and the message
    2235  */
    2236 function bbp_admin_repair_forum_visibility() {
    2237     $statement = __( 'Recalculating forum visibility &hellip; %s', 'bbpress' );
    2238 
    2239     // Bail if queries returned errors
    2240     if ( ! bbp_repair_forum_visibility() ) {
    2241         return array( 2, sprintf( $statement, __( 'Failed!',   'bbpress' ) ) );
    2242 
    2243     // Complete results
    2244     } else {
    2245         return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2246     }
    2247 }
    2248 
    2249 /**
    2250  * Recaches the parent forum meta for each topic and reply
    2251  *
    2252  * @since 2.1.0 bbPress (r3876)
    2253  *
    2254  * @uses wpdb::query() To run our recount sql queries
    2255  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    2256  * @uses bbp_get_topic_post_type() To get the topic post type
    2257  * @uses bbp_get_reply_post_type() To get the reply post type
    2258  * @return array An array of the status code and the message
    2259  */
    2260 function bbp_admin_repair_forum_meta() {
    2261 
    2262     // Define variables
    2263     $bbp_db    = bbp_db();
    2264     $statement = __( 'Recalculating the forum for each post &hellip; %s', 'bbpress' );
    2265     $result    = __( 'Failed!', 'bbpress' );
    2266 
    2267     // First, delete everything.
    2268     if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_forum_id'" ) ) ) {
    2269         return array( 1, sprintf( $statement, $result ) );
    2270     }
    2271 
    2272     // Post types and status
    2273     $tpt = bbp_get_topic_post_type();
    2274     $rpt = bbp_get_reply_post_type();
    2275 
    2276     // Next, give all the topics their parent forum id.
    2277     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2278             ( SELECT `topic`.`ID`, '_bbp_forum_id', `topic`.`post_parent`
    2279             FROM `$bbp_db->posts`
    2280                 AS `topic`
    2281             WHERE `topic`.`post_type` = '{$tpt}'
    2282             GROUP BY `topic`.`ID` )" ) ) ) {
    2283         return array( 2, sprintf( $statement, $result ) );
    2284     }
    2285 
    2286     // Next, give all the replies their parent forum id.
    2287     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2288             ( SELECT `reply`.`ID`, '_bbp_forum_id', `topic`.`post_parent`
    2289             FROM `$bbp_db->posts`
    2290                 AS `reply`
    2291             INNER JOIN `$bbp_db->posts`
    2292                 AS `topic`
    2293                 ON `reply`.`post_parent` = `topic`.`ID`
    2294             WHERE `topic`.`post_type` = '{$tpt}'
    2295                 AND `reply`.`post_type` = '{$rpt}'
    2296             GROUP BY `reply`.`ID` )" ) ) ) {
    2297         return array( 3, sprintf( $statement, $result ) );
    2298     }
    2299 
    2300     // Complete results
    2301     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2302 }
    2303 
    2304 /**
    2305  * Recaches the topic for each post
    2306  *
    2307  * @since 2.1.0 bbPress (r3876)
    2308  *
    2309  * @uses wpdb::query() To run our recount sql queries
    2310  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    2311  * @uses bbp_get_topic_post_type() To get the topic post type
    2312  * @uses bbp_get_reply_post_type() To get the reply post type
    2313  * @return array An array of the status code and the message
    2314  */
    2315 function bbp_admin_repair_topic_meta() {
    2316 
    2317     // Define variables
    2318     $bbp_db    = bbp_db();
    2319     $statement = __( 'Recalculating the topic for each post &hellip; %s', 'bbpress' );
    2320     $result    = __( 'Failed!', 'bbpress' );
    2321 
    2322     // First, delete everything.
    2323     if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_topic_id'" ) ) ) {
    2324         return array( 1, sprintf( $statement, $result ) );
    2325     }
    2326 
    2327     // Post types and status
    2328     $tpt = bbp_get_topic_post_type();
    2329     $rpt = bbp_get_reply_post_type();
    2330 
    2331     // Next, give all the topics with replies the ID their last reply.
    2332     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2333             ( SELECT `topic`.`ID`, '_bbp_topic_id', `topic`.`ID`
    2334             FROM `$bbp_db->posts`
    2335                 AS `topic`
    2336             WHERE `topic`.`post_type` = '{$tpt}'
    2337             GROUP BY `topic`.`ID` )" ) ) ) {
    2338         return array( 3, sprintf( $statement, $result ) );
    2339     }
    2340 
    2341     // Next, give all the topics with replies the ID their last reply.
    2342     if ( is_wp_error( $bbp_db->query( "INSERT INTO `{$bbp_db->postmeta}` (`post_id`, `meta_key`, `meta_value`)
    2343             ( SELECT `reply`.`ID`, '_bbp_topic_id', `topic`.`ID`
    2344             FROM `$bbp_db->posts`
    2345                 AS `reply`
    2346             INNER JOIN `$bbp_db->posts`
    2347                 AS `topic`
    2348                 ON `reply`.`post_parent` = `topic`.`ID`
    2349             WHERE `topic`.`post_type` = '{$tpt}'
    2350                 AND `reply`.`post_type` = '{$rpt}'
    2351             GROUP BY `reply`.`ID` )" ) ) ) {
    2352         return array( 4, sprintf( $statement, $result ) );
    2353     }
    2354 
    2355     // Complete results
    2356     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2357 }
    2358 
    2359 /**
    2360  * Recalculate reply menu order
    2361  *
    2362  * @since 2.5.4 bbPress (r5367)
    2363  *
    2364  * @uses wpdb::query() To run our recount sql queries
    2365  * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
    2366  * @uses bbp_get_reply_post_type() To get the reply post type
    2367  * @uses bbp_update_reply_position() To update the reply position
    2368  * @return array An array of the status code and the message
    2369  */
    2370 function bbp_admin_repair_reply_menu_order() {
    2371 
    2372     // Define variables
    2373     $bbp_db    = bbp_db();
    2374     $statement = __( 'Recalculating reply menu order &hellip; %s', 'bbpress' );
    2375     $result    = __( 'No reply positions to recalculate.',         'bbpress' );
    2376 
    2377     // Delete cases where `_bbp_reply_to` was accidentally set to itself
    2378     if ( is_wp_error( $bbp_db->query( "DELETE FROM `{$bbp_db->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`" ) ) ) {
    2379         return array( 1, sprintf( $statement, $result ) );
    2380     }
    2381 
    2382     // Post type
    2383     $rpt = bbp_get_reply_post_type();
    2384 
    2385     // Get an array of reply id's to update the menu oder for each reply
    2386     $replies = $bbp_db->get_results( "SELECT `a`.`ID` FROM `{$bbp_db->posts}` AS `a`
    2387                                         INNER JOIN (
    2388                                             SELECT `menu_order`, `post_parent`
    2389                                             FROM `{$bbp_db->posts}`
    2390                                             GROUP BY `menu_order`, `post_parent`
    2391                                             HAVING COUNT( * ) >1
    2392                                         )`b`
    2393                                         ON `a`.`menu_order` = `b`.`menu_order`
    2394                                         AND `a`.`post_parent` = `b`.`post_parent`
    2395                                         WHERE `post_type` = '{$rpt}'", OBJECT_K );
    2396 
    2397     // Bail if no replies returned
    2398     if ( empty( $replies ) ) {
    2399         return array( 1, sprintf( $statement, $result ) );
    2400     }
    2401 
    2402     // Recalculate the menu order position for each reply
    2403     foreach ( $replies as $reply ) {
    2404         bbp_update_reply_position( $reply->ID );
    2405     }
    2406 
    2407     // Cleanup
    2408     unset( $replies, $reply );
    2409 
    2410     // Flush the cache; things are about to get ugly.
    2411     wp_cache_flush();
    2412 
    2413     return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
    2414 }
    2415 
    2416 /** Reset *********************************************************************/
    2417 
    2418 /**
    2419  * Admin reset page
    2420  *
    2421  * @since 2.0.0 bbPress (r2613)
    2422  *
    2423  * @uses check_admin_referer() To verify the nonce and the referer
    2424  * @uses do_action() Calls 'admin_notices' to display the notices
    2425  * @uses wp_nonce_field() To add a hidden nonce field
    2426  */
    2427 function bbp_admin_reset_page() {
    2428 ?>
    2429 
    2430     <div class="wrap">
    2431         <h1><?php esc_html_e( 'Forum Tools', 'bbpress' ); ?></h1>
    2432         <h2 class="nav-tab-wrapper"><?php bbp_tools_admin_tabs( __( 'Reset Forums', 'bbpress' ) ); ?></h2>
    2433         <p><?php esc_html_e( 'Revert your forums back to a brand new installation, as if bbPress were never installed. This process cannot be undone.', 'bbpress' ); ?></p>
    2434 
    2435         <form class="settings" method="post" action="">
    2436             <table class="form-table">
    2437                 <tbody>
    2438                     <tr valign="top">
    2439                         <th scope="row"><?php esc_html_e( 'The following data will be removed:', 'bbpress' ) ?></th>
    2440                         <td>
    2441                             <?php esc_html_e( 'All Forums',           'bbpress' ); ?><br />
    2442                             <?php esc_html_e( 'All Topics',           'bbpress' ); ?><br />
    2443                             <?php esc_html_e( 'All Replies',          'bbpress' ); ?><br />
    2444                             <?php esc_html_e( 'All Topic Tags',       'bbpress' ); ?><br />
    2445                             <?php esc_html_e( 'All Meta Data',        'bbpress' ); ?><br />
    2446                             <?php esc_html_e( 'Forum Settings',       'bbpress' ); ?><br />
    2447                             <?php esc_html_e( 'Forum Activity',       'bbpress' ); ?><br />
    2448                             <?php esc_html_e( 'Forum User Roles',     'bbpress' ); ?><br />
    2449                             <?php esc_html_e( 'Forum Moderators',     'bbpress' ); ?><br />
    2450                             <?php esc_html_e( 'Importer Helper Data', 'bbpress' ); ?><br />
    2451                         </td>
    2452                     </tr>
    2453                     <tr valign="top">
    2454                         <th scope="row"><?php esc_html_e( 'Delete imported users?', 'bbpress' ); ?></th>
    2455                         <td>
    2456                             <fieldset>
    2457                                 <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
    2458                                 <label><input type="checkbox" class="checkbox" name="bbpress-delete-imported-users" id="bbpress-delete-imported-users" value="1" /> <?php esc_html_e( 'This option will delete all previously imported users, and cannot be undone.', 'bbpress' ); ?></label>
    2459                                 <p class="description"><?php esc_html_e( 'Proceeding without this checked removes the meta-data necessary to delete these users later.', 'bbpress' ); ?></p>
    2460                             </fieldset>
    2461                         </td>
    2462                     </tr>
    2463                     <tr valign="top">
    2464                         <th scope="row"><?php esc_html_e( 'Do you really want to do this?', 'bbpress' ); ?></th>
    2465                         <td>
    2466                             <fieldset>
    2467                                 <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
    2468                                 <label><input type="checkbox" class="checkbox" name="bbpress-are-you-sure" id="bbpress-are-you-sure" value="1" /> <?php esc_html_e( 'This process cannot be undone.', 'bbpress' ); ?></label>
    2469                                 <p class="description"><?php esc_html_e( 'Backup your database before proceeding.', 'bbpress' ); ?></p>
    2470                             </fieldset>
    2471                         </td>
    2472                     </tr>
    2473                 </tbody>
    2474             </table>
    2475 
    2476             <fieldset class="submit">
    2477                 <input class="button-primary" type="submit" name="submit" value="<?php esc_attr_e( 'Reset bbPress', 'bbpress' ); ?>" />
    2478                 <?php wp_nonce_field( 'bbpress-reset' ); ?>
    2479             </fieldset>
    2480         </form>
    2481     </div>
    2482 
    2483 <?php
    2484 }
    2485 
    2486 /**
    2487  * Handle the processing and feedback of the admin tools page
    2488  *
    2489  * @since 2.0.0 bbPress (r2613)
    2490  *
    2491  * @uses check_admin_referer() To verify the nonce and the referer
    2492  * @uses wp_cache_flush() To flush the cache
    2493  * @uses bbp_get_forum_post_type() To get the forum post type
    2494  * @uses bbp_get_topic_post_type() To get the topic post type
    2495  * @uses bbp_get_reply_post_type() To get the reply post type
    2496  */
    2497 function bbp_admin_reset_handler() {
    2498 
    2499     // Bail if not resetting
    2500     if ( ! bbp_is_post_request() || empty( $_POST['bbpress-are-you-sure'] ) ) {
    2501         return;
    2502     }
    2503 
    2504     // Only keymasters can proceed
    2505     if ( ! bbp_is_user_keymaster() ) {
    2506         return;
    2507     }
    2508 
    2509     check_admin_referer( 'bbpress-reset' );
    2510 
    2511     // Stores messages
    2512     $messages = array();
    2513     $failed   = __( 'Failed!',   'bbpress' );
    2514     $success  = __( 'Success!', 'bbpress' );
    2515 
    2516     // Flush the cache; things are about to get ugly.
    2517     wp_cache_flush();
    2518 
    2519     /** Posts *****************************************************************/
    2520 
    2521     // Post types and status
    2522     $fpt = bbp_get_forum_post_type();
    2523     $tpt = bbp_get_topic_post_type();
    2524     $rpt = bbp_get_reply_post_type();
    2525 
    2526     // Define variables
    2527     $bbp_db    = bbp_db();
    2528     $statement  = __( 'Deleting Posts&hellip; %s', 'bbpress' );
    2529 
    2530     $sql_posts  = $bbp_db->get_results( "SELECT `ID` FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')", OBJECT_K );
    2531     $sql_delete = "DELETE FROM `{$bbp_db->posts}` WHERE `post_type` IN ('{$fpt}', '{$tpt}', '{$rpt}')";
    2532     $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2533     $messages[] = sprintf( $statement, $result );
    2534 
    2535     /** Post Meta *************************************************************/
    2536 
    2537     if ( ! empty( $sql_posts ) ) {
    2538         $sql_meta = array();
    2539         foreach ( $sql_posts as $key => $value ) {
    2540             $sql_meta[] = $key;
    2541         }
    2542         $statement  = __( 'Deleting Post Meta&hellip; %s', 'bbpress' );
    2543         $sql_meta   = implode( "', '", $sql_meta );
    2544         $sql_delete = "DELETE FROM `{$bbp_db->postmeta}` WHERE `post_id` IN ('{$sql_meta}')";
    2545         $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2546         $messages[] = sprintf( $statement, $result );
    2547     }
    2548 
    2549     /** Post Revisions ********************************************************/
    2550 
    2551     if ( ! empty( $sql_posts ) ) {
    2552         $sql_meta = array();
    2553         foreach ( $sql_posts as $key => $value ) {
    2554             $sql_meta[] = $key;
    2555         }
    2556         $statement  = __( 'Deleting Post Revisions&hellip; %s', 'bbpress' );
    2557         $sql_meta   = implode( "', '", $sql_meta );
    2558         $sql_delete = "DELETE FROM `{$bbp_db->posts}` WHERE `post_parent` IN ('{$sql_meta}') AND `post_type` = 'revision'";
    2559         $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2560         $messages[] = sprintf( $statement, $result );
    2561     }
    2562 
    2563     /** Forum moderators ******************************************************/
    2564 
    2565     $statement  = __( 'Deleting Forum Moderators&hellip; %s', 'bbpress' );
    2566     $sql_delete = "DELETE a,b,c FROM `{$bbp_db->terms}` AS a LEFT JOIN `{$bbp_db->term_taxonomy}` AS c ON a.term_id = c.term_id LEFT JOIN `{$bbp_db->term_relationships}` AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'forum-mod'";
    2567     $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2568     $messages[] = sprintf( $statement, $result );
    2569 
    2570     /** Topic Tags ************************************************************/
    2571 
    2572     $statement  = __( 'Deleting Topic Tags&hellip; %s', 'bbpress' );
    2573     $sql_delete = "DELETE a,b,c FROM `{$bbp_db->terms}` AS a LEFT JOIN `{$bbp_db->term_taxonomy}` AS c ON a.term_id = c.term_id LEFT JOIN `{$bbp_db->term_relationships}` AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'topic-tag'";
    2574     $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2575     $messages[] = sprintf( $statement, $result );
    2576 
    2577     /** User ******************************************************************/
    2578 
    2579     // First, if we're deleting previously imported users, delete them now
    2580     if ( ! empty( $_POST['bbpress-delete-imported-users'] ) ) {
    2581         $sql_users  = $bbp_db->get_results( "SELECT `user_id` FROM `{$bbp_db->usermeta}` WHERE `meta_key` = '_bbp_old_user_id'", OBJECT_K );
    2582         if ( ! empty( $sql_users ) ) {
    2583             $sql_meta = array();
    2584             foreach ( $sql_users as $key => $value ) {
    2585                 $sql_meta[] = $key;
    2586             }
    2587             $statement  = __( 'Deleting Imported Users&hellip; %s', 'bbpress' );
    2588             $sql_meta   = implode( "', '", $sql_meta );
    2589             $sql_delete = "DELETE FROM `{$bbp_db->users}` WHERE `ID` IN ('{$sql_meta}')";
    2590             $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2591             $messages[] = sprintf( $statement, $result );
    2592             $statement  = __( 'Deleting Imported User Meta&hellip; %s', 'bbpress' );
    2593             $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `user_id` IN ('{$sql_meta}')";
    2594             $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2595             $messages[] = sprintf( $statement, $result );
    2596         }
    2597     }
    2598 
    2599     // Next, if we still have users that were not imported delete that meta data
    2600     $statement  = __( 'Deleting User Meta&hellip; %s', 'bbpress' );
    2601     $sql_delete = "DELETE FROM `{$bbp_db->usermeta}` WHERE `meta_key` LIKE '%%_bbp_%%'";
    2602     $result     = is_wp_error( $bbp_db->query( $sql_delete ) ) ? $failed : $success;
    2603     $messages[] = sprintf( $statement, $result );
    2604 
    2605     /** Converter *************************************************************/
    2606 
    2607     $statement  = __( 'Deleting Conversion Table&hellip; %s', 'bbpress' );
    2608     $table_name = $bbp_db->prefix . 'bbp_converter_translator';
    2609     if ( $bbp_db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) === $table_name ) {
    2610         $bbp_db->query( "DROP TABLE {$table_name}" );
    2611         $result = $success;
    2612     } else {
    2613         $result = $failed;
    2614     }
    2615     $messages[] = sprintf( $statement, $result );
    2616 
    2617     /** Options ***************************************************************/
    2618 
    2619     $statement  = __( 'Deleting Settings&hellip; %s', 'bbpress' );
    2620     bbp_delete_options();
    2621     $messages[] = sprintf( $statement, $success );
    2622 
    2623     /** Roles *****************************************************************/
    2624 
    2625     $statement  = __( 'Deleting Roles and Capabilities&hellip; %s', 'bbpress' );
    2626     bbp_remove_roles();
    2627     bbp_remove_caps();
    2628     $messages[] = sprintf( $statement, $success );
    2629 
    2630     /** Output ****************************************************************/
    2631 
    2632     if ( count( $messages ) ) {
    2633         foreach ( $messages as $message ) {
    2634             bbp_admin_tools_feedback( $message );
    2635         }
    2636     }
    2637 }
  • trunk/src/includes/admin/tools/upgrades.php

    r6281 r6282  
    1010// Exit if accessed directly
    1111defined( 'ABSPATH' ) || exit;
    12 
    13 /** Upgrades ******************************************************************/
    1412
    1513/**
     
    172170    $total     = 0;
    173171    $key       = $bbp_db->prefix . '_bbp_favorites';
    174     $favorites = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s", $key ) );
     172    $favs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s", $key ) );
    175173
    176174    // Bail if no closed topics found
    177     if ( empty( $favorites ) || is_wp_error( $favorites ) ) {
     175    if ( empty( $favs ) || is_wp_error( $favs ) ) {
    178176        return array( 1, sprintf( $statement, $result ) );
    179177    }
    180178
    181179    // Loop through each user's favorites
    182     foreach ( $favorites as $meta ) {
     180    foreach ( $favs as $meta ) {
    183181
    184182        // Get post IDs
     
    204202
    205203    // Cleanup
    206     unset( $favorites, $added, $post_ids );
     204    unset( $favs, $added, $post_ids );
    207205
    208206    // Complete results
     
    222220
    223221    // Define variables
    224     $bbp_db        = bbp_db();
    225     $statement     = __( 'Upgrading user topic subscriptions &hellip; %s', 'bbpress' );
    226     $result        = __( 'No topic subscriptions to upgrade.',             'bbpress' );
    227     $total         = 0;
    228     $key           = $bbp_db->prefix . '_bbp_subscriptions';
    229     $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     222    $bbp_db    = bbp_db();
     223    $statement = __( 'Upgrading user topic subscriptions &hellip; %s', 'bbpress' );
     224    $result    = __( 'No topic subscriptions to upgrade.',             'bbpress' );
     225    $total     = 0;
     226    $key       = $bbp_db->prefix . '_bbp_subscriptions';
     227    $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
    230228
    231229    // Bail if no topic subscriptions found
    232     if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
     230    if ( empty( $subs ) || is_wp_error( $subs ) ) {
    233231        return array( 1, sprintf( $statement, $result ) );
    234232    }
    235233
    236234    // Loop through each user's topic subscriptions
    237     foreach ( $subscriptions as $meta ) {
     235    foreach ( $subs as $meta ) {
    238236
    239237        // Get post IDs
     
    259257
    260258    // Cleanup
    261     unset( $subscriptions, $added, $post_ids );
     259    unset( $subs, $added, $post_ids );
    262260
    263261    // Complete results
     
    277275
    278276    // Define variables
    279     $bbp_db        = bbp_db();
    280     $statement     = __( 'Upgrading user forum subscriptions &hellip; %s', 'bbpress' );
    281     $result        = __( 'No forum subscriptions to upgrade.',             'bbpress' );
    282     $total         = 0;
    283     $key           = $bbp_db->prefix . '_bbp_forum_subscriptions';
    284     $subscriptions = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
     277    $bbp_db    = bbp_db();
     278    $statement = __( 'Upgrading user forum subscriptions &hellip; %s', 'bbpress' );
     279    $result    = __( 'No forum subscriptions to upgrade.',             'bbpress' );
     280    $total     = 0;
     281    $key       = $bbp_db->prefix . '_bbp_forum_subscriptions';
     282    $subs      = $bbp_db->get_results( $bbp_db->prepare( "SELECT * FROM {$bbp_db->usermeta} WHERE meta_key = %s ORDER BY user_id", $key ) );
    285283
    286284    // Bail if no forum subscriptions found
    287     if ( empty( $subscriptions ) || is_wp_error( $subscriptions ) ) {
     285    if ( empty( $subs ) || is_wp_error( $subs ) ) {
    288286        return array( 1, sprintf( $statement, $result ) );
    289287    }
    290288
    291289    // Loop through each user's forum subscriptions
    292     foreach ( $subscriptions as $meta ) {
     290    foreach ( $subs as $meta ) {
    293291
    294292        // Get post IDs
     
    314312
    315313    // Cleanup
    316     unset( $subscriptions, $added, $post_ids );
     314    unset( $subs, $added, $post_ids );
    317315
    318316    // Complete results
     
    345343
    346344    // Loop through each user's favorites
    347     foreach ( $favorites as $meta ) {
     345    foreach ( $favs as $meta ) {
    348346
    349347        // Get post IDs
Note: See TracChangeset for help on using the changeset viewer.