Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
57b7e5f
Add tests for `WP_List_Table::get_views_links()`.
Aug 27, 2022
809ab76
Add `WP_List_Table::get_views_links()`.
Jun 19, 2022
3a7181f
Posts: Use `::get_views_links()` in list table.
Aug 28, 2022
588c02b
Comments: Use `::get_views_links()` in list table.
Aug 28, 2022
7d4de21
Users: Use `::get_views_links()` in list table.
Jun 19, 2022
5a358f2
Plugins: Use `::get_views_links()` in list table.
Jun 19, 2022
d1fbe28
Plugin Install: Use `::get_views_links()` in list table.
Jun 19, 2022
fea9e02
Theme Install: Use `::get_views_links()` in list table.
Jun 19, 2022
4361d93
Privacy Requests: Use `::get_views_links()` in list table.
Aug 28, 2022
3de35a5
MS Sites: Use `::get_views_links()` in list table.
Jun 19, 2022
47f5f9d
MS Users: Use `::get_views_links()` in list table.
Jun 19, 2022
0425791
MS Themes: Use `::get_views_links()` in list table.
Jun 19, 2022
9f34460
WP_List_Table: Allow empty `$link_data` array.
Jul 26, 2022
bcb9adc
WP_List_Table: Use `empty()` for URL and label.
Jul 26, 2022
8b4c3cb
WP_List_Table: Improve escaping.
Jul 26, 2022
bcdadf3
Add tests for `WP_Posts_List_Table::get_views()`.
Aug 28, 2022
815daf5
Add tests for `WP_Comments_List_Table::get_views()`.
Aug 28, 2022
ea3e1cd
Add tests for `WP_Post_Comments_List_Table::get_views()`.
Aug 28, 2022
ac36df8
Add tests for `WP_Users_List_Table::get_views()`.
Aug 27, 2022
e54bf12
Add tests for `WP_Plugins_List_Table::get_views()`.
Aug 27, 2022
f400ab7
Add tests for `WP_Plugin_Install_List_Table::get_views()`.
Aug 27, 2022
9e30826
Add tests for `WP_Theme_Install_List_Table::get_views()`.
Aug 27, 2022
79639ea
Add tests for `WP_Privacy_Requests_List_Table::get_views()`.
Aug 28, 2022
0d11699
Add tests for `WP_MS_Sites_List_Table::get_views()`.
Aug 27, 2022
ae2ebd7
Add tests for `WP_MS_Users_List_Table::get_views()`.
Aug 27, 2022
45333b4
Add tests for `WP_MS_Themes_List_Table::get_views()`.
Aug 27, 2022
e4a63bb
Tests: Replace `\'` with `"` to fix failures.
Jun 19, 2022
5945f30
Tests: Replace `&` with `&` to fix failures.
Aug 28, 2022
be0780b
Tests: Get actual user counts in test data.
Aug 28, 2022
92a05b4
Tests: Fix docblock alignment.
Aug 28, 2022
d26fdc0
Tests: Replace `is_multisite()` with `ms-required` group.
Sep 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions src/wp-admin/includes/class-wp-comments-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ protected function get_views() {
}

foreach ( $stati as $status => $label ) {
$current_link_attributes = '';

if ( $status === $comment_status ) {
$current_link_attributes = ' class="current" aria-current="page"';
}

if ( 'mine' === $status ) {
$current_user_id = get_current_user_id();
$num_comments->mine = get_comments(
Expand Down Expand Up @@ -329,14 +323,18 @@ protected function get_views() {
$link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
*/

$status_links[ $status ] = "<a href='$link'$current_link_attributes>" . sprintf(
translate_nooped_plural( $label, $num_comments->$status ),
sprintf(
'<span class="%s-count">%s</span>',
( 'moderated' === $status ) ? 'pending' : $status,
number_format_i18n( $num_comments->$status )
)
) . '</a>';
$status_links[ $status ] = array(
'url' => esc_url( $link ),
'label' => sprintf(
translate_nooped_plural( $label, $num_comments->$status ),
sprintf(
'<span class="%s-count">%s</span>',
( 'moderated' === $status ) ? 'pending' : $status,
number_format_i18n( $num_comments->$status )
)
),
'current' => $status === $comment_status,
);
}

/**
Expand All @@ -348,7 +346,7 @@ protected function get_views() {
* @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine',
* 'Pending', 'Approved', 'Spam', and 'Trash'.
*/
return apply_filters( 'comment_status_links', $status_links );
return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
}

/**
Expand Down
72 changes: 72 additions & 0 deletions src/wp-admin/includes/class-wp-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,78 @@ public function ajax_response() {
die( wp_json_encode( $response ) );
}

/**
* Generates views links.
*
* @since 6.1.0
*
* @param array $link_data {
* An array of link data.
*
* @type string $url The link URL.
* @type string $label The link label.
* @type bool $current Optional. Whether this is the currently selected view.
* }
* @return array An array of link markup. Keys match the $link_data input array.
*/
protected function get_views_links( $link_data = array() ) {
if ( ! is_array( $link_data ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %s: The $link_data argument. */
__( 'The <code>%s</code> argument must be an array.' ),
'$link_data'
),
'6.1.0'
);

return array( '' );
}

$views_links = array();
foreach ( $link_data as $view => $link ) {
if ( empty( $link['url'] ) || ! is_string( $link['url'] ) || '' === trim( $link['url'] ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %1$s: The argument name. %2$s: The view name. */
__( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
'url',
esc_html( $view )
),
'6.1.0'
);

continue;
}

if ( empty( $link['label'] ) || ! is_string( $link['label'] ) || '' === trim( $link['label'] ) ) {
_doing_it_wrong(
__METHOD__,
sprintf(
/* translators: %1$s: The argument name. %2$s: The view name. */
__( 'The <code>%1$s</code> argument must be a non-empty string for <code>%2$s</code>.' ),
'label',
esc_html( $view )
),
'6.1.0'
);

continue;
}

$views_links[ $view ] = sprintf(
'<a href="%s"%s>%s</a>',
esc_url( $link['url'] ),
isset( $link['current'] ) && true === $link['current'] ? ' class="current" aria-current="page"' : '',
$link['label']
);
}

return $views_links;
}

/**
* Sends required variables to JavaScript land.
*
Expand Down
14 changes: 5 additions & 9 deletions src/wp-admin/includes/class-wp-ms-sites-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,23 +262,19 @@ protected function get_views() {
$url = 'sites.php';

foreach ( $statuses as $status => $label_count ) {
$current_link_attributes = $requested_status === $status || ( '' === $requested_status && 'all' === $status )
? ' class="current" aria-current="page"'
: '';
if ( (int) $counts[ $status ] > 0 ) {
$label = sprintf( translate_nooped_plural( $label_count, $counts[ $status ] ), number_format_i18n( $counts[ $status ] ) );
$full_url = 'all' === $status ? $url : add_query_arg( 'status', $status, $url );

$view_links[ $status ] = sprintf(
'<a href="%1$s"%2$s>%3$s</a>',
esc_url( $full_url ),
$current_link_attributes,
$label
$view_links[ $status ] = array(
'url' => esc_url( $full_url ),
'label' => $label,
'current' => $requested_status === $status || ( '' === $requested_status && 'all' === $status ),
);
}
}

return $view_links;
return $this->get_views_links( $view_links );
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/wp-admin/includes/class-wp-ms-themes-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,16 +444,15 @@ protected function get_views() {
}

if ( 'search' !== $type ) {
$status_links[ $type ] = sprintf(
"<a href='%s'%s>%s</a>",
esc_url( add_query_arg( 'theme_status', $type, $url ) ),
( $type === $status ) ? ' class="current" aria-current="page"' : '',
sprintf( $text, number_format_i18n( $count ) )
$status_links[ $type ] = array(
'url' => esc_url( add_query_arg( 'theme_status', $type, $url ) ),
'label' => sprintf( $text, number_format_i18n( $count ) ),
'current' => $type === $status,
);
}
}

return $status_links;
return $this->get_views_links( $status_links );
}

/**
Expand Down
29 changes: 13 additions & 16 deletions src/wp-admin/includes/class-wp-ms-users-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,10 @@ protected function get_views() {
$super_admins = get_super_admins();
$total_admins = count( $super_admins );

$current_link_attributes = 'super' !== $role ? ' class="current" aria-current="page"' : '';
$role_links = array();
$role_links['all'] = sprintf(
'<a href="%s"%s>%s</a>',
network_admin_url( 'users.php' ),
$current_link_attributes,
sprintf(
$role_links = array();
$role_links['all'] = array(
'url' => network_admin_url( 'users.php' ),
'label' => sprintf(
/* translators: Number of users. */
_nx(
'All <span class="count">(%s)</span>',
Expand All @@ -152,25 +149,25 @@ protected function get_views() {
'users'
),
number_format_i18n( $total_users )
)
),
'current' => 'super' !== $role,
);
$current_link_attributes = 'super' === $role ? ' class="current" aria-current="page"' : '';
$role_links['super'] = sprintf(
'<a href="%s"%s>%s</a>',
network_admin_url( 'users.php?role=super' ),
$current_link_attributes,
sprintf(

$role_links['super'] = array(
'url' => network_admin_url( 'users.php?role=super' ),
'label' => sprintf(
/* translators: Number of users. */
_n(
'Super Admin <span class="count">(%s)</span>',
'Super Admins <span class="count">(%s)</span>',
$total_admins
),
number_format_i18n( $total_admins )
)
),
'current' => 'super' === $role,
);

return $role_links;
return $this->get_views_links( $role_links );
}

/**
Expand Down
10 changes: 6 additions & 4 deletions src/wp-admin/includes/class-wp-plugin-install-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,16 @@ protected function get_views() {

$display_tabs = array();
foreach ( (array) $tabs as $action => $text ) {
$current_link_attributes = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
$href = self_admin_url( 'plugin-install.php?tab=' . $action );
$display_tabs[ 'plugin-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
$display_tabs[ 'plugin-install-' . $action ] = array(
'url' => self_admin_url( 'plugin-install.php?tab=' . $action ),
'label' => $text,
'current' => $action === $tab,
);
}
// No longer a real tab.
unset( $display_tabs['plugin-install-upload'] );

return $display_tabs;
return $this->get_views_links( $display_tabs );
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/wp-admin/includes/class-wp-plugins-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,16 +576,15 @@ protected function get_views() {
}

if ( 'search' !== $type ) {
$status_links[ $type ] = sprintf(
"<a href='%s'%s>%s</a>",
add_query_arg( 'plugin_status', $type, 'plugins.php' ),
( $type === $status ) ? ' class="current" aria-current="page"' : '',
sprintf( $text, number_format_i18n( $count ) )
$status_links[ $type ] = array(
'url' => add_query_arg( 'plugin_status', $type, 'plugins.php' ),
'label' => sprintf( $text, number_format_i18n( $count ) ),
'current' => $type === $status,
);
}
}

return $status_links;
return $this->get_views_links( $status_links );
}

/**
Expand Down
30 changes: 21 additions & 9 deletions src/wp-admin/includes/class-wp-posts-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,16 @@ protected function get_views() {
number_format_i18n( $this->user_posts_count )
);

$mine = $this->get_edit_link( $mine_args, $mine_inner_html, $class );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: WP_Posts_List_Table::get_edit_link() is still used by:

  • WP_Posts_List_Table::column_author()
  • WP_Posts_List_Table::column_default()

These can be replaced to use get_views_links(), but this would lose context.

This is another use case for a general function like the proposed wp_create_links().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the removal of $class being passed in here, it seems the variable is no longer used. This is was flagged by PHPStan. In #11151 I've included cb7a8c3 to address this.

$mine = array(
'url' => esc_url( add_query_arg( $mine_args, 'edit.php' ) ),
'label' => $mine_inner_html,
'current' => isset( $_GET['author'] ) && ( $current_user_id === (int) $_GET['author'] ),
);

$all_args['all_posts'] = 1;
$class = '';
}

if ( empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ) ) {
$class = 'current';
}

$all_inner_html = sprintf(
/* translators: %s: Number of posts. */
_nx(
Expand All @@ -352,7 +352,11 @@ protected function get_views() {
number_format_i18n( $total_posts )
);

$status_links['all'] = $this->get_edit_link( $all_args, $all_inner_html, $class );
$status_links['all'] = array(
'url' => esc_url( add_query_arg( $all_args, 'edit.php' ) ),
'label' => $all_inner_html,
'current' => empty( $class ) && ( $this->is_base_request() || isset( $_REQUEST['all_posts'] ) ),
);

if ( $mine ) {
$status_links['mine'] = $mine;
Expand Down Expand Up @@ -381,7 +385,11 @@ protected function get_views() {
number_format_i18n( $num_posts->$status_name )
);

$status_links[ $status_name ] = $this->get_edit_link( $status_args, $status_label, $class );
$status_links[ $status_name ] = array(
'url' => esc_url( add_query_arg( $status_args, 'edit.php' ) ),
'label' => $status_label,
'current' => isset( $_REQUEST['post_status'] ) && $status_name === $_REQUEST['post_status'],
);
}

if ( ! empty( $this->sticky_posts_count ) ) {
Expand All @@ -404,15 +412,19 @@ protected function get_views() {
);

$sticky_link = array(
'sticky' => $this->get_edit_link( $sticky_args, $sticky_inner_html, $class ),
'sticky' => array(
'url' => esc_url( add_query_arg( $sticky_args, 'edit.php' ) ),
'label' => $sticky_inner_html,
'current' => ! empty( $_REQUEST['show_sticky'] ),
),
);

// Sticky comes after Publish, or if not listed, after All.
$split = 1 + array_search( ( isset( $status_links['publish'] ) ? 'publish' : 'all' ), array_keys( $status_links ), true );
$status_links = array_merge( array_slice( $status_links, 0, $split ), $sticky_link, array_slice( $status_links, $split ) );
}

return $status_links;
return $this->get_views_links( $status_links );
}

/**
Expand Down
Loading