| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Media Library List Table class. |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | * @subpackage List_Table |
|---|
| 7 | * @since 3.1.0 |
|---|
| 8 | */ |
|---|
| 9 | class WP_Media_List_Table extends WP_List_Table { |
|---|
| 10 | |
|---|
| 11 | function WP_Media_List_Table() { |
|---|
| 12 | $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] ); |
|---|
| 13 | |
|---|
| 14 | parent::WP_List_Table( array( |
|---|
| 15 | 'plural' => 'media' |
|---|
| 16 | ) ); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function ajax_user_can() { |
|---|
| 20 | return current_user_can('upload_files'); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | function prepare_items() { |
|---|
| 24 | global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types; |
|---|
| 25 | |
|---|
| 26 | $q = $_REQUEST; |
|---|
| 27 | |
|---|
| 28 | if ( !empty( $lost ) ) |
|---|
| 29 | $q['post__in'] = implode( ',', $lost ); |
|---|
| 30 | |
|---|
| 31 | list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q ); |
|---|
| 32 | |
|---|
| 33 | $this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status']; |
|---|
| 34 | |
|---|
| 35 | $this->set_pagination_args( array( |
|---|
| 36 | 'total_items' => $wp_query->found_posts, |
|---|
| 37 | 'total_pages' => $wp_query->max_num_pages, |
|---|
| 38 | 'per_page' => $wp_query->query_vars['posts_per_page'], |
|---|
| 39 | ) ); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | function get_views() { |
|---|
| 43 | global $wpdb, $post_mime_types, $avail_post_mime_types; |
|---|
| 44 | |
|---|
| 45 | $type_links = array(); |
|---|
| 46 | $_num_posts = (array) wp_count_attachments(); |
|---|
| 47 | $_total_posts = array_sum($_num_posts) - $_num_posts['trash']; |
|---|
| 48 | if ( !isset( $total_orphans ) ) |
|---|
| 49 | $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" ); |
|---|
| 50 | $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts)); |
|---|
| 51 | foreach ( $matches as $type => $reals ) |
|---|
| 52 | foreach ( $reals as $real ) |
|---|
| 53 | $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real]; |
|---|
| 54 | |
|---|
| 55 | $class = ( empty($_GET['post_mime_type']) && !$this->detached && !isset($_GET['status']) ) ? ' class="current"' : ''; |
|---|
| 56 | $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>'; |
|---|
| 57 | foreach ( $post_mime_types as $mime_type => $label ) { |
|---|
| 58 | $class = ''; |
|---|
| 59 | |
|---|
| 60 | if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) ) |
|---|
| 61 | continue; |
|---|
| 62 | |
|---|
| 63 | if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) ) |
|---|
| 64 | $class = ' class="current"'; |
|---|
| 65 | if ( !empty( $num_posts[$mime_type] ) ) |
|---|
| 66 | $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>'; |
|---|
| 67 | } |
|---|
| 68 | $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>'; |
|---|
| 69 | |
|---|
| 70 | if ( !empty($_num_posts['trash']) ) |
|---|
| 71 | $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>'; |
|---|
| 72 | |
|---|
| 73 | return $type_links; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | function get_bulk_actions() { |
|---|
| 77 | $actions = array(); |
|---|
| 78 | $actions['delete'] = __( 'Delete Permanently' ); |
|---|
| 79 | if ( $this->detached ) |
|---|
| 80 | $actions['attach'] = __( 'Attach to a post' ); |
|---|
| 81 | |
|---|
| 82 | return $actions; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | function extra_tablenav( $which ) { |
|---|
| 86 | global $post_type; |
|---|
| 87 | $post_type_obj = get_post_type_object( $post_type ); |
|---|
| 88 | ?> |
|---|
| 89 | <div class="alignleft actions"> |
|---|
| 90 | <?php |
|---|
| 91 | if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) { |
|---|
| 92 | $this->months_dropdown( $post_type ); |
|---|
| 93 | |
|---|
| 94 | do_action( 'restrict_manage_posts' ); |
|---|
| 95 | submit_button( __( 'Filter' ), 'secondary', 'post-query-submit', false ); |
|---|
| 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' ), 'button-secondary 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 | if ( $this->detached ) { |
|---|
| 126 | ?> |
|---|
| 127 | <div class="tablenav"> |
|---|
| 128 | <?php $this->extra_tablenav( 'top' ); ?> |
|---|
| 129 | </div> |
|---|
| 130 | <br class="clear"> |
|---|
| 131 | <?php |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | _e( 'No media attachments found.' ); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | function get_columns() { |
|---|
| 138 | $posts_columns = array(); |
|---|
| 139 | $posts_columns['cb'] = '<input type="checkbox" />'; |
|---|
| 140 | $posts_columns['icon'] = ''; |
|---|
| 141 | /* translators: column name */ |
|---|
| 142 | $posts_columns['title'] = _x( 'File', 'column name' ); |
|---|
| 143 | $posts_columns['author'] = __( 'Author' ); |
|---|
| 144 | //$posts_columns['tags'] = _x( 'Tags', 'column name' ); |
|---|
| 145 | /* translators: column name */ |
|---|
| 146 | if ( !$this->detached ) { |
|---|
| 147 | $posts_columns['parent'] = _x( 'Attached to', 'column name' ); |
|---|
| 148 | $posts_columns['comments'] = '<div class="vers"><img alt="Comments" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></div>'; |
|---|
| 149 | } |
|---|
| 150 | /* translators: column name */ |
|---|
| 151 | $posts_columns['date'] = _x( 'Date', 'column name' ); |
|---|
| 152 | $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached ); |
|---|
| 153 | |
|---|
| 154 | return $posts_columns; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | function get_sortable_columns() { |
|---|
| 158 | return array( |
|---|
| 159 | 'title' => 'title', |
|---|
| 160 | 'author' => 'author', |
|---|
| 161 | 'parent' => 'parent', |
|---|
| 162 | 'comments' => 'comment_count', |
|---|
| 163 | 'date' => array( 'date', true ), |
|---|
| 164 | ); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | function display_rows() { |
|---|
| 168 | global $post, $id; |
|---|
| 169 | |
|---|
| 170 | add_filter( 'the_title','esc_html' ); |
|---|
| 171 | $alt = ''; |
|---|
| 172 | |
|---|
| 173 | while ( have_posts() ) : the_post(); |
|---|
| 174 | |
|---|
| 175 | if ( $this->is_trash && $post->post_status != 'trash' |
|---|
| 176 | || !$this->is_trash && $post->post_status == 'trash' ) |
|---|
| 177 | continue; |
|---|
| 178 | |
|---|
| 179 | $alt = ( 'alternate' == $alt ) ? '' : 'alternate'; |
|---|
| 180 | $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other'; |
|---|
| 181 | $att_title = _draft_or_post_title(); |
|---|
| 182 | ?> |
|---|
| 183 | <tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top"> |
|---|
| 184 | <?php |
|---|
| 185 | |
|---|
| 186 | list( $columns, $hidden ) = $this->get_column_info(); |
|---|
| 187 | foreach ( $columns as $column_name => $column_display_name ) { |
|---|
| 188 | $class = "class='$column_name column-$column_name'"; |
|---|
| 189 | |
|---|
| 190 | $style = ''; |
|---|
| 191 | if ( in_array( $column_name, $hidden ) ) |
|---|
| 192 | $style = ' style="display:none;"'; |
|---|
| 193 | |
|---|
| 194 | $attributes = $class . $style; |
|---|
| 195 | |
|---|
| 196 | switch ( $column_name ) { |
|---|
| 197 | |
|---|
| 198 | case 'cb': |
|---|
| 199 | ?> |
|---|
| 200 | <th scope="row" class="check-column"><?php if ( current_user_can( 'edit_post', $post->ID ) ) { ?><input type="checkbox" name="media[]" value="<?php the_ID(); ?>" /><?php } ?></th> |
|---|
| 201 | <?php |
|---|
| 202 | break; |
|---|
| 203 | |
|---|
| 204 | case 'icon': |
|---|
| 205 | $attributes = 'class="column-icon media-icon"' . $style; |
|---|
| 206 | ?> |
|---|
| 207 | <td <?php echo $attributes ?>><?php |
|---|
| 208 | if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) { |
|---|
| 209 | if ( $this->is_trash ) { |
|---|
| 210 | echo $thumb; |
|---|
| 211 | } else { |
|---|
| 212 | ?> |
|---|
| 213 | <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>"> |
|---|
| 214 | <?php echo $thumb; ?> |
|---|
| 215 | </a> |
|---|
| 216 | |
|---|
| 217 | <?php } |
|---|
| 218 | } |
|---|
| 219 | ?> |
|---|
| 220 | </td> |
|---|
| 221 | <?php |
|---|
| 222 | break; |
|---|
| 223 | |
|---|
| 224 | case 'title': |
|---|
| 225 | ?> |
|---|
| 226 | <td <?php echo $attributes ?>><strong><?php if ( $this->is_trash ) echo $att_title; else { ?><a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit “%s”' ), $att_title ) ); ?>"><?php echo $att_title; ?></a><?php } ?></strong> |
|---|
| 227 | <p> |
|---|
| 228 | <?php |
|---|
| 229 | if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) ) |
|---|
| 230 | echo esc_html( strtoupper( $matches[1] ) ); |
|---|
| 231 | else |
|---|
| 232 | echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) ); |
|---|
| 233 | ?> |
|---|
| 234 | </p> |
|---|
| 235 | <?php |
|---|
| 236 | echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) ); |
|---|
| 237 | ?> |
|---|
| 238 | </td> |
|---|
| 239 | <?php |
|---|
| 240 | break; |
|---|
| 241 | |
|---|
| 242 | case 'author': |
|---|
| 243 | ?> |
|---|
| 244 | <td <?php echo $attributes ?>><?php the_author() ?></td> |
|---|
| 245 | <?php |
|---|
| 246 | break; |
|---|
| 247 | |
|---|
| 248 | case 'tags': |
|---|
| 249 | ?> |
|---|
| 250 | <td <?php echo $attributes ?>><?php |
|---|
| 251 | $tags = get_the_tags(); |
|---|
| 252 | if ( !empty( $tags ) ) { |
|---|
| 253 | $out = array(); |
|---|
| 254 | foreach ( $tags as $c ) |
|---|
| 255 | $out[] = "<a href='edit.php?tag=$c->slug'> " . esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'post_tag', 'display' ) ) . "</a>"; |
|---|
| 256 | echo join( ', ', $out ); |
|---|
| 257 | } else { |
|---|
| 258 | _e( 'No Tags' ); |
|---|
| 259 | } |
|---|
| 260 | ?> |
|---|
| 261 | </td> |
|---|
| 262 | <?php |
|---|
| 263 | break; |
|---|
| 264 | |
|---|
| 265 | case 'desc': |
|---|
| 266 | ?> |
|---|
| 267 | <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td> |
|---|
| 268 | <?php |
|---|
| 269 | break; |
|---|
| 270 | |
|---|
| 271 | case 'date': |
|---|
| 272 | if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) { |
|---|
| 273 | $t_time = $h_time = __( 'Unpublished' ); |
|---|
| 274 | } else { |
|---|
| 275 | $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) ); |
|---|
| 276 | $m_time = $post->post_date; |
|---|
| 277 | $time = get_post_time( 'G', true, $post, false ); |
|---|
| 278 | if ( ( abs( $t_diff = time() - $time ) ) < 86400 ) { |
|---|
| 279 | if ( $t_diff < 0 ) |
|---|
| 280 | $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) ); |
|---|
| 281 | else |
|---|
| 282 | $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) ); |
|---|
| 283 | } else { |
|---|
| 284 | $h_time = mysql2date( __( 'Y/m/d' ), $m_time ); |
|---|
| 285 | } |
|---|
| 286 | } |
|---|
| 287 | ?> |
|---|
| 288 | <td <?php echo $attributes ?>><?php echo apply_filters( 'media_date_column_time', $h_time); ?></td> |
|---|
| 289 | <?php |
|---|
| 290 | break; |
|---|
| 291 | |
|---|
| 292 | case 'parent': |
|---|
| 293 | if ( $post->post_parent > 0 ) { |
|---|
| 294 | if ( get_post( $post->post_parent ) ) { |
|---|
| 295 | $title =_draft_or_post_title( $post->post_parent ); |
|---|
| 296 | } |
|---|
| 297 | ?> |
|---|
| 298 | <td <?php echo $attributes ?>> |
|---|
| 299 | <strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>"><?php echo $title ?></a></strong>, |
|---|
| 300 | <?php echo get_the_time( __( 'Y/m/d' ) ); ?> |
|---|
| 301 | </td> |
|---|
| 302 | <?php |
|---|
| 303 | } else { |
|---|
| 304 | ?> |
|---|
| 305 | <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br /> |
|---|
| 306 | <a class="hide-if-no-js" onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a></td> |
|---|
| 307 | <?php |
|---|
| 308 | } |
|---|
| 309 | break; |
|---|
| 310 | |
|---|
| 311 | case 'comments': |
|---|
| 312 | $attributes = 'class="comments column-comments num"' . $style; |
|---|
| 313 | ?> |
|---|
| 314 | <td <?php echo $attributes ?>> |
|---|
| 315 | <div class="post-com-count-wrapper"> |
|---|
| 316 | <?php |
|---|
| 317 | $pending_comments = get_pending_comments_num( $post->ID ); |
|---|
| 318 | |
|---|
| 319 | $this->comments_bubble( $post->ID, $pending_comments ); |
|---|
| 320 | ?> |
|---|
| 321 | </div> |
|---|
| 322 | </td> |
|---|
| 323 | <?php |
|---|
| 324 | break; |
|---|
| 325 | |
|---|
| 326 | default: |
|---|
| 327 | ?> |
|---|
| 328 | <td <?php echo $attributes ?>> |
|---|
| 329 | <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?> |
|---|
| 330 | </td> |
|---|
| 331 | <?php |
|---|
| 332 | break; |
|---|
| 333 | } |
|---|
| 334 | } |
|---|
| 335 | ?> |
|---|
| 336 | </tr> |
|---|
| 337 | <?php endwhile; |
|---|
| 338 | } |
|---|
| 339 | |
|---|
| 340 | function _get_row_actions( $post, $att_title ) { |
|---|
| 341 | $actions = array(); |
|---|
| 342 | |
|---|
| 343 | if ( $this->detached ) { |
|---|
| 344 | if ( current_user_can( 'edit_post', $post->ID ) ) |
|---|
| 345 | $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>'; |
|---|
| 346 | if ( current_user_can( 'delete_post', $post->ID ) ) |
|---|
| 347 | if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) { |
|---|
| 348 | $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>"; |
|---|
| 349 | } else { |
|---|
| 350 | $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : ''; |
|---|
| 351 | $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>"; |
|---|
| 352 | } |
|---|
| 353 | $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>'; |
|---|
| 354 | if ( current_user_can( 'edit_post', $post->ID ) ) |
|---|
| 355 | $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>'; |
|---|
| 356 | } |
|---|
| 357 | else { |
|---|
| 358 | if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash ) |
|---|
| 359 | $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>'; |
|---|
| 360 | if ( current_user_can( 'delete_post', $post->ID ) ) { |
|---|
| 361 | if ( $this->is_trash ) |
|---|
| 362 | $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&post=$post->ID", 'untrash-attachment_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>"; |
|---|
| 363 | elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) |
|---|
| 364 | $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>"; |
|---|
| 365 | if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) { |
|---|
| 366 | $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : ''; |
|---|
| 367 | $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>"; |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | if ( !$this->is_trash ) { |
|---|
| 371 | $title =_draft_or_post_title( $post->post_parent ); |
|---|
| 372 | $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View “%s”' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>'; |
|---|
| 373 | } |
|---|
| 374 | } |
|---|
| 375 | |
|---|
| 376 | $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached ); |
|---|
| 377 | |
|---|
| 378 | return $actions; |
|---|
| 379 | } |
|---|
| 380 | } |
|---|
| 381 | |
|---|
| 382 | ?> |
|---|