3 * Media Library List Table class.
6 * @subpackage List_Table
10 class WP_Media_List_Table extends WP_List_Table {
22 * @see WP_List_Table::__construct() for more information on default arguments.
24 * @param array $args An associative array of arguments.
26 public function __construct( $args = array() ) {
27 $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
30 'list' => __( 'List View' ),
31 'grid' => __( 'Grid View' )
34 parent::__construct( array(
36 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
44 public function ajax_user_can() {
45 return current_user_can('upload_files');
50 * @global WP_Query $wp_query
51 * @global array $post_mime_types
52 * @global array $avail_post_mime_types
53 * @global string $mode
55 public function prepare_items() {
56 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
58 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
60 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
62 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
64 $this->set_pagination_args( array(
65 'total_items' => $wp_query->found_posts,
66 'total_pages' => $wp_query->max_num_pages,
67 'per_page' => $wp_query->query_vars['posts_per_page'],
74 * @global array $post_mime_types
75 * @global array $avail_post_mime_types
78 protected function get_views() {
79 global $wpdb, $post_mime_types, $avail_post_mime_types;
81 $type_links = array();
82 $_num_posts = (array) wp_count_attachments();
83 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
84 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
85 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
87 foreach ( $matches as $type => $reals ) {
88 foreach ( $reals as $real ) {
89 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
92 $selected = empty( $_GET['attachment-filter'] ) ? ' selected="selected"' : '';
93 $type_links['all'] = "<option value=''$selected>" . sprintf( _nx( 'All (%s)', 'All (%s)', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</option>';
94 foreach ( $post_mime_types as $mime_type => $label ) {
95 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
99 if ( !empty( $_GET['attachment-filter'] ) && strpos( $_GET['attachment-filter'], 'post_mime_type:' ) === 0 && wp_match_mime_types( $mime_type, str_replace( 'post_mime_type:', '', $_GET['attachment-filter'] ) ) )
100 $selected = ' selected="selected"';
101 if ( !empty( $num_posts[$mime_type] ) )
102 $type_links[$mime_type] = '<option value="post_mime_type:' . esc_attr( $mime_type ) . '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
104 $type_links['detached'] = '<option value="detached"' . ( $this->detached ? ' selected="selected"' : '' ) . '>' . sprintf( _nx( 'Unattached (%s)', 'Unattached (%s)', $total_orphans, 'detached files' ), number_format_i18n( $total_orphans ) ) . '</option>';
106 if ( !empty($_num_posts['trash']) )
107 $type_links['trash'] = '<option value="trash"' . ( (isset($_GET['attachment-filter']) && $_GET['attachment-filter'] == 'trash' ) ? ' selected="selected"' : '') . '>' . sprintf( _nx( 'Trash (%s)', 'Trash (%s)', $_num_posts['trash'], 'uploaded files' ), number_format_i18n( $_num_posts['trash'] ) ) . '</option>';
116 protected function get_bulk_actions() {
119 if ( $this->is_trash ) {
120 $actions['untrash'] = __( 'Restore' );
121 $actions['delete'] = __( 'Delete Permanently' );
123 $actions['trash'] = __( 'Trash' );
126 $actions['delete'] = __( 'Delete Permanently' );
129 if ( $this->detached )
130 $actions['attach'] = __( 'Attach to a post' );
136 * @param string $which
138 protected function extra_tablenav( $which ) {
139 if ( 'bar' !== $which ) {
143 <div class="actions">
145 if ( ! is_singular() ) {
146 if ( ! $this->is_trash ) {
147 $this->months_dropdown( 'attachment' );
150 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
151 do_action( 'restrict_manage_posts' );
152 submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
155 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
156 submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
166 public function current_action() {
167 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
170 if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
173 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
176 return parent::current_action();
183 public function has_items() {
190 public function no_items() {
191 _e( 'No media attachments found.' );
195 * Override parent views so we can use the filter bar display.
197 * @global string $mode
199 public function views() {
202 $views = $this->get_views();
204 <div class="wp-filter">
205 <div class="filter-items">
206 <?php $this->view_switcher( $mode ); ?>
208 <label for="attachment-filter" class="screen-reader-text"><?php _e( 'Filter by type' ); ?></label>
209 <select class="attachment-filters" name="attachment-filter" id="attachment-filter">
211 if ( ! empty( $views ) ) {
212 foreach ( $views as $class => $view ) {
220 $this->extra_tablenav( 'bar' );
222 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
223 $views = apply_filters( "views_{$this->screen->id}", array() );
225 // Back compat for pre-4.0 view links.
226 if ( ! empty( $views ) ) {
227 echo '<ul class="filter-links">';
228 foreach ( $views as $class => $view ) {
229 echo "<li class='$class'>$view</li>";
236 <div class="search-form">
237 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
238 <input type="search" placeholder="<?php esc_attr_e( 'Search' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
247 public function get_columns() {
248 $posts_columns = array();
249 $posts_columns['cb'] = '<input type="checkbox" />';
250 /* translators: column name */
251 $posts_columns['title'] = _x( 'File', 'column name' );
252 $posts_columns['author'] = __( 'Author' );
254 $taxonomies = get_taxonomies_for_attachments( 'objects' );
255 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
258 * Filter the taxonomy columns for attachments in the Media list table.
262 * @param array $taxonomies An array of registered taxonomies to show for attachments.
263 * @param string $post_type The post type. Default 'attachment'.
265 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
266 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
268 foreach ( $taxonomies as $taxonomy ) {
269 if ( 'category' == $taxonomy )
270 $column_key = 'categories';
271 elseif ( 'post_tag' == $taxonomy )
272 $column_key = 'tags';
274 $column_key = 'taxonomy-' . $taxonomy;
276 $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
279 /* translators: column name */
280 if ( !$this->detached ) {
281 $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
282 if ( post_type_supports( 'attachment', 'comments' ) )
283 $posts_columns['comments'] = '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>';
285 /* translators: column name */
286 $posts_columns['date'] = _x( 'Date', 'column name' );
288 * Filter the Media list table columns.
292 * @param array $posts_columns An array of columns displayed in the Media list table.
293 * @param bool $detached Whether the list table contains media not attached
294 * to any posts. Default true.
296 return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
303 protected function get_sortable_columns() {
306 'author' => 'author',
307 'parent' => 'parent',
308 'comments' => 'comment_count',
309 'date' => array( 'date', true ),
314 * Handles the checkbox column output.
319 * @param WP_Post $post The current WP_Post object.
321 public function column_cb( $post ) {
322 if ( current_user_can( 'edit_post', $post->ID ) ) { ?>
323 <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>"><?php
324 echo sprintf( __( 'Select %s' ), _draft_or_post_title() );
326 <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
331 * Handles the title column output.
336 * @param WP_Post $post The current WP_Post object.
338 public function column_title( $post ) {
339 list( $mime ) = explode( '/', $post->post_mime_type );
341 $title = _draft_or_post_title();
342 $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
343 $link_start = $link_end = '';
345 if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
346 $link_start = '<a href="' . get_edit_post_link( $post->ID ) . '">';
350 $class = $thumb ? ' class="has-media-icon"' : '';
353 <strong<?php echo $class; ?>>
354 <?php echo $link_start; ?>
355 <?php if ( $thumb ) : ?>
356 <span class="media-icon <?php echo sanitize_html_class( $mime . '-icon' ); ?>"><?php echo $thumb; ?></span>
359 <span aria-hidden="true"><?php echo $title; ?></span>
360 <span class="screen-reader-text"><?php printf( __( 'Edit “%s”' ), $title ); ?></span>
361 <?php echo $link_end; ?>
362 <?php _media_states( $post ); ?>
364 <p class="filename"><span class="screen-reader-text"><?php _e( 'File name:' ); ?> </span><?php echo wp_basename( $post->guid ); ?></p>
369 * Handles the author column output.
374 * @param WP_Post $post The current WP_Post object.
376 public function column_author( $post ) {
377 printf( '<a href="%s">%s</a>',
378 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
384 * Handles the description column output.
389 * @param WP_Post $post The current WP_Post object.
391 public function column_desc( $post ) {
392 echo has_excerpt() ? $post->post_excerpt : '';
396 * Handles the date column output.
401 * @param WP_Post $post The current WP_Post object.
403 public function column_date( $post ) {
404 if ( '0000-00-00 00:00:00' == $post->post_date ) {
405 $h_time = __( 'Unpublished' );
407 $m_time = $post->post_date;
408 $time = get_post_time( 'G', true, $post, false );
409 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
411 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
413 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
416 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
424 * Handles the parent column output.
429 * @param WP_Post $post The current WP_Post object.
431 public function column_parent( $post ) {
432 $user_can_edit = current_user_can( 'edit_post', $post->ID );
434 if ( $post->post_parent > 0 ) {
435 $parent = get_post( $post->post_parent );
441 $title = _draft_or_post_title( $post->post_parent );
442 $parent_type = get_post_type_object( $parent->post_type );
445 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
446 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
447 <?php echo $title ?></a><?php
451 <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
453 if ( $user_can_edit ):
454 $detach_url = add_query_arg( array(
455 'parent_post_id' => $post->post_parent,
456 'media[]' => $post->ID,
457 '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
458 ), 'upload.php' ); ?>
459 <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
462 _e( '(Unattached)' ); ?><br />
463 <?php if ( $user_can_edit ) { ?>
464 <a class="hide-if-no-js"
465 onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
467 <?php _e( 'Attach' ); ?></a>
473 * Handles the comments column output.
478 * @param WP_Post $post The current WP_Post object.
480 public function column_comments( $post ) {
481 echo '<div class="post-com-count-wrapper">';
483 $pending_comments = get_pending_comments_num( $post->ID );
484 $this->comments_bubble( $post->ID, $pending_comments );
490 * Handles output for the default column.
495 * @param WP_Post $post The current WP_Post object.
496 * @param string $column_name Current column name.
498 public function column_default( $post, $column_name ) {
499 if ( 'categories' == $column_name ) {
500 $taxonomy = 'category';
501 } elseif ( 'tags' == $column_name ) {
502 $taxonomy = 'post_tag';
503 } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
504 $taxonomy = substr( $column_name, 9 );
510 $terms = get_the_terms( $post->ID, $taxonomy );
511 if ( is_array( $terms ) ) {
513 foreach ( $terms as $t ) {
514 $posts_in_term_qv = array();
515 $posts_in_term_qv['taxonomy'] = $taxonomy;
516 $posts_in_term_qv['term'] = $t->slug;
518 $out[] = sprintf( '<a href="%s">%s</a>',
519 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
520 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
523 /* translators: used between list items, there is a space after the comma */
524 echo join( __( ', ' ), $out );
526 echo '<span aria-hidden="true">—</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->no_terms . '</span>';
533 * Fires for each custom column in the Media list table.
535 * Custom columns are registered using the {@see 'manage_media_columns'} filter.
539 * @param string $column_name Name of the custom column.
540 * @param int $post_id Attachment ID.
542 do_action( 'manage_media_custom_column', $column_name, $post->ID );
547 * @global WP_Post $post
549 public function display_rows() {
552 add_filter( 'the_title','esc_html' );
554 while ( have_posts() ) : the_post();
556 ( $this->is_trash && $post->post_status != 'trash' )
557 || ( ! $this->is_trash && $post->post_status == 'trash' )
561 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
563 <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
564 <?php $this->single_row_columns( $post ); ?>
571 * Gets the name of the default primary column.
576 * @return string Name of the default primary column, in this case, 'title'.
578 protected function get_default_primary_column_name() {
583 * @param WP_Post $post
584 * @param string $att_title
588 private function _get_row_actions( $post, $att_title ) {
591 if ( $this->detached ) {
592 if ( current_user_can( 'edit_post', $post->ID ) )
593 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID ) . '">' . __( 'Edit' ) . '</a>';
594 if ( current_user_can( 'delete_post', $post->ID ) )
595 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
596 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
598 $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
599 $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
601 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
602 if ( current_user_can( 'edit_post', $post->ID ) )
603 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
606 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
607 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID ) . '">' . __( 'Edit' ) . '</a>';
608 if ( current_user_can( 'delete_post', $post->ID ) ) {
609 if ( $this->is_trash )
610 $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
611 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
612 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
613 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
614 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
615 $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
618 if ( !$this->is_trash ) {
619 $title =_draft_or_post_title( $post->post_parent );
620 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
625 * Filter the action links for each attachment in the Media list table.
629 * @param array $actions An array of action links for each attachment.
630 * Default 'Edit', 'Delete Permanently', 'View'.
631 * @param WP_Post $post WP_Post object for the current attachment.
632 * @param bool $detached Whether the list table contains media not attached
633 * to any posts. Default true.
635 return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
639 * Generates and displays row action links.
644 * @param object $post Attachment being acted upon.
645 * @param string $column_name Current column name.
646 * @param string $primary Primary column name.
647 * @return string Row actions output for media attachments.
649 protected function handle_row_actions( $post, $column_name, $primary ) {
650 if ( $primary !== $column_name ) {
654 $att_title = _draft_or_post_title();
655 return $this->row_actions( $this->_get_row_actions( $post, $att_title ) );