WordPress 3.4
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-media-list-table.php
1 <?php
2 /**
3  * Media Library List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Media_List_Table extends WP_List_Table {
11
12         function __construct() {
13                 $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
14
15                 parent::__construct( array(
16                         'plural' => 'media'
17                 ) );
18         }
19
20         function ajax_user_can() {
21                 return current_user_can('upload_files');
22         }
23
24         function prepare_items() {
25                 global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
26
27                 $q = $_REQUEST;
28
29                 if ( !empty( $lost ) )
30                         $q['post__in'] = implode( ',', $lost );
31
32                 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q );
33
34                 $this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status'];
35
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'],
40                 ) );
41         }
42
43         function get_views() {
44                 global $wpdb, $post_mime_types, $avail_post_mime_types;
45
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];
55
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 ) {
59                         $class = '';
60
61                         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
62                                 continue;
63
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>';
68                 }
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>';
70
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>';
73
74                 return $type_links;
75         }
76
77         function get_bulk_actions() {
78                 $actions = array();
79                 $actions['delete'] = __( 'Delete Permanently' );
80                 if ( $this->detached )
81                         $actions['attach'] = __( 'Attach to a post' );
82
83                 return $actions;
84         }
85
86         function extra_tablenav( $which ) {
87 ?>
88                 <div class="alignleft actions">
89 <?php
90                 if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
91                         $this->months_dropdown( 'attachment' );
92
93                         do_action( 'restrict_manage_posts' );
94                         submit_button( __( 'Filter' ), 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
95                 }
96
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 );
101                 } ?>
102                 </div>
103 <?php
104         }
105
106         function current_action() {
107                 if ( isset( $_REQUEST['find_detached'] ) )
108                         return 'find_detached';
109
110                 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
111                         return 'attach';
112
113                 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
114                         return 'delete_all';
115
116                 return parent::current_action();
117         }
118
119         function has_items() {
120                 return have_posts();
121         }
122
123         function no_items() {
124                 _e( 'No media attachments found.' );
125         }
126
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>';
140                 }
141                 /* translators: column name */
142                 $posts_columns['date'] = _x( 'Date', 'column name' );
143                 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
144
145                 return $posts_columns;
146         }
147
148         function get_sortable_columns() {
149                 return array(
150                         'title'    => 'title',
151                         'author'   => 'author',
152                         'parent'   => 'parent',
153                         'comments' => 'comment_count',
154                         'date'     => array( 'date', true ),
155                 );
156         }
157
158         function display_rows() {
159                 global $post, $id;
160
161                 add_filter( 'the_title','esc_html' );
162                 $alt = '';
163
164                 while ( have_posts() ) : the_post();
165                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
166
167                         if ( $this->is_trash && $post->post_status != 'trash'
168                         ||  !$this->is_trash && $post->post_status == 'trash' )
169                                 continue;
170
171                         $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
172                         $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
173                         $att_title = _draft_or_post_title();
174 ?>
175         <tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
176 <?php
177
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'";
181
182         $style = '';
183         if ( in_array( $column_name, $hidden ) )
184                 $style = ' style="display:none;"';
185
186         $attributes = $class . $style;
187
188         switch ( $column_name ) {
189
190         case 'cb':
191 ?>
192                 <th scope="row" class="check-column">
193                         <?php if ( $user_can_edit ) { ?>
194                                 <input type="checkbox" name="media[]" value="<?php the_ID(); ?>" />
195                         <?php } ?>
196                 </th>
197 <?php
198                 break;
199
200         case 'icon':
201                 $attributes = 'class="column-icon media-icon"' . $style;
202 ?>
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 ) {
206                                         echo $thumb;
207                                 } else {
208 ?>
209                                 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
210                                         <?php echo $thumb; ?>
211                                 </a>
212
213 <?php                   }
214                         }
215 ?>
216                 </td>
217 <?php
218                 break;
219
220         case 'title':
221 ?>
222                 <td <?php echo $attributes ?>><strong>
223                         <?php if ( $this->is_trash || ! $user_can_edit ) {
224                                 echo $att_title;
225                         } else { ?>
226                         <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
227                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
228                                 <?php echo $att_title; ?></a>
229                         <?php };
230                         _media_states( $post ); ?></strong>
231                         <p>
232 <?php
233                         if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
234                                 echo esc_html( strtoupper( $matches[1] ) );
235                         else
236                                 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
237 ?>
238                         </p>
239 <?php
240                 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
241 ?>
242                 </td>
243 <?php
244                 break;
245
246         case 'author':
247 ?>
248                 <td <?php echo $attributes ?>><?php the_author() ?></td>
249 <?php
250                 break;
251
252         case 'tags':
253 ?>
254                 <td <?php echo $attributes ?>><?php
255                 $tags = get_the_tags();
256                 if ( !empty( $tags ) ) {
257                         $out = array();
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 );
261                 } else {
262                         _e( 'No Tags' );
263                 }
264 ?>
265                 </td>
266 <?php
267                 break;
268
269         case 'desc':
270 ?>
271                 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
272 <?php
273                 break;
274
275         case 'date':
276                 if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
277                         $t_time = $h_time = __( 'Unpublished' );
278                 } else {
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 ) {
283                                 if ( $t_diff < 0 )
284                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
285                                 else
286                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
287                         } else {
288                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
289                         }
290                 }
291 ?>
292                 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
293 <?php
294                 break;
295
296         case 'parent':
297                 if ( $post->post_parent > 0 ) {
298                         if ( get_post( $post->post_parent ) ) {
299                                 $title =_draft_or_post_title( $post->post_parent );
300                         }
301 ?>
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><?php
306                                 } else {
307                                         echo $title;
308                                 } ?></strong>,
309                                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
310                         </td>
311 <?php
312                 } else {
313 ?>
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;"
318                                         href="#the-list">
319                                         <?php _e( 'Attach' ); ?></a>
320                         <?php } ?></td>
321 <?php
322                 }
323                 break;
324
325         case 'comments':
326                 $attributes = 'class="comments column-comments num"' . $style;
327 ?>
328                 <td <?php echo $attributes ?>>
329                         <div class="post-com-count-wrapper">
330 <?php
331                 $pending_comments = get_pending_comments_num( $post->ID );
332
333                 $this->comments_bubble( $post->ID, $pending_comments );
334 ?>
335                         </div>
336                 </td>
337 <?php
338                 break;
339
340         default:
341 ?>
342                 <td <?php echo $attributes ?>>
343                         <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?>
344                 </td>
345 <?php
346                 break;
347         }
348 }
349 ?>
350         </tr>
351 <?php endwhile;
352         }
353
354         function _get_row_actions( $post, $att_title ) {
355                 $actions = array();
356
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&amp;post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
363                                 } else {
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&amp;post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
366                                 }
367                         $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $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>';
370                 }
371                 else {
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&amp;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&amp;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&amp;post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
382                                 }
383                         }
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 &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
387                         }
388                 }
389
390                 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
391
392                 return $actions;
393         }
394 }