Skip to:
Content

bbPress.org

Changeset 6274


Ignore:
Timestamp:
02/02/2017 11:08:01 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Tools: Unify the tab/link experience:

  • Introduce function to get tools pages
  • Trust capability checks in core WordPress functions, and remove our own bespoke pre-checks
  • Add tool-box to wp-admin/tools.php linking to tools the user has access to

This change promotes exposure to bbPress's tools pages, and makes adding third-party tools pages easier.

See: #2959.

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

Legend:

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

    r6186 r6274  
    4747add_action( 'current_screen',          'bbp_current_screen'                );
    4848add_action( 'wpmu_new_blog',           'bbp_new_site',               10, 6 );
     49add_action( 'tool_box',                'bbp_admin_tool_box'                );
    4950
    5051// Hook on to admin_init
     
    8990add_action( 'load-tools_page_bbp-repair', 'bbp_admin_repair_handler' );
    9091add_action( 'load-tools_page_bbp-reset',  'bbp_admin_reset_handler'  );
     92add_action( 'bbp_admin_tool_box',         'bbp_admin_tools_box'      );
    9193
    9294// Add sample permalink filter
     
    266268
    267269/**
     270 * Dedicated action to output admin tools.php sections
     271 *
     272 * @since 2.6.0 bbPress (r6273)
     273 *
     274 * @uses do_action() Calls 'bbp_admin_tool_box'
     275 */
     276function bbp_admin_tool_box() {
     277        do_action( 'bbp_admin_tool_box' );
     278}
     279
     280/**
    268281 * Dedicated action to hook into the current screen
    269282 *
  • trunk/src/includes/admin/admin.php

    r6250 r6274  
    202202                $hooks = array();
    203203
    204                 // These are later removed in admin_head
    205                 if ( current_user_can( 'bbp_tools_page' ) ) {
    206                         if ( current_user_can( 'bbp_tools_repair_page' ) ) {
    207                                 $hooks[] = add_management_page(
    208                                         __( 'Repair Forums', 'bbpress' ),
    209                                         __( 'Forum Repair',  'bbpress' ),
    210                                         $this->minimum_capability,
    211                                         'bbp-repair',
    212                                         'bbp_admin_repair'
    213                                 );
     204                // Get the tools pages
     205                $tools = bbp_get_tools_admin_pages();
     206
     207                // Loop through tools and check
     208                foreach ( $tools as $tool ) {
     209
     210                        // Try to add the admin page
     211                        $page = add_management_page(
     212                                $tool['name'],
     213                                $tool['name'],
     214                                $tool['cap'],
     215                                $tool['page'],
     216                                $tool['func']
     217                        );
     218
     219                        // Add page to hook if user can view it
     220                        if ( false !== $page ) {
     221                                $hooks[] = $page;
    214222                        }
    215 
    216                         if ( current_user_can( 'bbp_tools_import_page' ) ) {
    217                                 $hooks[] = add_management_page(
    218                                         __( 'Import Forums', 'bbpress' ),
    219                                         __( 'Forum Import',  'bbpress' ),
    220                                         $this->minimum_capability,
    221                                         'bbp-converter',
    222                                         'bbp_converter_settings'
    223                                 );
    224                         }
    225 
    226                         if ( current_user_can( 'bbp_tools_reset_page' ) ) {
    227                                 $hooks[] = add_management_page(
    228                                         __( 'Reset Forums', 'bbpress' ),
    229                                         __( 'Forum Reset',  'bbpress' ),
    230                                         $this->minimum_capability,
    231                                         'bbp-reset',
    232                                         'bbp_admin_reset'
    233                                 );
    234                         }
    235 
    236                         // Fudge the highlighted subnav item when on a bbPress admin page
    237                         foreach ( $hooks as $hook ) {
    238                                 add_action( "admin_head-$hook", 'bbp_tools_modify_menu_highlight' );
    239                         }
    240 
    241                         // Forums Tools Root
    242                         add_management_page(
    243                                 __( 'Forums', 'bbpress' ),
    244                                 __( 'Forums', 'bbpress' ),
    245                                 $this->minimum_capability,
    246                                 'bbp-repair',
    247                                 'bbp_admin_repair'
    248                         );
    249                 }
     223                }
     224
     225                // Fudge the highlighted subnav item when on a bbPress admin page
     226                foreach ( $hooks as $hook ) {
     227                        add_action( "admin_head-{$hook}", 'bbp_tools_modify_menu_highlight' );
     228                }
     229
     230                // Forums Tools Root
     231                add_management_page(
     232                        __( 'Forums', 'bbpress' ),
     233                        __( 'Forums', 'bbpress' ),
     234                        'bbp_tools_page',
     235                        'bbp-repair',
     236                        'bbp_admin_repair'
     237                );
    250238
    251239                // Are settings enabled?
    252                 if ( ! bbp_settings_integration() && current_user_can( 'bbp_settings_page' ) ) {
     240                if ( ! bbp_settings_integration() ) {
    253241                        add_options_page(
    254242                                __( 'Forums',  'bbpress' ),
    255243                                __( 'Forums',  'bbpress' ),
    256                                 $this->minimum_capability,
     244                                'bbp_settings_page',
    257245                                'bbpress',
    258246                                'bbp_admin_settings'
     
    267255                                __( 'Welcome to bbPress',  'bbpress' ),
    268256                                __( 'Welcome to bbPress',  'bbpress' ),
    269                                 $this->minimum_capability,
     257                                'bbp_about_page',
    270258                                'bbp-about',
    271259                                array( $this, 'about_screen' )
     
    276264                                __( 'Welcome to bbPress',  'bbpress' ),
    277265                                __( 'Welcome to bbPress',  'bbpress' ),
    278                                 $this->minimum_capability,
     266                                'bbp_about_page',
    279267                                'bbp-credits',
    280268                                array( $this, 'credits_screen' )
  • trunk/src/includes/admin/functions.php

    r6115 r6274  
    278278
    279279                // Setup core admin tabs
    280                 $tabs = apply_filters( 'bbp_tools_admin_tabs', array(
    281                         '0' => array(
    282                                 'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-repair'    ), 'tools.php' ) ),
    283                                 'name' => __( 'Repair Forums', 'bbpress' )
    284                         ),
    285                         '1' => array(
    286                                 'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-converter' ), 'tools.php' ) ),
    287                                 'name' => __( 'Import Forums', 'bbpress' )
    288                         ),
    289                         '2' => array(
    290                                 'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-reset'     ), 'tools.php' ) ),
    291                                 'name' => __( 'Reset Forums', 'bbpress' )
    292                         )
    293                 ) );
     280                $tabs = bbp_get_tools_admin_pages();
    294281
    295282                // Loop through tabs and build navigation
    296                 foreach ( array_values( $tabs ) as $tab_data ) {
    297                         $is_current = (bool) ( $tab_data['name'] == $active_tab );
     283                foreach ( $tabs as $tab ) {
     284
     285                        // Skip if user cannot visit page
     286                        if ( ! current_user_can( $tab['cap'] ) ) {
     287                                continue;
     288                        }
     289
     290                        // Setup tab HTML
     291                        $is_current = (bool) ( $tab['name'] == $active_tab );
    298292                        $tab_class  = $is_current ? $active_class : $idle_class;
    299                         $tabs_html .= '<a href="' . esc_url( $tab_data['href'] ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab_data['name'] ) . '</a>';
     293                        $tab_url    = get_admin_url( '', add_query_arg( array( 'page' => $tab['page'] ), 'tools.php' ) );
     294                        $tabs_html .= '<a href="' . esc_url( $tab_url ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab['name'] ) . '</a>';
    300295                }
    301296
     
    303298                return $tabs_html;
    304299        }
     300
     301/**
     302 * Return possible tools pages
     303 *
     304 * @since 2.6.0 (r6273)
     305 *
     306 * @return array
     307 */
     308function bbp_get_tools_admin_pages() {
     309        return apply_filters( 'bbp_tools_admin_tabs', array(
     310                array(
     311                        'page' => 'bbp-repair',
     312                        'func' => 'bbp_admin_repair',
     313                        'cap'  => 'bbp_tools_repair_page',
     314                        'name' => esc_html__( 'Repair Forums', 'bbpress' ),
     315
     316                        // Deprecated 2.6.0
     317                        'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-repair'    ), 'tools.php' ) )
     318                ),
     319                array(
     320                        'page' => 'bbp-converter',
     321                        'func' => 'bbp_converter_settings',
     322                        'cap'  => 'bbp_tools_import_page',
     323                        'name' => esc_html__( 'Import Forums', 'bbpress' ),
     324
     325                        // Deprecated 2.6.0
     326                        'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-converter' ), 'tools.php' ) )
     327                ),
     328                array(
     329                        'page' => 'bbp-reset',
     330                        'func' => 'bbp_admin_reset',
     331                        'cap'  => 'bbp_tools_reset_page',
     332                        'name' => esc_html__( 'Reset Forums', 'bbpress' ),
     333
     334                        // Deprecated 2.6.0
     335                        'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-reset'     ), 'tools.php' ) )
     336                )
     337        ) );
     338}
  • trunk/src/includes/admin/tools.php

    r6257 r6274  
    1010// Exit if accessed directly
    1111defined( 'ABSPATH' ) || exit;
     12
     13/**
     14 * Output a bbPress specific tools box
     15 *
     16 * @since 2.6.0 bbPress (r6273)
     17 */
     18function bbp_admin_tools_box() {
     19
     20        // Bail if user cannot access tools page
     21        if ( ! current_user_can( 'bbp_tools_page' ) ) {
     22                return;
     23        }
     24
     25        // Get the tools pages
     26        $links = array();
     27        $tools = bbp_get_tools_admin_pages(); ?>
     28
     29        <div class="card">
     30                <h3 class="title"><?php _e( 'Forums', 'bbpress' ) ?></h3>
     31                <p><?php esc_html_e( 'bbPress provides the following tools to help you manage your forums:', 'bbpress' ); ?></p>
     32
     33                <?php
     34
     35                // Loop through tools and create links
     36                foreach ( $tools as $tool ) {
     37
     38                        // Skip if user cannot see this page
     39                        if ( ! current_user_can( $tool['cap'] ) ) {
     40                                continue;
     41                        }
     42
     43                        // Add link to array
     44                        $links[] = sprintf( '<a href="%s">%s</a>', get_admin_url( '', add_query_arg( array( 'page' => $tool['page'] ), 'tools.php' ) ), $tool['name'] );
     45                }
     46
     47                // Output links
     48                echo '<p class="bbp-tools-links">' . implode( ' &middot; ', $links ) . '</p>';
     49
     50        ?></div>
     51
     52<?php
     53}
    1254
    1355/** Repair ********************************************************************/
Note: See TracChangeset for help on using the changeset viewer.