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'] );
26 'list' => __( 'List View' ),
27 'grid' => __( 'Grid View' )
30 parent::__construct( array(
32 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
36 public function ajax_user_can() {
37 return current_user_can('upload_files');
40 public function prepare_items() {
41 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
43 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
45 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
47 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
49 $this->set_pagination_args( array(
50 'total_items' => $wp_query->found_posts,
51 'total_pages' => $wp_query->max_num_pages,
52 'per_page' => $wp_query->query_vars['posts_per_page'],
56 protected function get_views() {
57 global $wpdb, $post_mime_types, $avail_post_mime_types;
59 $type_links = array();
60 $_num_posts = (array) wp_count_attachments();
61 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
62 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
63 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
64 foreach ( $matches as $type => $reals )
65 foreach ( $reals as $real )
66 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
68 $selected = empty( $_GET['attachment-filter'] ) ? ' selected="selected"' : '';
69 $type_links['all'] = "<option value=''$selected>" . sprintf( _nx( 'All (%s)', 'All (%s)', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</option>';
70 foreach ( $post_mime_types as $mime_type => $label ) {
71 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
75 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'] ) ) )
76 $selected = ' selected="selected"';
77 if ( !empty( $num_posts[$mime_type] ) )
78 $type_links[$mime_type] = '<option value="post_mime_type:' . sanitize_mime_type( $mime_type ) . '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
80 $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>';
82 if ( !empty($_num_posts['trash']) )
83 $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>';
88 protected function get_bulk_actions() {
91 if ( $this->is_trash ) {
92 $actions['untrash'] = __( 'Restore' );
93 $actions['delete'] = __( 'Delete Permanently' );
95 $actions['trash'] = __( 'Trash' );
98 $actions['delete'] = __( 'Delete Permanently' );
101 if ( $this->detached )
102 $actions['attach'] = __( 'Attach to a post' );
108 * @param string $which
110 protected function extra_tablenav( $which ) {
111 if ( 'bar' !== $which ) {
115 <div class="actions">
117 if ( ! is_singular() ) {
118 if ( ! $this->is_trash ) {
119 $this->months_dropdown( 'attachment' );
122 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
123 do_action( 'restrict_manage_posts' );
124 submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
127 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
128 submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
134 public function current_action() {
135 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
138 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
141 return parent::current_action();
144 public function has_items() {
148 public function no_items() {
149 _e( 'No media attachments found.' );
153 * @param string $which
155 protected function pagination( $which ) {
156 parent::pagination( $which );
160 * Override parent views so we can use the filter bar display.
162 public function views() {
165 $views = $this->get_views();
167 <div class="wp-filter">
168 <div class="filter-items">
169 <?php $this->view_switcher( $mode ); ?>
171 <select class="attachment-filters" name="attachment-filter">
173 if ( ! empty( $views ) ) {
174 foreach ( $views as $class => $view ) {
182 $this->extra_tablenav( 'bar' );
184 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
185 $views = apply_filters( "views_{$this->screen->id}", array() );
187 // Back compat for pre-4.0 view links.
188 if ( ! empty( $views ) ) {
189 echo '<ul class="filter-links">';
190 foreach ( $views as $class => $view ) {
191 echo "<li class='$class'>$view</li>";
198 <div class="search-form">
199 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
200 <input type="search" placeholder="<?php esc_attr_e( 'Search' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
205 public function get_columns() {
206 $posts_columns = array();
207 $posts_columns['cb'] = '<input type="checkbox" />';
208 $posts_columns['icon'] = '';
209 /* translators: column name */
210 $posts_columns['title'] = _x( 'File', 'column name' );
211 $posts_columns['author'] = __( 'Author' );
213 $taxonomies = get_taxonomies_for_attachments( 'objects' );
214 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
217 * Filter the taxonomy columns for attachments in the Media list table.
221 * @param array $taxonomies An array of registered taxonomies to show for attachments.
222 * @param string $post_type The post type. Default 'attachment'.
224 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
225 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
227 foreach ( $taxonomies as $taxonomy ) {
228 if ( 'category' == $taxonomy )
229 $column_key = 'categories';
230 elseif ( 'post_tag' == $taxonomy )
231 $column_key = 'tags';
233 $column_key = 'taxonomy-' . $taxonomy;
235 $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
238 /* translators: column name */
239 if ( !$this->detached ) {
240 $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
241 if ( post_type_supports( 'attachment', 'comments' ) )
242 $posts_columns['comments'] = '<span class="vers"><span title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></span></span>';
244 /* translators: column name */
245 $posts_columns['date'] = _x( 'Date', 'column name' );
247 * Filter the Media list table columns.
251 * @param array $posts_columns An array of columns displayed in the Media list table.
252 * @param bool $detached Whether the list table contains media not attached
253 * to any posts. Default true.
255 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
257 return $posts_columns;
260 protected function get_sortable_columns() {
263 'author' => 'author',
264 'parent' => 'parent',
265 'comments' => 'comment_count',
266 'date' => array( 'date', true ),
270 public function display_rows() {
273 add_filter( 'the_title','esc_html' );
276 while ( have_posts() ) : the_post();
277 $user_can_edit = current_user_can( 'edit_post', $post->ID );
279 if ( $this->is_trash && $post->post_status != 'trash'
280 || !$this->is_trash && $post->post_status == 'trash' )
283 $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
284 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
285 $att_title = _draft_or_post_title();
287 <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
290 list( $columns, $hidden ) = $this->get_column_info();
291 foreach ( $columns as $column_name => $column_display_name ) {
292 $class = "class='$column_name column-$column_name'";
295 if ( in_array( $column_name, $hidden ) )
296 $style = ' style="display:none;"';
298 $attributes = $class . $style;
300 switch ( $column_name ) {
304 <th scope="row" class="check-column">
305 <?php if ( $user_can_edit ) { ?>
306 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
307 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
314 list( $mime ) = explode( '/', $post->post_mime_type );
315 $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
317 <td <?php echo $attributes ?>><?php
318 if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
319 if ( $this->is_trash || ! $user_can_edit ) {
323 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
324 <?php echo $thumb; ?>
336 <td <?php echo $attributes ?>><strong>
337 <?php if ( $this->is_trash || ! $user_can_edit ) {
340 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
341 title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
342 <?php echo $att_title; ?></a>
344 _media_states( $post ); ?></strong>
347 if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
348 echo esc_html( strtoupper( $matches[1] ) );
350 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
354 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
362 <td <?php echo $attributes ?>><?php
363 printf( '<a href="%s">%s</a>',
364 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
373 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
378 if ( '0000-00-00 00:00:00' == $post->post_date ) {
379 $h_time = __( 'Unpublished' );
381 $m_time = $post->post_date;
382 $time = get_post_time( 'G', true, $post, false );
383 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
385 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
387 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
389 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
393 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
398 if ( $post->post_parent > 0 )
399 $parent = get_post( $post->post_parent );
404 $title = _draft_or_post_title( $post->post_parent );
405 $parent_type = get_post_type_object( $parent->post_type );
407 <td <?php echo $attributes ?>><strong>
408 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
409 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
410 <?php echo $title ?></a><?php
414 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
419 <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
420 <?php if ( $user_can_edit ) { ?>
421 <a class="hide-if-no-js"
422 onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
424 <?php _e( 'Attach' ); ?></a>
431 $attributes = 'class="comments column-comments num"' . $style;
433 <td <?php echo $attributes ?>>
434 <div class="post-com-count-wrapper">
436 $pending_comments = get_pending_comments_num( $post->ID );
438 $this->comments_bubble( $post->ID, $pending_comments );
446 if ( 'categories' == $column_name )
447 $taxonomy = 'category';
448 elseif ( 'tags' == $column_name )
449 $taxonomy = 'post_tag';
450 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
451 $taxonomy = substr( $column_name, 9 );
456 echo '<td ' . $attributes . '>';
457 if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
459 foreach ( $terms as $t ) {
460 $posts_in_term_qv = array();
461 $posts_in_term_qv['taxonomy'] = $taxonomy;
462 $posts_in_term_qv['term'] = $t->slug;
464 $out[] = sprintf( '<a href="%s">%s</a>',
465 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
466 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
469 /* translators: used between list items, there is a space after the comma */
470 echo join( __( ', ' ), $out );
478 <td <?php echo $attributes ?>><?php
480 * Fires for each custom column in the Media list table.
482 * Custom columns are registered using the 'manage_media_columns' filter.
486 * @param string $column_name Name of the custom column.
487 * @param int $post_id Attachment ID.
489 do_action( 'manage_media_custom_column', $column_name, $post->ID );
501 * @param WP_Post $post
502 * @param string $att_title
504 private function _get_row_actions( $post, $att_title ) {
507 if ( $this->detached ) {
508 if ( current_user_can( 'edit_post', $post->ID ) )
509 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
510 if ( current_user_can( 'delete_post', $post->ID ) )
511 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
512 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
514 $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
515 $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>";
517 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
518 if ( current_user_can( 'edit_post', $post->ID ) )
519 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
522 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
523 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
524 if ( current_user_can( 'delete_post', $post->ID ) ) {
525 if ( $this->is_trash )
526 $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
527 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
528 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
529 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
530 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
531 $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>";
534 if ( !$this->is_trash ) {
535 $title =_draft_or_post_title( $post->post_parent );
536 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
541 * Filter the action links for each attachment in the Media list table.
545 * @param array $actions An array of action links for each attachment.
546 * Default 'Edit', 'Delete Permanently', 'View'.
547 * @param WP_Post $post WP_Post object for the current attachment.
548 * @param bool $detached Whether the list table contains media not attached
549 * to any posts. Default true.
551 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );