Make WordPress Core


Ignore:
Timestamp:
09/19/2022 09:06:08 PM (19 months 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-ms-users-list-table.php

    r54071 r54215  
    138138        $total_admins = count( $super_admins );
    139139
    140         $current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : '';
    141         $role_links              = array();
    142         $role_links['all']       = sprintf(
    143             '<a href="%s"%s>%s</a>',
    144             network_admin_url( 'users.php' ),
    145             $current_link_attributes,
    146             sprintf(
     140        $role_links        = array();
     141        $role_links['all'] = array(
     142            'url'     => network_admin_url( 'users.php' ),
     143            'label'   => sprintf(
    147144                /* translators: Number of users. */
    148145                _nx(
     
    153150                ),
    154151                number_format_i18n( $total_users )
    155             )
    156         );
    157         $current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : '';
    158         $role_links['super']     = sprintf(
    159             '<a href="%s"%s>%s</a>',
    160             network_admin_url( 'users.php?role=super' ),
    161             $current_link_attributes,
    162             sprintf(
     152            ),
     153            'current' => 'super' !== $role,
     154        );
     155
     156        $role_links['super'] = array(
     157            'url'     => network_admin_url( 'users.php?role=super' ),
     158            'label'   => sprintf(
    163159                /* translators: Number of users. */
    164160                _n(
     
    168164                ),
    169165                number_format_i18n( $total_admins )
    170             )
    171         );
    172 
    173         return $role_links;
     166            ),
     167            'current' => 'super' === $role,
     168        );
     169
     170        return $this->get_views_links( $role_links );
    174171    }
    175172
Note: See TracChangeset for help on using the changeset viewer.