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