List tables: introduce the concept of a "primary" column.

This becomes the column that contains the row actions, and allows for a more flexibility, particularly with custom post types and list tables. To (re)define the primary column, use the `list_table_primary_column` filter, which receives the column name and the screen ID as arguments.

props stephdau, DaveAl, jesin.
see #25408.

Built from https://develop.svn.wordpress.org/trunk@32644


git-svn-id: http://core.svn.wordpress.org/trunk@32614 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Helen Hou-Sandí
2015-05-29 02:41:25 +00:00
parent 282e28ad81
commit 4c0c7fd7ba
12 changed files with 1006 additions and 655 deletions

View File

@@ -794,6 +794,46 @@ class WP_List_Table {
return array();
}
/**
* Get name of default primary column
*
* @since 4.3.0
* @access protected
*
* @return string
*/
protected function get_default_primary_column_name() {
return '';
}
/**
* Get name of primary column.
*
* @since 4.3.0
* @access protected
*
* @return string Filtered name of primary column
*/
protected function get_primary_column_name() {
$columns = $this->get_columns();
$default = $this->get_default_primary_column_name();
/**
* Filter the name of the primary column for the current list table, with context as argument (eg: 'plugins').
*
* @since 4.3.0
*
* @param string $default Column name default for the specific list table (eg: 'name')
* @param string $context Screen ID for specific list table (eg: 'plugins')
*/
$column = apply_filters( 'list_table_primary_column', $default, $this->screen->id );
if ( empty( $column ) || ! isset( $columns[ $column ] ) ) {
$column = $default;
}
return $column;
}
/**
* Get a list of all, hidden and sortable columns, with filter applied
*
@@ -834,7 +874,8 @@ class WP_List_Table {
$sortable[$id] = $data;
}
$this->_column_headers = array( $columns, $hidden, $sortable );
$primary = $this->get_primary_column_name();
$this->_column_headers = array( $columns, $hidden, $sortable, $primary );
return $this->_column_headers;
}
@@ -1062,16 +1103,20 @@ class WP_List_Table {
* @param object $item The current item
*/
protected function single_row_columns( $item ) {
list( $columns, $hidden ) = $this->get_column_info();
list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class='$column_name column-$column_name'";
$classes = "$column_name column-$column_name";
if ( $primary === $column_name ) {
$classes .= ' has-row-actions column-primary';
}
$style = '';
if ( in_array( $column_name, $hidden ) )
if ( in_array( $column_name, $hidden ) ) {
$style = ' style="display:none;"';
}
$attributes = "$class$style";
$attributes = "class='$classes'$style";
if ( 'cb' == $column_name ) {
echo '<th scope="row" class="check-column">';
@@ -1081,16 +1126,34 @@ class WP_List_Table {
elseif ( method_exists( $this, 'column_' . $column_name ) ) {
echo "<td $attributes>";
echo call_user_func( array( $this, 'column_' . $column_name ), $item );
echo $this->handle_row_actions( $item, $column_name, $primary );
echo "</td>";
}
else {
echo "<td $attributes>";
echo $this->column_default( $item, $column_name );
echo $this->handle_row_actions( $item, $column_name, $primary );
echo "</td>";
}
}
}
/**
* Generate and display row actions links
*
* @since 4.3.0
* @access protected
*
* @param object $item Item being acted upon
* @param string $column_name Current column name
* @param string $primary Primary column name
*
* @return string
*/
protected function handle_row_actions( $item, $column_name, $primary ) {
return '';
}
/**
* Handle an incoming ajax request (called from admin-ajax.php)
*