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,
40 public function ajax_user_can() {
41 return current_user_can('upload_files');
44 public function prepare_items() {
45 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
47 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
49 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
51 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
53 $this->set_pagination_args( array(
54 'total_items' => $wp_query->found_posts,
55 'total_pages' => $wp_query->max_num_pages,
56 'per_page' => $wp_query->query_vars['posts_per_page'],
60 protected function get_views() {
61 global $wpdb, $post_mime_types, $avail_post_mime_types;
63 $type_links = array();
64 $_num_posts = (array) wp_count_attachments();
65 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
66 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
67 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
68 foreach ( $matches as $type => $reals )
69 foreach ( $reals as $real )
70 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
72 $selected = empty( $_GET['attachment-filter'] ) ? ' selected="selected"' : '';
73 $type_links['all'] = "<option value=''$selected>" . sprintf( _nx( 'All (%s)', 'All (%s)', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</option>';
74 foreach ( $post_mime_types as $mime_type => $label ) {
75 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
79 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'] ) ) )
80 $selected = ' selected="selected"';
81 if ( !empty( $num_posts[$mime_type] ) )
82 $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>';
84 $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>';
86 if ( !empty($_num_posts['trash']) )
87 $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>';
92 protected function get_bulk_actions() {
95 if ( $this->is_trash ) {
96 $actions['untrash'] = __( 'Restore' );
97 $actions['delete'] = __( 'Delete Permanently' );
99 $actions['trash'] = __( 'Trash' );
102 $actions['delete'] = __( 'Delete Permanently' );
105 if ( $this->detached )
106 $actions['attach'] = __( 'Attach to a post' );
112 * @param string $which
114 protected function extra_tablenav( $which ) {
115 if ( 'bar' !== $which ) {
119 <div class="actions">
121 if ( ! is_singular() ) {
122 if ( ! $this->is_trash ) {
123 $this->months_dropdown( 'attachment' );
126 /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
127 do_action( 'restrict_manage_posts' );
128 submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
131 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
132 submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
138 public function current_action() {
139 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
142 if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
145 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
148 return parent::current_action();
151 public function has_items() {
155 public function no_items() {
156 _e( 'No media attachments found.' );
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 <label for="attachment-filter" class="screen-reader-text"><?php _e( 'Filter by type' ); ?></label>
172 <select class="attachment-filters" name="attachment-filter" id="attachment-filter">
174 if ( ! empty( $views ) ) {
175 foreach ( $views as $class => $view ) {
183 $this->extra_tablenav( 'bar' );
185 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
186 $views = apply_filters( "views_{$this->screen->id}", array() );
188 // Back compat for pre-4.0 view links.
189 if ( ! empty( $views ) ) {
190 echo '<ul class="filter-links">';
191 foreach ( $views as $class => $view ) {
192 echo "<li class='$class'>$view</li>";
199 <div class="search-form">
200 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
201 <input type="search" placeholder="<?php esc_attr_e( 'Search' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
206 public function get_columns() {
207 $posts_columns = array();
208 $posts_columns['cb'] = '<input type="checkbox" />';
209 $posts_columns['icon'] = '';
210 /* translators: column name */
211 $posts_columns['title'] = _x( 'File', 'column name' );
212 $posts_columns['author'] = __( 'Author' );
214 $taxonomies = get_taxonomies_for_attachments( 'objects' );
215 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
218 * Filter the taxonomy columns for attachments in the Media list table.
222 * @param array $taxonomies An array of registered taxonomies to show for attachments.
223 * @param string $post_type The post type. Default 'attachment'.
225 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
226 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
228 foreach ( $taxonomies as $taxonomy ) {
229 if ( 'category' == $taxonomy )
230 $column_key = 'categories';
231 elseif ( 'post_tag' == $taxonomy )
232 $column_key = 'tags';
234 $column_key = 'taxonomy-' . $taxonomy;
236 $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
239 /* translators: column name */
240 if ( !$this->detached ) {
241 $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
242 if ( post_type_supports( 'attachment', 'comments' ) )
243 $posts_columns['comments'] = '<span class="vers"><span title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></span></span>';
245 /* translators: column name */
246 $posts_columns['date'] = _x( 'Date', 'column name' );
248 * Filter the Media list table columns.
252 * @param array $posts_columns An array of columns displayed in the Media list table.
253 * @param bool $detached Whether the list table contains media not attached
254 * to any posts. Default true.
256 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
258 return $posts_columns;
261 protected function get_sortable_columns() {
264 'author' => 'author',
265 'parent' => 'parent',
266 'comments' => 'comment_count',
267 'date' => array( 'date', true ),
271 public function display_rows() {
274 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 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
284 $att_title = _draft_or_post_title();
286 <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
289 list( $columns, $hidden ) = $this->get_column_info();
290 foreach ( $columns as $column_name => $column_display_name ) {
291 $class = "class='$column_name column-$column_name'";
294 if ( in_array( $column_name, $hidden ) )
295 $style = ' style="display:none;"';
297 $attributes = $class . $style;
299 switch ( $column_name ) {
303 <th scope="row" class="check-column">
304 <?php if ( $user_can_edit ) { ?>
305 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
306 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
313 list( $mime ) = explode( '/', $post->post_mime_type );
314 $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
316 <td <?php echo $attributes ?>><?php
317 if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
318 if ( $this->is_trash || ! $user_can_edit ) {
322 <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
323 <?php echo $thumb; ?>
335 <td <?php echo $attributes ?>><strong>
336 <?php if ( $this->is_trash || ! $user_can_edit ) {
339 <a href="<?php echo get_edit_post_link( $post->ID ); ?>"
340 title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
341 <?php echo $att_title; ?></a>
343 _media_states( $post ); ?></strong>
344 <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
346 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
354 <td <?php echo $attributes ?>><?php
355 printf( '<a href="%s">%s</a>',
356 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
365 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
370 if ( '0000-00-00 00:00:00' == $post->post_date ) {
371 $h_time = __( 'Unpublished' );
373 $m_time = $post->post_date;
374 $time = get_post_time( 'G', true, $post, false );
375 if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
377 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
379 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
381 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
385 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
390 if ( $post->post_parent > 0 )
391 $parent = get_post( $post->post_parent );
396 $title = _draft_or_post_title( $post->post_parent );
397 $parent_type = get_post_type_object( $parent->post_type );
399 <td <?php echo $attributes ?>><strong>
400 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
401 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
402 <?php echo $title ?></a><?php
406 <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
408 if ( $user_can_edit ):
409 $detach_url = add_query_arg( array(
410 'parent_post_id' => $post->post_parent,
411 'media[]' => $post->ID,
412 '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] )
413 ), 'upload.php' ); ?>
414 <a class="hide-if-no-js detach-from-parent" href="<?php echo $detach_url ?>"><?php _e( 'Detach' ); ?></a>
420 <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
421 <?php if ( $user_can_edit ) { ?>
422 <a class="hide-if-no-js"
423 onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
425 <?php _e( 'Attach' ); ?></a>
432 $attributes = 'class="comments column-comments num"' . $style;
434 <td <?php echo $attributes ?>>
435 <div class="post-com-count-wrapper">
437 $pending_comments = get_pending_comments_num( $post->ID );
439 $this->comments_bubble( $post->ID, $pending_comments );
447 if ( 'categories' == $column_name )
448 $taxonomy = 'category';
449 elseif ( 'tags' == $column_name )
450 $taxonomy = 'post_tag';
451 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
452 $taxonomy = substr( $column_name, 9 );
457 echo '<td ' . $attributes . '>';
458 if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
460 foreach ( $terms as $t ) {
461 $posts_in_term_qv = array();
462 $posts_in_term_qv['taxonomy'] = $taxonomy;
463 $posts_in_term_qv['term'] = $t->slug;
465 $out[] = sprintf( '<a href="%s">%s</a>',
466 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
467 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
470 /* translators: used between list items, there is a space after the comma */
471 echo join( __( ', ' ), $out );
479 <td <?php echo $attributes ?>><?php
481 * Fires for each custom column in the Media list table.
483 * Custom columns are registered using the 'manage_media_columns' filter.
487 * @param string $column_name Name of the custom column.
488 * @param int $post_id Attachment ID.
490 do_action( 'manage_media_custom_column', $column_name, $post->ID );
502 * @param WP_Post $post
503 * @param string $att_title
505 private function _get_row_actions( $post, $att_title ) {
508 if ( $this->detached ) {
509 if ( current_user_can( 'edit_post', $post->ID ) )
510 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID ) . '">' . __( 'Edit' ) . '</a>';
511 if ( current_user_can( 'delete_post', $post->ID ) )
512 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
513 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
515 $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
516 $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>";
518 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
519 if ( current_user_can( 'edit_post', $post->ID ) )
520 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
523 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
524 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID ) . '">' . __( 'Edit' ) . '</a>';
525 if ( current_user_can( 'delete_post', $post->ID ) ) {
526 if ( $this->is_trash )
527 $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
528 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
529 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
530 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
531 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
532 $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>";
535 if ( !$this->is_trash ) {
536 $title =_draft_or_post_title( $post->post_parent );
537 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
542 * Filter the action links for each attachment in the Media list table.
546 * @param array $actions An array of action links for each attachment.
547 * Default 'Edit', 'Delete Permanently', 'View'.
548 * @param WP_Post $post WP_Post object for the current attachment.
549 * @param bool $detached Whether the list table contains media not attached
550 * to any posts. Default true.
552 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );