#42066 closed enhancement (fixed)
List tables: consider a new method to generate the views links markup
Reported by: | afercia | Owned by: | costdev |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Administration | Keywords: | has-patch has-unit-tests has-testing-info commit has-dev-note |
Focuses: | Cc: |
Description
Splitting this out from #32399.
Most of the List Tables used in core comes with some "views links" to switch the table to a subset of items, say published, drafts, trashed posts or approved, pending comments or different user types.
While the logic to get the views differs depending on the type of items (post status, user role, active tab, etc.) the code that actually generates the links markup is basically always the same and could benefit from some abstraction in a general method. See also 32399#comment:32.
/cc @flixos90
Attachments (1)
Change History (35)
This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.
3 years ago
#4
@
3 years ago
To get the ball rolling, I'm attaching 42066.functions.php.diff
with a general function for discussion/consideration.
- Doing a search for the
get_views()
method, I've identified 11 files that generate admin status links. - I have diffs that convert each of these to use the new function, with some clean up in the
get_views()
methods. I'll attach these if this is a goer. - There will be some other files with a similar format but in a method not called
get_views()
. wp-class-media-list-table.php
generates<option>
elements. A similar general function could be written for this as well.
I've called the function generate_links()
, as it can be used in virtually any scenario.
- Accepts an array of arrays containing the data for each link.
- If the
is_current (bool)
key istrue
, the function will addclass="content" aria-content="page"
. - Allows for an array of additional attributes to be specified in the
attributes
key. - Handles invalid/empty arguments - I'll add unit tests if this is a goer.
- Doesn't perform
esc_url()
oresc_attr()
and leaves that up to the caller.
---
Calling the function looks like so:
<?php $post_link_data = array( 'all' => array( 'url' => 'edit.php', 'label' => 'All', // `generate_links()` adds/appends `class="current" and aria-current="page"`. // You can specify your own by setting this to `false` and adding your own // `class` and `aria-current` key/value pairs to the `attributes` array below. 'is_current' => true, 'attributes' => array( 'title' => _( 'View All Posts' ), 'id' => 'all-of-the-posts', 'class' => 'button-primary', ), ), // ... ); $post_links = generate_links( $post_link_data ); /* $post_links = array( 'all' => '<a href="edit.php" title="View All Posts" id="all-of-the-posts" class="button-primary current" aria-current="page">All</a>', // ... ); */
For links that have a similar set of attributes, we could have a wrapper function specifically for say, the admin status links, to apply attributes as needed.
e.g.
<?php function generate_admin_status_links( $link_data ) { // Additional attributes $link_data = array_map( function( $link ) { $link['attributes']['admin-attribute-1'] = 'Howdy, admin!'; $link['attributes']['admin-attribute-2'] = 'Howdy again, admin!'; return $link; }, $link_data ); return generate_links( $link_data ); }
You could also do the same for a generate_clean_links()
wrapper that performs esc_url()
, esc_attr()
, etc. on the data before calling generate_links()
.
N.B. The indentation on the diff is a little messed up on trac. It was run through WPCS before uploading. It's best to download and apply for readability.
Notes
- Can replace
array_map()
calls withforeach( $link_data as &$link )
for performance per this benchmark.
This ticket was mentioned in Slack in #core by costdev. View the logs.
3 years ago
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
#7
@
3 years ago
- Keywords has-patch needs-testing added
Reviewed during today's bug srcub.
Marking this as needs-testing
to make sure it's peer-reviewed and works as expected :)
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
#9
@
3 years ago
- Milestone changed from 5.9 to Future Release
- Owner set to costdev
- Status changed from new to assigned
As per today's bug scrub, this ticket needs a general consensus on the proposed approach. Moving to Future release and adding @costdev as the ticket owner.
#10
@
2 years ago
Additional commit props: @garrett-eclipse
Garrett provided a patch on #46122 which covered some of the code needed for this ticket.
This ticket was mentioned in PR #2828 on WordPress/wordpress-develop by costdev.
2 years ago
#11
- Keywords has-unit-tests added
This PR introduces WP_List_Table::get_admin_status_links()
to abstract the link markup generation for list tables.
Includes PHPUnit tests with full line/branch coverage.
Trac ticket: https://core.trac.wordpress.org/ticket/42066
#12
@
2 years ago
- Keywords dev-feedback added
I've submitted PR 2828, which introduces WP_List_Table::get_admin_status_links()
to abstract link markup generation in list tables.
The PR includes PHPUnit tests for the new method and also implements the use of this method in:
WP_MS_Sites_List_Table
WP_MS_Themes_List_Table
WP_MS_Users_List_Table
WP_Plugins_List_Table
WP_Plugin_Install_List_Table
WP_Theme_Install_List_Table
WP_Users_List_Table
This is a more direct approach to the ticket's summary than using the proposed general function mentioned earlier.
@afercia @peterwilsoncc @audrasjb Let me know your thoughts on this method, and whether it should also cover the markup for class="current" aria-current="page"
and instead change the "current"
key to bool
type.
Should a general function be preferred to cover link markup generation in most cases, I drafted PR 2380 with wp_create_links()
for this, which also supports additional attributes (including boolean attributes) and which is easily applied to this use case.
#13
@
2 years ago
Is there any reason that "status" is being used here, other than that's mostly what Core is using for different views?
Personally, I've long wished for a get_views
method in the list table. I usually end up recreating the code in my final. These views could be used for many more things other than "status", so I don't think we should lock in on that.
Going back to the original request in the ticket, can we not just promote get_views
to the parent class with a default output that outputs HTML for link data in an array for a new property views
. If it's not set, get_views
doesn't output anything. Extending classes could just add their links to the views
array. The output would be default unless the extending class overrides get_views
.
If I've missed the case against doing this, I apologize in advance.
#14
@
2 years ago
Ignore my comment above. I was way off. So, I think the current patch is OK. /unprops davidbaumwald.
#15
@
2 years ago
- Milestone changed from Future Release to 6.1
As there's a new patch and activity, moving this ticket into 6.1 for consideration.
#16
@
2 years ago
@costdev thanks for looking into this.
whether it should also cover the markup for class="current" aria-current="page" and instead change the "current" key to bool type.
I'd say moving all the HTML to the new method would be cleaner.
Aside:
naming is hard. Other names in this context refer to 'views', 'get_views' so I'd propose to rename get_admin_status_links
to get_views_links
.
As per backwards compatibility with plugins that extend the list table classes and with existing filters e.g. apply_filters( "views_{$this->screen->id}", $views )
I definitely defer to your expertise.
#17
@
2 years ago
Thanks for the feedback @davidbaumwald and @afercia, and thanks @hellofromTonya for milestoning this one!
I have updated PR 2828 to:
- Rename
get_admin_status_links
toget_views_links
. - Update all references within child classes in the PR.
- Abstract the
current
HTML markup as well. Thecurrent
key is nowbool
. - Update the tests.
- Rebase the PR branch on
trunk
.
I also tested some of the plugins in this result and this result, including:
- WordPress San-Thumbnail Posts Plugin
- BuddyPress
- Media Library Assistant
- Contact Form to Manage and respond to conversations with customers — Happyforms
- ATUM WooCommerce Inventory Management and Stock Tracking
All of the plugins' views links seem to appear and function as expected. The PR is ready for review.
#18
@
2 years ago
I've updated the PR 2828 with additional tests for BC as requested by @peterwilsoncc. I haven't pushed fixes for the test failures so that these can be reviewed:
Types of test failure:
- Some of the classes use
href=""
, others usehref=''
. This could be fixed tohref=""
for all classes for consistency. &
is not escaped in thehref
, but is escaped in the PR. The links still work, so I think this is an acceptable change and I can update the tests to account for it.- For a multisite test, I mistakenly used a local user/superadmin count value, rather than getting the actual numbers, so locally it passes and on CI it fails. I have a fix for this ready to push once a decision is made about the two above types of failure.
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
2 years ago
#20
@
2 years ago
Reviewed during today's bug scrub.
Thanks @costdev! I think your proposal for the test failures are all relevant.
- Then let's make it consistent everywhere
- Updating tests looks like the best solution here
- Let's go with this fix then!
#21
@
2 years ago
- Keywords dev-feedback removed
Thanks @audrasjb! I've updated PR 2828 with the following changes:
- As agreed, the tests have been updated to reflect that the affected list tables that previously rendered
href=''
will now renderhref=""
. - As agreed, the tests have been updated to reflect that
&
will now be escaped to&
- This is done by
esc_url()
,&
fans should direct their attention there 😝
- This is done by
- As agreed, the faulty test introduced in the PR has been fixed to use actual user counts rather than what happened to be my local user counts (my bad!)
- After rebasing on
trunk
, the new tests forWP_Comments_List_Table
andWP_Post_Comments_List_Table
were faulty.- They would run as part of the whole test suite, but not in isolation. I'm not sure what commit may have affected these, but either way, the solution was to make sure that
$this->table->prepare_items()
was called in these two particular tests. - This fix has been applied to the original test commits for Comments and Post Comments.
- They would run as part of the whole test suite, but not in isolation. I'm not sure what commit may have affected these, but either way, the solution was to make sure that
The PR is ready for another review and testing.
- Removing
dev-feedback
.
#22
@
2 years ago
- Keywords has-testing-info added
Testing Instructions
Steps to test
- Apply PR 2828.
- Navigate to the following admin pages:
Posts
Comments
Pages
Plugins
Plugins > Add New
Users
Tools > Export Personal Data
- If you have a multisite test site set up:
Sites
Users
- Other list tables touched by the PR but I'm not sure where to navigate to 😝:
- Plugin Install (this doesn't appear to be
Plugins > Add New
) - Themes/MS Themes (this doesn't appear to be
Appearance > Themes
) - Theme Install (this doesn't appear to be
Appearance > Themes > Add New
)
- Plugin Install (this doesn't appear to be
- Additionally, you can install and activate some of the plugins in this result and this result, and check their respective list tables to ensure they continue to function as expected.
- Click on all other statuses in the list table (e.g. "Draft", etc). You may need to create a few items (Posts, Pages, Users, etc).
Expected Results
- ✅ The first status (e.g. "All") should be selected before and after applying the PR.
- ✅ Clicking on another status should correctly filter the list table and highlight the selected status.
- (in short, everything should work as it used to)
This ticket was mentioned in Slack in #core-test by costdev. View the logs.
2 years ago
This ticket was mentioned in Slack in #core-test by costdev. View the logs.
2 years ago
#25
@
2 years ago
- Keywords needs-testing removed
Test Report
This report validates that the indicated patch addresses the issue.
Patch tested: https://github.com/WordPress/wordpress-develop/pull/2828
Environment
- OS: Ubuntu 20.04.5 LTS
- Web Server: nginx/1.21.5
- PHP: 7.4.27
- WordPress: 6.1-alpha-53344-src
- Browser: Version 105.0.5195.102 (Official Build) (64-bit)
- Theme: Twenty Twenty-Two
- Active Plugins: -
Actual Results
✅ Works as expected with patch.
Hi @costdev, Thanks for testing instructions. I have tested patch with below List tables as per testing instructions and it works as expected.
Posts
Comments
Pages
Plugins
Plugins > Add New
Users
Tools > Export Personal Data
Tools > Erase Personal Data
Plugins > Add New
Appearance > Themes > Add New
Multisite:
Sites
Users
Themes
Themes > Add New
Plugins > Add New
Additionally, I tested the list tables added by "BuddyPress" plugin and it seems working fine.
Thanks
This ticket was mentioned in Slack in #core-test by dharmesh. View the logs.
2 years ago
#27
@
2 years ago
- Keywords commit added
Thanks for testing the PR so extensively @Dharm1025!
This one seems ready for commit
consideration. Adding the keyword so a committer can take a look. 🙂
#28
@
2 years ago
Hi
I have also tested this, sharing the report below
Environment
OS: macOS Monterey
PHP: 7.3.5
WordPress: 6.0.2
Browser: Chrome 105.0.5195.52 (Official Build) (x86_64)
Theme: Twenty Twenty-Two
Result
Tested with below settings, works as expected
- Posts
- Comments
- Pages
- Plugins
- Plugins > Add New
- Users
- Tools > Export Personal Data
- Tools > Erase Personal Data
- Appearance > Themes > Add New
Also tested with below plugins, works as expected
- WordPress San-Thumbnail Posts Plugin
- Media Library Assistant
- BuddyPress
dream-encode commented on PR #2828:
2 years ago
#30
Merged into core in https://core.trac.wordpress.org/changeset/54215.
#31
@
2 years ago
- Keywords needs-dev-note added
While this probably doesn't need a full, separate Dev Note, this new function deserves at least a call-out on the Misc Dev Note.
This ticket was raised in a triage meeting on Slack and it was agreed that abstracting the link generation makes sense. The ticket has been set for the 5.9 milestone.