class-wp-media-list-table.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /**
  3. * List Table API: WP_Media_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying media items in a list table.
  11. *
  12. * @since 3.1.0
  13. *
  14. * @see WP_List_Table
  15. */
  16. class WP_Media_List_Table extends WP_List_Table {
  17. /**
  18. * Holds the number of pending comments for each post.
  19. *
  20. * @since 4.4.0
  21. * @var array
  22. */
  23. protected $comment_pending_count = array();
  24. private $detached;
  25. private $is_trash;
  26. /**
  27. * Constructor.
  28. *
  29. * @since 3.1.0
  30. *
  31. * @see WP_List_Table::__construct() for more information on default arguments.
  32. *
  33. * @param array $args An associative array of arguments.
  34. */
  35. public function __construct( $args = array() ) {
  36. $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
  37. $this->modes = array(
  38. 'list' => __( 'List view' ),
  39. 'grid' => __( 'Grid view' ),
  40. );
  41. parent::__construct(
  42. array(
  43. 'plural' => 'media',
  44. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  45. )
  46. );
  47. }
  48. /**
  49. * @return bool
  50. */
  51. public function ajax_user_can() {
  52. return current_user_can( 'upload_files' );
  53. }
  54. /**
  55. * @global string $mode List table view mode.
  56. * @global WP_Query $wp_query WordPress Query object.
  57. * @global array $post_mime_types
  58. * @global array $avail_post_mime_types
  59. */
  60. public function prepare_items() {
  61. global $mode, $wp_query, $post_mime_types, $avail_post_mime_types;
  62. $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
  63. /*
  64. * Exclude attachments scheduled for deletion in the next two hours
  65. * if they are for zip packages for interrupted or failed updates.
  66. * See File_Upload_Upgrader class.
  67. */
  68. $not_in = array();
  69. $crons = _get_cron_array();
  70. if ( is_array( $crons ) ) {
  71. foreach ( $crons as $cron ) {
  72. if ( isset( $cron['upgrader_scheduled_cleanup'] ) ) {
  73. $details = reset( $cron['upgrader_scheduled_cleanup'] );
  74. if ( ! empty( $details['args'][0] ) ) {
  75. $not_in[] = (int) $details['args'][0];
  76. }
  77. }
  78. }
  79. }
  80. if ( ! empty( $_REQUEST['post__not_in'] ) && is_array( $_REQUEST['post__not_in'] ) ) {
  81. $not_in = array_merge( array_values( $_REQUEST['post__not_in'] ), $not_in );
  82. }
  83. if ( ! empty( $not_in ) ) {
  84. $_REQUEST['post__not_in'] = $not_in;
  85. }
  86. list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
  87. $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
  88. $this->set_pagination_args(
  89. array(
  90. 'total_items' => $wp_query->found_posts,
  91. 'total_pages' => $wp_query->max_num_pages,
  92. 'per_page' => $wp_query->query_vars['posts_per_page'],
  93. )
  94. );
  95. update_post_parent_caches( $wp_query->posts );
  96. }
  97. /**
  98. * @global array $post_mime_types
  99. * @global array $avail_post_mime_types
  100. * @return array
  101. */
  102. protected function get_views() {
  103. global $post_mime_types, $avail_post_mime_types;
  104. $type_links = array();
  105. $filter = empty( $_GET['attachment-filter'] ) ? '' : $_GET['attachment-filter'];
  106. $type_links['all'] = sprintf(
  107. '<option value=""%s>%s</option>',
  108. selected( $filter, true, false ),
  109. __( 'All media items' )
  110. );
  111. foreach ( $post_mime_types as $mime_type => $label ) {
  112. if ( ! wp_match_mime_types( $mime_type, $avail_post_mime_types ) ) {
  113. continue;
  114. }
  115. $selected = selected(
  116. $filter && 0 === strpos( $filter, 'post_mime_type:' ) &&
  117. wp_match_mime_types( $mime_type, str_replace( 'post_mime_type:', '', $filter ) ),
  118. true,
  119. false
  120. );
  121. $type_links[ $mime_type ] = sprintf(
  122. '<option value="post_mime_type:%s"%s>%s</option>',
  123. esc_attr( $mime_type ),
  124. $selected,
  125. $label[0]
  126. );
  127. }
  128. $type_links['detached'] = '<option value="detached"' . ( $this->detached ? ' selected="selected"' : '' ) . '>' . _x( 'Unattached', 'media items' ) . '</option>';
  129. $type_links['mine'] = sprintf(
  130. '<option value="mine"%s>%s</option>',
  131. selected( 'mine' === $filter, true, false ),
  132. _x( 'Mine', 'media items' )
  133. );
  134. if ( $this->is_trash || ( defined( 'MEDIA_TRASH' ) && MEDIA_TRASH ) ) {
  135. $type_links['trash'] = sprintf(
  136. '<option value="trash"%s>%s</option>',
  137. selected( 'trash' === $filter, true, false ),
  138. _x( 'Trash', 'attachment filter' )
  139. );
  140. }
  141. return $type_links;
  142. }
  143. /**
  144. * @return array
  145. */
  146. protected function get_bulk_actions() {
  147. $actions = array();
  148. if ( MEDIA_TRASH ) {
  149. if ( $this->is_trash ) {
  150. $actions['untrash'] = __( 'Restore' );
  151. $actions['delete'] = __( 'Delete permanently' );
  152. } else {
  153. $actions['trash'] = __( 'Move to Trash' );
  154. }
  155. } else {
  156. $actions['delete'] = __( 'Delete permanently' );
  157. }
  158. if ( $this->detached ) {
  159. $actions['attach'] = __( 'Attach' );
  160. }
  161. return $actions;
  162. }
  163. /**
  164. * @param string $which
  165. */
  166. protected function extra_tablenav( $which ) {
  167. if ( 'bar' !== $which ) {
  168. return;
  169. }
  170. ?>
  171. <div class="actions">
  172. <?php
  173. if ( ! $this->is_trash ) {
  174. $this->months_dropdown( 'attachment' );
  175. }
  176. /** This action is documented in wp-admin/includes/class-wp-posts-list-table.php */
  177. do_action( 'restrict_manage_posts', $this->screen->post_type, $which );
  178. submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
  179. if ( $this->is_trash && $this->has_items()
  180. && current_user_can( 'edit_others_posts' )
  181. ) {
  182. submit_button( __( 'Empty Trash' ), 'apply', 'delete_all', false );
  183. }
  184. ?>
  185. </div>
  186. <?php
  187. }
  188. /**
  189. * @return string
  190. */
  191. public function current_action() {
  192. if ( isset( $_REQUEST['found_post_id'] ) && isset( $_REQUEST['media'] ) ) {
  193. return 'attach';
  194. }
  195. if ( isset( $_REQUEST['parent_post_id'] ) && isset( $_REQUEST['media'] ) ) {
  196. return 'detach';
  197. }
  198. if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
  199. return 'delete_all';
  200. }
  201. return parent::current_action();
  202. }
  203. /**
  204. * @return bool
  205. */
  206. public function has_items() {
  207. return have_posts();
  208. }
  209. /**
  210. */
  211. public function no_items() {
  212. if ( $this->is_trash ) {
  213. _e( 'No media files found in Trash.' );
  214. } else {
  215. _e( 'No media files found.' );
  216. }
  217. }
  218. /**
  219. * Override parent views so we can use the filter bar display.
  220. *
  221. * @global string $mode List table view mode.
  222. */
  223. public function views() {
  224. global $mode;
  225. $views = $this->get_views();
  226. $this->screen->render_screen_reader_content( 'heading_views' );
  227. ?>
  228. <div class="wp-filter">
  229. <div class="filter-items">
  230. <?php $this->view_switcher( $mode ); ?>
  231. <label for="attachment-filter" class="screen-reader-text"><?php _e( 'Filter by type' ); ?></label>
  232. <select class="attachment-filters" name="attachment-filter" id="attachment-filter">
  233. <?php
  234. if ( ! empty( $views ) ) {
  235. foreach ( $views as $class => $view ) {
  236. echo "\t$view\n";
  237. }
  238. }
  239. ?>
  240. </select>
  241. <?php
  242. $this->extra_tablenav( 'bar' );
  243. /** This filter is documented in wp-admin/inclues/class-wp-list-table.php */
  244. $views = apply_filters( "views_{$this->screen->id}", array() );
  245. // Back compat for pre-4.0 view links.
  246. if ( ! empty( $views ) ) {
  247. echo '<ul class="filter-links">';
  248. foreach ( $views as $class => $view ) {
  249. echo "<li class='$class'>$view</li>";
  250. }
  251. echo '</ul>';
  252. }
  253. ?>
  254. </div>
  255. <div class="search-form">
  256. <label for="media-search-input" class="media-search-input-label"><?php esc_html_e( 'Search' ); ?></label>
  257. <input type="search" id="media-search-input" class="search" name="s" value="<?php _admin_search_query(); ?>">
  258. </div>
  259. </div>
  260. <?php
  261. }
  262. /**
  263. * @return array
  264. */
  265. public function get_columns() {
  266. $posts_columns = array();
  267. $posts_columns['cb'] = '<input type="checkbox" />';
  268. /* translators: Column name. */
  269. $posts_columns['title'] = _x( 'File', 'column name' );
  270. $posts_columns['author'] = __( 'Author' );
  271. $taxonomies = get_taxonomies_for_attachments( 'objects' );
  272. $taxonomies = wp_filter_object_list( $taxonomies, array( 'show_admin_column' => true ), 'and', 'name' );
  273. /**
  274. * Filters the taxonomy columns for attachments in the Media list table.
  275. *
  276. * @since 3.5.0
  277. *
  278. * @param string[] $taxonomies An array of registered taxonomy names to show for attachments.
  279. * @param string $post_type The post type. Default 'attachment'.
  280. */
  281. $taxonomies = apply_filters( 'manage_taxonomies_for_attachment_columns', $taxonomies, 'attachment' );
  282. $taxonomies = array_filter( $taxonomies, 'taxonomy_exists' );
  283. foreach ( $taxonomies as $taxonomy ) {
  284. if ( 'category' === $taxonomy ) {
  285. $column_key = 'categories';
  286. } elseif ( 'post_tag' === $taxonomy ) {
  287. $column_key = 'tags';
  288. } else {
  289. $column_key = 'taxonomy-' . $taxonomy;
  290. }
  291. $posts_columns[ $column_key ] = get_taxonomy( $taxonomy )->labels->name;
  292. }
  293. /* translators: Column name. */
  294. if ( ! $this->detached ) {
  295. $posts_columns['parent'] = _x( 'Uploaded to', 'column name' );
  296. if ( post_type_supports( 'attachment', 'comments' ) ) {
  297. $posts_columns['comments'] = sprintf(
  298. '<span class="vers comment-grey-bubble" title="%1$s" aria-hidden="true"></span><span class="screen-reader-text">%2$s</span>',
  299. esc_attr__( 'Comments' ),
  300. __( 'Comments' )
  301. );
  302. }
  303. }
  304. /* translators: Column name. */
  305. $posts_columns['date'] = _x( 'Date', 'column name' );
  306. /**
  307. * Filters the Media list table columns.
  308. *
  309. * @since 2.5.0
  310. *
  311. * @param string[] $posts_columns An array of columns displayed in the Media list table.
  312. * @param bool $detached Whether the list table contains media not attached
  313. * to any posts. Default true.
  314. */
  315. return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
  316. }
  317. /**
  318. * @return array
  319. */
  320. protected function get_sortable_columns() {
  321. return array(
  322. 'title' => 'title',
  323. 'author' => 'author',
  324. 'parent' => 'parent',
  325. 'comments' => 'comment_count',
  326. 'date' => array( 'date', true ),
  327. );
  328. }
  329. /**
  330. * Handles the checkbox column output.
  331. *
  332. * @since 4.3.0
  333. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  334. *
  335. * @param WP_Post $item The current WP_Post object.
  336. */
  337. public function column_cb( $item ) {
  338. // Restores the more descriptive, specific name for use within this method.
  339. $post = $item;
  340. if ( current_user_can( 'edit_post', $post->ID ) ) {
  341. ?>
  342. <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>">
  343. <?php
  344. /* translators: %s: Attachment title. */
  345. printf( __( 'Select %s' ), _draft_or_post_title() );
  346. ?>
  347. </label>
  348. <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
  349. <?php
  350. }
  351. }
  352. /**
  353. * Handles the title column output.
  354. *
  355. * @since 4.3.0
  356. *
  357. * @param WP_Post $post The current WP_Post object.
  358. */
  359. public function column_title( $post ) {
  360. list( $mime ) = explode( '/', $post->post_mime_type );
  361. $title = _draft_or_post_title();
  362. $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
  363. $link_start = '';
  364. $link_end = '';
  365. if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
  366. $link_start = sprintf(
  367. '<a href="%s" aria-label="%s">',
  368. get_edit_post_link( $post->ID ),
  369. /* translators: %s: Attachment title. */
  370. esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title ) )
  371. );
  372. $link_end = '</a>';
  373. }
  374. $class = $thumb ? ' class="has-media-icon"' : '';
  375. ?>
  376. <strong<?php echo $class; ?>>
  377. <?php
  378. echo $link_start;
  379. if ( $thumb ) :
  380. ?>
  381. <span class="media-icon <?php echo sanitize_html_class( $mime . '-icon' ); ?>"><?php echo $thumb; ?></span>
  382. <?php
  383. endif;
  384. echo $title . $link_end;
  385. _media_states( $post );
  386. ?>
  387. </strong>
  388. <p class="filename">
  389. <span class="screen-reader-text"><?php _e( 'File name:' ); ?> </span>
  390. <?php
  391. $file = get_attached_file( $post->ID );
  392. echo esc_html( wp_basename( $file ) );
  393. ?>
  394. </p>
  395. <?php
  396. }
  397. /**
  398. * Handles the author column output.
  399. *
  400. * @since 4.3.0
  401. *
  402. * @param WP_Post $post The current WP_Post object.
  403. */
  404. public function column_author( $post ) {
  405. printf(
  406. '<a href="%s">%s</a>',
  407. esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
  408. get_the_author()
  409. );
  410. }
  411. /**
  412. * Handles the description column output.
  413. *
  414. * @since 4.3.0
  415. *
  416. * @param WP_Post $post The current WP_Post object.
  417. */
  418. public function column_desc( $post ) {
  419. echo has_excerpt() ? $post->post_excerpt : '';
  420. }
  421. /**
  422. * Handles the date column output.
  423. *
  424. * @since 4.3.0
  425. *
  426. * @param WP_Post $post The current WP_Post object.
  427. */
  428. public function column_date( $post ) {
  429. if ( '0000-00-00 00:00:00' === $post->post_date ) {
  430. $h_time = __( 'Unpublished' );
  431. } else {
  432. $time = get_post_timestamp( $post );
  433. $time_diff = time() - $time;
  434. if ( $time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
  435. /* translators: %s: Human-readable time difference. */
  436. $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
  437. } else {
  438. $h_time = get_the_time( __( 'Y/m/d' ), $post );
  439. }
  440. }
  441. /**
  442. * Filters the published time of an attachment displayed in the Media list table.
  443. *
  444. * @since 6.0.0
  445. *
  446. * @param string $h_time The published time.
  447. * @param WP_Post $post Attachment object.
  448. * @param string $column_name The column name.
  449. */
  450. echo apply_filters( 'media_date_column_time', $h_time, $post, 'date' );
  451. }
  452. /**
  453. * Handles the parent column output.
  454. *
  455. * @since 4.3.0
  456. *
  457. * @param WP_Post $post The current WP_Post object.
  458. */
  459. public function column_parent( $post ) {
  460. $user_can_edit = current_user_can( 'edit_post', $post->ID );
  461. if ( $post->post_parent > 0 ) {
  462. $parent = get_post( $post->post_parent );
  463. } else {
  464. $parent = false;
  465. }
  466. if ( $parent ) {
  467. $title = _draft_or_post_title( $post->post_parent );
  468. $parent_type = get_post_type_object( $parent->post_type );
  469. if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) {
  470. printf( '<strong><a href="%s">%s</a></strong>', get_edit_post_link( $post->post_parent ), $title );
  471. } elseif ( $parent_type && current_user_can( 'read_post', $post->post_parent ) ) {
  472. printf( '<strong>%s</strong>', $title );
  473. } else {
  474. _e( '(Private post)' );
  475. }
  476. if ( $user_can_edit ) :
  477. $detach_url = add_query_arg(
  478. array(
  479. 'parent_post_id' => $post->post_parent,
  480. 'media[]' => $post->ID,
  481. '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] ),
  482. ),
  483. 'upload.php'
  484. );
  485. printf(
  486. '<br /><a href="%s" class="hide-if-no-js detach-from-parent" aria-label="%s">%s</a>',
  487. $detach_url,
  488. /* translators: %s: Title of the post the attachment is attached to. */
  489. esc_attr( sprintf( __( 'Detach from &#8220;%s&#8221;' ), $title ) ),
  490. __( 'Detach' )
  491. );
  492. endif;
  493. } else {
  494. _e( '(Unattached)' );
  495. ?>
  496. <?php
  497. if ( $user_can_edit ) {
  498. $title = _draft_or_post_title( $post->post_parent );
  499. printf(
  500. '<br /><a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
  501. $post->ID,
  502. /* translators: %s: Attachment title. */
  503. esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $title ) ),
  504. __( 'Attach' )
  505. );
  506. }
  507. }
  508. }
  509. /**
  510. * Handles the comments column output.
  511. *
  512. * @since 4.3.0
  513. *
  514. * @param WP_Post $post The current WP_Post object.
  515. */
  516. public function column_comments( $post ) {
  517. echo '<div class="post-com-count-wrapper">';
  518. if ( isset( $this->comment_pending_count[ $post->ID ] ) ) {
  519. $pending_comments = $this->comment_pending_count[ $post->ID ];
  520. } else {
  521. $pending_comments = get_pending_comments_num( $post->ID );
  522. }
  523. $this->comments_bubble( $post->ID, $pending_comments );
  524. echo '</div>';
  525. }
  526. /**
  527. * Handles output for the default column.
  528. *
  529. * @since 4.3.0
  530. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  531. *
  532. * @param WP_Post $item The current WP_Post object.
  533. * @param string $column_name Current column name.
  534. */
  535. public function column_default( $item, $column_name ) {
  536. // Restores the more descriptive, specific name for use within this method.
  537. $post = $item;
  538. if ( 'categories' === $column_name ) {
  539. $taxonomy = 'category';
  540. } elseif ( 'tags' === $column_name ) {
  541. $taxonomy = 'post_tag';
  542. } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
  543. $taxonomy = substr( $column_name, 9 );
  544. } else {
  545. $taxonomy = false;
  546. }
  547. if ( $taxonomy ) {
  548. $terms = get_the_terms( $post->ID, $taxonomy );
  549. if ( is_array( $terms ) ) {
  550. $output = array();
  551. foreach ( $terms as $t ) {
  552. $posts_in_term_qv = array();
  553. $posts_in_term_qv['taxonomy'] = $taxonomy;
  554. $posts_in_term_qv['term'] = $t->slug;
  555. $output[] = sprintf(
  556. '<a href="%s">%s</a>',
  557. esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
  558. esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
  559. );
  560. }
  561. echo implode( wp_get_list_item_separator(), $output );
  562. } else {
  563. echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->no_terms . '</span>';
  564. }
  565. return;
  566. }
  567. /**
  568. * Fires for each custom column in the Media list table.
  569. *
  570. * Custom columns are registered using the {@see 'manage_media_columns'} filter.
  571. *
  572. * @since 2.5.0
  573. *
  574. * @param string $column_name Name of the custom column.
  575. * @param int $post_id Attachment ID.
  576. */
  577. do_action( 'manage_media_custom_column', $column_name, $post->ID );
  578. }
  579. /**
  580. * @global WP_Post $post Global post object.
  581. */
  582. public function display_rows() {
  583. global $post, $wp_query;
  584. $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
  585. reset( $wp_query->posts );
  586. $this->comment_pending_count = get_pending_comments_num( $post_ids );
  587. add_filter( 'the_title', 'esc_html' );
  588. while ( have_posts() ) :
  589. the_post();
  590. if ( $this->is_trash && 'trash' !== $post->post_status
  591. || ! $this->is_trash && 'trash' === $post->post_status
  592. ) {
  593. continue;
  594. }
  595. $post_owner = ( get_current_user_id() === (int) $post->post_author ) ? 'self' : 'other';
  596. ?>
  597. <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
  598. <?php $this->single_row_columns( $post ); ?>
  599. </tr>
  600. <?php
  601. endwhile;
  602. }
  603. /**
  604. * Gets the name of the default primary column.
  605. *
  606. * @since 4.3.0
  607. *
  608. * @return string Name of the default primary column, in this case, 'title'.
  609. */
  610. protected function get_default_primary_column_name() {
  611. return 'title';
  612. }
  613. /**
  614. * @param WP_Post $post
  615. * @param string $att_title
  616. * @return array
  617. */
  618. private function _get_row_actions( $post, $att_title ) {
  619. $actions = array();
  620. if ( $this->detached ) {
  621. if ( current_user_can( 'edit_post', $post->ID ) ) {
  622. $actions['edit'] = sprintf(
  623. '<a href="%s" aria-label="%s">%s</a>',
  624. get_edit_post_link( $post->ID ),
  625. /* translators: %s: Attachment title. */
  626. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
  627. __( 'Edit' )
  628. );
  629. }
  630. if ( current_user_can( 'delete_post', $post->ID ) ) {
  631. if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  632. $actions['trash'] = sprintf(
  633. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  634. wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
  635. /* translators: %s: Attachment title. */
  636. esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
  637. _x( 'Trash', 'verb' )
  638. );
  639. } else {
  640. $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
  641. $actions['delete'] = sprintf(
  642. '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
  643. wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
  644. $delete_ays,
  645. /* translators: %s: Attachment title. */
  646. esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
  647. __( 'Delete Permanently' )
  648. );
  649. }
  650. }
  651. $actions['view'] = sprintf(
  652. '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
  653. get_permalink( $post->ID ),
  654. /* translators: %s: Attachment title. */
  655. esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
  656. __( 'View' )
  657. );
  658. if ( current_user_can( 'edit_post', $post->ID ) ) {
  659. $actions['attach'] = sprintf(
  660. '<a href="#the-list" onclick="findPosts.open( \'media[]\', \'%s\' ); return false;" class="hide-if-no-js aria-button-if-js" aria-label="%s">%s</a>',
  661. $post->ID,
  662. /* translators: %s: Attachment title. */
  663. esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $att_title ) ),
  664. __( 'Attach' )
  665. );
  666. }
  667. } else {
  668. if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
  669. $actions['edit'] = sprintf(
  670. '<a href="%s" aria-label="%s">%s</a>',
  671. get_edit_post_link( $post->ID ),
  672. /* translators: %s: Attachment title. */
  673. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
  674. __( 'Edit' )
  675. );
  676. }
  677. if ( current_user_can( 'delete_post', $post->ID ) ) {
  678. if ( $this->is_trash ) {
  679. $actions['untrash'] = sprintf(
  680. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  681. wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ),
  682. /* translators: %s: Attachment title. */
  683. esc_attr( sprintf( __( 'Restore &#8220;%s&#8221; from the Trash' ), $att_title ) ),
  684. __( 'Restore' )
  685. );
  686. } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  687. $actions['trash'] = sprintf(
  688. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  689. wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
  690. /* translators: %s: Attachment title. */
  691. esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
  692. _x( 'Trash', 'verb' )
  693. );
  694. }
  695. if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
  696. $delete_ays = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
  697. $actions['delete'] = sprintf(
  698. '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
  699. wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
  700. $delete_ays,
  701. /* translators: %s: Attachment title. */
  702. esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
  703. __( 'Delete Permanently' )
  704. );
  705. }
  706. }
  707. if ( ! $this->is_trash ) {
  708. $actions['view'] = sprintf(
  709. '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
  710. get_permalink( $post->ID ),
  711. /* translators: %s: Attachment title. */
  712. esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
  713. __( 'View' )
  714. );
  715. $actions['copy'] = sprintf(
  716. '<span class="copy-to-clipboard-container"><button type="button" class="button-link copy-attachment-url media-library" data-clipboard-text="%s" aria-label="%s">%s</button><span class="success hidden" aria-hidden="true">%s</span></span>',
  717. esc_url( wp_get_attachment_url( $post->ID ) ),
  718. /* translators: %s: Attachment title. */
  719. esc_attr( sprintf( __( 'Copy &#8220;%s&#8221; URL to clipboard' ), $att_title ) ),
  720. __( 'Copy URL to clipboard' ),
  721. __( 'Copied!' )
  722. );
  723. }
  724. }
  725. /**
  726. * Filters the action links for each attachment in the Media list table.
  727. *
  728. * @since 2.8.0
  729. *
  730. * @param string[] $actions An array of action links for each attachment.
  731. * Default 'Edit', 'Delete Permanently', 'View'.
  732. * @param WP_Post $post WP_Post object for the current attachment.
  733. * @param bool $detached Whether the list table contains media not attached
  734. * to any posts. Default true.
  735. */
  736. return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
  737. }
  738. /**
  739. * Generates and displays row action links.
  740. *
  741. * @since 4.3.0
  742. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  743. *
  744. * @param WP_Post $item Attachment being acted upon.
  745. * @param string $column_name Current column name.
  746. * @param string $primary Primary column name.
  747. * @return string Row actions output for media attachments, or an empty string
  748. * if the current column is not the primary column.
  749. */
  750. protected function handle_row_actions( $item, $column_name, $primary ) {
  751. if ( $primary !== $column_name ) {
  752. return '';
  753. }
  754. $att_title = _draft_or_post_title();
  755. $actions = $this->_get_row_actions(
  756. $item, // WP_Post object for an attachment.
  757. $att_title
  758. );
  759. return $this->row_actions( $actions );
  760. }
  761. }