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-posts-list-table.php

    r54099 r54215  
    332332            );
    333333
    334             $mine = $this->get_edit_link( $mine_args, $mine_inner_html, $class );
     334            $mine = array(
     335                'url'     => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
     336                'label'   => $mine_inner_html,
     337                'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
     338            );
    335339
    336340            $all_args['all_posts'] = 1;
    337341            $class                 = '';
    338         }
    339 
    340         if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ) ) {
    341             $class = 'current';
    342342        }
    343343
     
    353353        );
    354354
    355         $status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
     355        $status_links['all'] = array(
     356            'url'     => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
     357            'label'   => $all_inner_html,
     358            'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
     359        );
    356360
    357361        if ( $mine ) {
     
    382386            );
    383387
    384             $status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class );
     388            $status_links[ $status_name ] = array(
     389                'url'     => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
     390                'label'   => $status_label,
     391                'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
     392            );
    385393        }
    386394
     
    405413
    406414            $sticky_link = array(
    407                 'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class ),
     415                'sticky' => array(
     416                    'url'     => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
     417                    'label'   => $sticky_inner_html,
     418                    'current' => ! empty( $_REQUEST['show_sticky'] ),
     419                ),
    408420            );
    409421
     
    413425        }
    414426
    415         return $status_links;
     427        return $this->get_views_links( $status_links );
    416428    }
    417429
Note: See TracChangeset for help on using the changeset viewer.