Skip to:
Content

bbPress.org

Changeset 5885


Ignore:
Timestamp:
08/06/2015 06:23:06 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Tools: Update "Repair Tools" to use a list-table UI:

  • Includes helper functions for: search, filtering, and bulk actions
  • Improves tool registration by including strings and feedback into an array stored in bbpress()->admin->tools
  • Introduces "Overhead" property for alerting Key Masters to the performance impact running each tool
  • Introduces "Components" properties for identifying which counts what tools are intersecting
  • Todo: use a WP_List_Table subclass (which bbPress has avoided needing until now)

See #2841.

Location:
trunk/src/includes/admin
Files:
3 edited

Legend:

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

    r5833 r5885  
    7474add_action( 'bbp_new_site', 'bbp_create_initial_content', 8 );
    7575
     76// Load the default repair tools
     77add_action( 'load-tools_page_bbp-repair', 'bbp_register_default_repair_tools' );
     78
    7679// Contextual Helpers
    7780add_action( 'load-settings_page_bbpress',    'bbp_admin_settings_help' );
  • trunk/src/includes/admin/admin.php

    r5884 r5885  
    6868     */
    6969    public $show_separator = false;
     70
     71    /** Tools *****************************************************************/
     72
     73    /**
     74     * @var array Array of available repair tools
     75     */
     76    public $tools = array();
    7077
    7178    /** Functions *************************************************************/
  • trunk/src/includes/admin/tools.php

    r5884 r5885  
    1616 * Admin repair page
    1717 *
    18  * @since bbPress (r2613)
     18 * @since bbPress (r2613) Converted from bbPress 1.2
     19 * @since bbPress (r5885) Upgraded to list-table UI
     20 *
     21 * @todo Use a real list table
    1922 *
    2023 * @uses bbp_admin_repair_list() To get the recount list
     
    2528 */
    2629function bbp_admin_repair() {
    27 ?>
     30
     31    // Get the registered repair tools
     32    $tools = bbp_admin_repair_list(); ?>
    2833
    2934    <div class="wrap">
     
    3237
    3338        <p><?php esc_html_e( 'bbPress keeps track of relationships between forums, topics, replies, and topic tags, 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>
    34         <p class="description"><?php esc_html_e( 'Some of these tools create substantial database overhead. Avoid running more than 1 repair job at a time.', 'bbpress' ); ?></p>
    35 
    36         <form class="settings" method="post" action="">
    37             <table class="form-table">
    38                 <tbody>
    39                     <tr valign="top">
    40                         <th scope="row"><?php esc_html_e( 'Relationships to Repair:', 'bbpress' ) ?></th>
    41                         <td>
    42                             <fieldset>
    43                                 <legend class="screen-reader-text"><span><?php esc_html_e( 'Repair', 'bbpress' ) ?></span></legend>
    44 
    45                                 <?php foreach ( bbp_admin_repair_list() as $item ) : ?>
    46 
    47                                     <label><input type="checkbox" class="checkbox" name="<?php echo esc_attr( $item[0] ) . '" id="' . esc_attr( str_replace( '_', '-', $item[0] ) ); ?>" value="1" /> <?php echo esc_html( $item[1] ); ?></label><br />
    48 
    49                                 <?php endforeach; ?>
    50 
    51                             </fieldset>
     39        <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>
     40
     41        <?php bbp_admin_repair_tool_overhead_filters(); ?>
     42
     43        <form class="settings" method="get" action="">
     44
     45            <?php bbp_admin_repair_list_search_form(); ?>
     46
     47            <input type="hidden" name="page" value="bbp-repair" />
     48            <?php wp_nonce_field( 'bbpress-do-counts' ); ?>
     49
     50            <div class="tablenav top">
     51                <div class="alignleft actions bulkactions">
     52                    <label for="bulk-action-selector-top" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
     53                    <select name="action" id="bulk-action-selector-top">
     54                        <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
     55                        <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
     56                    </select>
     57                    <input type="submit" id="doaction" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
     58                </div>
     59                <div class="alignleft actions">
     60
     61                    <?php bbp_admin_repair_list_components_filter(); ?>
     62
     63                </div>
     64                <br class="clear">
     65            </div>
     66            <table class="wp-list-table widefat striped posts">
     67                <thead>
     68                    <tr>
     69                        <td id="cb" class="manage-column column-cb check-column">
     70                            <label class="screen-reader-text" for="cb-select-all-1">
     71                                <?php esc_html_e( 'Select All', 'bbpress' ); ?>
     72                            </label>
     73                            <input id="cb-select-all-1" type="checkbox">
    5274                        </td>
     75                        <th scope="col" id="description" class="manage-column column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
     76                        <th scope="col" id="components" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
     77                        <th scope="col" id="overhead" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
    5378                    </tr>
     79                </thead>
     80
     81                <tbody id="the-list">
     82
     83                    <?php if ( ! empty( $tools ) ) : ?>
     84
     85                        <?php foreach ( $tools as $item ) : ?>
     86
     87                            <tr id="bbp-repair-tools" class="inactive">
     88                                <th scope="row" class="check-column">
     89                                    <label class="screen-reader-text" for="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>"></label>
     90                                    <input type="checkbox" name="checked[]" value="<?php echo esc_attr( $item['id'] ); ?>" id="<?php echo esc_attr( str_replace( '_', '-', $item['id'] ) ); ?>">
     91                                </th>
     92                                <td class="bbp-tool-title column-primary">
     93                                    <strong><?php echo esc_html( $item['description'] ); ?></strong>
     94                                    <div class="row-actions hide-if-no-js">
     95                                        <span class="run">
     96                                            <a href="<?php bbp_admin_repair_tool_run_url( $item['id'] ); ?>" aria-label="<?php printf( esc_html__( 'Run %s', 'bbpress' ), '' ); ?>"><?php esc_html_e( 'Run', 'bbpress' ); ?></a>
     97                                        </span>
     98                                    </div>
     99                                    <button type="button" class="toggle-row">
     100                                        <span class="screen-reader-text"><?php esc_html_e( 'Show more details', 'bbpress' ); ?></span>
     101                                    </button>
     102                                </td>
     103                                <td class="column-components desc">
     104                                    <div class="bbp-tool-overhead">
     105
     106                                        <?php echo implode( ', ', bbp_get_admin_repair_tool_components( $item ) ); ?>
     107
     108                                    </div>
     109                                </td>
     110                                <td class="column-overhead desc">
     111                                    <div class="bbp-tool-overhead">
     112
     113                                        <?php echo esc_html( $item['overhead'] ); ?>
     114
     115                                    </div>
     116                                </td>
     117                            </tr>
     118
     119                        <?php endforeach; ?>
     120
     121                    <?php else : ?>
     122
     123                        <tr>
     124                            <td colspan="4">
     125                                <?php esc_html_e( 'No repair tools match this criteria.', 'bbpress' ); ?>
     126                            </td>
     127                        </tr>
     128
     129                    <?php endif; ?>
     130
    54131                </tbody>
     132                <tfoot>
     133                    <tr>
     134                        <td class="manage-column column-cb check-column">
     135                            <label class="screen-reader-text" for="cb-select-all-2">
     136                                <?php esc_html_e( 'Select All', 'bbpress' ); ?>
     137                            </label>
     138                            <input id="cb-select-all-2" type="checkbox">
     139                        </td>
     140                        <th scope="col" class="manage-column column-description"><?php esc_html_e( 'Description', 'bbpress' ); ?></th>
     141                        <th scope="col" class="manage-column column-components"><?php esc_html_e( 'Components', 'bbpress' ); ?></th>
     142                        <th scope="col" class="manage-column column-overhead"><?php esc_html_e( 'Overhead', 'bbpress' ); ?></th>
     143                    </tr>
     144                </tfoot>
    55145            </table>
    56 
    57             <fieldset class="submit">
    58                 <input class="button-primary" type="submit" name="submit" value="<?php esc_attr_e( 'Repair Items', 'bbpress' ); ?>" />
    59                 <?php wp_nonce_field( 'bbpress-do-counts' ); ?>
    60             </fieldset>
     146            <div class="tablenav bottom">
     147                <div class="alignleft actions bulkactions">
     148                    <label for="bulk-action-selector-bottom" class="screen-reader-text"><?php esc_html_e( 'Select bulk action', 'bbpress' ); ?></label>
     149                    <select name="action2" id="bulk-action-selector-bottom">
     150                        <option value="" selected="selected"><?php esc_html_e( 'Bulk Actions', 'bbpress' ); ?></option>
     151                        <option value="run" class="hide-if-no-js"><?php esc_html_e( 'Run', 'bbpress' ); ?></option>
     152                    </select>
     153                    <input type="submit" id="doaction2" class="button action" value="<?php esc_attr_e( 'Apply', 'bbpress' ); ?>">
     154                </div>
     155            </div>
    61156        </form>
    62157    </div>
     
    77172function bbp_admin_repair_handler() {
    78173
    79     if ( ! bbp_is_post_request() ) {
     174    if ( ! bbp_is_get_request() ) {
     175        return;
     176    }
     177
     178    // Get the current action or bail
     179    if ( ! empty( $_GET['action'] ) ) {
     180        $action = sanitize_key( $_GET['action'] );
     181    } elseif ( ! empty( $_GET['action2'] ) ) {
     182        $action = sanitize_key( $_GET['action2'] );
     183    } else {
     184        return;
     185    }
     186
     187    // Bail if not running an action
     188    if ( 'run' !== $action ) {
    80189        return;
    81190    }
     
    86195    $messages = array();
    87196
     197    // Kill all the caches, because we don't know what's where anymore
    88198    wp_cache_flush();
    89199
    90     foreach ( (array) bbp_admin_repair_list() as $item ) {
    91         if ( isset( $item[2] ) && isset( $_POST[$item[0]] ) && 1 === absint( $_POST[$item[0]] ) && is_callable( $item[2] ) ) {
    92             $messages[] = call_user_func( $item[2] );
    93         }
    94     }
    95 
     200    // Get the list
     201    $list = bbp_get_admin_repair_tools();
     202
     203    // Run through checked repair tools
     204    if ( ! empty( $_GET['checked'] ) ) {
     205        foreach ( $_GET['checked'] as $item_id ) {
     206            if ( isset( $list[ $item_id ] ) && is_callable( $list[ $item_id ]['callback'] ) ) {
     207                $messages[] = call_user_func( $list[ $item_id ]['callback'] );
     208            }
     209        }
     210    }
     211
     212    // Feedback
    96213    if ( count( $messages ) ) {
    97214        foreach ( $messages as $message ) {
     
    99216        }
    100217    }
     218
     219    // @todo Redirect away from here
     220}
     221
     222/**
     223 * Output the URL to run a specific repair tool
     224 *
     225 * @since bbPress (r5885)
     226 *
     227 * @param string $component
     228 */
     229function bbp_admin_repair_tool_run_url( $component = '' ) {
     230    echo esc_url( bbp_get_admin_repair_tool_run_url( $component ) );
     231}
     232
     233/**
     234 * Return the URL to run a specific repair tool
     235 *
     236 * @since bbPress (r5885)
     237 *
     238 * @param string $component
     239 */
     240function bbp_get_admin_repair_tool_run_url( $component = '' ) {
     241    $tools  = admin_url( 'tools.php' );
     242    $args   = array( 'page' => 'bbp-repair', 'action' => 'run', 'checked' => array( $component ) );
     243    $url    = add_query_arg( $args, $tools );
     244    $nonced = wp_nonce_url( $url, 'bbpress-do-counts' );
     245
     246    return apply_filters( 'bbp_get_admin_repair_tool_run_url', $nonced, $component );
    101247}
    102248
     
    284430
    285431/**
     432 * Register an admin area repair tool
     433 *
     434 * @since bbPress (r5885)
     435 *
     436 * @param array $args
     437 * @return
     438 */
     439function bbp_register_repair_tool( $args = array() ) {
     440
     441    // Parse arguments
     442    $r = bbp_parse_args( $args, array(
     443        'id'          => '',
     444        'description' => '',
     445        'callback'    => '',
     446        'priority'    => 0,
     447        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     448        'components'  => array(),
     449
     450        // @todo
     451        'success'     => esc_html__( 'The repair was completed successfully', 'bbpress' ),
     452        'failure'     => esc_html__( 'The repair was not successful',         'bbpress' )
     453    ), 'register_repair_tool' );
     454
     455    // Bail if missing required values
     456    if ( empty( $r['id'] ) || empty( $r['priority'] ) || empty( $r['description'] ) || empty( $r['callback'] ) ) {
     457        return;
     458    }
     459
     460    // Add tool to the registered tools array
     461    bbpress()->admin->tools[ $r['id'] ] = array(
     462        'description' => $r['description'],
     463        'priority'    => $r['priority'],
     464        'callback'    => $r['callback'],
     465        'overhead'    => $r['overhead'],
     466        'components'  => $r['components'],
     467
     468        // @todo
     469        'success'     => $r['success'],
     470        'failure'     => $r['failure'],
     471    );
     472}
     473
     474/**
     475 * Register the default repair tools
     476 *
     477 * @since bbPress (r5885)
     478 */
     479function bbp_register_default_repair_tools() {
     480
     481    // Topic meta
     482    bbp_register_repair_tool( array(
     483        'id'          => 'bbp-sync-topic-meta',
     484        'description' => __( 'Recalculate parent topic for each reply', 'bbpress' ),
     485        'callback'    => 'bbp_admin_repair_topic_meta',
     486        'priority'    => 0,
     487        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     488        'components'  => array( bbp_get_reply_post_type() )
     489    ) );
     490
     491    // Forum meta
     492    bbp_register_repair_tool( array(
     493        'id'          => 'bbp-sync-forum-meta',
     494        'description' => __( 'Recalculate parent forum for each topic and reply', 'bbpress' ),
     495        'callback'    => 'bbp_admin_repair_forum_meta',
     496        'priority'    => 5,
     497        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     498        'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     499    ) );
     500
     501    // Forum visibility
     502    bbp_register_repair_tool( array(
     503        'id'          => 'bbp-sync-forum-visibility',
     504        'description' => __( 'Recalculate private and hidden forums', 'bbpress' ),
     505        'callback'    => 'bbp_admin_repair_forum_visibility',
     506        'priority'    => 10,
     507        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     508        'components'  => array( bbp_get_forum_post_type() )
     509    ) );
     510
     511    // Sync all topics in all forums
     512    bbp_register_repair_tool( array(
     513        'id'          => 'bbp-sync-all-topics-forums',
     514        'description' => __( 'Recalculate last activity in each topic and forum', 'bbpress' ),
     515        'callback'    => 'bbp_admin_repair_freshness',
     516        'priority'    => 15,
     517        'overhead'    => esc_html__( 'High', 'bbpress' ),
     518        'components'  => array( bbp_get_forum_post_type(), bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     519    ) );
     520
     521    // Sync all sticky topics in all forums
     522    bbp_register_repair_tool( array(
     523        'id'          => 'bbp-sync-all-topics-sticky',
     524        'description' => __( 'Recalculate sticky relationship of each topic', 'bbpress' ),
     525        'callback'    => 'bbp_admin_repair_sticky',
     526        'priority'    => 20,
     527        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     528        'components'  => array( bbp_get_topic_post_type() )
     529    ) );
     530
     531    // Sync all hierarchical reply positions
     532    bbp_register_repair_tool( array(
     533        'id'          => 'bbp-sync-all-reply-positions',
     534        'description' => __( 'Recalculate the position of each reply', 'bbpress' ),
     535        'callback'    => 'bbp_admin_repair_reply_menu_order',
     536        'priority'    => 25,
     537        'overhead'    => esc_html__( 'High', 'bbpress' ),
     538        'components'  => array( bbp_get_reply_post_type() )
     539    ) );
     540
     541    // Sync all BuddyPress group forum relationships
     542    bbp_register_repair_tool( array(
     543        'id'          => 'bbp-group-forums',
     544        'description' => __( 'Repair BuddyPress Group Forum relationships', 'bbpress' ),
     545        'callback'    => 'bbp_admin_repair_group_forum_relationship',
     546        'priority'    => 30,
     547        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     548        'components'  => array( bbp_get_forum_post_type() )
     549    ) );
     550
     551    // Update closed topic counts
     552    bbp_register_repair_tool( array(
     553        'id'          => 'bbp-sync-closed-topics',
     554        'description' => __( 'Repair closed topics', 'bbpress' ),
     555        'callback'    => 'bbp_admin_repair_closed_topics',
     556        'priority'    => 35,
     557        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     558        'components'  => array( bbp_get_topic_post_type() )
     559    ) );
     560
     561    // Count topics
     562    bbp_register_repair_tool( array(
     563        'id'          => 'bbp-forum-topics',
     564        'description' => __( 'Count topics in each forum', 'bbpress' ),
     565        'callback'    => 'bbp_admin_repair_forum_topic_count',
     566        'priority'    => 40,
     567        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     568        'components'  => array( bbp_get_forum_post_type(), bbp_get_topic_post_type() )
     569    ) );
     570
     571    // Count forum replies
     572    bbp_register_repair_tool( array(
     573        'id'          => 'bbp-forum-replies',
     574        'description' => __( 'Count replies in each forum', 'bbpress' ),
     575        'callback'    => 'bbp_admin_repair_forum_reply_count',
     576        'priority'    => 45,
     577        'overhead'    => esc_html__( 'High', 'bbpress' ),
     578        'components'  => array( bbp_get_forum_post_type(), bbp_get_reply_post_type() )
     579    ) );
     580
     581    // Count topic replies
     582    bbp_register_repair_tool( array(
     583        'id'          => 'bbp-topic-replies',
     584        'description' => __( 'Count replies in each topic', 'bbpress' ),
     585        'callback'    => 'bbp_admin_repair_topic_reply_count',
     586        'priority'    => 50,
     587        'overhead'    => esc_html__( 'High', 'bbpress' ),
     588        'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     589    ) );
     590
     591    // Count topic voices
     592    bbp_register_repair_tool( array(
     593        'id'          => 'bbp-topic-voices',
     594        'description' => __( 'Count voices in each topic', 'bbpress' ),
     595        'callback'    => 'bbp_admin_repair_topic_voice_count',
     596        'priority'    => 55,
     597        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     598        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     599    ) );
     600
     601    // Count non-published replies to each topic
     602    bbp_register_repair_tool( array(
     603        'id'          => 'bbp-topic-hidden-replies',
     604        'description' => __( 'Count pending, spammed, & trashed replies in each topic', 'bbpress' ),
     605        'callback'    => 'bbp_admin_repair_topic_hidden_reply_count',
     606        'priority'    => 60,
     607        'overhead'    => esc_html__( 'High', 'bbpress' ),
     608        'components'  => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() )
     609    ) );
     610
     611    // Recount topics for each user
     612    bbp_register_repair_tool( array(
     613        'id'          => 'bbp-user-topics',
     614        'description' => __( 'Recount topics for each user', 'bbpress' ),
     615        'callback'    => 'bbp_admin_repair_user_topic_count',
     616        'priority'    => 65,
     617        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     618        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     619    ) );
     620
     621    // Recount topics for each user
     622    bbp_register_repair_tool( array(
     623        'id'          => 'bbp-user-replies',
     624        'description' => __( 'Recount replies for each user', 'bbpress' ),
     625        'callback'    => 'bbp_admin_repair_user_reply_count',
     626        'priority'    => 70,
     627        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     628        'components'  => array( bbp_get_reply_post_type(), bbp_get_user_rewrite_id() )
     629    ) );
     630
     631    // Remove unpublished topics from user favorites
     632    bbp_register_repair_tool( array(
     633        'id'          => 'bbp-user-favorites',
     634        'description' => __( 'Remove unpublished topics from user favorites', 'bbpress' ),
     635        'callback'    => 'bbp_admin_repair_user_favorites',
     636        'priority'    => 75,
     637        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     638        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     639    ) );
     640
     641    // Remove unpublished topics from user subscriptions
     642    bbp_register_repair_tool( array(
     643        'id'          => 'bbp-user-topic-subscriptions',
     644        'description' => __( 'Remove unpublished topics from user subscriptions', 'bbpress' ),
     645        'callback'    => 'bbp_admin_repair_user_topic_subscriptions',
     646        'priority'    => 80,
     647        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     648        'components'  => array( bbp_get_topic_post_type(), bbp_get_user_rewrite_id() )
     649    ) );
     650
     651    // Remove unpublished forums from user subscriptions
     652    bbp_register_repair_tool( array(
     653        'id'          => 'bbp-user-forum-subscriptions',
     654        'description' => __( 'Remove unpublished forums from user subscriptions', 'bbpress' ),
     655        'callback'    => 'bbp_admin_repair_user_forum_subscriptions',
     656        'priority'    => 85,
     657        'overhead'    => esc_html__( 'Medium', 'bbpress' ),
     658        'components'  => array( bbp_get_forum_post_type(), bbp_get_user_rewrite_id() )
     659    ) );
     660
     661    // Remove unpublished forums from user subscriptions
     662    bbp_register_repair_tool( array(
     663        'id'          => 'bbp-user-role-map',
     664        'description' => __( 'Remap existing users to default forum roles', 'bbpress' ),
     665        'callback'    => 'bbp_admin_repair_user_roles',
     666        'priority'    => 90,
     667        'overhead'    => esc_html__( 'Low', 'bbpress' ),
     668        'components'  => array( bbp_get_user_rewrite_id() )
     669    ) );
     670}
     671
     672/**
     673 * Get the array of available repair tools
     674 *
     675 * @since bbPress (r5885)
     676 *
     677 * @return array
     678 */
     679function bbp_get_admin_repair_tools() {
     680
     681    // Get tools array
     682    $tools = ! empty( bbpress()->admin->tools )
     683        ? bbpress()->admin->tools
     684        : array();
     685
     686    return apply_filters( 'bbp_get_admin_repair_tools', $tools );
     687}
     688
     689function bbp_get_admin_repair_tool_registered_components() {
     690    $tools   = bbp_get_admin_repair_tools();
     691    $plucked = wp_list_pluck( $tools, 'components' );
     692    $retval  = array();
     693
     694    foreach ( $plucked as $components ) {
     695        foreach ( $components as $component ) {
     696            if ( in_array( $component, $retval ) ) {
     697                continue;
     698            }
     699            $retval[] = $component;
     700        }
     701    }
     702
     703    return apply_filters( 'bbp_get_admin_repair_tool_registered_components', $retval );
     704}
     705
     706/**
     707 * Output the repair list search form
     708 *
     709 * @since bbPress (r5885)
     710 */
     711function bbp_admin_repair_list_search_form() {
     712
     713    // Get the search string if there is one
     714    $search = ! empty( $_GET['s'] )
     715        ? stripslashes( $_GET['s'] )
     716        : ''; ?>
     717
     718    <p class="search-box">
     719        <label class="screen-reader-text" for="bbp-repair-search-input"><?php esc_html_e( 'Search Tools:', 'bbpress' ); ?></label>
     720        <input type="search" id="bbp-repair-search-input" name="s" value="<?php echo esc_attr( $search ); ?>">
     721        <input type="submit" id="search-submit" class="button" value="<?php esc_html_e( 'Search Tools', 'bbpress' ); ?>">
     722    </p>
     723
     724    <?php
     725}
     726
     727function bbp_admin_repair_list_components_filter() {
     728
     729    $selected = ! empty( $_GET['components'] )
     730        ? sanitize_key( $_GET['components'] )
     731        : '';
     732
     733    $components = bbp_get_admin_repair_tool_registered_components(); ?>
     734
     735    <label class="screen-reader-text" for="cat"><?php esc_html_e( 'Filter by Component', 'bbpress' ); ?></label>
     736    <select name="components" id="components" class="postform">
     737        <option value="" <?php selected( $selected, false ); ?>><?php esc_html_e( 'All Components', 'bbpress' ); ?></option>
     738
     739        <?php foreach ( $components as $component ) : ?>
     740
     741            <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>
     742
     743        <?php endforeach; ?>
     744
     745    </select>
     746    <input type="submit" name="filter_action" id="components-submit" class="button" value="<?php esc_html_e( 'Filter', 'bbpress' ); ?>">
     747
     748    <?php
     749}
     750
     751/**
     752 * Maybe translate a repair tool component name
     753 *
     754 * @since bbPress (r5885)
     755 *
     756 * @param string $component
     757 * @return string
     758 */
     759function bbp_admin_repair_tool_translate_component( $component = '' ) {
     760
     761    // Get the name of the component
     762    switch ( $component ) {
     763        case 'bbp_user' :
     764            $name = esc_html__( 'Users', 'bbpress' );
     765            break;
     766        case bbp_get_forum_post_type() :
     767            $name = esc_html__( 'Forums', 'bbpress' );
     768            break;
     769        case bbp_get_topic_post_type() :
     770            $name = esc_html__( 'Topics', 'bbpress' );
     771            break;
     772        case bbp_get_reply_post_type() :
     773            $name = esc_html__( 'Replies', 'bbpress' );
     774            break;
     775        default;
     776            $name = ucwords( $component );
     777            break;
     778    }
     779
     780    return $name;
     781}
     782
     783/**
    286784 * Get the array of the repair list
    287785 *
     
    292790 */
    293791function bbp_admin_repair_list() {
    294     $repair_list = array(
    295         0  => array( 'bbp-sync-topic-meta',          __( 'Recalculate parent topic for each reply',           'bbpress' ), 'bbp_admin_repair_topic_meta'               ),
    296         5  => array( 'bbp-sync-forum-meta',          __( 'Recalculate parent forum for each topic and reply', 'bbpress' ), 'bbp_admin_repair_forum_meta'               ),
    297         10 => array( 'bbp-sync-forum-visibility',    __( 'Recalculate private and hidden forums',             'bbpress' ), 'bbp_admin_repair_forum_visibility'         ),
    298         15 => array( 'bbp-sync-all-topics-forums',   __( 'Recalculate last activity in each topic and forum', 'bbpress' ), 'bbp_admin_repair_freshness'                ),
    299         20 => array( 'bbp-sync-all-topics-sticky',   __( 'Recalculate sticky relationship of each topic',     'bbpress' ), 'bbp_admin_repair_sticky'                   ),
    300         25 => array( 'bbp-sync-all-reply-positions', __( 'Recalculate the position of each reply',            'bbpress' ), 'bbp_admin_repair_reply_menu_order'         ),
    301         30 => array( 'bbp-group-forums',             __( 'Repair BuddyPress Group Forum relationships',       'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
    302         35 => array( 'bbp-sync-closed-topics',       __( 'Repair closed topics',                              'bbpress' ), 'bbp_admin_repair_closed_topics'            ),
    303         40 => array( 'bbp-forum-topics',             __( 'Count topics in each forum',                        'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
    304         45 => array( 'bbp-forum-replies',            __( 'Count replies in each forum',                       'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
    305         50 => array( 'bbp-topic-replies',            __( 'Count replies in each topic',                       'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
    306         55 => array( 'bbp-topic-voices',             __( 'Count voices in each topic',                        'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
    307         60 => array( 'bbp-topic-hidden-replies',     __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
    308         65 => array( 'bbp-user-topics',              __( 'Count topics for each user',                        'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
    309         70 => array( 'bbp-user-replies',             __( 'Count replies for each user',                       'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
    310         75 => array( 'bbp-user-favorites',           __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
    311         80 => array( 'bbp-user-topic-subscriptions', __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_topic_subscriptions' ),
    312         85 => array( 'bbp-user-forum-subscriptions', __( 'Remove trashed forums from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_forum_subscriptions' ),
    313         90 => array( 'bbp-user-role-map',            __( 'Remap existing users to default forum roles',       'bbpress' ), 'bbp_admin_repair_user_roles'               )
    314     );
     792
     793    // Define empty array
     794    $repair_list = array();
     795
     796    // Get the available tools
     797    $list      = bbp_get_admin_repair_tools();
     798    $search    = ! empty( $_GET['s']          ) ? stripslashes( $_GET['s']          ) : '';
     799    $overhead  = ! empty( $_GET['overhead']   ) ? sanitize_key( $_GET['overhead']   ) : '';
     800    $component = ! empty( $_GET['components'] ) ? sanitize_key( $_GET['components'] ) : '';
     801
     802    // Overhead filter
     803    if ( ! empty( $overhead ) ) {
     804        $list = wp_list_filter( $list, array( 'overhead' => ucwords( $overhead ) ) );
     805    }
     806
     807    // Loop through and key by priority for sorting
     808    foreach ( $list as $id => $tool ) {
     809
     810        // Component filter
     811        if ( ! empty( $component ) ) {
     812            if ( ! in_array( $component, $tool['components'] ) ) {
     813                continue;
     814            }
     815        }
     816
     817        // Search
     818        if ( ! empty( $search ) ) {
     819            if ( ! strstr( strtolower( $tool['description'] ), strtolower( $search ) ) ) {
     820                continue;
     821            }
     822        }
     823
     824        // Add to repair list
     825        $repair_list[ $tool['priority'] ] = array(
     826            'id'          => sanitize_key( $id ),
     827            'description' => $tool['description'],
     828            'callback'    => $tool['callback'],
     829            'overhead'    => $tool['overhead'],
     830            'components'  => $tool['components'],
     831        );
     832    }
     833
     834    // Sort
    315835    ksort( $repair_list );
    316836
    317837    return (array) apply_filters( 'bbp_repair_list', $repair_list );
     838}
     839
     840/**
     841 * Get filter links for components for a specific admir repair tool
     842 *
     843 * @since bbPress (r5885)
     844 *
     845 * @param array $item
     846 * @return array
     847 */
     848function bbp_get_admin_repair_tool_components( $item = array() ) {
     849
     850    // Get the tools URL
     851    $tools_url = add_query_arg( array( 'page' => 'bbp-repair' ), admin_url( 'tools.php' ) );
     852
     853    // Define links array
     854    $links = array();
     855
     856    // Loop through tool components and build links
     857    foreach ( $item['components'] as $component ) {
     858        $args       = array( 'components' => $component );
     859        $filter_url = add_query_arg( $args, $tools_url );
     860        $name       = bbp_admin_repair_tool_translate_component( $component );
     861        $links[]    = '<a href="' . esc_url( $filter_url ) . '">' . esc_html( $name ) . '</a>';
     862    }
     863
     864    // Filter & return
     865    return apply_filters( 'bbp_get_admin_repair_tool_components', $links, $item );
     866}
     867//
     868function bbp_admin_repair_tool_overhead_filters( $args = array() ) {
     869    echo bbp_get_admin_repair_tool_overhead_filters( $args );
     870}
     871
     872/**
     873 * Get filter links for components for a specific admir repair tool
     874 *
     875 * @since bbPress (r5885)
     876 *
     877 * @param array $args
     878 * @return array
     879 */
     880function bbp_get_admin_repair_tool_overhead_filters( $args = array() ) {
     881
     882    // Parse args
     883    $r = bbp_parse_args( $args, array(
     884        'before'            => '<ul class="subsubsub">',
     885        'after'             => '</ul>',
     886        'link_before'       => '<li>',
     887        'link_after'        => '</li>',
     888        'count_before'      => ' <span class="count">(',
     889        'count_after'       => ')</span>',
     890        'separator'         => ' | ',
     891    ), 'get_admin_repair_tool_overhead_filters' );
     892
     893    // Count the tools
     894    $tools = bbp_get_admin_repair_tools();
     895
     896    // Get the tools URL
     897    $tools_url = add_query_arg( array( 'page' => 'bbp-repair' ), admin_url( 'tools.php' ) );
     898
     899    // Define arrays
     900    $overheads = array();
     901
     902    // Loop through tools and count overheads
     903    foreach ( $tools as $id => $tool ) {
     904
     905        // Get the overhead level
     906        $overhead = $tool['overhead'];
     907
     908        // Set an empty count
     909        if ( empty( $overheads[ $overhead ] ) ) {
     910            $overheads[ $overhead ] = 0;
     911        }
     912
     913        // Bump the overhead count
     914        $overheads[ $overhead ]++;
     915    }
     916
     917    // Create the "All" link
     918    $current = empty( $_GET['overhead'] ) ? 'current' : '';
     919    $output  = $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['separator'] . $r['link_after'];
     920
     921    // Default ticker
     922    $i = 0;
     923
     924    // Loop through overheads and build filter
     925    foreach ( $overheads as $overhead => $count ) {
     926
     927        // Separator count
     928        $i++;
     929
     930        // Build the filter URL
     931        $key        = sanitize_key( $overhead );
     932        $args       = array( 'overhead' => $key );
     933        $filter_url = add_query_arg( $args, $tools_url );
     934
     935        // Figure out separator and active class
     936        $show_sep = count( $overheads ) > $i ? $r['separator'] : '';
     937        $current  = ! empty( $_GET['overhead'] ) && ( sanitize_key( $_GET['overhead'] ) === $key ) ? 'current' : '';
     938
     939        // Counts to show
     940        if ( ! empty( $count ) ) {
     941            $overhead_count = $r['count_before'] . $count . $r['count_after'];
     942        }
     943
     944        // Build the link
     945        $output .= $r['link_before'] . '<a href="' . esc_url( $filter_url ) . '" class="' . esc_attr( $current ) . '">' . $overhead . $overhead_count . '</a>' . $show_sep . $r['link_after'];
     946    }
     947
     948    // Surround output with before & after strings
     949    $output = $r['before'] . $output . $r['after'];
     950
     951    // Filter & return
     952    return apply_filters( 'bbp_get_admin_repair_tool_components', $output, $r, $args );
    318953}
    319954
     
    15342169                    </tr>
    15352170                    <tr valign="top">
    1536                         <th scope="row"><?php esc_html_e( 'Are you sure you want to do this?', 'bbpress' ); ?></th>
     2171                        <th scope="row"><?php esc_html_e( 'Do you really want to do this?', 'bbpress' ); ?></th>
    15372172                        <td>
    15382173                            <fieldset>
    15392174                                <legend class="screen-reader-text"><span><?php esc_html_e( "Say it ain't so!", 'bbpress' ); ?></span></legend>
    15402175                                <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>
    1541                                 <p class="description"><?php esc_html_e( 'Human sacrifice, dogs and cats living together... mass hysteria!', 'bbpress' ); ?></p>
    15422176                            </fieldset>
    15432177                        </td>
Note: See TracChangeset for help on using the changeset viewer.