[wp-trac] [WordPress Trac] #42066: List tables: consider a new method to generate the views links markup
WordPress Trac
noreply at wordpress.org
Thu Sep 9 12:46:20 UTC 2021
#42066: List tables: consider a new method to generate the views links markup
----------------------------+---------------------
Reporter: afercia | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: 5.9
Component: Administration | Version:
Severity: normal | Resolution:
Keywords: | Focuses:
----------------------------+---------------------
Comment (by costdev):
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.
1. Accepts an array of arrays containing the data for each link.
2. If the `is_current (bool)` key is `true`, the function will add
`class="content" aria-content="page"`.
3. Allows for an array of additional attributes to be specified in the
`attributes` key.
4. Handles invalid/empty arguments - I'll add unit tests if this is a
goer.
5. Doesn't perform `esc_url()` or `esc_attr()` and leaves that up to the
caller.
---
Calling the function looks like so:
{{{#!php
<?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
<?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()`.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42066#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list