Make WordPress Core


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

    r54070 r54215  
    186186        }
    187187
    188         $role_links              = array();
    189         $avail_roles             = array();
    190         $all_text                = __( 'All' );
    191         $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
     188        $role_links  = array();
     189        $avail_roles = array();
     190        $all_text    = __( 'All' );
    192191
    193192        if ( $count_users ) {
     
    216215        }
    217216
    218         $role_links['all'] = sprintf( '<a href="%s"%s>%s</a>', $url, $current_link_attributes, $all_text );
     217        $role_links['all'] = array(
     218            'url'     => $url,
     219            'label'   => $all_text,
     220            'current' => empty( $role ),
     221        );
    219222
    220223        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    221224            if ( $count_users && ! isset( $avail_roles[ $this_role ] ) ) {
    222225                continue;
    223             }
    224 
    225             $current_link_attributes = '';
    226 
    227             if ( $this_role === $role ) {
    228                 $current_link_attributes = ' class="current" aria-current="page"';
    229226            }
    230227
     
    239236            }
    240237
    241             $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
     238            $role_links[ $this_role ] = array(
     239                'url'     => esc_url( add_query_arg( 'role', $this_role, $url ) ),
     240                'label'   => $name,
     241                'current' => $this_role === $role,
     242            );
    242243        }
    243244
    244245        if ( ! empty( $avail_roles['none'] ) ) {
    245 
    246             $current_link_attributes = '';
    247 
    248             if ( 'none' === $role ) {
    249                 $current_link_attributes = ' class="current" aria-current="page"';
    250             }
    251246
    252247            $name = __( 'No role' );
     
    258253            );
    259254
    260             $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$current_link_attributes>$name</a>";
    261         }
    262 
    263         return $role_links;
     255            $role_links['none'] = array(
     256                'url'     => esc_url( add_query_arg( 'role', 'none', $url ) ),
     257                'label'   => $name,
     258                'current' => 'none' === $role,
     259            );
     260        }
     261
     262        return $this->get_views_links( $role_links );
    264263    }
    265264
Note: See TracChangeset for help on using the changeset viewer.