Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/06/2021 08:40:08 AM (5 years ago)
Author:
imath
Message:

Use WordPress "site-health" style tabs for BP Admin tabs

The BuddyPress settings and tools screens admin tabs are now using the "site-health" style. As aside from the "site-health" screens, the Privacy screens are also using this tabs' style, we think using this style will improve the way BuddyPress Admin screens integrate inside the WordPress Administration.

We've put a back compatibility process for plugins extending the BuddyPress settings or/and tools screens adding their custom tabs, so that they are automatically converted into these new tabs. Plugin developers are encouraged to progressively update the way they are adding these tabs.

  • bp_core_admin_tabbed_screen_header() has been introduced to generate the BuddyPress Admin screens headers using tabs.
  • bp_core_get_admin_settings_tabs() has been introduced to return the settings tabs, using the 'bp_core_get_admin_settings_tabs' filter, plugin developers can add their custom tab to the BuddyPress settings screens.
  • bp_core_get_admin_settings_tabs() has been introduced to return the settings tabs. Using the 'bp_core_get_admin_settings_tabs' filter, plugin developers can add their custom tabs to the BuddyPress settings screens.
  • bp_core_get_admin_tools_tabs() has been introduced to return the tools tabs. Using the 'bp_core_get_admin_tools_tabs' filter, plugin developers can add their custom tabs to the BuddyPress tools screens.
  • To make sure BuddyPress generate the inline style to custom tabs, plugin developers need to hook the 'bp_admin_submenu_pages' action which passes by reference an associative array keyed according to the screens to style (settings or tools) and containing the plugin page hookname of their custom screens.

Props oztaser

Fixes #8588

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/admin/bp-core-admin-functions.php

    r13118 r13137  
    392392
    393393/**
     394 * Outputs the BP Admin Tabbed header.
     395 *
     396 * @since 10.0.0
     397 *
     398 * @param string $title      The title of the Admin page.
     399 * @param string $active_tab The current displayed tab.
     400 * @param string $context    The context of use for the tabs. Defaults to 'settings'.
     401 *                           Possible values are 'settings' & 'tools'.
     402 */
     403function bp_core_admin_tabbed_screen_header( $title = '', $active_tab = '', $context = 'settings' ) {
     404    $bp = buddypress();
     405
     406    // Globalize the active tab for backcompat purpose.
     407    $bp->admin->active_nav_tab = $active_tab;
     408
     409    /**
     410     * Fires before the output of the BP Admin tabbed screen header.
     411     *
     412     * @since 10.0.0
     413     *
     414     * @param string $active_tab The BP Admin active tab.
     415     * @param string $context    The context of use for the tabs.
     416     */
     417    do_action( 'bp_core_admin_tabbed_screen_header', $active_tab, $context );
     418    ?>
     419    <div class="buddypress-header">
     420        <div class="buddypress-title-section">
     421            <h1><span class="bp-badge"></span> <?php echo esc_html( $title ); ?></h1>
     422        </div>
     423        <nav class="buddypress-tabs-wrapper">
     424            <?php if ( isset( $bp->admin->nav_tabs ) ) : ?>
     425                <?php foreach ( $bp->admin->nav_tabs as $nav_tab ) : ?>
     426
     427                    <?php echo $nav_tab; ?>
     428
     429                <?php endforeach; ?>
     430            <?php else : ?>
     431                <?php bp_core_admin_tabs( esc_html( $active_tab ), $context ); ?>
     432            <?php endif; ?>
     433        </nav>
     434    </div>
     435
     436    <hr class="wp-header-end">
     437    <?php
     438}
     439
     440/**
    394441 * Output the tabs in the admin area.
    395442 *
     
    401448 *                           Possible values are 'settings' & 'tools'.
    402449 */
    403 function bp_core_admin_tabs( $active_tab = '', $context = 'settings' ) {
     450function bp_core_admin_tabs( $active_tab = '', $context = 'settings', $echo = true ) {
    404451    $tabs_html    = '';
    405     $idle_class   = 'nav-tab';
    406     $active_class = 'nav-tab nav-tab-active';
     452    $idle_class   = 'buddypress-nav-tab';
     453    $active_class = 'buddypress-nav-tab active';
    407454
    408455    /**
     
    413460     * @param array $value Array of tabs to output to the admin area.
    414461     */
    415     $tabs = apply_filters( 'bp_core_admin_tabs', bp_core_get_admin_tabs( $active_tab, $context ) );
     462    $tabs         = apply_filters( 'bp_core_admin_tabs', bp_core_get_admin_tabs( $active_tab, $context ) );
     463    $tabs_html    = array();
    416464
    417465    // Loop through tabs and build navigation.
    418466    foreach ( array_values( $tabs ) as $tab_data ) {
    419         $is_current = (bool) ( $tab_data['name'] == $active_tab );
    420         $tab_class  = $is_current ? $active_class : $idle_class;
    421         $tabs_html .= '<a href="' . esc_url( $tab_data['href'] ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab_data['name'] ) . '</a>';
    422     }
    423 
    424     echo $tabs_html;
    425 
     467        $is_current     = (bool) ( $tab_data['name'] == $active_tab );
     468        $tab_class      = $is_current ? $active_class : $idle_class;
     469        $tabs_html[]    = '<a href="' . esc_url( $tab_data['href'] ) . '" class="' . esc_attr( $tab_class ) . '">' . esc_html( $tab_data['name'] ) . '</a>';
     470    }
     471
     472    if ( ! $echo ) {
     473        return $tabs_html;
     474    }
     475
     476    echo implode( "\n", $tabs_html );
    426477    /**
    427478     * Fires after the output of tabs for the admin area.
     
    429480     * @since 1.5.0
    430481     * @since 8.0.0 Adds the `$context` parameter.
     482     * @since 10.0.0 Adds the `$active_tab` parameter.
    431483     *
    432484     * @param string $context The context of use for the tabs.
    433485     */
    434     do_action( 'bp_admin_tabs', $context );
     486    do_action( 'bp_admin_tabs', $context, $active_tab );
     487}
     488
     489/**
     490 * Returns the BP Admin settings tabs.
     491 *
     492 * @since 10.0.0
     493 *
     494 * @param bool $apply_filters Whether to apply filters or not.
     495 * @return array              The BP Admin settings tabs.
     496 */
     497function bp_core_get_admin_settings_tabs( $apply_filters = true ) {
     498    $settings_tabs = array(
     499        '0' => array(
     500            'id'   => 'bp-components',
     501            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-components' ), 'admin.php' ) ),
     502            'name' => __( 'Components', 'buddypress' ),
     503        ),
     504        '2' => array(
     505            'id'   => 'bp-settings',
     506            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-settings' ), 'admin.php' ) ),
     507            'name' => __( 'Options', 'buddypress' ),
     508        ),
     509        '1' => array(
     510            'id'   => 'bp-page-settings',
     511            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) ),
     512            'name' => __( 'Pages', 'buddypress' ),
     513        ),
     514        '3' => array(
     515            'id'   => 'bp-credits',
     516            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-credits' ), 'admin.php' ) ),
     517            'name' => __( 'Credits', 'buddypress' ),
     518        ),
     519    );
     520
     521    if ( ! $apply_filters ) {
     522        return $settings_tabs;
     523    }
     524
     525    /**
     526     * Filter here to edit the BP Admin settings tabs.
     527     *
     528     * @since 10.0.0
     529     *
     530     * @param array $settings_tabs The BP Admin settings tabs.
     531     */
     532    return apply_filters( 'bp_core_get_admin_settings_tabs', $settings_tabs );
     533}
     534
     535/**
     536 * Returns the BP Admin tools tabs.
     537 *
     538 * @since 10.0.0
     539 *
     540 * @param bool $apply_filters Whether to apply filters or not.
     541 * @return array              The BP Admin tools tabs.
     542 */
     543function bp_core_get_admin_tools_tabs( $apply_filters = true ) {
     544    $tools_page = 'tools.php';
     545    if ( bp_core_do_network_admin() ) {
     546        $tools_page = 'admin.php';
     547    }
     548
     549    $tools_tabs = array(
     550        '0' => array(
     551            'id'   => 'bp-tools',
     552            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-tools' ), $tools_page ) ),
     553            'name' => __( 'Repair', 'buddypress' ),
     554        ),
     555        '1' => array(
     556            'id'   => 'bp-members-invitations',
     557            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-members-invitations' ), $tools_page ) ),
     558            'name' => __( 'Manage Invitations', 'buddypress' ),
     559        ),
     560        '2' => array(
     561            'id'   => 'bp-optouts',
     562            'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-optouts' ), $tools_page ) ),
     563            'name' => __( 'Manage Opt-outs', 'buddypress' ),
     564        ),
     565    );
     566
     567    if ( ! $apply_filters ) {
     568        return $tools_tabs;
     569    }
     570
     571    /**
     572     * Filter here to edit the BP Admin tools tabs.
     573     *
     574     * @since 10.0.0
     575     *
     576     * @param array $tools_tabs The BP Admin tools tabs.
     577     */
     578    return apply_filters( 'bp_core_get_admin_tools_tabs', $tools_tabs );
    435579}
    436580
     
    450594
    451595    if ( 'settings' === $context ) {
    452         $tabs = array(
    453             '0' => array(
    454                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-components' ), 'admin.php' ) ),
    455                 'name' => __( 'Components', 'buddypress' ),
    456             ),
    457             '2' => array(
    458                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-settings' ), 'admin.php' ) ),
    459                 'name' => __( 'Options', 'buddypress' ),
    460             ),
    461             '1' => array(
    462                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-page-settings' ), 'admin.php' ) ),
    463                 'name' => __( 'Pages', 'buddypress' ),
    464             ),
    465             '3' => array(
    466                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-credits' ), 'admin.php' ) ),
    467                 'name' => __( 'Credits', 'buddypress' ),
    468             ),
    469         );
     596        $tabs = bp_core_get_admin_settings_tabs();
    470597    } elseif ( 'tools' === $context ) {
    471         $tools_page = 'tools.php';
    472         if ( bp_core_do_network_admin() ) {
    473             $tools_page = 'admin.php';
    474         }
    475 
    476         $tabs = array(
    477             '0' => array(
    478                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-tools' ), $tools_page ) ),
    479                 'name' => __( 'Repair', 'buddypress' ),
    480             ),
    481             '1' => array(
    482                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-members-invitations' ), $tools_page ) ),
    483                 'name' => __( 'Manage Invitations', 'buddypress' ),
    484             ),
    485             '2' => array(
    486                 'href' => bp_get_admin_url( add_query_arg( array( 'page' => 'bp-optouts' ), $tools_page ) ),
    487                 'name' => __( 'Manage Opt-outs', 'buddypress' ),
    488             ),
    489         );
     598        $tabs = bp_core_get_admin_tools_tabs();
    490599    }
    491600
     
    501610    return apply_filters( 'bp_core_get_admin_tabs', $tabs, $context );
    502611}
     612
     613/**
     614 * Makes sure plugins using `bp_core_admin_tabs()` to output their custom BP Admin Tabs are well displayed
     615 * inside the 10.0.0 tabbed header.
     616 *
     617 * @since 10.0.0
     618 *
     619 * @param string $context    The context of use for the tabs.
     620 * @param string $active_tab The active tab.
     621 */
     622function bp_backcompat_admin_tabs( $context = '', $active_tab = '' ) {
     623    $bp = buddypress();
     624
     625    // Only add the back compat for Settings or Tools sub pages.
     626    if ( 'settings' !== $context && 'tools' !== $context ) {
     627        return;
     628    }
     629
     630    // Globalize the active tab for backcompat purpose.
     631    if ( ! $bp->admin->active_nav_tab || $active_tab !== $bp->admin->active_nav_tab ) {
     632        _doing_it_wrong(
     633            'bp_core_admin_tabs()',
     634            __( 'BuddyPress Settings and Tools Screens are now using a new tabbed header. Please use `bp_core_admin_tabbed_screen_header()` instead of bp_core_admin_tabs() to output tabs.', 'buddypress' ),
     635            '10.0.0'
     636        );
     637
     638        // Let's try to use JavaScript to force the use of the 10.0.0 Admin tabbed screen header.
     639        wp_enqueue_script(
     640            'bp-backcompat-admin-tabs',
     641            sprintf( '%1$sbackcompat-admin-tabs%2$s.js', $bp->admin->js_url, bp_core_get_minified_asset_suffix() ),
     642            array(),
     643            bp_get_version(),
     644            true
     645        );
     646    }
     647}
     648add_action( 'bp_admin_tabs', 'bp_backcompat_admin_tabs', 1, 2 );
    503649
    504650/** Help **********************************************************************/
     
    12921438 * @since 2.8.0
    12931439 *
    1294  * @param string $classes CSS classes for the body tag in the admin, a comma separated string.
     1440 * @param string $classes CSS classes for the body tag in the admin, a space separated string.
    12951441 *
    12961442 * @return string
    12971443 */
    12981444function bp_core_admin_body_classes( $classes ) {
    1299     return $classes . ' buddypress';
     1445    $bp = buddypress();
     1446
     1447    $bp_class = ' buddypress';
     1448    if ( isset( $bp->admin->nav_tabs ) && $bp->admin->nav_tabs ) {
     1449        $bp_class .= ' bp-is-tabbed-screen';
     1450    }
     1451
     1452    return $classes . $bp_class;
    13001453}
    13011454add_filter( 'admin_body_class', 'bp_core_admin_body_classes' );
Note: See TracChangeset for help on using the changeset viewer.