Make WordPress Core


Ignore:
Timestamp:
04/17/2021 01:12:25 AM (4 years ago)
Author:
Clorith
Message:

Site Health: Support custom sub-menus and pages.

Allow developers to extend the Site Health screen with their own custom navigation tabs and pages.

This implements a new filter, site_health_navigation_tabs, which takes an associated array of tab identifiers/slugs, and tab labels, allowing developers to add their own subpage to the Site Health interface as new tabs.

To output a custom page, or add to an existing page, the site_health_tab_content action is triggered whenever the tab query argument is present and not empty. This action includes the current tab as its argument, which a developer would match against to only output content when relevant.

Props ramiy for initial patch.
Fixes #47225.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/site-health.php

    r50131 r50764  
    77 */
    88
    9 if ( isset( $_GET['tab'] ) && 'debug' === $_GET['tab'] ) {
    10     require_once __DIR__ . '/site-health-info.php';
    11     return;
    12 }
    13 
    149/** WordPress Administration Bootstrap */
    1510require_once __DIR__ . '/admin.php';
     
    1712wp_reset_vars( array( 'action' ) );
    1813
    19 $title = __( 'Site Health Status' );
     14$tabs = array(
     15    /* translators: Tab heading for Site Health Status page. */
     16    ''      => _x( 'Status', 'Site Health' ),
     17    /* translators: Tab heading for Site Health Info page. */
     18    'debug' => _x( 'Info', 'Site Health' ),
     19);
     20
     21/**
     22 * An associated array of extra tabs for the Site Health navigation bar.
     23 *
     24 * Add a custom page to the Site Health screen, based on a tab slug and label.
     25 * The label you provide will also be used as part of the site title.
     26 *
     27 * @since 5.8.0
     28 *
     29 * @param array $tabs An associated array of tab slugs and their label.
     30 */
     31$tabs = apply_filters( 'site_health_navigation_tabs', $tabs );
     32
     33$wrapper_classes = array(
     34    'health-check-tabs-wrapper',
     35    'hide-if-no-js',
     36    'tab-count-' . count( $tabs ),
     37);
     38
     39$title = sprintf(
     40    // translators: %s: The currently displayed tab.
     41    __( 'Site Health %s' ),
     42    ( isset( $_GET['tab'] ) ? esc_html( $tabs[ $_GET['tab'] ] ) : esc_html( $tabs[0] ) )
     43);
    2044
    2145if ( ! current_user_can( 'view_site_health_checks' ) ) {
     
    87111    </div>
    88112
    89     <nav class="health-check-tabs-wrapper hide-if-no-js" aria-label="<?php esc_attr_e( 'Secondary menu' ); ?>">
    90         <a href="<?php echo esc_url( admin_url( 'site-health.php' ) ); ?>" class="health-check-tab active" aria-current="true">
    91             <?php
    92             /* translators: Tab heading for Site Health Status page. */
    93             _ex( 'Status', 'Site Health' );
    94             ?>
    95         </a>
    96 
    97         <a href="<?php echo esc_url( admin_url( 'site-health.php?tab=debug' ) ); ?>" class="health-check-tab">
    98             <?php
    99             /* translators: Tab heading for Site Health Info page. */
    100             _ex( 'Info', 'Site Health' );
    101             ?>
    102         </a>
     113    <nav class="<?php echo implode( ' ', $wrapper_classes ); ?>" aria-label="<?php esc_attr_e( 'Secondary menu' ); ?>">
     114        <?php
     115        $tabs_slice = $tabs;
     116
     117        /*
     118         * If there are more than 4 tabs, only output the first 3 inline,
     119         * the remaining links will be added to a sub-navigation.
     120         */
     121        if ( count( $tabs ) > 4 ) {
     122            $tabs_slice = array_slice( $tabs, 0, 3 );
     123        }
     124
     125        foreach ( $tabs_slice as $slug => $label ) {
     126            printf(
     127                '<a href="%s" class="health-check-tab %s">%s</a>',
     128                esc_url(
     129                    add_query_arg(
     130                        array(
     131                            'tab' => $slug,
     132                        ),
     133                        admin_url( 'site-health.php' )
     134                    )
     135                ),
     136                ( isset( $_GET['tab'] ) && $_GET['tab'] === $slug ? 'active' : '' ),
     137                esc_html( $label )
     138            );
     139        }
     140        ?>
     141
     142        <?php if ( count( $tabs ) > 4 ) : ?>
     143            <button type="button" class="health-check-tab health-check-offscreen-nav-wrapper" aria-haspopup="true">
     144                <span class="dashicons dashicons-ellipsis"></span>
     145                <span class="screen-reader-text"><?php _e( 'Toggle extra menu items' ); ?></span>
     146
     147                <div class="health-check-offscreen-nav">
     148                    <?php
     149                    // Remove the first few entries from the array as being already output.
     150                    $tabs_slice = array_slice( $tabs, 3 );
     151                    foreach ( $tabs_slice as $slug => $label ) {
     152                        printf(
     153                            '<a href="%s" class="health-check-tab %s">%s</a>',
     154                            esc_url(
     155                                add_query_arg(
     156                                    array(
     157                                        'tab' => $slug,
     158                                    ),
     159                                    admin_url( 'site-health.php' )
     160                                )
     161                            ),
     162                            ( isset( $_GET['tab'] ) && $_GET['tab'] === $slug ? 'active' : '' ),
     163                            esc_html( $label )
     164                        );
     165                    }
     166                    ?>
     167                </div>
     168            </button>
     169        <?php endif; ?>
    103170    </nav>
    104171</div>
    105172
    106173<hr class="wp-header-end">
     174
     175<?php
     176if ( isset( $_GET['tab'] ) && ! empty( $_GET['tab'] ) ) {
     177    /**
     178     * Output content of a custom Site Health tab.
     179     *
     180     * This action fires right after the Site Health header, and users are still subject to
     181     * the capability checks for the Site Health page to view any custom tabs and their contents.
     182     *
     183     * @since 5.8.0
     184     *
     185     * @param string $tab The slug of the tab that was requested.
     186     */
     187    do_action( 'site_health_tab_content', $_GET['tab'] );
     188
     189    require_once ABSPATH . 'wp-admin/admin-footer.php';
     190    return;
     191} else {
     192    ?>
    107193
    108194<div class="notice notice-error hide-if-js">
     
    194280</script>
    195281
    196 <?php
     282    <?php
     283}
    197284require_once ABSPATH . 'wp-admin/admin-footer.php';
Note: See TracChangeset for help on using the changeset viewer.