From 1de436b466451eabb7d34aebbaa4fec1b4935320 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 12 Jun 2015 20:17:59 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@32740 602fd350-edb4-49c9-b593-d223f7449a82 --- .../includes/class-wp-posts-list-table.php | 568 ++++++++++-------- 1 file changed, 317 insertions(+), 251 deletions(-) diff --git a/src/wp-admin/includes/class-wp-posts-list-table.php b/src/wp-admin/includes/class-wp-posts-list-table.php index c5f95119f7..a0d2358070 100644 --- a/src/wp-admin/includes/class-wp-posts-list-table.php +++ b/src/wp-admin/includes/class-wp-posts-list-table.php @@ -47,6 +47,12 @@ class WP_Posts_List_Table extends WP_List_Table { private $is_trash; + /** + * @since 4.3.0 + * @var int + */ + protected $current_level = 0; + /** * Constructor. * @@ -671,45 +677,290 @@ class WP_Posts_List_Table extends WP_List_Table { } /** - * @global string $mode - * @global WP_Post $post + * @since 4.3.0 * - * @param int|WP_Post $post - * @param int $level + * @param WP_Post $post */ - public function single_row( $post, $level = 0 ) { + public function column_cb( $post ) { + $can_edit_post = current_user_can( 'edit_post', $post->ID ); + $title = _draft_or_post_title(); + ?> + + + + +
+ + + hierarchical_display ) { + 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. + $find_main_page = (int) $post->post_parent; + while ( $find_main_page > 0 ) { + $parent = get_post( $find_main_page ); - $post = get_post( $post ); + if ( is_null( $parent ) ) { + break; + } - $GLOBALS['post'] = $post; - setup_postdata( $post ); + $this->current_level++; + $find_main_page = (int) $parent->post_parent; + + if ( ! isset( $parent_name ) ) { + /** This filter is documented in wp-includes/post-template.php */ + $parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID ); + } + } + } + } + + $pad = str_repeat( '— ', $this->current_level ); + echo ""; + + $format = get_post_format( $post->ID ); + if ( $format ) { + $label = get_post_format_string( $format ); + + echo '' . $label . ": "; + } - $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 ); + $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; + if ( $can_edit_post && $post->post_status != 'trash' ) { + $edit_link = get_edit_post_link( $post->ID ); + echo '' . $pad . $title . ''; } else { - $classes .= ' level-0'; + echo $pad . $title; } - ?> - - post_type ); + echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name ); + } + echo "\n"; + + if ( $can_edit_post && $post->post_status != 'trash' ) { + $lock_holder = wp_check_post_lock( $post->ID ); + + if ( $lock_holder ) { + $lock_holder = get_userdata( $lock_holder ); + $locked_avatar = get_avatar( $lock_holder->ID, 18 ); + $locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) ); + } else { + $locked_avatar = $locked_text = ''; + } + + echo '
' . $locked_avatar . ' ' . $locked_text . "
\n"; + } + + if ( ! $this->hierarchical_display && 'excerpt' == $mode && current_user_can( 'read_post', $post->ID ) ) { + the_excerpt(); + } + + get_inline_data( $post ); + } + + /** + * @since 4.3.0 + * + * @global string $mode + * + * @param WP_Post $post + */ + public function column_date( $post ) { + global $mode; + + if ( '0000-00-00 00:00:00' == $post->post_date ) { + $t_time = $h_time = __( 'Unpublished' ); + $time_diff = 0; + } else { + $t_time = get_the_time( __( 'Y/m/d g:i:s a' ) ); + $m_time = $post->post_date; + $time = get_post_time( 'G', true, $post ); + + $time_diff = time() - $time; + + if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) { + $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) ); + } else { + $h_time = mysql2date( __( 'Y/m/d' ), $m_time ); + } + } + + if ( 'excerpt' == $mode ) { + /** + * Filter the published time of the post. + * + * If $mode equals 'excerpt', the published time and date are both displayed. + * If $mode equals 'list' (default), the publish date is displayed, with the + * time and date together available as an abbreviation definition. + * + * @since 2.5.1 + * + * @param array $t_time The published time. + * @param WP_Post $post Post object. + * @param string $column_name The column name. + * @param string $mode The list display mode ('excerpt' or 'list'). + */ + echo apply_filters( 'post_date_column_time', $t_time, $post, 'date', $mode ); + } else { + + /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */ + echo '' . apply_filters( 'post_date_column_time', $h_time, $post, 'date', $mode ) . ''; + } + echo '
'; + if ( 'publish' == $post->post_status ) { + _e( 'Published' ); + } elseif ( 'future' == $post->post_status ) { + if ( $time_diff > 0 ) { + echo '' . __( 'Missed schedule' ) . ''; + } else { + _e( 'Scheduled' ); + } + } else { + _e( 'Last Modified' ); + } + } + + /** + * @since 4.3.0 + * + * @param WP_Post $post + */ + public function column_comments( $post ) { + ?> +
+ comment_pending_count[$post->ID] ) ? $this->comment_pending_count[$post->ID] : 0; + + $this->comments_bubble( $post->ID, $pending_comments ); + ?> +
+ %s', + esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'author' => get_the_author_meta( 'ID' ) ), 'edit.php' )), + get_the_author() + ); + } + + /** + * @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'; + } elseif ( 'tags' == $column_name ) { + $taxonomy = 'post_tag'; + } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) { + $taxonomy = substr( $column_name, 9 ); + } else { + $taxonomy = false; + } + if ( $taxonomy ) { + $taxonomy_object = get_taxonomy( $taxonomy ); + $terms = get_the_terms( $post->ID, $taxonomy ); + if ( is_array( $terms ) ) { + $out = array(); + foreach ( $terms as $t ) { + $posts_in_term_qv = array(); + if ( 'post' != $post->post_type ) { + $posts_in_term_qv['post_type'] = $post->post_type; + } + if ( $taxonomy_object->query_var ) { + $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug; + } else { + $posts_in_term_qv['taxonomy'] = $taxonomy; + $posts_in_term_qv['term'] = $t->slug; + } + + $out[] = sprintf( '%s', + esc_url( add_query_arg( $posts_in_term_qv, 'edit.php' ) ), + esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) ) + ); + } + /* translators: used between list items, there is a space after the comma */ + echo join( __( ', ' ), $out ); + } else { + echo '' . $taxonomy_object->labels->not_found . ''; + } + return; + } + + if ( is_post_type_hierarchical( $post->post_type ) ) { + + /** + * Fires in each custom column on the Posts list table. + * + * This hook only fires if the current post type is hierarchical, + * such as pages. + * + * @since 2.5.0 + * + * @param string $column_name The name of the column to display. + * @param int $post_id The current post ID. + */ + do_action( 'manage_pages_custom_column', $column_name, $post->ID ); + } else { + + /** + * Fires in each custom column in the Posts list table. + * + * This hook only fires if the current post type is non-hierarchical, + * such as posts. + * + * @since 1.5.0 + * + * @param string $column_name The name of the column to display. + * @param int $post_id The current post ID. + */ + do_action( 'manage_posts_custom_column', $column_name, $post->ID ); + } + + /** + * Fires for each custom column of a specific post type in the Posts list table. + * + * The dynamic portion of the hook name, `$post->post_type`, refers to the post type. + * + * @since 3.1.0 + * + * @param string $column_name The name of the column to display. + * @param int $post_id The current post ID. + */ + do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID ); + } + + /** + * @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 ) { @@ -729,239 +980,54 @@ class WP_Posts_List_Table extends WP_List_Table { $attributes = "class='$classes'"; if ( 'cb' === $column_name ) { - ?> - - - - -
- - - column_cb( $item ); } else { echo ""; - switch ( $column_name ) { - - case 'title': - if ( $this->hierarchical_display ) { - if ( 0 == $level && (int) $post->post_parent > 0 ) { - // Sent level 0 by accident, by default, or because we don't know the actual level. - $find_main_page = (int) $post->post_parent; - while ( $find_main_page > 0 ) { - $parent = get_post( $find_main_page ); - - if ( is_null( $parent ) ) - break; - - $level++; - $find_main_page = (int) $parent->post_parent; - - if ( !isset( $parent_name ) ) { - /** This filter is documented in wp-includes/post-template.php */ - $parent_name = apply_filters( 'the_title', $parent->post_title, $parent->ID ); - } - } - } - } - - $pad = str_repeat( '— ', $level ); - echo ""; - - if ( $format = get_post_format( $post->ID ) ) { - $label = get_post_format_string( $format ); - - echo '' . $label . ": "; - } - - if ( $can_edit_post && $post->post_status != 'trash' ) { - echo '' . $pad . $title . ''; - } else { - echo $pad . $title; - } - _post_states( $post ); - - if ( isset( $parent_name ) ) - echo ' | ' . $post_type_object->labels->parent_item_colon . ' ' . esc_html( $parent_name ); - - echo "\n"; - - if ( $can_edit_post && $post->post_status != 'trash' ) { - if ( $lock_holder ) { - $locked_avatar = get_avatar( $lock_holder->ID, 18 ); - $locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) ); - } else { - $locked_avatar = $locked_text = ''; - } - - echo '
' . $locked_avatar . ' ' . $locked_text . "
\n"; - } - - if ( ! $this->hierarchical_display && 'excerpt' == $mode && current_user_can( 'read_post', $post->ID ) ) - the_excerpt(); - - get_inline_data( $post ); - break; - - case 'date': - if ( '0000-00-00 00:00:00' == $post->post_date ) { - $t_time = $h_time = __( 'Unpublished' ); - $time_diff = 0; - } else { - $t_time = get_the_time( __( 'Y/m/d g:i:s a' ) ); - $m_time = $post->post_date; - $time = get_post_time( 'G', true, $post ); - - $time_diff = time() - $time; - - if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) - $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) ); - else - $h_time = mysql2date( __( 'Y/m/d' ), $m_time ); - } - - if ( 'excerpt' == $mode ) { - - /** - * Filter the published time of the post. - * - * If $mode equals 'excerpt', the published time and date are both displayed. - * If $mode equals 'list' (default), the publish date is displayed, with the - * time and date together available as an abbreviation definition. - * - * @since 2.5.1 - * - * @param array $t_time The published time. - * @param WP_Post $post Post object. - * @param string $column_name The column name. - * @param string $mode The list display mode ('excerpt' or 'list'). - */ - echo apply_filters( 'post_date_column_time', $t_time, $post, $column_name, $mode ); - } else { - - /** This filter is documented in wp-admin/includes/class-wp-posts-list-table.php */ - echo '' . apply_filters( 'post_date_column_time', $h_time, $post, $column_name, $mode ) . ''; - } - echo '
'; - if ( 'publish' == $post->post_status ) { - _e( 'Published' ); - } elseif ( 'future' == $post->post_status ) { - if ( $time_diff > 0 ) - echo '' . __( 'Missed schedule' ) . ''; - else - _e( 'Scheduled' ); - } else { - _e( 'Last Modified' ); - } - break; - - case 'comments': - ?> -
- comment_pending_count[$post->ID] ) ? $this->comment_pending_count[$post->ID] : 0; - - $this->comments_bubble( $post->ID, $pending_comments ); - ?> -
- %s', - esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'author' => get_the_author_meta( 'ID' ) ), 'edit.php' )), - get_the_author() - ); - break; - - default: - if ( 'categories' == $column_name ) - $taxonomy = 'category'; - elseif ( 'tags' == $column_name ) - $taxonomy = 'post_tag'; - elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) - $taxonomy = substr( $column_name, 9 ); - else - $taxonomy = false; - - if ( $taxonomy ) { - $taxonomy_object = get_taxonomy( $taxonomy ); - $terms = get_the_terms( $post->ID, $taxonomy ); - if ( is_array( $terms ) ) { - $out = array(); - foreach ( $terms as $t ) { - $posts_in_term_qv = array(); - if ( 'post' != $post->post_type ) - $posts_in_term_qv['post_type'] = $post->post_type; - if ( $taxonomy_object->query_var ) { - $posts_in_term_qv[ $taxonomy_object->query_var ] = $t->slug; - } else { - $posts_in_term_qv['taxonomy'] = $taxonomy; - $posts_in_term_qv['term'] = $t->slug; - } - - $out[] = sprintf( '%s', - esc_url( add_query_arg( $posts_in_term_qv, 'edit.php' ) ), - esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) ) - ); - } - /* translators: used between list items, there is a space after the comma */ - echo join( __( ', ' ), $out ); - } else { - echo '' . $taxonomy_object->labels->not_found . ''; - } - break; - } - - if ( is_post_type_hierarchical( $post->post_type ) ) { - - /** - * Fires in each custom column on the Posts list table. - * - * This hook only fires if the current post type is hierarchical, - * such as pages. - * - * @since 2.5.0 - * - * @param string $column_name The name of the column to display. - * @param int $post_id The current post ID. - */ - do_action( 'manage_pages_custom_column', $column_name, $post->ID ); - } else { - - /** - * Fires in each custom column in the Posts list table. - * - * This hook only fires if the current post type is non-hierarchical, - * such as posts. - * - * @since 1.5.0 - * - * @param string $column_name The name of the column to display. - * @param int $post_id The current post ID. - */ - do_action( 'manage_posts_custom_column', $column_name, $post->ID ); - } - - /** - * Fires for each custom column of a specific post type in the Posts list table. - * - * The dynamic portion of the hook name, `$post->post_type`, refers to the post type. - * - * @since 3.1.0 - * - * @param string $column_name The name of the column to display. - * @param int $post_id The current post ID. - */ - do_action( "manage_{$post->post_type}_posts_custom_column", $column_name, $post->ID ); - break; + 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( $post, $column_name, $primary ); + echo $this->handle_row_actions( $item, $column_name, $primary ); echo ''; } } + } + + /** + * @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'; + } ?> + + single_row_columns( $post ); ?>