3 * Media Library List Table class.
6 * @subpackage List_Table
10 class WP_Media_List_Table extends WP_List_Table {
12 function __construct() {
13 $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
15 parent::__construct( array(
20 function ajax_user_can() {
21 return current_user_can('upload_files');
24 function prepare_items() {
25 global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
29 if ( !empty( $lost ) )
30 $q['post__in'] = implode( ',', $lost );
32 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q );
34 $this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status'];
36 $this->set_pagination_args( array(
37 'total_items' => $wp_query->found_posts,
38 'total_pages' => $wp_query->max_num_pages,
39 'per_page' => $wp_query->query_vars['posts_per_page'],
43 function get_views() {
44 global $wpdb, $post_mime_types, $avail_post_mime_types;
46 $type_links = array();
47 $_num_posts = (array) wp_count_attachments();
48 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
49 if ( !isset( $total_orphans ) )
50 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
51 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
52 foreach ( $matches as $type => $reals )
53 foreach ( $reals as $real )
54 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
56 $class = ( empty($_GET['post_mime_type']) && !$this->detached && !isset($_GET['status']) ) ? ' class="current"' : '';
57 $type_links['all'] = "<a href='upload.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</a>';
58 foreach ( $post_mime_types as $mime_type => $label ) {
61 if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
64 if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
65 $class = ' class="current"';
66 if ( !empty( $num_posts[$mime_type] ) )
67 $type_links[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</a>';
69 $type_links['detached'] = '<a href="upload.php?detached=1"' . ( $this->detached ? ' class="current"' : '' ) . '>' . sprintf( _nx( 'Unattached <span class="count">(%s)</span>', 'Unattached <span class="count">(%s)</span>', $total_orphans, 'detached files' ), number_format_i18n( $total_orphans ) ) . '</a>';
71 if ( !empty($_num_posts['trash']) )
72 $type_links['trash'] = '<a href="upload.php?status=trash"' . ( (isset($_GET['status']) && $_GET['status'] == 'trash' ) ? ' class="current"' : '') . '>' . sprintf( _nx( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>', $_num_posts['trash'], 'uploaded files' ), number_format_i18n( $_num_posts['trash'] ) ) . '</a>';
77 function get_bulk_actions() {
79 $actions['delete'] = __( 'Delete Permanently' );
80 if ( $this->detached )
81 $actions['attach'] = __( 'Attach to a post' );
86 function extra_tablenav( $which ) {
88 <div class="alignleft actions">
90 if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
91 $this->months_dropdown( 'attachment' );
93 do_action( 'restrict_manage_posts' );
94 submit_button( __( 'Filter' ), 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
97 if ( $this->detached ) {
98 submit_button( __( 'Scan for lost attachments' ), 'secondary', 'find_detached', false );
99 } elseif ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
100 submit_button( __( 'Empty Trash' ), 'button-secondary apply', 'delete_all', false );
106 function current_action() {
107 if ( isset( $_REQUEST['find_detached'] ) )
108 return 'find_detached';
110 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
113 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
116 return parent::current_action();
119 function has_items() {
123 function no_items() {
124 _e( 'No media attachments found.' );
127 function get_columns() {
128 $posts_columns = array();
129 $posts_columns['cb'] = '<input type="checkbox" />';
130 $posts_columns['icon'] = '';
131 /* translators: column name */
132 $posts_columns['title'] = _x( 'File', 'column name' );
133 $posts_columns['author'] = __( 'Author' );
134 //$posts_columns['tags'] = _x( 'Tags', 'column name' );
135 /* translators: column name */
136 if ( !$this->detached ) {
137 $posts_columns['parent'] = _x( 'Attached to', 'column name' );
138 if ( post_type_supports( 'attachment', 'comments' ) )
139 $posts_columns['comments'] = '<span class="vers"><img alt="' . esc_attr__( 'Comments' ) . '" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></span>';
141 /* translators: column name */
142 $posts_columns['date'] = _x( 'Date', 'column name' );
143 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
145 return $posts_columns;
148 function get_sortable_columns() {
151 'author' => 'author',
152 'parent' => 'parent',
153 'comments' => 'comment_count',
154 'date' => array( 'date', true ),
158 function display_rows() {
161 add_filter( 'the_title','esc_html' );
164 while ( have_posts() ) : the_post();
165 $user_can_edit = current_user_can( 'edit_post', $post->ID );
167 if ( $this->is_trash && $post->post_status != 'trash'
168 || !$this->is_trash && $post->post_status == 'trash' )
171 $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
172 $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
173 $att_title = _draft_or_post_title();
175 <tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
178 list( $columns, $hidden ) = $this->get_column_info();
179 foreach ( $columns as $column_name => $column_display_name ) {
180 $class = "class='$column_name column-$column_name'";
183 if ( in_array( $column_name, $hidden ) )
184 $style = ' style="display:none;"';
186 $attributes = $class . $style;
188 switch ( $column_name ) {
192 <th scope="row" class="check-column">
193 <?php if ( $user_can_edit ) { ?>
194 <input type="checkbox" name="media[]" value="<?php the_ID(); ?>" />
201 $attributes = 'class="column-icon media-icon"' . $style;
203 <td <?php echo $attributes ?>><?php
204 if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
205 if ( $this->is_trash || ! $user_can_edit ) {
209 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
210 <?php echo $thumb; ?>
222 <td <?php echo $attributes ?>><strong>
223 <?php if ( $this->is_trash || ! $user_can_edit ) {
226 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
227 title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>">
228 <?php echo $att_title; ?></a>
230 _media_states( $post ); ?></strong>
233 if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
234 echo esc_html( strtoupper( $matches[1] ) );
236 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
240 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
248 <td <?php echo $attributes ?>><?php the_author() ?></td>
254 <td <?php echo $attributes ?>><?php
255 $tags = get_the_tags();
256 if ( !empty( $tags ) ) {
258 foreach ( $tags as $c )
259 $out[] = "<a href='edit.php?tag=$c->slug'> " . esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'post_tag', 'display' ) ) . "</a>";
260 echo join( ', ', $out );
271 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
276 if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
277 $t_time = $h_time = __( 'Unpublished' );
279 $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
280 $m_time = $post->post_date;
281 $time = get_post_time( 'G', true, $post, false );
282 if ( ( abs( $t_diff = time() - $time ) ) < 86400 ) {
284 $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
286 $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
288 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
292 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
297 if ( $post->post_parent > 0 ) {
298 if ( get_post( $post->post_parent ) ) {
299 $title =_draft_or_post_title( $post->post_parent );
302 <td <?php echo $attributes ?>><strong>
303 <?php if( current_user_can( 'edit_post', $post->post_parent ) ) { ?>
304 <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
305 <?php echo $title ?></a>
309 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
314 <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
315 <?php if( $user_can_edit ) {?>
316 <a class="hide-if-no-js"
317 onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
319 <?php _e( 'Attach' ); ?></a>
326 $attributes = 'class="comments column-comments num"' . $style;
328 <td <?php echo $attributes ?>>
329 <div class="post-com-count-wrapper">
331 $pending_comments = get_pending_comments_num( $post->ID );
333 $this->comments_bubble( $post->ID, $pending_comments );
342 <td <?php echo $attributes ?>>
343 <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?>
354 function _get_row_actions( $post, $att_title ) {
357 if ( $this->detached ) {
358 if ( current_user_can( 'edit_post', $post->ID ) )
359 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
360 if ( current_user_can( 'delete_post', $post->ID ) )
361 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
362 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
364 $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
365 $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
367 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
368 if ( current_user_can( 'edit_post', $post->ID ) )
369 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
372 if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
373 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
374 if ( current_user_can( 'delete_post', $post->ID ) ) {
375 if ( $this->is_trash )
376 $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-attachment_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
377 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
378 $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
379 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
380 $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
381 $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
384 if ( !$this->is_trash ) {
385 $title =_draft_or_post_title( $post->post_parent );
386 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
390 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );