WordPress 4.2
[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         private $detached;
13
14         private $is_trash;
15
16         /**
17          * Constructor.
18          *
19          * @since 3.1.0
20          * @access public
21          *
22          * @see WP_List_Table::__construct() for more information on default arguments.
23          *
24          * @param array $args An associative array of arguments.
25          */
26         public function __construct( $args = array() ) {
27                 $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
28
29                 $this->modes = array(
30                         'list' => __( 'List View' ),
31                         'grid' => __( 'Grid View' )
32                 );
33
34                 parent::__construct( array(
35                         'plural' => 'media',
36                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
37                 ) );
38         }
39
40         public function ajax_user_can() {
41                 return current_user_can('upload_files');
42         }
43
44         public function prepare_items() {
45                 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
46
47                 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
48
49                 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
50
51                 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
52
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'],
57                 ) );
58         }
59
60         protected function get_views() {
61                 global $wpdb, $post_mime_types, $avail_post_mime_types;
62
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];
71
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) )
76                                 continue;
77
78                         $selected = '';
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>';
83                 }
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>';
85
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>';
88
89                 return $type_links;
90         }
91
92         protected function get_bulk_actions() {
93                 $actions = array();
94                 if ( MEDIA_TRASH ) {
95                         if ( $this->is_trash ) {
96                                 $actions['untrash'] = __( 'Restore' );
97                                 $actions['delete'] = __( 'Delete Permanently' );
98                         } else {
99                                 $actions['trash'] = __( 'Trash' );
100                         }
101                 } else {
102                         $actions['delete'] = __( 'Delete Permanently' );
103                 }
104
105                 if ( $this->detached )
106                         $actions['attach'] = __( 'Attach to a post' );
107
108                 return $actions;
109         }
110
111         /**
112          * @param string $which
113          */
114         protected function extra_tablenav( $which ) {
115                 if ( 'bar' !== $which ) {
116                         return;
117                 }
118 ?>
119                 <div class="actions">
120 <?php
121                 if ( ! is_singular() ) {
122                         if ( ! $this->is_trash ) {
123                                 $this->months_dropdown( 'attachment' );
124                         }
125
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' ) );
129                 }
130
131                 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
132                         submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
133                 } ?>
134                 </div>
135 <?php
136         }
137
138         public function current_action() {
139                 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
140                         return 'attach';
141
142                 if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) )
143                         return 'detach';
144
145                 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
146                         return 'delete_all';
147
148                 return parent::current_action();
149         }
150
151         public function has_items() {
152                 return have_posts();
153         }
154
155         public function no_items() {
156                 _e( 'No media attachments found.' );
157         }
158
159         /**
160          * Override parent views so we can use the filter bar display.
161          */
162         public function views() {
163                 global $mode;
164
165                 $views = $this->get_views();
166 ?>
167 <div class="wp-filter">
168         <div class="filter-items">
169                 <?php $this->view_switcher( $mode ); ?>
170
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">
173                         <?php
174                         if ( ! empty( $views ) ) {
175                                 foreach ( $views as $class => $view ) {
176                                         echo "\t$view\n";
177                                 }
178                         }
179                         ?>
180                 </select>
181
182 <?php
183                 $this->extra_tablenav( 'bar' );
184
185                 /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
186                 $views = apply_filters( "views_{$this->screen->id}", array() );
187
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>";
193                         }
194                         echo '</ul>';
195                 }
196 ?>
197         </div>
198
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>
202         </div>
203         <?php
204         }
205
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' );
213
214                 $taxonomies = get_taxonomies_for_attachments( 'objects' );
215                 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
216
217                 /**
218                  * Filter the taxonomy columns for attachments in the Media list table.
219                  *
220                  * @since 3.5.0
221                  *
222                  * @param array  $taxonomies An array of registered taxonomies to show for attachments.
223                  * @param string $post_type  The post type. Default 'attachment'.
224                  */
225                 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
226                 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
227
228                 foreach ( $taxonomies as $taxonomy ) {
229                         if ( 'category' == $taxonomy )
230                                 $column_key = 'categories';
231                         elseif ( 'post_tag' == $taxonomy )
232                                 $column_key = 'tags';
233                         else
234                                 $column_key = 'taxonomy-' . $taxonomy;
235
236                         $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
237                 }
238
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>';
244                 }
245                 /* translators: column name */
246                 $posts_columns['date'] = _x( 'Date', 'column name' );
247                 /**
248                  * Filter the Media list table columns.
249                  *
250                  * @since 2.5.0
251                  *
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.
255                  */
256                 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
257
258                 return $posts_columns;
259         }
260
261         protected function get_sortable_columns() {
262                 return array(
263                         'title'    => 'title',
264                         'author'   => 'author',
265                         'parent'   => 'parent',
266                         'comments' => 'comment_count',
267                         'date'     => array( 'date', true ),
268                 );
269         }
270
271         public function display_rows() {
272                 global $post;
273
274                 add_filter( 'the_title','esc_html' );
275
276                 while ( have_posts() ) : the_post();
277                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
278
279                         if ( $this->is_trash && $post->post_status != 'trash'
280                         ||  !$this->is_trash && $post->post_status == 'trash' )
281                                 continue;
282
283                         $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
284                         $att_title = _draft_or_post_title();
285 ?>
286         <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
287 <?php
288
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'";
292
293         $style = '';
294         if ( in_array( $column_name, $hidden ) )
295                 $style = ' style="display:none;"';
296
297         $attributes = $class . $style;
298
299         switch ( $column_name ) {
300
301         case 'cb':
302 ?>
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(); ?>" />
307                         <?php } ?>
308                 </th>
309 <?php
310                 break;
311
312         case 'icon':
313                 list( $mime ) = explode( '/', $post->post_mime_type );
314                 $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
315 ?>
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 ) {
319                                         echo $thumb;
320                                 } else {
321 ?>
322                                 <a href="<?php echo get_edit_post_link( $post->ID ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
323                                         <?php echo $thumb; ?>
324                                 </a>
325
326 <?php                   }
327                         }
328 ?>
329                 </td>
330 <?php
331                 break;
332
333         case 'title':
334 ?>
335                 <td <?php echo $attributes ?>><strong>
336                         <?php if ( $this->is_trash || ! $user_can_edit ) {
337                                 echo $att_title;
338                         } else { ?>
339                         <a href="<?php echo get_edit_post_link( $post->ID ); ?>"
340                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
341                                 <?php echo $att_title; ?></a>
342                         <?php };
343                         _media_states( $post ); ?></strong>
344                         <p class="filename"><?php echo wp_basename( $post->guid ); ?></p>
345 <?php
346                 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
347 ?>
348                 </td>
349 <?php
350                 break;
351
352         case 'author':
353 ?>
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' ) ),
357                                 get_the_author()
358                         );
359                 ?></td>
360 <?php
361                 break;
362
363         case 'desc':
364 ?>
365                 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
366 <?php
367                 break;
368
369         case 'date':
370                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
371                         $h_time = __( 'Unpublished' );
372                 } else {
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 ) {
376                                 if ( $t_diff < 0 )
377                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
378                                 else
379                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
380                         } else {
381                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
382                         }
383                 }
384 ?>
385                 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
386 <?php
387                 break;
388
389         case 'parent':
390                 if ( $post->post_parent > 0 )
391                         $parent = get_post( $post->post_parent );
392                 else
393                         $parent = false;
394
395                 if ( $parent ) {
396                         $title = _draft_or_post_title( $post->post_parent );
397                         $parent_type = get_post_type_object( $parent->post_type );
398 ?>
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
403                                 } else {
404                                         echo $title;
405                                 } ?></strong>,
406                                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?><br />
407                                 <?php
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>
415                                 <?php endif; ?>
416                         </td>
417 <?php
418                 } else {
419 ?>
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;"
424                                         href="#the-list">
425                                         <?php _e( 'Attach' ); ?></a>
426                         <?php } ?></td>
427 <?php
428                 }
429                 break;
430
431         case 'comments':
432                 $attributes = 'class="comments column-comments num"' . $style;
433 ?>
434                 <td <?php echo $attributes ?>>
435                         <div class="post-com-count-wrapper">
436 <?php
437                 $pending_comments = get_pending_comments_num( $post->ID );
438
439                 $this->comments_bubble( $post->ID, $pending_comments );
440 ?>
441                         </div>
442                 </td>
443 <?php
444                 break;
445
446         default:
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 );
453                 else
454                         $taxonomy = false;
455
456                 if ( $taxonomy ) {
457                         echo '<td ' . $attributes . '>';
458                         if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
459                                 $out = array();
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;
464
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' ) )
468                                         );
469                                 }
470                                 /* translators: used between list items, there is a space after the comma */
471                                 echo join( __( ', ' ), $out );
472                         } else {
473                                 echo '&#8212;';
474                         }
475                         echo '</td>';
476                         break;
477                 }
478 ?>
479                 <td <?php echo $attributes ?>><?php
480                         /**
481                          * Fires for each custom column in the Media list table.
482                          *
483                          * Custom columns are registered using the 'manage_media_columns' filter.
484                          *
485                          * @since 2.5.0
486                          *
487                          * @param string $column_name Name of the custom column.
488                          * @param int    $post_id     Attachment ID.
489                          */
490                         do_action( 'manage_media_custom_column', $column_name, $post->ID );
491                 ?></td>
492 <?php
493                 break;
494         }
495 }
496 ?>
497         </tr>
498 <?php endwhile;
499         }
500
501         /**
502          * @param WP_Post $post
503          * @param string  $att_title
504          */
505         private function _get_row_actions( $post, $att_title ) {
506                 $actions = array();
507
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&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
514                                 } else {
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&amp;post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
517                                 }
518                         $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $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>';
521                 }
522                 else {
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&amp;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&amp;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&amp;post=$post->ID", 'delete-post_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
533                                 }
534                         }
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 &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
538                         }
539                 }
540
541                 /**
542                  * Filter the action links for each attachment in the Media list table.
543                  *
544                  * @since 2.8.0
545                  *
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.
551                  */
552                 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
553
554                 return $actions;
555         }
556 }