PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/puppet-br/puppet-br.org-wordpress
PHP | 396 lines | 328 code | 56 blank | 12 comment | 84 complexity | a992c6244a48b1aad0266155343558ca 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. ?>
  69. <div class="alignleft actions">
  70. <?php
  71. if ( 'top' == $which && !is_singular() && !$this->detached && !$this->is_trash ) {
  72. $this->months_dropdown( 'attachment' );
  73. do_action( 'restrict_manage_posts' );
  74. submit_button( __( 'Filter' ), 'secondary', false, false, array( 'id' => 'post-query-submit' ) );
  75. }
  76. if ( $this->detached ) {
  77. submit_button( __( 'Scan for lost attachments' ), 'secondary', 'find_detached', false );
  78. } elseif ( $this->is_trash && current_user_can( 'edit_others_posts' ) ) {
  79. submit_button( __( 'Empty Trash' ), 'button-secondary apply', 'delete_all', false );
  80. } ?>
  81. </div>
  82. <?php
  83. }
  84. function current_action() {
  85. if ( isset( $_REQUEST['find_detached'] ) )
  86. return 'find_detached';
  87. if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) )
  88. return 'attach';
  89. if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
  90. return 'delete_all';
  91. return parent::current_action();
  92. }
  93. function has_items() {
  94. return have_posts();
  95. }
  96. function no_items() {
  97. _e( 'No media attachments found.' );
  98. }
  99. function get_columns() {
  100. $posts_columns = array();
  101. $posts_columns['cb'] = '<input type="checkbox" />';
  102. $posts_columns['icon'] = '';
  103. /* translators: column name */
  104. $posts_columns['title'] = _x( 'File', 'column name' );
  105. $posts_columns['author'] = __( 'Author' );
  106. //$posts_columns['tags'] = _x( 'Tags', 'column name' );
  107. /* translators: column name */
  108. if ( !$this->detached ) {
  109. $posts_columns['parent'] = _x( 'Attached to', 'column name' );
  110. if ( post_type_supports( 'attachment', 'comments' ) )
  111. $posts_columns['comments'] = '<span class="vers"><img alt="' . esc_attr__( 'Comments' ) . '" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></span>';
  112. }
  113. /* translators: column name */
  114. $posts_columns['date'] = _x( 'Date', 'column name' );
  115. $posts_columns = apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
  116. return $posts_columns;
  117. }
  118. function get_sortable_columns() {
  119. return array(
  120. 'title' => 'title',
  121. 'author' => 'author',
  122. 'parent' => 'parent',
  123. 'comments' => 'comment_count',
  124. 'date' => array( 'date', true ),
  125. );
  126. }
  127. function display_rows() {
  128. global $post, $id;
  129. add_filter( 'the_title','esc_html' );
  130. $alt = '';
  131. while ( have_posts() ) : the_post();
  132. $user_can_edit = current_user_can( 'edit_post', $post->ID );
  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">
  153. <?php if ( $user_can_edit ) { ?>
  154. <input type="checkbox" name="media[]" value="<?php the_ID(); ?>" />
  155. <?php } ?>
  156. </th>
  157. <?php
  158. break;
  159. case 'icon':
  160. $attributes = 'class="column-icon media-icon"' . $style;
  161. ?>
  162. <td <?php echo $attributes ?>><?php
  163. if ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) {
  164. if ( $this->is_trash || ! $user_can_edit ) {
  165. echo $thumb;
  166. } else {
  167. ?>
  168. <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
  169. <?php echo $thumb; ?>
  170. </a>
  171. <?php }
  172. }
  173. ?>
  174. </td>
  175. <?php
  176. break;
  177. case 'title':
  178. ?>
  179. <td <?php echo $attributes ?>><strong>
  180. <?php if ( $this->is_trash || ! $user_can_edit ) {
  181. echo $att_title;
  182. } else { ?>
  183. <a href="<?php echo get_edit_post_link( $post->ID, true ); ?>"
  184. title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ); ?>">
  185. <?php echo $att_title; ?></a>
  186. <?php };
  187. _media_states( $post ); ?></strong>
  188. <p>
  189. <?php
  190. if ( preg_match( '/^.*?\.(\w+)$/', get_attached_file( $post->ID ), $matches ) )
  191. echo esc_html( strtoupper( $matches[1] ) );
  192. else
  193. echo strtoupper( str_replace( 'image/', '', get_post_mime_type() ) );
  194. ?>
  195. </p>
  196. <?php
  197. echo $this->row_actions( $this->_get_row_actions( $post, $att_title ) );
  198. ?>
  199. </td>
  200. <?php
  201. break;
  202. case 'author':
  203. ?>
  204. <td <?php echo $attributes ?>><?php the_author() ?></td>
  205. <?php
  206. break;
  207. case 'tags':
  208. ?>
  209. <td <?php echo $attributes ?>><?php
  210. $tags = get_the_tags();
  211. if ( !empty( $tags ) ) {
  212. $out = array();
  213. foreach ( $tags as $c )
  214. $out[] = "<a href='edit.php?tag=$c->slug'> " . esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'post_tag', 'display' ) ) . "</a>";
  215. echo join( ', ', $out );
  216. } else {
  217. _e( 'No Tags' );
  218. }
  219. ?>
  220. </td>
  221. <?php
  222. break;
  223. case 'desc':
  224. ?>
  225. <td <?php echo $attributes ?>><?php echo has_excerpt() ? $post->post_excerpt : ''; ?></td>
  226. <?php
  227. break;
  228. case 'date':
  229. if ( '0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name ) {
  230. $t_time = $h_time = __( 'Unpublished' );
  231. } else {
  232. $t_time = get_the_time( __( 'Y/m/d g:i:s A' ) );
  233. $m_time = $post->post_date;
  234. $time = get_post_time( 'G', true, $post, false );
  235. if ( ( abs( $t_diff = time() - $time ) ) < 86400 ) {
  236. if ( $t_diff < 0 )
  237. $h_time = sprintf( __( '%s from now' ), human_time_diff( $time ) );
  238. else
  239. $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
  240. } else {
  241. $h_time = mysql2date( __( 'Y/m/d' ), $m_time );
  242. }
  243. }
  244. ?>
  245. <td <?php echo $attributes ?>><?php echo $h_time ?></td>
  246. <?php
  247. break;
  248. case 'parent':
  249. if ( $post->post_parent > 0 ) {
  250. if ( get_post( $post->post_parent ) ) {
  251. $title =_draft_or_post_title( $post->post_parent );
  252. }
  253. ?>
  254. <td <?php echo $attributes ?>><strong>
  255. <?php if( current_user_can( 'edit_post', $post->post_parent ) ) { ?>
  256. <a href="<?php echo get_edit_post_link( $post->post_parent ); ?>">
  257. <?php echo $title ?></a>
  258. <?php } else {
  259. echo $title;
  260. } ?></strong>,
  261. <?php echo get_the_time( __( 'Y/m/d' ) ); ?>
  262. </td>
  263. <?php
  264. } else {
  265. ?>
  266. <td <?php echo $attributes ?>><?php _e( '(Unattached)' ); ?><br />
  267. <?php if( $user_can_edit ) {?>
  268. <a class="hide-if-no-js"
  269. onclick="findPosts.open( 'media[]','<?php echo $post->ID ?>' ); return false;"
  270. href="#the-list">
  271. <?php _e( 'Attach' ); ?></a>
  272. <?php } ?></td>
  273. <?php
  274. }
  275. break;
  276. case 'comments':
  277. $attributes = 'class="comments column-comments num"' . $style;
  278. ?>
  279. <td <?php echo $attributes ?>>
  280. <div class="post-com-count-wrapper">
  281. <?php
  282. $pending_comments = get_pending_comments_num( $post->ID );
  283. $this->comments_bubble( $post->ID, $pending_comments );
  284. ?>
  285. </div>
  286. </td>
  287. <?php
  288. break;
  289. default:
  290. ?>
  291. <td <?php echo $attributes ?>>
  292. <?php do_action( 'manage_media_custom_column', $column_name, $id ); ?>
  293. </td>
  294. <?php
  295. break;
  296. }
  297. }
  298. ?>
  299. </tr>
  300. <?php endwhile;
  301. }
  302. function _get_row_actions( $post, $att_title ) {
  303. $actions = array();
  304. if ( $this->detached ) {
  305. if ( current_user_can( 'edit_post', $post->ID ) )
  306. $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
  307. if ( current_user_can( 'delete_post', $post->ID ) )
  308. if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  309. $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
  310. } else {
  311. $delete_ays = !MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
  312. $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>";
  313. }
  314. $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
  315. if ( current_user_can( 'edit_post', $post->ID ) )
  316. $actions['attach'] = '<a href="#the-list" onclick="findPosts.open( \'media[]\',\''.$post->ID.'\' );return false;" class="hide-if-no-js">'.__( 'Attach' ).'</a>';
  317. }
  318. else {
  319. if ( current_user_can( 'edit_post', $post->ID ) && !$this->is_trash )
  320. $actions['edit'] = '<a href="' . get_edit_post_link( $post->ID, true ) . '">' . __( 'Edit' ) . '</a>';
  321. if ( current_user_can( 'delete_post', $post->ID ) ) {
  322. if ( $this->is_trash )
  323. $actions['untrash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-attachment_' . $post->ID ) . "'>" . __( 'Restore' ) . "</a>";
  324. elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH )
  325. $actions['trash'] = "<a class='submitdelete' href='" . wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-attachment_' . $post->ID ) . "'>" . __( 'Trash' ) . "</a>";
  326. if ( $this->is_trash || !EMPTY_TRASH_DAYS || !MEDIA_TRASH ) {
  327. $delete_ays = ( !$this->is_trash && !MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
  328. $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>";
  329. }
  330. }
  331. if ( !$this->is_trash ) {
  332. $title =_draft_or_post_title( $post->post_parent );
  333. $actions['view'] = '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $title ) ) . '" rel="permalink">' . __( 'View' ) . '</a>';
  334. }
  335. }
  336. $actions = apply_filters( 'media_row_actions', $actions, $post, $this->detached );
  337. return $actions;
  338. }
  339. }
  340. ?>