]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-media-list-table.php
WordPress 3.9
[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                 $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                         /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
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                 /**
142                  * Filter the taxonomy columns for attachments in the Media list table.
143                  *
144                  * @since 3.5.0
145                  *
146                  * @param array  $taxonomies An array of registered taxonomies to show for attachments.
147                  * @param string $post_type  The post type. Default 'attachment'.
148                  */
149                 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
150                 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
151
152                 foreach ( $taxonomies as $taxonomy ) {
153                         if ( 'category' == $taxonomy )
154                                 $column_key = 'categories';
155                         elseif ( 'post_tag' == $taxonomy )
156                                 $column_key = 'tags';
157                         else
158                                 $column_key = 'taxonomy-' . $taxonomy;
159
160                         $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
161                 }
162
163                 /* translators: column name */
164                 if ( !$this->detached ) {
165                         $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
166                         if ( post_type_supports( 'attachment', 'comments' ) )
167                                 $posts_columns['comments'] = '<span class="vers"><span title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></span></span>';
168                 }
169                 /* translators: column name */
170                 $posts_columns['date'] = _x( 'Date', 'column name' );
171                 /**
172                  * Filter the Media list table columns.
173                  *
174                  * @since 2.5.0
175                  *
176                  * @param array $posts_columns An array of columns displayed in the Media list table.
177                  * @param bool  $detached      Whether the list table contains media not attached
178                  *                             to any posts. Default true.
179                  */
180                 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
181
182                 return $posts_columns;
183         }
184
185         function get_sortable_columns() {
186                 return array(
187                         'title'    => 'title',
188                         'author'   => 'author',
189                         'parent'   => 'parent',
190                         'comments' => 'comment_count',
191                         'date'     => array( 'date', true ),
192                 );
193         }
194
195         function display_rows() {
196                 global $post;
197
198                 add_filter( 'the_title','esc_html' );
199                 $alt = '';
200
201                 while ( have_posts() ) : the_post();
202                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
203
204                         if ( $this->is_trash && $post->post_status != 'trash'
205                         ||  !$this->is_trash && $post->post_status == 'trash' )
206                                 continue;
207
208                         $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
209                         $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
210                         $att_title = _draft_or_post_title();
211 ?>
212         <tr id='post-<?php echo $post->ID; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>'>
213 <?php
214
215 list( $columns, $hidden ) = $this->get_column_info();
216 foreach ( $columns as $column_name => $column_display_name ) {
217         $class = "class='$column_name column-$column_name'";
218
219         $style = '';
220         if ( in_array( $column_name, $hidden ) )
221                 $style = ' style="display:none;"';
222
223         $attributes = $class . $style;
224
225         switch ( $column_name ) {
226
227         case 'cb':
228 ?>
229                 <th scope="row" class="check-column">
230                         <?php if ( $user_can_edit ) { ?>
231                                 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
232                                 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
233                         <?php } ?>
234                 </th>
235 <?php
236                 break;
237
238         case 'icon':
239                 $attributes = 'class="column-icon media-icon"' . $style;
240 ?>
241                 <td <?php echo $attributes ?>><?php
242                         if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
243                                 if ( $this->is_trash || ! $user_can_edit ) {
244                                         echo $thumb;
245                                 } else {
246 ?>
247                                 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
248                                         <?php echo $thumb; ?>
249                                 </a>
250
251 <?php                   }
252                         }
253 ?>
254                 </td>
255 <?php
256                 break;
257
258         case 'title':
259 ?>
260                 <td <?php echo $attributes ?>><strong>
261                         <?php if ( $this->is_trash || ! $user_can_edit ) {
262                                 echo $att_title;
263                         } else { ?>
264                         <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
265                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
266                                 <?php echo $att_title; ?></a>
267                         <?php };
268                         _media_states( $post ); ?></strong>
269                         <p>
270 <?php
271                         if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
272                                 echo esc_html( strtoupper( $matches[1] ) );
273                         else
274                                 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
275 ?>
276                         </p>
277 <?php
278                 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
279 ?>
280                 </td>
281 <?php
282                 break;
283
284         case 'author':
285 ?>
286                 <td <?php echo $attributes ?>><?php
287                         printf( '<a href="%s">%s</a>',
288                                 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
289                                 get_the_author()
290                         );
291                 ?></td>
292 <?php
293                 break;
294
295         case 'desc':
296 ?>
297                 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
298 <?php
299                 break;
300
301         case 'date':
302                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
303                         $h_time = __( 'Unpublished' );
304                 } else {
305                         $m_time = $post->post_date;
306                         $time = get_post_time( 'G', true, $post, false );
307                         if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
308                                 if ( $t_diff < 0 )
309                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
310                                 else
311                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
312                         } else {
313                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
314                         }
315                 }
316 ?>
317                 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
318 <?php
319                 break;
320
321         case 'parent':
322                 if ( $post->post_parent > 0 )
323                         $parent = get_post( $post->post_parent );
324                 else
325                         $parent = false;
326
327                 if ( $parent ) {
328                         $title = _draft_or_post_title( $post->post_parent );
329                         $parent_type = get_post_type_object( $parent->post_type );
330 ?>
331                         <td <?php echo $attributes ?>><strong>
332                                 <?php if ( current_user_can( 'edit_post', $post->post_parent ) && $parent_type && $parent_type->show_ui ) { ?>
333                                         <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
334                                                 <?php echo $title ?></a><?php
335                                 } else {
336                                         echo $title;
337                                 } ?></strong>,
338                                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
339                         </td>
340 <?php
341                 } else {
342 ?>
343                         <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
344                         <?php if ( $user_can_edit ) { ?>
345                                 <a class="hide-if-no-js"
346                                         onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
347                                         href="#the-list">
348                                         <?php _e( 'Attach' ); ?></a>
349                         <?php } ?></td>
350 <?php
351                 }
352                 break;
353
354         case 'comments':
355                 $attributes = 'class="comments column-comments num"' . $style;
356 ?>
357                 <td <?php echo $attributes ?>>
358                         <div class="post-com-count-wrapper">
359 <?php
360                 $pending_comments = get_pending_comments_num( $post->ID );
361
362                 $this->comments_bubble( $post->ID, $pending_comments );
363 ?>
364                         </div>
365                 </td>
366 <?php
367                 break;
368
369         default:
370                 if ( 'categories' == $column_name )
371                         $taxonomy = 'category';
372                 elseif ( 'tags' == $column_name )
373                         $taxonomy = 'post_tag';
374                 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
375                         $taxonomy = substr( $column_name, 9 );
376                 else
377                         $taxonomy = false;
378
379                 if ( $taxonomy ) {
380                         $taxonomy_object = get_taxonomy( $taxonomy );
381                         echo '<td ' . $attributes . '>';
382                         if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
383                                 $out = array();
384                                 foreach ( $terms as $t ) {
385                                         $posts_in_term_qv = array();
386                                         $posts_in_term_qv['taxonomy'] = $taxonomy;
387                                         $posts_in_term_qv['term'] = $t->slug;
388
389                                         $out[] = sprintf( '<a href="%s">%s</a>',
390                                                 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
391                                                 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
392                                         );
393                                 }
394                                 /* translators: used between list items, there is a space after the comma */
395                                 echo join( __( ', ' ), $out );
396                         } else {
397                                 echo '&#8212;';
398                         }
399                         echo '</td>';
400                         break;
401                 }
402 ?>
403                 <td <?php echo $attributes ?>>
404                         <?php
405                                 /**
406                                  * Fires for each custom column in the Media list table.
407                                  *
408                                  * Custom columns are registered using the 'manage_media_columns' filter.
409                                  *
410                                  * @since 2.5.0
411                                  *
412                                  * @param string $column_name Name of the custom column.
413                                  * @param int    $post_id     Attachment ID.
414                                  */
415                         ?>
416                         <?php do_action( 'manage_media_custom_column', $column_name, $post->ID ); ?>
417                 </td>
418 <?php
419                 break;
420         }
421 }
422 ?>
423         </tr>
424 <?php endwhile;
425         }
426
427         function _get_row_actions( $post, $att_title ) {
428                 $actions = array();
429
430                 if ( $this->detached ) {
431                         if ( current_user_can( 'edit_post', $post->ID ) )
432                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
433                         if ( current_user_can( 'delete_post', $post->ID ) )
434                                 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
435                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
436                                 } else {
437                                         $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
438                                         $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>";
439                                 }
440                         $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
441                         if ( current_user_can( 'edit_post', $post->ID ) )
442                                 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
443                 }
444                 else {
445                         if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
446                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
447                         if ( current_user_can( 'delete_post', $post->ID ) ) {
448                                 if ( $this->is_trash )
449                                         $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
450                                 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
451                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
452                                 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
453                                         $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
454                                         $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>";
455                                 }
456                         }
457                         if ( !$this->is_trash ) {
458                                 $title =_draft_or_post_title( $post->post_parent );
459                                 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
460                         }
461                 }
462
463                 /**
464                  * Filter the action links for each attachment in the Media list table.
465                  *
466                  * @since 2.8.0
467                  *
468                  * @param array   $actions  An array of action links for each attachment.
469                  *                          Default 'Edit', 'Delete Permanently', 'View'.
470                  * @param WP_Post $post     WP_Post object for the current attachment.
471                  * @param bool    $detached Whether the list table contains media not attached
472                  *                          to any posts. Default true.
473                  */
474                 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
475
476                 return $actions;
477         }
478 }