Override ->single_row_columns() in WP_Posts_List_Table.

Break apart the giant `switch` statement in `->single_row()` into `column_{$column_name}` methods.
To maintain the `->single_row_columns( $item )` interface, add a property, `$current_level`, to allow access to `$level`.

This list table class is now easier to subclass.

Props joehoyle, wonderboymusic.
See #29881.

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


git-svn-id: http://core.svn.wordpress.org/trunk@32711 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-06-12 20:18:24 +00:00
parent d0aa8e4a6e
commit 3f4cadf4aa
2 changed files with 318 additions and 252 deletions

View File

@ -47,6 +47,12 @@ class WP_Posts_List_Table extends WP_List_Table {
private $is_trash; private $is_trash;
/**
* @since 4.3.0
* @var int
*/
protected $current_level = 0;
/** /**
* Constructor. * Constructor.
* *
@ -671,64 +677,13 @@ class WP_Posts_List_Table extends WP_List_Table {
} }
/** /**
* @global string $mode * @since 4.3.0
* @global WP_Post $post
* *
* @param int|WP_Post $post * @param WP_Post $post
* @param int $level
*/ */
public function single_row( $post, $level = 0 ) { public function column_cb( $post ) {
global $mode;
$global_post = get_post();
$post = get_post( $post );
$GLOBALS['post'] = $post;
setup_postdata( $post );
$edit_link = get_edit_post_link( $post->ID );
$title = _draft_or_post_title();
$post_type_object = get_post_type_object( $post->post_type );
$can_edit_post = current_user_can( 'edit_post', $post->ID ); $can_edit_post = current_user_can( 'edit_post', $post->ID );
$title = _draft_or_post_title();
$classes = 'iedit author-' . ( get_current_user_id() == $post->post_author ? 'self' : 'other' );
$lock_holder = wp_check_post_lock( $post->ID );
if ( $lock_holder ) {
$classes .= ' wp-locked';
$lock_holder = get_userdata( $lock_holder );
}
if ( $post->post_parent ) {
$count = count( get_post_ancestors( $post->ID ) );
$classes .= ' level-'. $count;
} else {
$classes .= ' level-0';
}
?>
<tr id="post-<?php echo $post->ID; ?>" class="<?php echo implode( ' ', get_post_class( $classes, $post->ID ) ); ?>">
<?php
list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$classes = "$column_name column-$column_name";
if ( $primary === $column_name ) {
$classes .= ' has-row-actions column-primary';
}
if ( 'title' === $column_name ) {
$classes .= ' page-title'; // Special addition for title column
}
if ( in_array( $column_name, $hidden ) ) {
$classes .= ' hidden';
}
$attributes = "class='$classes'";
if ( 'cb' === $column_name ) {
?> ?>
<th scope="row" class="check-column"> <th scope="row" class="check-column">
<?php if ( $can_edit_post ) { ?> <?php if ( $can_edit_post ) { ?>
@ -738,26 +693,33 @@ class WP_Posts_List_Table extends WP_List_Table {
<?php } ?> <?php } ?>
</th> </th>
<?php <?php
} else { }
echo "<td $attributes>";
switch ( $column_name ) { /**
* @since 4.3.0
*
* @global string $mode
*
* @param WP_Post $post
*/
public function column_title( $post ) {
global $mode;
case 'title':
if ( $this->hierarchical_display ) { if ( $this->hierarchical_display ) {
if ( 0 == $level && (int) $post->post_parent > 0 ) { if ( 0 === $this->current_level && (int) $post->post_parent > 0 ) {
// Sent level 0 by accident, by default, or because we don't know the actual level. // Sent level 0 by accident, by default, or because we don't know the actual level.
$find_main_page = (int) $post->post_parent; $find_main_page = (int) $post->post_parent;
while ( $find_main_page > 0 ) { while ( $find_main_page > 0 ) {
$parent = get_post( $find_main_page ); $parent = get_post( $find_main_page );
if ( is_null( $parent ) ) if ( is_null( $parent ) ) {
break; break;
}
$level++; $this->current_level++;
$find_main_page = (int) $parent->post_parent; $find_main_page = (int) $parent->post_parent;
if ( !isset( $parent_name ) ) { if ( ! isset( $parent_name ) ) {
/** This filter is documented in wp-includes/post-template.php */ /** This filter is documented in wp-includes/post-template.php */
$parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID ); $parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID );
} }
@ -765,29 +727,38 @@ class WP_Posts_List_Table extends WP_List_Table {
} }
} }
$pad = str_repeat( '&#8212; ', $level ); $pad = str_repeat( '&#8212; ', $this->current_level );
echo "<strong>"; echo "<strong>";
if ( $format = get_post_format( $post->ID ) ) { $format = get_post_format( $post->ID );
if ( $format ) {
$label = get_post_format_string( $format ); $label = get_post_format_string( $format );
echo '<a href="' . esc_url( add_query_arg( array( 'post_format' => $format, 'post_type' => $post->post_type ), 'edit.php' ) ) . '" class="post-state-format post-format-icon post-format-' . $format . '" title="' . $label . '">' . $label . ":</a> "; echo '<a href="' . esc_url( add_query_arg( array( 'post_format' => $format, 'post_type' => $post->post_type ), 'edit.php' ) ) . '" class="post-state-format post-format-icon post-format-' . $format . '" title="' . $label . '">' . $label . ":</a> ";
} }
$can_edit_post = current_user_can( 'edit_post', $post->ID );
$title = _draft_or_post_title();
if ( $can_edit_post && $post->post_status != 'trash' ) { if ( $can_edit_post && $post->post_status != 'trash' ) {
$edit_link = get_edit_post_link( $post->ID );
echo '<a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ) . '">' . $pad . $title . '</a>'; echo '<a class="row-title" href="' . $edit_link . '" title="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ) . '">' . $pad . $title . '</a>';
} else { } else {
echo $pad . $title; echo $pad . $title;
} }
_post_states( $post ); _post_states( $post );
if ( isset( $parent_name ) ) if ( isset( $parent_name ) ) {
$post_type_object = get_post_type_object( $post->post_type );
echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name ); echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name );
}
echo "</strong>\n"; echo "</strong>\n";
if ( $can_edit_post && $post->post_status != 'trash' ) { if ( $can_edit_post && $post->post_status != 'trash' ) {
$lock_holder = wp_check_post_lock( $post->ID );
if ( $lock_holder ) { if ( $lock_holder ) {
$lock_holder = get_userdata( $lock_holder );
$locked_avatar = get_avatar( $lock_holder->ID, 18 ); $locked_avatar = get_avatar( $lock_holder->ID, 18 );
$locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) ); $locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) );
} else { } else {
@ -797,13 +768,23 @@ class WP_Posts_List_Table extends WP_List_Table {
echo '<div class="locked-info"><span class="locked-avatar">' . $locked_avatar . '</span> <span class="locked-text">' . $locked_text . "</span></div>\n"; echo '<div class="locked-info"><span class="locked-avatar">' . $locked_avatar . '</span> <span class="locked-text">' . $locked_text . "</span></div>\n";
} }
if ( ! $this->hierarchical_display && 'excerpt' == $mode && current_user_can( 'read_post', $post->ID ) ) if ( ! $this->hierarchical_display && 'excerpt' == $mode && current_user_can( 'read_post', $post->ID ) ) {
the_excerpt(); the_excerpt();
}
get_inline_data( $post ); get_inline_data( $post );
break; }
/**
* @since 4.3.0
*
* @global string $mode
*
* @param WP_Post $post
*/
public function column_date( $post ) {
global $mode;
case 'date':
if ( '0000-00-00 00:00:00' == $post->post_date ) { if ( '0000-00-00 00:00:00' == $post->post_date ) {
$t_time = $h_time = __( 'Unpublished' ); $t_time = $h_time = __( 'Unpublished' );
$time_diff = 0; $time_diff = 0;
@ -814,14 +795,14 @@ class WP_Posts_List_Table extends WP_List_Table {
$time_diff = time() - $time; $time_diff = time() - $time;
if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) ); $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
else } else {
$h_time = mysql2date( __( 'Y/m/d' ), $m_time ); $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
} }
}
if ( 'excerpt' == $mode ) { if ( 'excerpt' == $mode ) {
/** /**
* Filter the published time of the post. * Filter the published time of the post.
* *
@ -836,26 +817,32 @@ class WP_Posts_List_Table extends WP_List_Table {
* @param string $column_name The column name. * @param string $column_name The column name.
* @param string $mode The list display mode ('excerpt' or 'list'). * @param string $mode The list display mode ('excerpt' or 'list').
*/ */
echo apply_filters( 'post_date_column_time', $t_time, $post, $column_name, $mode ); echo apply_filters( 'post_date_column_time', $t_time, $post, 'date', $mode );
} else { } else {
/** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */ /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */
echo '<abbr title="' . $t_time . '">' . apply_filters( 'post_date_column_time', $h_time, $post, $column_name, $mode ) . '</abbr>'; echo '<abbr title="' . $t_time . '">' . apply_filters( 'post_date_column_time', $h_time, $post, 'date', $mode ) . '</abbr>';
} }
echo '<br />'; echo '<br />';
if ( 'publish' == $post->post_status ) { if ( 'publish' == $post->post_status ) {
_e( 'Published' ); _e( 'Published' );
} elseif ( 'future' == $post->post_status ) { } elseif ( 'future' == $post->post_status ) {
if ( $time_diff > 0 ) if ( $time_diff > 0 ) {
echo '<strong class="attention">' . __( 'Missed schedule' ) . '</strong>'; echo '<strong class="attention">' . __( 'Missed schedule' ) . '</strong>';
else } else {
_e( 'Scheduled' ); _e( 'Scheduled' );
}
} else { } else {
_e( 'Last Modified' ); _e( 'Last Modified' );
} }
break; }
case 'comments': /**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_comments( $post ) {
?> ?>
<div class="post-com-count-wrapper"> <div class="post-com-count-wrapper">
<?php <?php
@ -865,25 +852,36 @@ class WP_Posts_List_Table extends WP_List_Table {
?> ?>
</div> </div>
<?php <?php
break; }
case 'author': /**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_author( $post ) {
printf( '<a href="%s">%s</a>', printf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'author' => get_the_author_meta( 'ID' ) ), 'edit.php' )), esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'author' => get_the_author_meta( 'ID' ) ), 'edit.php' )),
get_the_author() get_the_author()
); );
break; }
default: /**
if ( 'categories' == $column_name ) * @since 4.3.0
*
* @param WP_Post $post
* @param string $column_name
*/
public function column_default( $post, $column_name ) {
if ( 'categories' == $column_name ) {
$taxonomy = 'category'; $taxonomy = 'category';
elseif ( 'tags' == $column_name ) } elseif ( 'tags' == $column_name ) {
$taxonomy = 'post_tag'; $taxonomy = 'post_tag';
elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
$taxonomy = substr( $column_name, 9 ); $taxonomy = substr( $column_name, 9 );
else } else {
$taxonomy = false; $taxonomy = false;
}
if ( $taxonomy ) { if ( $taxonomy ) {
$taxonomy_object = get_taxonomy( $taxonomy ); $taxonomy_object = get_taxonomy( $taxonomy );
$terms = get_the_terms( $post->ID, $taxonomy ); $terms = get_the_terms( $post->ID, $taxonomy );
@ -891,8 +889,9 @@ class WP_Posts_List_Table extends WP_List_Table {
$out = array(); $out = array();
foreach ( $terms as $t ) { foreach ( $terms as $t ) {
$posts_in_term_qv = array(); $posts_in_term_qv = array();
if ( 'post' != $post->post_type ) if ( 'post' != $post->post_type ) {
$posts_in_term_qv['post_type'] = $post->post_type; $posts_in_term_qv['post_type'] = $post->post_type;
}
if ( $taxonomy_object->query_var ) { if ( $taxonomy_object->query_var ) {
$posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug; $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug;
} else { } else {
@ -910,7 +909,7 @@ class WP_Posts_List_Table extends WP_List_Table {
} else { } else {
echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . $taxonomy_object->labels->not_found . '</span>'; echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . $taxonomy_object->labels->not_found . '</span>';
} }
break; return;
} }
if ( is_post_type_hierarchical( $post->post_type ) ) { if ( is_post_type_hierarchical( $post->post_type ) ) {
@ -954,14 +953,81 @@ class WP_Posts_List_Table extends WP_List_Table {
* @param int $post_id The current post ID. * @param int $post_id The current post ID.
*/ */
do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID ); do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID );
break;
} }
echo $this->handle_row_actions( $post, $column_name, $primary ); /**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function single_row_columns( $item ) {
list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
foreach ( $columns as $column_name => $column_display_name ) {
$classes = "$column_name column-$column_name";
if ( $primary === $column_name ) {
$classes .= ' has-row-actions column-primary';
}
if ( 'title' === $column_name ) {
$classes .= ' page-title'; // Special addition for title column
}
if ( in_array( $column_name, $hidden ) ) {
$classes .= ' hidden';
}
$attributes = "class='$classes'";
if ( 'cb' === $column_name ) {
$this->column_cb( $item );
} else {
echo "<td $attributes>";
if ( method_exists( $this, 'column_' . $column_name ) ) {
call_user_func( array( $this, 'column_' . $column_name ), $item );
} else {
$this->column_default( $item, $column_name );
}
echo $this->handle_row_actions( $item, $column_name, $primary );
echo '</td>'; echo '</td>';
} }
} }
}
/**
* @global WP_Post $post
*
* @param int|WP_Post $post
* @param int $level
*/
public function single_row( $post, $level = 0 ) {
$global_post = get_post();
$post = get_post( $post );
$this->current_level = $level;
$GLOBALS['post'] = $post;
setup_postdata( $post );
$classes = 'iedit author-' . ( get_current_user_id() == $post->post_author ? 'self' : 'other' );
$lock_holder = wp_check_post_lock( $post->ID );
if ( $lock_holder ) {
$classes .= ' wp-locked';
$lock_holder = get_userdata( $lock_holder );
}
if ( $post->post_parent ) {
$count = count( get_post_ancestors( $post->ID ) );
$classes .= ' level-'. $count;
} else {
$classes .= ' level-0';
}
?> ?>
<tr id="post-<?php echo $post->ID; ?>" class="<?php echo implode( ' ', get_post_class( $classes, $post->ID ) ); ?>">
<?php $this->single_row_columns( $post ); ?>
</tr> </tr>
<?php <?php
$GLOBALS['post'] = $global_post; $GLOBALS['post'] = $global_post;

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.3-alpha-32739'; $wp_version = '4.3-alpha-32740';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.