Make WordPress Core


Ignore:
Timestamp:
09/19/2022 09:06:08 PM (3 years ago)
Author:
davidbaumwald
Message:

Administration: Add new get_views_links method to WP_List_Table.

Many WP_List_Table child classes in core use mostly the same code to create their "view" links markup. To DRY-up the code, a new WP_List_Table->get_view_links method is being introduced to consolidate the HTML link generation when provided an array of links.

This change also implements this new method in the relevant WP_List_Table_xxx child classes get_views methods. Finally, unit tests are being added to validate view links markup and test for some "unhappy paths".

Props afercia, costdev, garrett-eclipse, Dharm1025, juhise, peterwilsoncc.
Fixes #42066.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-list-table.php

    r54133 r54215  
    15151515
    15161516    /**
     1517     * Generates views links.
     1518     *
     1519     * @since 6.1.0
     1520     *
     1521     * @param array $link_data {
     1522     *     An array of link data.
     1523     *
     1524     *     @type string $url     The link URL.
     1525     *     @type string $label   The link label.
     1526     *     @type bool   $current Optional. Whether this is the currently selected view.
     1527     * }
     1528     * @return array An array of link markup. Keys match the $link_data input array.
     1529     */
     1530    protected function get_views_links( $link_data = array() ) {
     1531        if ( ! is_array( $link_data ) ) {
     1532            _doing_it_wrong(
     1533                __METHOD__,
     1534                sprintf(
     1535                    /* translators: %s: The $link_data argument. */
     1536                    __( 'The <code>%s</code> argument must be an array.' ),
     1537                    '$link_data'
     1538                ),
     1539                '6.1.0'
     1540            );
     1541
     1542            return array( '' );
     1543        }
     1544
     1545        $views_links = array();
     1546        foreach ( $link_data as $view => $link ) {
     1547            if ( empty( $link['url'] ) || ! is_string( $link['url'] ) || '' === trim( $link['url'] ) ) {
     1548                _doing_it_wrong(
     1549                    __METHOD__,
     1550                    sprintf(
     1551                        /* translators: %1$s: The argument name. %2$s: The view name. */
     1552                        __( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
     1553                        'url',
     1554                        esc_html( $view )
     1555                    ),
     1556                    '6.1.0'
     1557                );
     1558
     1559                continue;
     1560            }
     1561
     1562            if ( empty( $link['label'] ) || ! is_string( $link['label'] ) || '' === trim( $link['label'] ) ) {
     1563                _doing_it_wrong(
     1564                    __METHOD__,
     1565                    sprintf(
     1566                        /* translators: %1$s: The argument name. %2$s: The view name. */
     1567                        __( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
     1568                        'label',
     1569                        esc_html( $view )
     1570                    ),
     1571                    '6.1.0'
     1572                );
     1573
     1574                continue;
     1575            }
     1576
     1577            $views_links[ $view ] = sprintf(
     1578                '<a href="%s"%s>%s</a>',
     1579                esc_url( $link['url'] ),
     1580                isset( $link['current'] ) && true === $link['current'] ? ' class="current" aria-current="page"' : '',
     1581                $link['label']
     1582            );
     1583        }
     1584
     1585        return $views_links;
     1586    }
     1587
     1588    /**
    15171589     * Sends required variables to JavaScript land.
    15181590     *
Note: See TracChangeset for help on using the changeset viewer.