3 * Media Library List Table class.
6 * @subpackage List_Table
10 class WP_Media_List_Table extends WP_List_Table {
18 * @see WP_List_Table::__construct() for more information on default arguments.
20 * @param array $args An associative array of arguments.
22 public function __construct( $args = array() ) {
23 $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
25 parent::__construct( array(
27 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
31 public function ajax_user_can() {
32 return current_user_can('upload_files');
35 public function prepare_items() {
36 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
38 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
40 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
42 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
44 $this->set_pagination_args( array(
45 'total_items' => $wp_query->found_posts,
46 'total_pages' => $wp_query->max_num_pages,
47 'per_page' => $wp_query->query_vars['posts_per_page'],
51 protected function get_views() {
52 global $wpdb, $post_mime_types, $avail_post_mime_types;
54 $type_links = array();
55 $_num_posts = (array) wp_count_attachments();
56 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
57 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
58 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
59 foreach ( $matches as $type => $reals )
60 foreach ( $reals as $real )
61 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
63 $selected = empty( $_GET['attachment-filter'] ) ? ' selected="selected"' : '';
64 $type_links['all'] = "<option value=''$selected>" . sprintf( _nx( 'All (%s)', 'All (%s)', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</option>';
65 foreach ( $post_mime_types as $mime_type => $label ) {
66 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
70 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'] ) ) )
71 $selected = ' selected="selected"';
72 if ( !empty( $num_posts[$mime_type] ) )
73 $type_links[$mime_type] = '<option value="post_mime_type:' . urlencode( $mime_type ) . '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
75 $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>';
77 if ( !empty($_num_posts['trash']) )
78 $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>';
83 protected function get_bulk_actions() {
85 $actions['delete'] = __( 'Delete Permanently' );
86 if ( $this->detached )
87 $actions['attach'] = __( 'Attach to a post' );
92 protected function extra_tablenav( $which ) {
93 if ( 'bar' !== $which ) {
99 if ( ! is_singular() ) {
100 if ( ! $this->is_trash ) {
101 $this->months_dropdown( 'attachment' );
104 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
105 do_action( 'restrict_manage_posts' );
106 submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
109 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
110 submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
116 public function current_action() {
117 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
120 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
123 return parent::current_action();
126 public function has_items() {
130 public function no_items() {
131 _e( 'No media attachments found.' );
134 protected function pagination( $which ) {
137 parent::pagination( $which );
141 * Display a view switcher
146 protected function view_switcher( $current_mode ) {
148 'list' => __( 'List View' ),
149 'grid' => __( 'Grid View' )
153 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
154 <div class="view-switch">
156 foreach ( $modes as $mode => $title ) {
157 $classes = array( 'view-' . $mode );
158 if ( $current_mode == $mode )
159 $classes[] = 'current';
161 "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n",
162 esc_url( add_query_arg( 'mode', $mode ) ),
163 implode( ' ', $classes ),
173 * Override parent views so we can use the filter bar display.
175 public function views() {
178 $views = $this->get_views();
180 <div class="wp-filter">
181 <?php $this->view_switcher( $mode ); ?>
183 <select class="attachment-filters" name="attachment-filter">
185 if ( ! empty( $views ) ) {
186 foreach ( $views as $class => $view ) {
194 $this->extra_tablenav( 'bar' );
196 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
197 $views = apply_filters( "views_{$this->screen->id}", array() );
199 // Back compat for pre-4.0 view links.
200 if ( ! empty( $views ) ) {
201 echo '<ul class="filter-links">';
202 foreach ( $views as $class => $view ) {
203 echo "<li class='$class'>$view</li>";
209 <div class="search-form">
210 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
211 <input type="search" placeholder="<?php esc_attr_e( 'Search' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
216 public function get_columns() {
217 $posts_columns = array();
218 $posts_columns['cb'] = '<input type="checkbox" />';
219 $posts_columns['icon'] = '';
220 /* translators: column name */
221 $posts_columns['title'] = _x( 'File', 'column name' );
222 $posts_columns['author'] = __( 'Author' );
224 $taxonomies = get_taxonomies_for_attachments( 'objects' );
225 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
228 * Filter the taxonomy columns for attachments in the Media list table.
232 * @param array $taxonomies An array of registered taxonomies to show for attachments.
233 * @param string $post_type The post type. Default 'attachment'.
235 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
236 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
238 foreach ( $taxonomies as $taxonomy ) {
239 if ( 'category' == $taxonomy )
240 $column_key = 'categories';
241 elseif ( 'post_tag' == $taxonomy )
242 $column_key = 'tags';
244 $column_key = 'taxonomy-' . $taxonomy;
246 $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
249 /* translators: column name */
250 if ( !$this->detached ) {
251 $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
252 if ( post_type_supports( 'attachment', 'comments' ) )
253 $posts_columns['comments'] = '<span class="vers"><span title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></span></span>';
255 /* translators: column name */
256 $posts_columns['date'] = _x( 'Date', 'column name' );
258 * Filter the Media list table columns.
262 * @param array $posts_columns An array of columns displayed in the Media list table.
263 * @param bool $detached Whether the list table contains media not attached
264 * to any posts. Default true.
266 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
268 return $posts_columns;
271 protected function get_sortable_columns() {
274 'author' => 'author',
275 'parent' => 'parent',
276 'comments' => 'comment_count',
277 'date' => array( 'date', true ),
281 public function display_rows() {
284 add_filter( 'the_title','esc_html' );
287 while ( have_posts() ) : the_post();
288 $user_can_edit = current_user_can( 'edit_post', $post->ID );
290 if ( $this->is_trash && $post->post_status != 'trash'
291 || !$this->is_trash && $post->post_status == 'trash' )
294 $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
295 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
296 $att_title = _draft_or_post_title();
298 <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
301 list( $columns, $hidden ) = $this->get_column_info();
302 foreach ( $columns as $column_name => $column_display_name ) {
303 $class = "class='$column_name column-$column_name'";
306 if ( in_array( $column_name, $hidden ) )
307 $style = ' style="display:none;"';
309 $attributes = $class . $style;
311 switch ( $column_name ) {
315 <th scope="row" class="check-column">
316 <?php if ( $user_can_edit ) { ?>
317 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
318 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
325 list( $mime ) = explode( '/', $post->post_mime_type );
326 $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
328 <td <?php echo $attributes ?>><?php
329 if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
330 if ( $this->is_trash || ! $user_can_edit ) {
334 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
335 <?php echo $thumb; ?>
347 <td <?php echo $attributes ?>><strong>
348 <?php if ( $this->is_trash || ! $user_can_edit ) {
351 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
352 title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
353 <?php echo $att_title; ?></a>
355 _media_states( $post ); ?></strong>
358 if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
359 echo esc_html( strtoupper( $matches[1] ) );
361 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
365 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
373 <td <?php echo $attributes ?>><?php
374 printf( '<a href="%s">%s</a>',
375 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
384 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
389 if ( '0000-00-00 00:00:00' == $post->post_date ) {
390 $h_time = __( 'Unpublished' );
392 $m_time = $post->post_date;
393 $time = get_post_time( 'G', true, $post, false );
394 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
396 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
398 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
400 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
404 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
409 if ( $post->post_parent > 0 )
410 $parent = get_post( $post->post_parent );
415 $title = _draft_or_post_title( $post->post_parent );
416 $parent_type = get_post_type_object( $parent->post_type );
418 <td <?php echo $attributes ?>><strong>
419 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
420 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
421 <?php echo $title ?></a><?php
425 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
430 <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
431 <?php if ( $user_can_edit ) { ?>
432 <a class="hide-if-no-js"
433 onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
435 <?php _e( 'Attach' ); ?></a>
442 $attributes = 'class="comments column-comments num"' . $style;
444 <td <?php echo $attributes ?>>
445 <div class="post-com-count-wrapper">
447 $pending_comments = get_pending_comments_num( $post->ID );
449 $this->comments_bubble( $post->ID, $pending_comments );
457 if ( 'categories' == $column_name )
458 $taxonomy = 'category';
459 elseif ( 'tags' == $column_name )
460 $taxonomy = 'post_tag';
461 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
462 $taxonomy = substr( $column_name, 9 );
467 echo '<td ' . $attributes . '>';
468 if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
470 foreach ( $terms as $t ) {
471 $posts_in_term_qv = array();
472 $posts_in_term_qv['taxonomy'] = $taxonomy;
473 $posts_in_term_qv['term'] = $t->slug;
475 $out[] = sprintf( '<a href="%s">%s</a>',
476 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
477 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
480 /* translators: used between list items, there is a space after the comma */
481 echo join( __( ', ' ), $out );
489 <td <?php echo $attributes ?>>
492 * Fires for each custom column in the Media list table.
494 * Custom columns are registered using the 'manage_media_columns' filter.
498 * @param string $column_name Name of the custom column.
499 * @param int $post_id Attachment ID.
502 <?php do_action( 'manage_media_custom_column', $column_name, $post->ID ); ?>
513 private function _get_row_actions( $post, $att_title ) {
516 if ( $this->detached ) {
517 if ( current_user_can( 'edit_post', $post->ID ) )
518 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
519 if ( current_user_can( 'delete_post', $post->ID ) )
520 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
521 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
523 $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
524 $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>";
526 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
527 if ( current_user_can( 'edit_post', $post->ID ) )
528 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
531 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
532 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
533 if ( current_user_can( 'delete_post', $post->ID ) ) {
534 if ( $this->is_trash )
535 $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
536 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
537 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
538 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
539 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
540 $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>";
543 if ( !$this->is_trash ) {
544 $title =_draft_or_post_title( $post->post_parent );
545 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
550 * Filter the action links for each attachment in the Media list table.
554 * @param array $actions An array of action links for each attachment.
555 * Default 'Edit', 'Delete Permanently', 'View'.
556 * @param WP_Post $post WP_Post object for the current attachment.
557 * @param bool $detached Whether the list table contains media not attached
558 * to any posts. Default true.
560 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );