WordPress 4.0
[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         /**
13          * Constructor.
14          *
15          * @since 3.1.0
16          * @access public
17          *
18          * @see WP_List_Table::__construct() for more information on default arguments.
19          *
20          * @param array $args An associative array of arguments.
21          */
22         public function __construct( $args = array() ) {
23                 $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
24
25                 parent::__construct( array(
26                         'plural' => 'media',
27                         'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
28                 ) );
29         }
30
31         public function ajax_user_can() {
32                 return current_user_can('upload_files');
33         }
34
35         public function prepare_items() {
36                 global $wp_query, $post_mime_types, $avail_post_mime_types, $mode;
37
38                 list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
39
40                 $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' == $_REQUEST['attachment-filter'];
41
42                 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
43
44                 $this->set_pagination_args( array(
45                         'total_items' => $wp_query->found_posts,
46                         'total_pages' => $wp_query->max_num_pages,
47                         'per_page' => $wp_query->query_vars['posts_per_page'],
48                 ) );
49         }
50
51         protected function get_views() {
52                 global $wpdb, $post_mime_types, $avail_post_mime_types;
53
54                 $type_links = array();
55                 $_num_posts = (array) wp_count_attachments();
56                 $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
57                 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
58                 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
59                 foreach ( $matches as $type => $reals )
60                         foreach ( $reals as $real )
61                                 $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
62
63                 $selected = empty( $_GET['attachment-filter'] ) ? ' selected="selected"' : '';
64                 $type_links['all'] = "<option value=''$selected>" . sprintf( _nx( 'All (%s)', 'All (%s)', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</option>';
65                 foreach ( $post_mime_types as $mime_type => $label ) {
66                         if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
67                                 continue;
68
69                         $selected = '';
70                         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'] ) ) )
71                                 $selected = ' selected="selected"';
72                         if ( !empty( $num_posts[$mime_type] ) )
73                                 $type_links[$mime_type] = '<option value="post_mime_type:' . urlencode( $mime_type ) . '"' . $selected . '>' . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</option>';
74                 }
75                 $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>';
76
77                 if ( !empty($_num_posts['trash']) )
78                         $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>';
79
80                 return $type_links;
81         }
82
83         protected function get_bulk_actions() {
84                 $actions = array();
85                 $actions['delete'] = __( 'Delete Permanently' );
86                 if ( $this->detached )
87                         $actions['attach'] = __( 'Attach to a post' );
88
89                 return $actions;
90         }
91
92         protected function extra_tablenav( $which ) {
93                 if ( 'bar' !== $which ) {
94                         return;
95                 }
96 ?>
97                 <div class="actions">
98 <?php
99                 if ( ! is_singular() ) {
100                         if ( ! $this->is_trash ) {
101                                 $this->months_dropdown( 'attachment' );
102                         }
103
104                         /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
105                         do_action( 'restrict_manage_posts' );
106                         submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
107                 }
108
109                 if ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
110                         submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
111                 } ?>
112                 </div>
113 <?php
114         }
115
116         public function current_action() {
117                 if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
118                         return 'attach';
119
120                 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
121                         return 'delete_all';
122
123                 return parent::current_action();
124         }
125
126         public function has_items() {
127                 return have_posts();
128         }
129
130         public function no_items() {
131                 _e( 'No media attachments found.' );
132         }
133
134         protected function pagination( $which ) {
135                 global $mode;
136
137                 parent::pagination( $which );
138         }
139
140         /**
141          * Display a view switcher
142          *
143          * @since 3.1.0
144          * @access protected
145          */
146         protected function view_switcher( $current_mode ) {
147                 $modes = array(
148                         'list'    => __( 'List View' ),
149                         'grid' => __( 'Grid View' )
150                 );
151
152 ?>
153                 <input type="hidden" name="mode" value="<?php echo esc_attr( $current_mode ); ?>" />
154                 <div class="view-switch">
155 <?php
156                         foreach ( $modes as $mode => $title ) {
157                                 $classes = array( 'view-' . $mode );
158                                 if ( $current_mode == $mode )
159                                         $classes[] = 'current';
160                                 printf(
161                                         "<a href='%s' class='%s' id='view-switch-$mode'><span class='screen-reader-text'>%s</span></a>\n",
162                                         esc_url( add_query_arg( 'mode', $mode ) ),
163                                         implode( ' ', $classes ),
164                                         $title
165                                 );
166                         }
167                 ?>
168                 </div>
169 <?php
170         }
171
172         /**
173          * Override parent views so we can use the filter bar display.
174          */
175         public function views() {
176                 global $mode;
177
178                 $views = $this->get_views();
179 ?>
180 <div class="wp-filter">
181         <?php $this->view_switcher( $mode ); ?>
182
183         <select class="attachment-filters" name="attachment-filter">
184                 <?php
185                 if ( ! empty( $views ) ) {
186                         foreach ( $views as $class => $view ) {
187                                 echo "\t$view\n";
188                         }
189                 }
190                 ?>
191         </select>
192
193 <?php
194         $this->extra_tablenav( 'bar' );
195
196         /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
197         $views = apply_filters( "views_{$this->screen->id}", array() );
198
199         // Back compat for pre-4.0 view links.
200         if ( ! empty( $views ) ) {
201                 echo '<ul class="filter-links">';
202                 foreach ( $views as $class => $view ) {
203                         echo "<li class='$class'>$view</li>";
204                 }
205                 echo '</ul>';
206         }
207 ?>
208
209         <div class="search-form">
210                 <label for="media-search-input" class="screen-reader-text"><?php esc_html_e( 'Search Media' ); ?></label>
211                 <input type="search" placeholder="<?php esc_attr_e( 'Search' ) ?>" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>"></div>
212         </div>
213         <?php
214         }
215
216         public function get_columns() {
217                 $posts_columns = array();
218                 $posts_columns['cb'] = '<input type="checkbox" />';
219                 $posts_columns['icon'] = '';
220                 /* translators: column name */
221                 $posts_columns['title'] = _x( 'File', 'column name' );
222                 $posts_columns['author'] = __( 'Author' );
223
224                 $taxonomies = get_taxonomies_for_attachments( 'objects' );
225                 $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
226
227                 /**
228                  * Filter the taxonomy columns for attachments in the Media list table.
229                  *
230                  * @since 3.5.0
231                  *
232                  * @param array  $taxonomies An array of registered taxonomies to show for attachments.
233                  * @param string $post_type  The post type. Default 'attachment'.
234                  */
235                 $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
236                 $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
237
238                 foreach ( $taxonomies as $taxonomy ) {
239                         if ( 'category' == $taxonomy )
240                                 $column_key = 'categories';
241                         elseif ( 'post_tag' == $taxonomy )
242                                 $column_key = 'tags';
243                         else
244                                 $column_key = 'taxonomy-' . $taxonomy;
245
246                         $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
247                 }
248
249                 /* translators: column name */
250                 if ( !$this->detached ) {
251                         $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
252                         if ( post_type_supports( 'attachment', 'comments' ) )
253                                 $posts_columns['comments'] = '<span class="vers"><span title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></span></span>';
254                 }
255                 /* translators: column name */
256                 $posts_columns['date'] = _x( 'Date', 'column name' );
257                 /**
258                  * Filter the Media list table columns.
259                  *
260                  * @since 2.5.0
261                  *
262                  * @param array $posts_columns An array of columns displayed in the Media list table.
263                  * @param bool  $detached      Whether the list table contains media not attached
264                  *                             to any posts. Default true.
265                  */
266                 $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
267
268                 return $posts_columns;
269         }
270
271         protected function get_sortable_columns() {
272                 return array(
273                         'title'    => 'title',
274                         'author'   => 'author',
275                         'parent'   => 'parent',
276                         'comments' => 'comment_count',
277                         'date'     => array( 'date', true ),
278                 );
279         }
280
281         public function display_rows() {
282                 global $post;
283
284                 add_filter( 'the_title','esc_html' );
285                 $alt = '';
286
287                 while ( have_posts() ) : the_post();
288                         $user_can_edit = current_user_can( 'edit_post', $post->ID );
289
290                         if ( $this->is_trash && $post->post_status != 'trash'
291                         ||  !$this->is_trash && $post->post_status == 'trash' )
292                                 continue;
293
294                         $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
295                         $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
296                         $att_title = _draft_or_post_title();
297 ?>
298         <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
299 <?php
300
301 list( $columns, $hidden ) = $this->get_column_info();
302 foreach ( $columns as $column_name => $column_display_name ) {
303         $class = "class='$column_name column-$column_name'";
304
305         $style = '';
306         if ( in_array( $column_name, $hidden ) )
307                 $style = ' style="display:none;"';
308
309         $attributes = $class . $style;
310
311         switch ( $column_name ) {
312
313         case 'cb':
314 ?>
315                 <th scope="row" class="check-column">
316                         <?php if ( $user_can_edit ) { ?>
317                                 <label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
318                                 <input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
319                         <?php } ?>
320                 </th>
321 <?php
322                 break;
323
324         case 'icon':
325                 list( $mime ) = explode( '/', $post->post_mime_type );
326                 $attributes = 'class="column-icon media-icon ' . $mime . '-icon"' . $style;
327 ?>
328                 <td <?php echo $attributes ?>><?php
329                         if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
330                                 if ( $this->is_trash || ! $user_can_edit ) {
331                                         echo $thumb;
332                                 } else {
333 ?>
334                                 <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
335                                         <?php echo $thumb; ?>
336                                 </a>
337
338 <?php                   }
339                         }
340 ?>
341                 </td>
342 <?php
343                 break;
344
345         case 'title':
346 ?>
347                 <td <?php echo $attributes ?>><strong>
348                         <?php if ( $this->is_trash || ! $user_can_edit ) {
349                                 echo $att_title;
350                         } else { ?>
351                         <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
352                                 title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
353                                 <?php echo $att_title; ?></a>
354                         <?php };
355                         _media_states( $post ); ?></strong>
356                         <p>
357 <?php
358                         if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
359                                 echo esc_html( strtoupper( $matches[1] ) );
360                         else
361                                 echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
362 ?>
363                         </p>
364 <?php
365                 echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
366 ?>
367                 </td>
368 <?php
369                 break;
370
371         case 'author':
372 ?>
373                 <td <?php echo $attributes ?>><?php
374                         printf( '<a href="%s">%s</a>',
375                                 esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
376                                 get_the_author()
377                         );
378                 ?></td>
379 <?php
380                 break;
381
382         case 'desc':
383 ?>
384                 <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
385 <?php
386                 break;
387
388         case 'date':
389                 if ( '0000-00-00 00:00:00' == $post->post_date ) {
390                         $h_time = __( 'Unpublished' );
391                 } else {
392                         $m_time = $post->post_date;
393                         $time = get_post_time( 'G', true, $post, false );
394                         if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
395                                 if ( $t_diff < 0 )
396                                         $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
397                                 else
398                                         $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
399                         } else {
400                                 $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
401                         }
402                 }
403 ?>
404                 <td <?php echo $attributes ?>><?php echo $h_time ?></td>
405 <?php
406                 break;
407
408         case 'parent':
409                 if ( $post->post_parent > 0 )
410                         $parent = get_post( $post->post_parent );
411                 else
412                         $parent = false;
413
414                 if ( $parent ) {
415                         $title = _draft_or_post_title( $post->post_parent );
416                         $parent_type = get_post_type_object( $parent->post_type );
417 ?>
418                         <td <?php echo $attributes ?>><strong>
419                                 <?php if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) { ?>
420                                         <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
421                                                 <?php echo $title ?></a><?php
422                                 } else {
423                                         echo $title;
424                                 } ?></strong>,
425                                 <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
426                         </td>
427 <?php
428                 } else {
429 ?>
430                         <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
431                         <?php if ( $user_can_edit ) { ?>
432                                 <a class="hide-if-no-js"
433                                         onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
434                                         href="#the-list">
435                                         <?php _e( 'Attach' ); ?></a>
436                         <?php } ?></td>
437 <?php
438                 }
439                 break;
440
441         case 'comments':
442                 $attributes = 'class="comments column-comments num"' . $style;
443 ?>
444                 <td <?php echo $attributes ?>>
445                         <div class="post-com-count-wrapper">
446 <?php
447                 $pending_comments = get_pending_comments_num( $post->ID );
448
449                 $this->comments_bubble( $post->ID, $pending_comments );
450 ?>
451                         </div>
452                 </td>
453 <?php
454                 break;
455
456         default:
457                 if ( 'categories' == $column_name )
458                         $taxonomy = 'category';
459                 elseif ( 'tags' == $column_name )
460                         $taxonomy = 'post_tag';
461                 elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
462                         $taxonomy = substr( $column_name, 9 );
463                 else
464                         $taxonomy = false;
465
466                 if ( $taxonomy ) {
467                         echo '<td ' . $attributes . '>';
468                         if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
469                                 $out = array();
470                                 foreach ( $terms as $t ) {
471                                         $posts_in_term_qv = array();
472                                         $posts_in_term_qv['taxonomy'] = $taxonomy;
473                                         $posts_in_term_qv['term'] = $t->slug;
474
475                                         $out[] = sprintf( '<a href="%s">%s</a>',
476                                                 esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
477                                                 esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
478                                         );
479                                 }
480                                 /* translators: used between list items, there is a space after the comma */
481                                 echo join( __( ', ' ), $out );
482                         } else {
483                                 echo '&#8212;';
484                         }
485                         echo '</td>';
486                         break;
487                 }
488 ?>
489                 <td <?php echo $attributes ?>>
490                         <?php
491                                 /**
492                                  * Fires for each custom column in the Media list table.
493                                  *
494                                  * Custom columns are registered using the 'manage_media_columns' filter.
495                                  *
496                                  * @since 2.5.0
497                                  *
498                                  * @param string $column_name Name of the custom column.
499                                  * @param int    $post_id     Attachment ID.
500                                  */
501                         ?>
502                         <?php do_action( 'manage_media_custom_column', $column_name, $post->ID ); ?>
503                 </td>
504 <?php
505                 break;
506         }
507 }
508 ?>
509         </tr>
510 <?php endwhile;
511         }
512
513         private function _get_row_actions( $post, $att_title ) {
514                 $actions = array();
515
516                 if ( $this->detached ) {
517                         if ( current_user_can( 'edit_post', $post->ID ) )
518                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
519                         if ( current_user_can( 'delete_post', $post->ID ) )
520                                 if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
521                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
522                                 } else {
523                                         $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
524                                         $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>";
525                                 }
526                         $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
527                         if ( current_user_can( 'edit_post', $post->ID ) )
528                                 $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
529                 }
530                 else {
531                         if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
532                                 $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
533                         if ( current_user_can( 'delete_post', $post->ID ) ) {
534                                 if ( $this->is_trash )
535                                         $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
536                                 elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
537                                         $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
538                                 if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
539                                         $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
540                                         $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>";
541                                 }
542                         }
543                         if ( !$this->is_trash ) {
544                                 $title =_draft_or_post_title( $post->post_parent );
545                                 $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
546                         }
547                 }
548
549                 /**
550                  * Filter the action links for each attachment in the Media list table.
551                  *
552                  * @since 2.8.0
553                  *
554                  * @param array   $actions  An array of action links for each attachment.
555                  *                          Default 'Edit', 'Delete Permanently', 'View'.
556                  * @param WP_Post $post     WP_Post object for the current attachment.
557                  * @param bool    $detached Whether the list table contains media not attached
558                  *                          to any posts. Default true.
559                  */
560                 $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
561
562                 return $actions;
563         }
564 }