wp/wp-admin/includes/class-wp-media-list-table.php
changeset 0 d970ebf37754
child 5 5e2f62d02dcd
equal deleted inserted replaced
-1:000000000000 0:d970ebf37754
       
     1 <?php
       
     2 /**
       
     3  * Media Library List Table class.
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage List_Table
       
     7  * @since 3.1.0
       
     8  * @access private
       
     9  */
       
    10 class WP_Media_List_Table extends WP_List_Table {
       
    11 
       
    12 	function __construct( $args = array() ) {
       
    13 		$this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
       
    14 
       
    15 		parent::__construct( array(
       
    16 			'plural' => 'media',
       
    17 			'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
       
    18 		) );
       
    19 	}
       
    20 
       
    21 	function ajax_user_can() {
       
    22 		return current_user_can('upload_files');
       
    23 	}
       
    24 
       
    25 	function prepare_items() {
       
    26 		global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
       
    27 
       
    28 		$q = $_REQUEST;
       
    29 
       
    30 		if ( !empty( $lost ) )
       
    31 			$q['post__in'] = implode( ',', $lost );
       
    32 
       
    33 		list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q );
       
    34 
       
    35  		$this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status'];
       
    36 
       
    37 		$this->set_pagination_args( array(
       
    38 			'total_items' => $wp_query->found_posts,
       
    39 			'total_pages' => $wp_query->max_num_pages,
       
    40 			'per_page' => $wp_query->query_vars['posts_per_page'],
       
    41 		) );
       
    42 	}
       
    43 
       
    44 	function get_views() {
       
    45 		global $wpdb, $post_mime_types, $avail_post_mime_types;
       
    46 
       
    47 		$type_links = array();
       
    48 		$_num_posts = (array) wp_count_attachments();
       
    49 		$_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
       
    50 		$total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
       
    51 		$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
       
    52 		foreach ( $matches as $type => $reals )
       
    53 			foreach ( $reals as $real )
       
    54 				$num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
       
    55 
       
    56 		$class = ( empty($_GET['post_mime_type']) && !$this->detached && !isset($_GET['status']) ) ? ' class="current"' : '';
       
    57 		$type_links['all'] = "<a href='upload.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $_total_posts, 'uploaded files' ), number_format_i18n( $_total_posts ) ) . '</a>';
       
    58 		foreach ( $post_mime_types as $mime_type => $label ) {
       
    59 			$class = '';
       
    60 
       
    61 			if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
       
    62 				continue;
       
    63 
       
    64 			if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
       
    65 				$class = ' class="current"';
       
    66 			if ( !empty( $num_posts[$mime_type] ) )
       
    67 				$type_links[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), number_format_i18n( $num_posts[$mime_type] )) . '</a>';
       
    68 		}
       
    69 		$type_links['detached'] = '<a href="upload.php?detached=1"' . ( $this->detached ? ' class="current"' : '' ) . '>' . sprintf( _nx( 'Unattached <span class="count">(%s)</span>', 'Unattached <span class="count">(%s)</span>', $total_orphans, 'detached files' ), number_format_i18n( $total_orphans ) ) . '</a>';
       
    70 
       
    71 		if ( !empty($_num_posts['trash']) )
       
    72 			$type_links['trash'] = '<a href="upload.php?status=trash"' . ( (isset($_GET['status']) && $_GET['status'] == 'trash' ) ? ' class="current"' : '') . '>' . sprintf( _nx( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>', $_num_posts['trash'], 'uploaded files' ), number_format_i18n( $_num_posts['trash'] ) ) . '</a>';
       
    73 
       
    74 		return $type_links;
       
    75 	}
       
    76 
       
    77 	function get_bulk_actions() {
       
    78 		$actions = array();
       
    79 		$actions['delete'] = __( 'Delete Permanently' );
       
    80 		if ( $this->detached )
       
    81 			$actions['attach'] = __( 'Attach to a post' );
       
    82 
       
    83 		return $actions;
       
    84 	}
       
    85 
       
    86 	function extra_tablenav( $which ) {
       
    87 ?>
       
    88 		<div class="alignleft actions">
       
    89 <?php
       
    90 		if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
       
    91 			$this->months_dropdown( 'attachment' );
       
    92 
       
    93 			do_action( 'restrict_manage_posts' );
       
    94 			submit_button( __( 'Filter' ), 'button', false, false, array( 'id' => 'post-query-submit' ) );
       
    95 		}
       
    96 
       
    97 		if ( $this->detached ) {
       
    98 			submit_button( __( 'Scan for lost attachments' ), 'secondary', 'find_detached', false );
       
    99 		} elseif ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
       
   100 			submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
       
   101 		} ?>
       
   102 		</div>
       
   103 <?php
       
   104 	}
       
   105 
       
   106 	function current_action() {
       
   107 		if ( isset( $_REQUEST['find_detached'] ) )
       
   108 			return 'find_detached';
       
   109 
       
   110 		if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
       
   111 			return 'attach';
       
   112 
       
   113 		if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
       
   114 			return 'delete_all';
       
   115 
       
   116 		return parent::current_action();
       
   117 	}
       
   118 
       
   119 	function has_items() {
       
   120 		return have_posts();
       
   121 	}
       
   122 
       
   123 	function no_items() {
       
   124 		_e( 'No media attachments found.' );
       
   125 	}
       
   126 
       
   127 	function get_columns() {
       
   128 		$posts_columns = array();
       
   129 		$posts_columns['cb'] = '<input type="checkbox" />';
       
   130 		$posts_columns['icon'] = '';
       
   131 		/* translators: column name */
       
   132 		$posts_columns['title'] = _x( 'File', 'column name' );
       
   133 		$posts_columns['author'] = __( 'Author' );
       
   134 
       
   135 		$taxonomies = array();
       
   136 
       
   137 		$taxonomies = get_taxonomies_for_attachments( 'objects' );
       
   138 		$taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
       
   139 
       
   140 		$taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
       
   141 		$taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
       
   142 
       
   143 		foreach ( $taxonomies as $taxonomy ) {
       
   144 			if ( 'category' == $taxonomy )
       
   145 				$column_key = 'categories';
       
   146 			elseif ( 'post_tag' == $taxonomy )
       
   147 				$column_key = 'tags';
       
   148 			else
       
   149 				$column_key = 'taxonomy-' . $taxonomy;
       
   150 
       
   151 			$posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
       
   152 		}
       
   153 
       
   154 		/* translators: column name */
       
   155 		if ( !$this->detached ) {
       
   156 			$posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
       
   157 			if ( post_type_supports( 'attachment', 'comments' ) )
       
   158 				$posts_columns['comments'] = '<span class="vers"><div title="' . esc_attr__( 'Comments' ) . '" class="comment-grey-bubble"></div></span>';
       
   159 		}
       
   160 		/* translators: column name */
       
   161 		$posts_columns['date'] = _x( 'Date', 'column name' );
       
   162 		$posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
       
   163 
       
   164 		return $posts_columns;
       
   165 	}
       
   166 
       
   167 	function get_sortable_columns() {
       
   168 		return array(
       
   169 			'title'    => 'title',
       
   170 			'author'   => 'author',
       
   171 			'parent'   => 'parent',
       
   172 			'comments' => 'comment_count',
       
   173 			'date'     => array( 'date', true ),
       
   174 		);
       
   175 	}
       
   176 
       
   177 	function display_rows() {
       
   178 		global $post;
       
   179 
       
   180 		add_filter( 'the_title','esc_html' );
       
   181 		$alt = '';
       
   182 
       
   183 		while ( have_posts() ) : the_post();
       
   184 			$user_can_edit = current_user_can( 'edit_post', $post->ID );
       
   185 
       
   186 			if ( $this->is_trash && $post->post_status != 'trash'
       
   187 			||  !$this->is_trash && $post->post_status == 'trash' )
       
   188 				continue;
       
   189 
       
   190 			$alt = ( 'alternate' == $alt ) ? '' : 'alternate';
       
   191 			$post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
       
   192 			$att_title = _draft_or_post_title();
       
   193 ?>
       
   194 	<tr id='post-<?php echo $post->ID; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
       
   195 <?php
       
   196 
       
   197 list( $columns, $hidden ) = $this->get_column_info();
       
   198 foreach ( $columns as $column_name => $column_display_name ) {
       
   199 	$class = "class='$column_name column-$column_name'";
       
   200 
       
   201 	$style = '';
       
   202 	if ( in_array( $column_name, $hidden ) )
       
   203 		$style = ' style="display:none;"';
       
   204 
       
   205 	$attributes = $class . $style;
       
   206 
       
   207 	switch ( $column_name ) {
       
   208 
       
   209 	case 'cb':
       
   210 ?>
       
   211 		<th scope="row" class="check-column">
       
   212 			<?php if ( $user_can_edit ) { ?>
       
   213 				<label class="screen-reader-text" for="cb-select-<?php the_ID(); ?>"><?php echo sprintf( __( 'Select %s' ), $att_title );?></label>
       
   214 				<input type="checkbox" name="media[]" id="cb-select-<?php the_ID(); ?>" value="<?php the_ID(); ?>" />
       
   215 			<?php } ?>
       
   216 		</th>
       
   217 <?php
       
   218 		break;
       
   219 
       
   220 	case 'icon':
       
   221 		$attributes = 'class="column-icon media-icon"' . $style;
       
   222 ?>
       
   223 		<td <?php echo $attributes ?>><?php
       
   224 			if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
       
   225 				if ( $this->is_trash || ! $user_can_edit ) {
       
   226 					echo $thumb;
       
   227 				} else {
       
   228 ?>
       
   229 				<a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
       
   230 					<?php echo $thumb; ?>
       
   231 				</a>
       
   232 
       
   233 <?php			}
       
   234 			}
       
   235 ?>
       
   236 		</td>
       
   237 <?php
       
   238 		break;
       
   239 
       
   240 	case 'title':
       
   241 ?>
       
   242 		<td <?php echo $attributes ?>><strong>
       
   243 			<?php if ( $this->is_trash || ! $user_can_edit ) {
       
   244 				echo $att_title;
       
   245 			} else { ?>
       
   246 			<a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
       
   247 				title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
       
   248 				<?php echo $att_title; ?></a>
       
   249 			<?php };
       
   250 			_media_states( $post ); ?></strong>
       
   251 			<p>
       
   252 <?php
       
   253 			if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
       
   254 				echo esc_html( strtoupper( $matches[1] ) );
       
   255 			else
       
   256 				echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
       
   257 ?>
       
   258 			</p>
       
   259 <?php
       
   260 		echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
       
   261 ?>
       
   262 		</td>
       
   263 <?php
       
   264 		break;
       
   265 
       
   266 	case 'author':
       
   267 ?>
       
   268 		<td <?php echo $attributes ?>><?php
       
   269 			printf( '<a href="%s">%s</a>',
       
   270 				esc_url( add_query_arg( array( 'author' => get_the_author_meta('ID') ), 'upload.php' ) ),
       
   271 				get_the_author()
       
   272 			);
       
   273 		?></td>
       
   274 <?php
       
   275 		break;
       
   276 
       
   277 	case 'desc':
       
   278 ?>
       
   279 		<td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
       
   280 <?php
       
   281 		break;
       
   282 
       
   283 	case 'date':
       
   284 		if ( '0000-00-00 00:00:00' == $post->post_date ) {
       
   285 			$h_time = __( 'Unpublished' );
       
   286 		} else {
       
   287 			$m_time = $post->post_date;
       
   288 			$time = get_post_time( 'G', true, $post, false );
       
   289 			if ( ( abs( $t_diff = time() - $time ) ) < DAY_IN_SECONDS ) {
       
   290 				if ( $t_diff < 0 )
       
   291 					$h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
       
   292 				else
       
   293 					$h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
       
   294 			} else {
       
   295 				$h_time = mysql2date( __( 'Y/m/d' ), $m_time );
       
   296 			}
       
   297 		}
       
   298 ?>
       
   299 		<td <?php echo $attributes ?>><?php echo $h_time ?></td>
       
   300 <?php
       
   301 		break;
       
   302 
       
   303 	case 'parent':
       
   304 		if ( $post->post_parent > 0 )
       
   305 			$parent = get_post( $post->post_parent );
       
   306 		else
       
   307 			$parent = false;
       
   308 
       
   309 		if ( $parent ) {
       
   310 			$title = _draft_or_post_title( $post->post_parent );
       
   311 			$parent_type = get_post_type_object( $parent->post_type );
       
   312 ?>
       
   313 			<td <?php echo $attributes ?>><strong>
       
   314 				<?php if ( current_user_can( 'edit_post', $post->post_parent ) && $parent_type->show_ui ) { ?>
       
   315 					<a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
       
   316 						<?php echo $title ?></a><?php
       
   317 				} else {
       
   318 					echo $title;
       
   319 				} ?></strong>,
       
   320 				<?php echo get_the_time( __( 'Y/m/d' ) ); ?>
       
   321 			</td>
       
   322 <?php
       
   323 		} else {
       
   324 ?>
       
   325 			<td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
       
   326 			<?php if ( $user_can_edit ) { ?>
       
   327 				<a class="hide-if-no-js"
       
   328 					onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
       
   329 					href="#the-list">
       
   330 					<?php _e( 'Attach' ); ?></a>
       
   331 			<?php } ?></td>
       
   332 <?php
       
   333 		}
       
   334 		break;
       
   335 
       
   336 	case 'comments':
       
   337 		$attributes = 'class="comments column-comments num"' . $style;
       
   338 ?>
       
   339 		<td <?php echo $attributes ?>>
       
   340 			<div class="post-com-count-wrapper">
       
   341 <?php
       
   342 		$pending_comments = get_pending_comments_num( $post->ID );
       
   343 
       
   344 		$this->comments_bubble( $post->ID, $pending_comments );
       
   345 ?>
       
   346 			</div>
       
   347 		</td>
       
   348 <?php
       
   349 		break;
       
   350 
       
   351 	default:
       
   352 		if ( 'categories' == $column_name )
       
   353 			$taxonomy = 'category';
       
   354 		elseif ( 'tags' == $column_name )
       
   355 			$taxonomy = 'post_tag';
       
   356 		elseif ( 0 === strpos( $column_name, 'taxonomy-' ) )
       
   357 			$taxonomy = substr( $column_name, 9 );
       
   358 		else
       
   359 			$taxonomy = false;
       
   360 
       
   361 		if ( $taxonomy ) {
       
   362 			$taxonomy_object = get_taxonomy( $taxonomy );
       
   363 			echo '<td ' . $attributes . '>';
       
   364 			if ( $terms = get_the_terms( $post->ID, $taxonomy ) ) {
       
   365 				$out = array();
       
   366 				foreach ( $terms as $t ) {
       
   367 					$posts_in_term_qv = array();
       
   368 					$posts_in_term_qv['taxonomy'] = $taxonomy;
       
   369 					$posts_in_term_qv['term'] = $t->slug;
       
   370 
       
   371 					$out[] = sprintf( '<a href="%s">%s</a>',
       
   372 						esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
       
   373 						esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
       
   374 					);
       
   375 				}
       
   376 				/* translators: used between list items, there is a space after the comma */
       
   377 				echo join( __( ', ' ), $out );
       
   378 			} else {
       
   379 				echo '&#8212;';
       
   380 			}
       
   381 			echo '</td>';
       
   382 			break;
       
   383 		}
       
   384 ?>
       
   385 		<td <?php echo $attributes ?>>
       
   386 			<?php do_action( 'manage_media_custom_column', $column_name, $post->ID ); ?>
       
   387 		</td>
       
   388 <?php
       
   389 		break;
       
   390 	}
       
   391 }
       
   392 ?>
       
   393 	</tr>
       
   394 <?php endwhile;
       
   395 	}
       
   396 
       
   397 	function _get_row_actions( $post, $att_title ) {
       
   398 		$actions = array();
       
   399 
       
   400 		if ( $this->detached ) {
       
   401 			if ( current_user_can( 'edit_post', $post->ID ) )
       
   402 				$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
       
   403 			if ( current_user_can( 'delete_post', $post->ID ) )
       
   404 				if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
       
   405 					$actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
       
   406 				} else {
       
   407 					$delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
       
   408 					$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>";
       
   409 				}
       
   410 			$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
       
   411 			if ( current_user_can( 'edit_post', $post->ID ) )
       
   412 				$actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
       
   413 		}
       
   414 		else {
       
   415 			if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
       
   416 				$actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
       
   417 			if ( current_user_can( 'delete_post', $post->ID ) ) {
       
   418 				if ( $this->is_trash )
       
   419 					$actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
       
   420 				elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
       
   421 					$actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
       
   422 				if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
       
   423 					$delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
       
   424 					$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>";
       
   425 				}
       
   426 			}
       
   427 			if ( !$this->is_trash ) {
       
   428 				$title =_draft_or_post_title( $post->post_parent );
       
   429 				$actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
       
   430 			}
       
   431 		}
       
   432 
       
   433 		$actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
       
   434 
       
   435 		return $actions;
       
   436 	}
       
   437 }