3 * List Table API: WP_Media_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying media items in a list table.
18 class WP_Media_List_Table extends WP_List_Table {
20 * Holds the number of pending comments for each post.
26 protected $comment_pending_count = array();
38 * @see WP_List_Table::__construct() for more information on default arguments.
40 * @param array $args An associative array of arguments.
42 public function __construct( $args = array() ) {
43 $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
46 'list' => __( 'List View' ),
47 'grid' => __( 'Grid View' )
50 parent::__construct( array(
52 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
60 public function ajax_user_can() {
61 return current_user_can('upload_files');
66 * @global WP_Query $wp_query
67 * @global array $post_mime_types
68 * @global array $avail_post_mime_types
69 * @global string $mode
71 public function prepare_items() {
72 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
74 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
76 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
78 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
80 $this->set_pagination_args( array(
81 'total_items' => $wp_query->found_posts,
82 'total_pages' => $wp_query->max_num_pages,
83 'per_page' => $wp_query->query_vars['posts_per_page'],
88 * @global array $post_mime_types
89 * @global array $avail_post_mime_types
92 protected function get_views() {
93 global $post_mime_types, $avail_post_mime_types;
95 $type_links = array();
97 $filter = empty( $_GET['attachment-filter'] ) ? '' : $_GET['attachment-filter'];
99 $type_links['all'] = sprintf(
100 '<option value=""%s>%s</option>',
101 selected( $filter, true, false ),
102 __( 'All media items' )
105 foreach ( $post_mime_types as $mime_type => $label ) {
106 if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
110 $selected = selected(
111 $filter && 0 === strpos( $filter, 'post_mime_type:' ) &&
112 wp_match_mime_types( $mime_type, str_replace( 'post_mime_type:', '', $filter ) ),
117 $type_links[$mime_type] = sprintf(
118 '<option value="post_mime_type:%s"%s>%s</option>',
119 esc_attr( $mime_type ),
124 $type_links['detached'] = '<option value="detached"' . ( $this->detached ? ' selected="selected"' : '' ) . '>' . __( 'Unattached' ) . '</option>';
126 if ( $this->is_trash || ( defined( 'MEDIA_TRASH') && MEDIA_TRASH ) ) {
127 $type_links['trash'] = sprintf(
128 '<option value="trash"%s>%s</option>',
129 selected( 'trash' === $filter, true, false ),
130 _x( 'Trash', 'attachment filter' )
140 protected function get_bulk_actions() {
143 if ( $this->is_trash ) {
144 $actions['untrash'] = __( 'Restore' );
145 $actions['delete'] = __( 'Delete Permanently' );
147 $actions['trash'] = _x( 'Trash', 'verb' );
150 $actions['delete'] = __( 'Delete Permanently' );
153 if ( $this->detached )
154 $actions['attach'] = __( 'Attach' );
160 * @param string $which
162 protected function extra_tablenav( $which ) {
163 if ( 'bar' !== $which ) {
167 <div class="actions">
169 if ( ! is_singular() ) {
170 if ( ! $this->is_trash ) {
171 $this->months_dropdown( 'attachment' );
174 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
175 do_action( 'restrict_manage_posts', $this->screen->post_type );
177 submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
180 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
181 submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
191 public function current_action() {
192 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
195 if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
198 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
201 return parent::current_action();
208 public function has_items() {
215 public function no_items() {
216 _e( 'No media files found.' );
220 * Override parent views so we can use the filter bar display.
222 * @global string $mode
224 public function views() {
227 $views = $this->get_views();
229 $this->screen->render_screen_reader_content( 'heading_views' );
231 <div class="wp-filter">
232 <div class="filter-items">
233 <?php $this->view_switcher( $mode ); ?>
235 <label for="attachment-filter" class="screen-reader-text"><?php _e( 'Filter by type' ); ?></label>
236 <select class="attachment-filters" name="attachment-filter" id="attachment-filter">
238 if ( ! empty( $views ) ) {
239 foreach ( $views as $class => $view ) {
247 $this->extra_tablenav( 'bar' );
249 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
250 $views = apply_filters( "views_{$this->screen->id}", array() );
252 // Back compat for pre-4.0 view links.
253 if ( ! empty( $views ) ) {
254 echo '<ul class="filter-links">';
255 foreach ( $views as $class => $view ) {
256 echo "<li class='$class'>$view</li>";
263 <div class="search-form">
264 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
265 <input type="search" placeholder="<?php esc_attr_e( 'Search media items...' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
274 public function get_columns() {
275 $posts_columns = array();
276 $posts_columns['cb'] = '<input type="checkbox" />';
277 /* translators: column name */
278 $posts_columns['title'] = _x( 'File', 'column name' );
279 $posts_columns['author'] = __( 'Author' );
281 $taxonomies = get_taxonomies_for_attachments( 'objects' );
282 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
285 * Filters the taxonomy columns for attachments in the Media list table.
289 * @param array $taxonomies An array of registered taxonomies to show for attachments.
290 * @param string $post_type The post type. Default 'attachment'.
292 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
293 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
295 foreach ( $taxonomies as $taxonomy ) {
296 if ( 'category' === $taxonomy ) {
297 $column_key = 'categories';
298 } elseif ( 'post_tag' === $taxonomy ) {
299 $column_key = 'tags';
301 $column_key = 'taxonomy-' . $taxonomy;
303 $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
306 /* translators: column name */
307 if ( !$this->detached ) {
308 $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
309 if ( post_type_supports( 'attachment', 'comments' ) )
310 $posts_columns['comments'] = '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>';
312 /* translators: column name */
313 $posts_columns['date'] = _x( 'Date', 'column name' );
315 * Filters the Media list table columns.
319 * @param array $posts_columns An array of columns displayed in the Media list table.
320 * @param bool $detached Whether the list table contains media not attached
321 * to any posts. Default true.
323 return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
330 protected function get_sortable_columns() {
333 'author' => 'author',
334 'parent' => 'parent',
335 'comments' => 'comment_count',
336 'date' => array( 'date', true ),
341 * Handles the checkbox column output.
346 * @param WP_Post $post The current WP_Post object.
348 public function column_cb( $post ) {
349 if ( current_user_can( 'edit_post', $post->ID ) ) { ?>
350 <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>"><?php
351 echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
353 <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
358 * Handles the title column output.
363 * @param WP_Post $post The current WP_Post object.
365 public function column_title( $post ) {
366 list( $mime ) = explode( '/', $post->post_mime_type );
368 $title = _draft_or_post_title();
369 $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
370 $link_start = $link_end = '';
372 if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
373 $link_start = sprintf(
374 '<a href="%s" aria-label="%s">',
375 get_edit_post_link( $post->ID ),
376 /* translators: %s: attachment title */
377 esc_attr( sprintf( __( '“%s” (Edit)' ), $title ) )
382 $class = $thumb ? ' class="has-media-icon"' : '';
384 <strong<?php echo $class; ?>>
388 <span class="media-icon <?php echo sanitize_html_class( $mime . '-icon' ); ?>"><?php echo $thumb; ?></span>
390 echo $title . $link_end;
391 _media_states( $post );
395 <span class="screen-reader-text"><?php _e( 'File name:' ); ?> </span>
397 $file = get_attached_file( $post->ID );
398 echo esc_html( wp_basename( $file ) );
405 * Handles the author column output.
410 * @param WP_Post $post The current WP_Post object.
412 public function column_author( $post ) {
413 printf( '<a href="%s">%s</a>',
414 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
420 * Handles the description column output.
425 * @param WP_Post $post The current WP_Post object.
427 public function column_desc( $post ) {
428 echo has_excerpt() ? $post->post_excerpt : '';
432 * Handles the date column output.
437 * @param WP_Post $post The current WP_Post object.
439 public function column_date( $post ) {
440 if ( '0000-00-00 00:00:00' === $post->post_date ) {
441 $h_time = __( 'Unpublished' );
443 $m_time = $post->post_date;
444 $time = get_post_time( 'G', true, $post, false );
445 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
447 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
449 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
452 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
460 * Handles the parent column output.
465 * @param WP_Post $post The current WP_Post object.
467 public function column_parent( $post ) {
468 $user_can_edit = current_user_can( 'edit_post', $post->ID );
470 if ( $post->post_parent > 0 ) {
471 $parent = get_post( $post->post_parent );
477 $title = _draft_or_post_title( $post->post_parent );
478 $parent_type = get_post_type_object( $parent->post_type );
480 if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) {
482 <strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
483 <?php echo $title ?></a></strong><?php
484 } elseif ( $parent_type && current_user_can( 'read_post', $post->post_parent ) ) {
486 <strong><?php echo $title ?></strong><?php
488 _e( '(Private post)' );
491 if ( $user_can_edit ):
492 $detach_url = add_query_arg( array(
493 'parent_post_id' => $post->post_parent,
494 'media[]' => $post->ID,
495 '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
498 '<br /><a href="%s" class="hide-if-no-js detach-from-parent" aria-label="%s">%s</a>',
500 /* translators: %s: title of the post the attachment is attached to */
501 esc_attr( sprintf( __( 'Detach from “%s”' ), $title ) ),
506 _e( '(Unattached)' ); ?>
507 <?php if ( $user_can_edit ) {
508 $title = _draft_or_post_title( $post->post_parent );
510 '<br /><a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
512 /* translators: %s: attachment title */
513 esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $title ) ),
521 * Handles the comments column output.
526 * @param WP_Post $post The current WP_Post object.
528 public function column_comments( $post ) {
529 echo '<div class="post-com-count-wrapper">';
531 if ( isset( $this->comment_pending_count[ $post->ID ] ) ) {
532 $pending_comments = $this->comment_pending_count[ $post->ID ];
534 $pending_comments = get_pending_comments_num( $post->ID );
537 $this->comments_bubble( $post->ID, $pending_comments );
543 * Handles output for the default column.
548 * @param WP_Post $post The current WP_Post object.
549 * @param string $column_name Current column name.
551 public function column_default( $post, $column_name ) {
552 if ( 'categories' === $column_name ) {
553 $taxonomy = 'category';
554 } elseif ( 'tags' === $column_name ) {
555 $taxonomy = 'post_tag';
556 } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
557 $taxonomy = substr( $column_name, 9 );
563 $terms = get_the_terms( $post->ID, $taxonomy );
564 if ( is_array( $terms ) ) {
566 foreach ( $terms as $t ) {
567 $posts_in_term_qv = array();
568 $posts_in_term_qv['taxonomy'] = $taxonomy;
569 $posts_in_term_qv['term'] = $t->slug;
571 $out[] = sprintf( '<a href="%s">%s</a>',
572 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
573 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
576 /* translators: used between list items, there is a space after the comma */
577 echo join( __( ', ' ), $out );
579 echo '<span aria-hidden="true">—</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->no_terms . '</span>';
586 * Fires for each custom column in the Media list table.
588 * Custom columns are registered using the {@see 'manage_media_columns'} filter.
592 * @param string $column_name Name of the custom column.
593 * @param int $post_id Attachment ID.
595 do_action( 'manage_media_custom_column', $column_name, $post->ID );
600 * @global WP_Post $post
602 public function display_rows() {
603 global $post, $wp_query;
605 $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
606 reset( $wp_query->posts );
608 $this->comment_pending_count = get_pending_comments_num( $post_ids );
610 add_filter( 'the_title','esc_html' );
612 while ( have_posts() ) : the_post();
614 ( $this->is_trash && $post->post_status != 'trash' )
615 || ( ! $this->is_trash && $post->post_status === 'trash' )
619 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
621 <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
622 <?php $this->single_row_columns( $post ); ?>
629 * Gets the name of the default primary column.
634 * @return string Name of the default primary column, in this case, 'title'.
636 protected function get_default_primary_column_name() {
641 * @param WP_Post $post
642 * @param string $att_title
646 private function _get_row_actions( $post, $att_title ) {
649 if ( $this->detached ) {
650 if ( current_user_can( 'edit_post', $post->ID ) ) {
651 $actions['edit'] = sprintf(
652 '<a href="%s" aria-label="%s">%s</a>',
653 get_edit_post_link( $post->ID ),
654 /* translators: %s: attachment title */
655 esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ),
659 if ( current_user_can( 'delete_post', $post->ID ) ) {
660 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
661 $actions['trash'] = sprintf(
662 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
663 wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ),
664 /* translators: %s: attachment title */
665 esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ),
666 _x( 'Trash', 'verb' )
669 $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
670 $actions['delete'] = sprintf(
671 '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
672 wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ),
674 /* translators: %s: attachment title */
675 esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ),
676 __( 'Delete Permanently' )
680 $actions['view'] = sprintf(
681 '<a href="%s" aria-label="%s" rel="permalink">%s</a>',
682 get_permalink( $post->ID ),
683 /* translators: %s: attachment title */
684 esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ),
688 if ( current_user_can( 'edit_post', $post->ID ) ) {
689 $actions['attach'] = sprintf(
690 '<a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
692 /* translators: %s: attachment title */
693 esc_attr( sprintf( __( 'Attach “%s” to existing content' ), $att_title ) ),
699 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash ) {
700 $actions['edit'] = sprintf(
701 '<a href="%s" aria-label="%s">%s</a>',
702 get_edit_post_link( $post->ID ),
703 /* translators: %s: attachment title */
704 esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ),
708 if ( current_user_can( 'delete_post', $post->ID ) ) {
709 if ( $this->is_trash ) {
710 $actions['untrash'] = sprintf(
711 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
712 wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ),
713 /* translators: %s: attachment title */
714 esc_attr( sprintf( __( 'Restore “%s” from the Trash' ), $att_title ) ),
717 } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
718 $actions['trash'] = sprintf(
719 '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
720 wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ),
721 /* translators: %s: attachment title */
722 esc_attr( sprintf( __( 'Move “%s” to the Trash' ), $att_title ) ),
723 _x( 'Trash', 'verb' )
726 if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
727 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
728 $actions['delete'] = sprintf(
729 '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
730 wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ),
732 /* translators: %s: attachment title */
733 esc_attr( sprintf( __( 'Delete “%s” permanently' ), $att_title ) ),
734 __( 'Delete Permanently' )
738 if ( ! $this->is_trash ) {
739 $actions['view'] = sprintf(
740 '<a href="%s" aria-label="%s" rel="permalink">%s</a>',
741 get_permalink( $post->ID ),
742 /* translators: %s: attachment title */
743 esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ),
750 * Filters the action links for each attachment in the Media list table.
754 * @param array $actions An array of action links for each attachment.
755 * Default 'Edit', 'Delete Permanently', 'View'.
756 * @param WP_Post $post WP_Post object for the current attachment.
757 * @param bool $detached Whether the list table contains media not attached
758 * to any posts. Default true.
760 return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
764 * Generates and displays row action links.
769 * @param object $post Attachment being acted upon.
770 * @param string $column_name Current column name.
771 * @param string $primary Primary column name.
772 * @return string Row actions output for media attachments.
774 protected function handle_row_actions( $post, $column_name, $primary ) {
775 if ( $primary !== $column_name ) {
779 $att_title = _draft_or_post_title();
780 return $this->row_actions( $this->_get_row_actions( $post, $att_title ) );