Wordpress 3.5
[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( $args = array() ) {
13                 $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
14
15                 parent::__construct( array(
16                         'plural' => 'media',
17                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
18                 ) );
19         }
20
21         function ajax_user_can() {
22                 return current_user_can('upload_files');
23         }
24
25         function prepare_items() {
26                 global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
27
28                 $q = $_REQUEST;
29
30                 if ( !empty( $lost ) )
31                         $q['post__in'] = implode( ',', $lost );
32
33                 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q );
34
35                 $this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status'];
36
37                 $this->set_pagination_args( array(
38                         'total_items' => $wp_query->found_posts,
39                         'total_pages' => $wp_query->max_num_pages,
40                         'per_page' => $wp_query->query_vars['posts_per_page'],
41                 ) );
42         }
43
44         function get_views() {
45                 global $wpdb, $post_mime_types, $avail_post_mime_types;
46
47                 $type_links = array();
48                 $_num_posts = (array) wp_count_attachments();
49                 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
50                 if ( !isset( $total_orphans ) )
51                                 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
52                 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
53                 foreach ( $matches as $type => $reals )
54                         foreach ( $reals as $real )
55                                 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
56
57                 $class = ( empty($_GET['post_mime_type']) && !$this->detached && !isset($_GET['status']) ) ? ' class="current"' : '';
58                 $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>';
59                 foreach ( $post_mime_types as $mime_type => $label ) {
60                         $class = '';
61
62                         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
63                                 continue;
64
65                         if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
66                                 $class = ' class="current"';
67                         if ( !empty( $num_posts[$mime_type] ) )
68                                 $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                 }
70                 $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
72                 if ( !empty($_num_posts['trash']) )
73                         $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>';
74
75                 return $type_links;
76         }
77
78         function get_bulk_actions() {
79                 $actions = array();
80                 $actions['delete'] = __( 'Delete Permanently' );
81                 if ( $this->detached )
82                         $actions['attach'] = __( 'Attach to a post' );
83
84                 return $actions;
85         }
86
87         function extra_tablenav( $which ) {
88 ?>
89                 <div class="alignleft actions">
90 <?php
91                 if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
92                         $this->months_dropdown( 'attachment' );
93
94                         do_action( 'restrict_manage_posts' );
95                         submit_button( __( 'Filter' ), 'button', false, false, array( 'id' => 'post-query-submit' ) );
96                 }
97
98                 if ( $this->detached ) {
99                         submit_button( __( 'Scan for lost attachments' ), 'secondary', 'find_detached', false );
100                 } elseif ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
101                         submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
102                 } ?>
103                 </div>
104 <?php
105         }
106
107         function current_action() {
108                 if ( isset( $_REQUEST['find_detached'] ) )
109                         return 'find_detached';
110
111                 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
112                         return 'attach';
113
114                 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
115                         return 'delete_all';
116
117                 return parent::current_action();
118         }
119
120         function has_items() {
121                 return have_posts();
122         }
123
124         function no_items() {
125                 _e( 'No media attachments found.' );
126         }
127
128         function get_columns() {
129                 $posts_columns = array();
130                 $posts_columns['cb'] = '<input type="checkbox" />';
131                 $posts_columns['icon'] = '';
132                 /* translators: column name */
133                 $posts_columns['title'] = _x( 'File', 'column name' );
134                 $posts_columns['author'] = __( 'Author' );
135
136                 $taxonomies = array();
137
138                 $taxonomies = get_taxonomies_for_attachments( 'objects' );
139                 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
140
141                 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
142                 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
143
144                 foreach ( $taxonomies as $taxonomy ) {
145                         if ( 'category' == $taxonomy )
146                                 $column_key = 'categories';
147                         elseif ( 'post_tag' == $taxonomy )
148                                 $column_key = 'tags';
149                         else
150                                 $column_key = 'taxonomy-' . $taxonomy;
151
152                         $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
153                 }
154
155                 /* translators: column name */
156                 if ( !$this->detached ) {
157                         $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
158                         if ( post_type_supports( 'attachment', 'comments' ) )
159                                 $posts_columns['comments'] = '<span class="vers"><div title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></div></span>';
160                 }
161                 /* translators: column name */
162                 $posts_columns['date'] = _x( 'Date', 'column name' );
163                 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
164
165                 return $posts_columns;
166         }
167
168         function get_sortable_columns() {
169                 return array(
170                         'title'    => 'title',
171                         'author'   => 'author',
172                         'parent'   => 'parent',
173                         'comments' => 'comment_count',
174                         'date'     => array( 'date', true ),
175                 );
176         }
177
178         function display_rows() {
179                 global $post, $id;
180
181                 add_filter( 'the_title','esc_html' );
182                 $alt = '';
183
184                 while ( have_posts() ) : the_post();
185                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
186
187                         if ( $this->is_trash && $post->post_status != 'trash'
188                         ||  !$this->is_trash && $post->post_status == 'trash' )
189                                 continue;
190
191                         $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
192                         $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
193                         $att_title = _draft_or_post_title();
194 ?>
195         <tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
196 <?php
197
198 list( $columns, $hidden ) = $this->get_column_info();
199 foreach ( $columns as $column_name => $column_display_name ) {
200         $class = "class='$column_name column-$column_name'";
201
202         $style = '';
203         if ( in_array( $column_name, $hidden ) )
204                 $style = ' style="display:none;"';
205
206         $attributes = $class . $style;
207
208         switch ( $column_name ) {
209
210         case 'cb':
211 ?>
212                 <th scope="row" class="check-column">
213                         <?php if ( $user_can_edit ) { ?>
214                                 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
215                                 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
216                         <?php } ?>
217                 </th>
218 <?php
219                 break;
220
221         case 'icon':
222                 $attributes = 'class="column-icon media-icon"' . $style;
223 ?>
224                 <td <?php echo $attributes ?>><?php
225                         if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
226                                 if ( $this->is_trash || ! $user_can_edit ) {
227                                         echo $thumb;
228                                 } else {
229 ?>
230                                 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
231                                         <?php echo $thumb; ?>
232                                 </a>
233
234 <?php                   }
235                         }
236 ?>
237                 </td>
238 <?php
239                 break;
240
241         case 'title':
242 ?>
243                 <td <?php echo $attributes ?>><strong>
244                         <?php if ( $this->is_trash || ! $user_can_edit ) {
245                                 echo $att_title;
246                         } else { ?>
247                         <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
248                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
249                                 <?php echo $att_title; ?></a>
250                         <?php };
251                         _media_states( $post ); ?></strong>
252                         <p>
253 <?php
254                         if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
255                                 echo esc_html( strtoupper( $matches[1] ) );
256                         else
257                                 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
258 ?>
259                         </p>
260 <?php
261                 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
262 ?>
263                 </td>
264 <?php
265                 break;
266
267         case 'author':
268 ?>
269                 <td <?php echo $attributes ?>><?php the_author() ?></td>
270 <?php
271                 break;
272
273         case 'desc':
274 ?>
275                 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
276 <?php
277                 break;
278
279         case 'date':
280                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
281                         $h_time = __( 'Unpublished' );
282                 } else {
283                         $m_time = $post->post_date;
284                         $time = get_post_time( 'G', true, $post, false );
285                         if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
286                                 if ( $t_diff < 0 )
287                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
288                                 else
289                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
290                         } else {
291                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
292                         }
293                 }
294 ?>
295                 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
296 <?php
297                 break;
298
299         case 'parent':
300                 if ( $post->post_parent > 0 ) {
301                         if ( get_post( $post->post_parent ) ) {
302                                 $title =_draft_or_post_title( $post->post_parent );
303                         }
304 ?>
305                         <td <?php echo $attributes ?>><strong>
306                                 <?php if( current_user_can( 'edit_post', $post->post_parent ) ) { ?>
307                                         <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
308                                                 <?php echo $title ?></a><?php
309                                 } else {
310                                         echo $title;
311                                 } ?></strong>,
312                                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
313                         </td>
314 <?php
315                 } else {
316 ?>
317                         <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
318                         <?php if( $user_can_edit ) {?>
319                                 <a class="hide-if-no-js"
320                                         onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
321                                         href="#the-list">
322                                         <?php _e( 'Attach' ); ?></a>
323                         <?php } ?></td>
324 <?php
325                 }
326                 break;
327
328         case 'comments':
329                 $attributes = 'class="comments column-comments num"' . $style;
330 ?>
331                 <td <?php echo $attributes ?>>
332                         <div class="post-com-count-wrapper">
333 <?php
334                 $pending_comments = get_pending_comments_num( $post->ID );
335
336                 $this->comments_bubble( $post->ID, $pending_comments );
337 ?>
338                         </div>
339                 </td>
340 <?php
341                 break;
342
343         default:
344                 if ( 'categories' == $column_name )
345                         $taxonomy = 'category';
346                 elseif ( 'tags' == $column_name )
347                         $taxonomy = 'post_tag';
348                 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
349                         $taxonomy = substr( $column_name, 9 );
350                 else
351                         $taxonomy = false;
352
353                 if ( $taxonomy ) {
354                         $taxonomy_object = get_taxonomy( $taxonomy );
355                         echo '<td ' . $attributes . '>';
356                         if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
357                                 $out = array();
358                                 foreach ( $terms as $t ) {
359                                         $posts_in_term_qv = array();
360                                         $posts_in_term_qv['taxonomy'] = $taxonomy;
361                                         $posts_in_term_qv['term'] = $t->slug;
362
363                                         $out[] = sprintf( '<a href="%s">%s</a>',
364                                                 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
365                                                 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
366                                         );
367                                 }
368                                 /* translators: used between list items, there is a space after the comma */
369                                 echo join( __( ', ' ), $out );
370                         } else {
371                                 echo '&#8212;';
372                         }
373                         echo '</td>';
374                         break;
375                 }
376 ?>
377                 <td <?php echo $attributes ?>>
378                         <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?>
379                 </td>
380 <?php
381                 break;
382         }
383 }
384 ?>
385         </tr>
386 <?php endwhile;
387         }
388
389         function _get_row_actions( $post, $att_title ) {
390                 $actions = array();
391
392                 if ( $this->detached ) {
393                         if ( current_user_can( 'edit_post', $post->ID ) )
394                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
395                         if ( current_user_can( 'delete_post', $post->ID ) )
396                                 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
397                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
398                                 } else {
399                                         $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
400                                         $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
401                                 }
402                         $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
403                         if ( current_user_can( 'edit_post', $post->ID ) )
404                                 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
405                 }
406                 else {
407                         if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
408                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
409                         if ( current_user_can( 'delete_post', $post->ID ) ) {
410                                 if ( $this->is_trash )
411                                         $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
412                                 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
413                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
414                                 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
415                                         $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
416                                         $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
417                                 }
418                         }
419                         if ( !$this->is_trash ) {
420                                 $title =_draft_or_post_title( $post->post_parent );
421                                 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
422                         }
423                 }
424
425                 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
426
427                 return $actions;
428         }
429 }