PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-admin/includes/class-wp-media-list-table.php

https://github.com/MikeLockz/lockwitz
PHP | 374 lines | 306 code | 56 blank | 12 comment | 78 complexity | ce483caba97efd7d39cb4dd9540109c8 MD5 | raw file
  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. function __construct() {
  12. $this->detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
  13. parent::__construct( array(
  14. 'plural' => 'media'
  15. ) );
  16. }
  17. function ajax_user_can() {
  18. return current_user_can('upload_files');
  19. }
  20. function prepare_items() {
  21. global $lost, $wpdb, $wp_query, $post_mime_types, $avail_post_mime_types;
  22. $q = $_REQUEST;
  23. if ( !empty( $lost ) )
  24. $q['post__in'] = implode( ',', $lost );
  25. list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $q );
  26. $this->is_trash = isset( $_REQUEST['status'] ) && 'trash' == $_REQUEST['status'];
  27. $this->set_pagination_args( array(
  28. 'total_items' => $wp_query->found_posts,
  29. 'total_pages' => $wp_query->max_num_pages,
  30. 'per_page' => $wp_query->query_vars['posts_per_page'],
  31. ) );
  32. }
  33. function get_views() {
  34. global $wpdb, $post_mime_types, $avail_post_mime_types;
  35. $type_links = array();
  36. $_num_posts = (array) wp_count_attachments();
  37. $_total_posts = array_sum($_num_posts) - $_num_posts['trash'];
  38. if ( !isset( $total_orphans ) )
  39. $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" );
  40. $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  41. foreach ( $matches as $type => $reals )
  42. foreach ( $reals as $real )
  43. $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
  44. $class = ( empty($_GET['post_mime_type']) && !$this->detached && !isset($_GET['status']) ) ? ' class="current"' : '';
  45. $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>';
  46. foreach ( $post_mime_types as $mime_type => $label ) {
  47. $class = '';
  48. if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  49. continue;
  50. if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  51. $class = ' class="current"';
  52. if ( !empty( $num_posts[$mime_type] ) )
  53. $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>';
  54. }
  55. $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>';
  56. if ( !empty($_num_posts['trash']) )
  57. $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>';
  58. return $type_links;
  59. }
  60. function get_bulk_actions() {
  61. $actions = array();
  62. $actions['delete'] = __( 'Delete Permanently' );
  63. if ( $this->detached )
  64. $actions['attach'] = __( 'Attach to a post' );
  65. return $actions;
  66. }
  67. function extra_tablenav( $which ) {
  68. global $post_type;
  69. $post_type_obj = get_post_type_object( $post_type );
  70. ?>
  71. <div class="alignleft actions">
  72. <?php
  73. if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
  74. $this->months_dropdown( $post_type );
  75. do_action( 'restrict_manage_posts' );
  76. submit_button( __( 'Filter' ), 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
  77. }
  78. if ( $this->detached ) {
  79. submit_button( __( 'Scan for lost attachments' ), 'secondary', 'find_detached', false );
  80. } elseif ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
  81. submit_button( __( 'Empty Trash' ), 'button-secondary apply', 'delete_all', false );
  82. } ?>
  83. </div>
  84. <?php
  85. }
  86. function current_action() {
  87. if ( isset( $_REQUEST['find_detached'] ) )
  88. return 'find_detached';
  89. if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
  90. return 'attach';
  91. if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
  92. return 'delete_all';
  93. return parent::current_action();
  94. }
  95. function has_items() {
  96. return have_posts();
  97. }
  98. function no_items() {
  99. _e( 'No media attachments found.' );
  100. }
  101. function get_columns() {
  102. $posts_columns = array();
  103. $posts_columns['cb'] = '<input type="checkbox" />';
  104. $posts_columns['icon'] = '';
  105. /* translators: column name */
  106. $posts_columns['title'] = _x( 'File', 'column name' );
  107. $posts_columns['author'] = __( 'Author' );
  108. //$posts_columns['tags'] = _x( 'Tags', 'column name' );
  109. /* translators: column name */
  110. if ( !$this->detached ) {
  111. $posts_columns['parent'] = _x( 'Attached to', 'column name' );
  112. $posts_columns['comments'] = '<span class="vers"><img alt="Comments" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></span>';
  113. }
  114. /* translators: column name */
  115. $posts_columns['date'] = _x( 'Date', 'column name' );
  116. $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
  117. return $posts_columns;
  118. }
  119. function get_sortable_columns() {
  120. return array(
  121. 'title' => 'title',
  122. 'author' => 'author',
  123. 'parent' => 'parent',
  124. 'comments' => 'comment_count',
  125. 'date' => array( 'date', true ),
  126. );
  127. }
  128. function display_rows() {
  129. global $post, $id;
  130. add_filter( 'the_title','esc_html' );
  131. $alt = '';
  132. while ( have_posts() ) : the_post();
  133. if ( $this->is_trash && $post->post_status != 'trash'
  134. || !$this->is_trash && $post->post_status == 'trash' )
  135. continue;
  136. $alt = ( 'alternate' == $alt ) ? '' : 'alternate';
  137. $post_owner = ( get_current_user_id() == $post->post_author ) ? 'self' : 'other';
  138. $att_title = _draft_or_post_title();
  139. ?>
  140. <tr id='post-<?php echo $id; ?>' class='<?php echo trim( $alt . ' author-' . $post_owner . ' status-' . $post->post_status ); ?>' valign="top">
  141. <?php
  142. list( $columns, $hidden ) = $this->get_column_info();
  143. foreach ( $columns as $column_name => $column_display_name ) {
  144. $class = "class='$column_name column-$column_name'";
  145. $style = '';
  146. if ( in_array( $column_name, $hidden ) )
  147. $style = ' style="display:none;"';
  148. $attributes = $class . $style;
  149. switch ( $column_name ) {
  150. case 'cb':
  151. ?>
  152. <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>
  153. <?php
  154. break;
  155. case 'icon':
  156. $attributes = 'class="column-icon media-icon"' . $style;
  157. ?>
  158. <td <?php echo $attributes ?>><?php
  159. if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
  160. if ( $this->is_trash ) {
  161. echo $thumb;
  162. } else {
  163. ?>
  164. <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
  165. <?php echo $thumb; ?>
  166. </a>
  167. <?php }
  168. }
  169. ?>
  170. </td>
  171. <?php
  172. break;
  173. case 'title':
  174. ?>
  175. <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 &#8220;%s&#8221;' ), $att_title ) ); ?>"><?php echo $att_title; ?></a><?php }; _media_states( $post ); ?></strong>
  176. <p>
  177. <?php
  178. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
  179. echo esc_html( strtoupper( $matches[1] ) );
  180. else
  181. echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
  182. ?>
  183. </p>
  184. <?php
  185. echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
  186. ?>
  187. </td>
  188. <?php
  189. break;
  190. case 'author':
  191. ?>
  192. <td <?php echo $attributes ?>><?php the_author() ?></td>
  193. <?php
  194. break;
  195. case 'tags':
  196. ?>
  197. <td <?php echo $attributes ?>><?php
  198. $tags = get_the_tags();
  199. if ( !empty( $tags ) ) {
  200. $out = array();
  201. foreach ( $tags as $c )
  202. $out[] = "<a href='edit.php?tag=$c->slug'> " . esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'post_tag', 'display' ) ) . "</a>";
  203. echo join( ', ', $out );
  204. } else {
  205. _e( 'No Tags' );
  206. }
  207. ?>
  208. </td>
  209. <?php
  210. break;
  211. case 'desc':
  212. ?>
  213. <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
  214. <?php
  215. break;
  216. case 'date':
  217. if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
  218. $t_time = $h_time = __( 'Unpublished' );
  219. } else {
  220. $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
  221. $m_time = $post->post_date;
  222. $time = get_post_time( 'G', true, $post, false );
  223. if ( ( abs( $t_diff = time() - $time ) ) < 86400 ) {
  224. if ( $t_diff < 0 )
  225. $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
  226. else
  227. $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
  228. } else {
  229. $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
  230. }
  231. }
  232. ?>
  233. <td <?php echo $attributes ?>><?php echo $h_time ?></td>
  234. <?php
  235. break;
  236. case 'parent':
  237. if ( $post->post_parent > 0 ) {
  238. if ( get_post( $post->post_parent ) ) {
  239. $title =_draft_or_post_title( $post->post_parent );
  240. }
  241. ?>
  242. <td <?php echo $attributes ?>>
  243. <strong><a href="<?php echo get_edit_post_link( $post->post_parent ); ?>"><?php echo $title ?></a></strong>,
  244. <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
  245. </td>
  246. <?php
  247. } else {
  248. ?>
  249. <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
  250. <a class="hide-if-no-js" onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' );return false;" href="#the-list"><?php _e( 'Attach' ); ?></a></td>
  251. <?php
  252. }
  253. break;
  254. case 'comments':
  255. $attributes = 'class="comments column-comments num"' . $style;
  256. ?>
  257. <td <?php echo $attributes ?>>
  258. <div class="post-com-count-wrapper">
  259. <?php
  260. $pending_comments = get_pending_comments_num( $post->ID );
  261. $this->comments_bubble( $post->ID, $pending_comments );
  262. ?>
  263. </div>
  264. </td>
  265. <?php
  266. break;
  267. default:
  268. ?>
  269. <td <?php echo $attributes ?>>
  270. <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?>
  271. </td>
  272. <?php
  273. break;
  274. }
  275. }
  276. ?>
  277. </tr>
  278. <?php endwhile;
  279. }
  280. function _get_row_actions( $post, $att_title ) {
  281. $actions = array();
  282. if ( $this->detached ) {
  283. if ( current_user_can( 'edit_post', $post->ID ) )
  284. $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
  285. if ( current_user_can( 'delete_post', $post->ID ) )
  286. if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  287. $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
  288. } else {
  289. $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
  290. $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
  291. }
  292. $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
  293. if ( current_user_can( 'edit_post', $post->ID ) )
  294. $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
  295. }
  296. else {
  297. if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
  298. $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
  299. if ( current_user_can( 'delete_post', $post->ID ) ) {
  300. if ( $this->is_trash )
  301. $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-attachment_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
  302. elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
  303. $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
  304. if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
  305. $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
  306. $actions['delete'] = "<a class='submitdelete'$delete_ays href='" . wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-attachment_' . $post->ID ) . "'>" . __( 'Delete Permanently' ) . "</a>";
  307. }
  308. }
  309. if ( !$this->is_trash ) {
  310. $title =_draft_or_post_title( $post->post_parent );
  311. $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
  312. }
  313. }
  314. $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
  315. return $actions;
  316. }
  317. }
  318. ?>