In `WP_Media_List_Table::display_rows()`:

* Move the giant `switch` statement into methods
* Call `-single_row_columns()`, which it inherits from `WP_List_Table`

See #29881.


git-svn-id: https://develop.svn.wordpress.org/trunk@32754 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-06-13 15:56:13 +00:00
parent c32f856cf3
commit 31d50764c3
1 changed files with 228 additions and 204 deletions

View File

@ -310,6 +310,224 @@ class WP_Media_List_Table extends WP_List_Table {
);
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_cb( $post ) {
if ( current_user_can( 'edit_post', $post->ID ) ) { ?>
<label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>"><?php
echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
?></label>
<input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
<?php }
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_title( $post ) {
list( $mime ) = explode( '/', $post->post_mime_type );
$user_can_edit = current_user_can( 'edit_post', $post->ID );
$att_title = _draft_or_post_title();
?>
<div class="media-icon <?php echo $mime ?>-icon">
<?php
$thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true );
if ( $thumb ) {
if ( $this->is_trash || ! $user_can_edit ) {
echo $thumb;
} else { ?>
<a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php
echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) );
?>"><?php echo $thumb; ?></a><?php
}
}
?>
</div>
<div class="media-info">
<strong>
<?php
if ( $this->is_trash || ! $user_can_edit ) {
echo $att_title;
} else { ?>
<a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php
echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) );
?>"><?php echo $att_title; ?></a><?php
}
_media_states( $post );
?>
</strong>
<p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
</div>
<?php
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_author( $post ) {
printf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
get_the_author()
);
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_desc( $post ) {
echo has_excerpt() ? $post->post_excerpt : '';
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_date( $post ) {
if ( '0000-00-00 00:00:00' == $post->post_date ) {
$h_time = __( 'Unpublished' );
} else {
$m_time = $post->post_date;
$time = get_post_time( 'G', true, $post, false );
if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
if ( $t_diff < 0 ) {
$h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
} else {
$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
}
} else {
$h_time = mysql2date( __( 'Y/m/d' ), $m_time );
}
}
echo $h_time;
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_parent( $post ) {
$user_can_edit = current_user_can( 'edit_post', $post->ID );
if ( $post->post_parent > 0 ) {
$parent = get_post( $post->post_parent );
} else {
$parent = false;
}
if ( $parent ) {
$title = _draft_or_post_title( $post->post_parent );
$parent_type = get_post_type_object( $parent->post_type );
?>
<strong>
<?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
<a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
<?php echo $title ?></a><?php
} else {
echo $title;
} ?></strong>,
<?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
<?php
if ( $user_can_edit ):
$detach_url = add_query_arg( array(
'parent_post_id' => $post->post_parent,
'media[]' => $post->ID,
'_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
), 'upload.php' ); ?>
<a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
<?php endif;
} else {
_e( '(Unattached)' ); ?><br />
<?php if ( $user_can_edit ) { ?>
<a class="hide-if-no-js"
onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
href="#the-list">
<?php _e( 'Attach' ); ?></a>
<?php }
}
}
/**
* @since 4.3.0
*
* @param WP_Post $post
*/
public function column_comments( $post ) {
echo '<div class="post-com-count-wrapper">';
$pending_comments = get_pending_comments_num( $post->ID );
$this->comments_bubble( $post->ID, $pending_comments );
echo '</div>';
}
/**
* @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 ) {
$terms = get_the_terms( $post->ID, $taxonomy );
if ( is_array( $terms ) ) {
$out = array();
foreach ( $terms as $t ) {
$posts_in_term_qv = array();
$posts_in_term_qv['taxonomy'] = $taxonomy;
$posts_in_term_qv['term'] = $t->slug;
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( $posts_in_term_qv, 'upload.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 '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->not_found . '</span>';
}
return;
}
/**
* Fires for each custom column in the Media list table.
*
* Custom columns are registered using the {@see 'manage_media_columns'} filter.
*
* @since 2.5.0
*
* @param string $column_name Name of the custom column.
* @param int $post_id Attachment ID.
*/
do_action( 'manage_media_custom_column', $column_name, $post->ID );
}
/**
*
* @global WP_Post $post
@ -320,213 +538,19 @@ class WP_Media_List_Table extends WP_List_Table {
add_filter( 'the_title','esc_html' );
while ( have_posts() ) : the_post();
$user_can_edit = current_user_can( 'edit_post', $post->ID );
if ( $this->is_trash && $post->post_status != 'trash'
|| !$this->is_trash && $post->post_status == 'trash' )
if (
( $this->is_trash && $post->post_status != 'trash' )
|| ( ! $this->is_trash && $post->post_status == 'trash' )
) {
continue;
}
$post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
$att_title = _draft_or_post_title();
?>
<tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
<?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 ( in_array( $column_name, $hidden ) ) {
$classes .= ' hidden';
}
$attributes = "class='$classes'";
if ( 'cb' === $column_name ) {
?>
<th scope="row" class="check-column">
<?php if ( $user_can_edit ) { ?>
<label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
<input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
<?php } ?>
</th>
<?php
} else {
echo "<td $attributes>";
switch ( $column_name ) {
case 'title':
list( $mime ) = explode( '/', $post->post_mime_type );
echo "<div class='media-icon {$mime}-icon'>";
if ( $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true ) ) {
if ( $this->is_trash || ! $user_can_edit ) {
echo $thumb;
} else { ?>
<a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
<?php echo $thumb; ?>
</a><?php
}
}
echo '</div><div class="media-info">';
?>
<strong>
<?php if ( $this->is_trash || ! $user_can_edit ) {
echo $att_title;
} else { ?>
<a href="<?php echo get_edit_post_link( $post->ID ); ?>"
title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
<?php echo $att_title; ?></a>
<?php }
_media_states( $post ); ?></strong>
<p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
</div>
<?php
break;
case 'author':
printf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
get_the_author()
);
break;
case 'desc':
echo has_excerpt() ? $post->post_excerpt : '';
break;
case 'date':
if ( '0000-00-00 00:00:00' == $post->post_date ) {
$h_time = __( 'Unpublished' );
} else {
$m_time = $post->post_date;
$time = get_post_time( 'G', true, $post, false );
if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
if ( $t_diff < 0 )
$h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
else
$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
} else {
$h_time = mysql2date( __( 'Y/m/d' ), $m_time );
}
}
echo $h_time;
break;
case 'parent':
if ( $post->post_parent > 0 )
$parent = get_post( $post->post_parent );
else
$parent = false;
if ( $parent ) {
$title = _draft_or_post_title( $post->post_parent );
$parent_type = get_post_type_object( $parent->post_type );
?>
<strong>
<?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
<a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
<?php echo $title ?></a><?php
} else {
echo $title;
} ?></strong>,
<?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
<?php
if ( $user_can_edit ):
$detach_url = add_query_arg( array(
'parent_post_id' => $post->post_parent,
'media[]' => $post->ID,
'_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
), 'upload.php' ); ?>
<a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
<?php endif;
} else {
_e( '(Unattached)' ); ?><br />
<?php if ( $user_can_edit ) { ?>
<a class="hide-if-no-js"
onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
href="#the-list">
<?php _e( 'Attach' ); ?></a>
<?php }
}
break;
case 'comments':
echo '<div class="post-com-count-wrapper">';
$pending_comments = get_pending_comments_num( $post->ID );
$this->comments_bubble( $post->ID, $pending_comments );
echo '</div>';
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 ) {
$terms = get_the_terms( $post->ID, $taxonomy );
if ( is_array( $terms ) ) {
$out = array();
foreach ( $terms as $t ) {
$posts_in_term_qv = array();
$posts_in_term_qv['taxonomy'] = $taxonomy;
$posts_in_term_qv['term'] = $t->slug;
$out[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( $posts_in_term_qv, 'upload.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 '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->not_found . '</span>';
}
break;
}
/**
* Fires for each custom column in the Media list table.
*
* Custom columns are registered using the {@see 'manage_media_columns'} filter.
*
* @since 2.5.0
*
* @param string $column_name Name of the custom column.
* @param int $post_id Attachment ID.
*/
do_action( 'manage_media_custom_column', $column_name, $post->ID );
break;
}
if( $primary === $column_name ) {
echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
}
echo '</td>';
}
}
?>
</tr>
<?php endwhile;
<tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
<?php $this->single_row_columns( $post ); ?>
</tr>
<?php
endwhile;
}
/**