Nenhuma Descrição

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. * @access private
  14. *
  15. * @see WP_List_Table
  16. */
  17. class WP_Media_List_Table extends WP_List_Table {
  18. /**
  19. * Holds the number of pending comments for each post.
  20. *
  21. * @since 4.4.0
  22. * @var array
  23. */
  24. protected $comment_pending_count = array();
  25. private $detached;
  26. private $is_trash;
  27. /**
  28. * Constructor.
  29. *
  30. * @since 3.1.0
  31. *
  32. * @see WP_List_Table::__construct() for more information on default arguments.
  33. *
  34. * @param array $args An associative array of arguments.
  35. */
  36. public function __construct( $args = array() ) {
  37. $this->detached = ( isset( $_REQUEST['attachment-filter'] ) && 'detached' === $_REQUEST['attachment-filter'] );
  38. $this->modes = array(
  39. 'list' => __( 'List view' ),
  40. 'grid' => __( 'Grid view' ),
  41. );
  42. parent::__construct(
  43. array(
  44. 'plural' => 'media',
  45. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  46. )
  47. );
  48. }
  49. /**
  50. * @return bool
  51. */
  52. public function ajax_user_can() {
  53. return current_user_can( 'upload_files' );
  54. }
  55. /**
  56. * @global string $mode List table view mode.
  57. * @global WP_Query $wp_query WordPress Query object.
  58. * @global array $post_mime_types
  59. * @global array $avail_post_mime_types
  60. */
  61. public function prepare_items() {
  62. global $mode, $wp_query, $post_mime_types, $avail_post_mime_types;
  63. $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
  64. /*
  65. * Exclude attachments scheduled for deletion in the next two hours
  66. * if they are for zip packages for interrupted or failed updates.
  67. * See File_Upload_Upgrader class.
  68. */
  69. $not_in = array();
  70. $crons = _get_cron_array();
  71. if ( is_array( $crons ) ) {
  72. foreach ( $crons as $cron ) {
  73. if ( isset( $cron['upgrader_scheduled_cleanup'] ) ) {
  74. $details = reset( $cron['upgrader_scheduled_cleanup'] );
  75. if ( ! empty( $details['args'][0] ) ) {
  76. $not_in[] = (int) $details['args'][0];
  77. }
  78. }
  79. }
  80. }
  81. if ( ! empty( $_REQUEST['post__not_in'] ) && is_array( $_REQUEST['post__not_in'] ) ) {
  82. $not_in = array_merge( array_values( $_REQUEST['post__not_in'] ), $not_in );
  83. }
  84. if ( ! empty( $not_in ) ) {
  85. $_REQUEST['post__not_in'] = $not_in;
  86. }
  87. list( $post_mime_types, $avail_post_mime_types ) = wp_edit_attachments_query( $_REQUEST );
  88. $this->is_trash = isset( $_REQUEST['attachment-filter'] ) && 'trash' === $_REQUEST['attachment-filter'];
  89. $this->set_pagination_args(
  90. array(
  91. 'total_items' => $wp_query->found_posts,
  92. 'total_pages' => $wp_query->max_num_pages,
  93. 'per_page' => $wp_query->query_vars['posts_per_page'],
  94. )
  95. );
  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'] = '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>';
  298. }
  299. }
  300. /* translators: Column name. */
  301. $posts_columns['date'] = _x( 'Date', 'column name' );
  302. /**
  303. * Filters the Media list table columns.
  304. *
  305. * @since 2.5.0
  306. *
  307. * @param string[] $posts_columns An array of columns displayed in the Media list table.
  308. * @param bool $detached Whether the list table contains media not attached
  309. * to any posts. Default true.
  310. */
  311. return apply_filters( 'manage_media_columns', $posts_columns, $this->detached );
  312. }
  313. /**
  314. * @return array
  315. */
  316. protected function get_sortable_columns() {
  317. return array(
  318. 'title' => 'title',
  319. 'author' => 'author',
  320. 'parent' => 'parent',
  321. 'comments' => 'comment_count',
  322. 'date' => array( 'date', true ),
  323. );
  324. }
  325. /**
  326. * Handles the checkbox column output.
  327. *
  328. * @since 4.3.0
  329. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  330. *
  331. * @param WP_Post $item The current WP_Post object.
  332. */
  333. public function column_cb( $item ) {
  334. // Restores the more descriptive, specific name for use within this method.
  335. $post = $item;
  336. if ( current_user_can( 'edit_post', $post->ID ) ) {
  337. ?>
  338. <label class="screen-reader-text" for="cb-select-<?php echo $post->ID; ?>">
  339. <?php
  340. /* translators: %s: Attachment title. */
  341. printf( __( 'Select %s' ), _draft_or_post_title() );
  342. ?>
  343. </label>
  344. <input type="checkbox" name="media[]" id="cb-select-<?php echo $post->ID; ?>" value="<?php echo $post->ID; ?>" />
  345. <?php
  346. }
  347. }
  348. /**
  349. * Handles the title column output.
  350. *
  351. * @since 4.3.0
  352. *
  353. * @param WP_Post $post The current WP_Post object.
  354. */
  355. public function column_title( $post ) {
  356. list( $mime ) = explode( '/', $post->post_mime_type );
  357. $title = _draft_or_post_title();
  358. $thumb = wp_get_attachment_image( $post->ID, array( 60, 60 ), true, array( 'alt' => '' ) );
  359. $link_start = '';
  360. $link_end = '';
  361. if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
  362. $link_start = sprintf(
  363. '<a href="%s" aria-label="%s">',
  364. get_edit_post_link( $post->ID ),
  365. /* translators: %s: Attachment title. */
  366. esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $title ) )
  367. );
  368. $link_end = '</a>';
  369. }
  370. $class = $thumb ? ' class="has-media-icon"' : '';
  371. ?>
  372. <strong<?php echo $class; ?>>
  373. <?php
  374. echo $link_start;
  375. if ( $thumb ) :
  376. ?>
  377. <span class="media-icon <?php echo sanitize_html_class( $mime . '-icon' ); ?>"><?php echo $thumb; ?></span>
  378. <?php
  379. endif;
  380. echo $title . $link_end;
  381. _media_states( $post );
  382. ?>
  383. </strong>
  384. <p class="filename">
  385. <span class="screen-reader-text"><?php _e( 'File name:' ); ?> </span>
  386. <?php
  387. $file = get_attached_file( $post->ID );
  388. echo esc_html( wp_basename( $file ) );
  389. ?>
  390. </p>
  391. <?php
  392. }
  393. /**
  394. * Handles the author column output.
  395. *
  396. * @since 4.3.0
  397. *
  398. * @param WP_Post $post The current WP_Post object.
  399. */
  400. public function column_author( $post ) {
  401. printf(
  402. '<a href="%s">%s</a>',
  403. esc_url( add_query_arg( array( 'author' => get_the_author_meta( 'ID' ) ), 'upload.php' ) ),
  404. get_the_author()
  405. );
  406. }
  407. /**
  408. * Handles the description column output.
  409. *
  410. * @since 4.3.0
  411. *
  412. * @param WP_Post $post The current WP_Post object.
  413. */
  414. public function column_desc( $post ) {
  415. echo has_excerpt() ? $post->post_excerpt : '';
  416. }
  417. /**
  418. * Handles the date column output.
  419. *
  420. * @since 4.3.0
  421. *
  422. * @param WP_Post $post The current WP_Post object.
  423. */
  424. public function column_date( $post ) {
  425. if ( '0000-00-00 00:00:00' === $post->post_date ) {
  426. $h_time = __( 'Unpublished' );
  427. } else {
  428. $time = get_post_timestamp( $post );
  429. $time_diff = time() - $time;
  430. if ( $time && $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
  431. /* translators: %s: Human-readable time difference. */
  432. $h_time = sprintf( __( '%s ago' ), human_time_diff( $time ) );
  433. } else {
  434. $h_time = get_the_time( __( 'Y/m/d' ), $post );
  435. }
  436. }
  437. /**
  438. * Filters the published time of an attachment displayed in the Media list table.
  439. *
  440. * @since 6.0.0
  441. *
  442. * @param string $h_time The published time.
  443. * @param WP_Post $post Attachment object.
  444. * @param string $column_name The column name.
  445. */
  446. echo apply_filters( 'media_date_column_time', $h_time, $post, 'date' );
  447. }
  448. /**
  449. * Handles the parent column output.
  450. *
  451. * @since 4.3.0
  452. *
  453. * @param WP_Post $post The current WP_Post object.
  454. */
  455. public function column_parent( $post ) {
  456. $user_can_edit = current_user_can( 'edit_post', $post->ID );
  457. if ( $post->post_parent > 0 ) {
  458. $parent = get_post( $post->post_parent );
  459. } else {
  460. $parent = false;
  461. }
  462. if ( $parent ) {
  463. $title = _draft_or_post_title( $post->post_parent );
  464. $parent_type = get_post_type_object( $parent->post_type );
  465. if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $post->post_parent ) ) {
  466. printf( '<strong><a href="%s">%s</a></strong>', get_edit_post_link( $post->post_parent ), $title );
  467. } elseif ( $parent_type && current_user_can( 'read_post', $post->post_parent ) ) {
  468. printf( '<strong>%s</strong>', $title );
  469. } else {
  470. _e( '(Private post)' );
  471. }
  472. if ( $user_can_edit ) :
  473. $detach_url = add_query_arg(
  474. array(
  475. 'parent_post_id' => $post->post_parent,
  476. 'media[]' => $post->ID,
  477. '_wpnonce' => wp_create_nonce( 'bulk-' . $this->_args['plural'] ),
  478. ),
  479. 'upload.php'
  480. );
  481. printf(
  482. '<br /><a href="%s" class="hide-if-no-js detach-from-parent" aria-label="%s">%s</a>',
  483. $detach_url,
  484. /* translators: %s: Title of the post the attachment is attached to. */
  485. esc_attr( sprintf( __( 'Detach from &#8220;%s&#8221;' ), $title ) ),
  486. __( 'Detach' )
  487. );
  488. endif;
  489. } else {
  490. _e( '(Unattached)' );
  491. ?>
  492. <?php
  493. if ( $user_can_edit ) {
  494. $title = _draft_or_post_title( $post->post_parent );
  495. printf(
  496. '<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>',
  497. $post->ID,
  498. /* translators: %s: Attachment title. */
  499. esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $title ) ),
  500. __( 'Attach' )
  501. );
  502. }
  503. }
  504. }
  505. /**
  506. * Handles the comments column output.
  507. *
  508. * @since 4.3.0
  509. *
  510. * @param WP_Post $post The current WP_Post object.
  511. */
  512. public function column_comments( $post ) {
  513. echo '<div class="post-com-count-wrapper">';
  514. if ( isset( $this->comment_pending_count[ $post->ID ] ) ) {
  515. $pending_comments = $this->comment_pending_count[ $post->ID ];
  516. } else {
  517. $pending_comments = get_pending_comments_num( $post->ID );
  518. }
  519. $this->comments_bubble( $post->ID, $pending_comments );
  520. echo '</div>';
  521. }
  522. /**
  523. * Handles output for the default column.
  524. *
  525. * @since 4.3.0
  526. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  527. *
  528. * @param WP_Post $item The current WP_Post object.
  529. * @param string $column_name Current column name.
  530. */
  531. public function column_default( $item, $column_name ) {
  532. // Restores the more descriptive, specific name for use within this method.
  533. $post = $item;
  534. if ( 'categories' === $column_name ) {
  535. $taxonomy = 'category';
  536. } elseif ( 'tags' === $column_name ) {
  537. $taxonomy = 'post_tag';
  538. } elseif ( 0 === strpos( $column_name, 'taxonomy-' ) ) {
  539. $taxonomy = substr( $column_name, 9 );
  540. } else {
  541. $taxonomy = false;
  542. }
  543. if ( $taxonomy ) {
  544. $terms = get_the_terms( $post->ID, $taxonomy );
  545. if ( is_array( $terms ) ) {
  546. $out = array();
  547. foreach ( $terms as $t ) {
  548. $posts_in_term_qv = array();
  549. $posts_in_term_qv['taxonomy'] = $taxonomy;
  550. $posts_in_term_qv['term'] = $t->slug;
  551. $out[] = sprintf(
  552. '<a href="%s">%s</a>',
  553. esc_url( add_query_arg( $posts_in_term_qv, 'upload.php' ) ),
  554. esc_html( sanitize_term_field( 'name', $t->name, $t->term_id, $taxonomy, 'display' ) )
  555. );
  556. }
  557. echo implode( wp_get_list_item_separator(), $out );
  558. } else {
  559. echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' . get_taxonomy( $taxonomy )->labels->no_terms . '</span>';
  560. }
  561. return;
  562. }
  563. /**
  564. * Fires for each custom column in the Media list table.
  565. *
  566. * Custom columns are registered using the {@see 'manage_media_columns'} filter.
  567. *
  568. * @since 2.5.0
  569. *
  570. * @param string $column_name Name of the custom column.
  571. * @param int $post_id Attachment ID.
  572. */
  573. do_action( 'manage_media_custom_column', $column_name, $post->ID );
  574. }
  575. /**
  576. * @global WP_Post $post Global post object.
  577. */
  578. public function display_rows() {
  579. global $post, $wp_query;
  580. $post_ids = wp_list_pluck( $wp_query->posts, 'ID' );
  581. reset( $wp_query->posts );
  582. $this->comment_pending_count = get_pending_comments_num( $post_ids );
  583. add_filter( 'the_title', 'esc_html' );
  584. while ( have_posts() ) :
  585. the_post();
  586. if ( $this->is_trash && 'trash' !== $post->post_status
  587. || ! $this->is_trash && 'trash' === $post->post_status
  588. ) {
  589. continue;
  590. }
  591. $post_owner = ( get_current_user_id() === (int) $post->post_author ) ? 'self' : 'other';
  592. ?>
  593. <tr id="post-<?php echo $post->ID; ?>" class="<?php echo trim( ' author-' . $post_owner . ' status-' . $post->post_status ); ?>">
  594. <?php $this->single_row_columns( $post ); ?>
  595. </tr>
  596. <?php
  597. endwhile;
  598. }
  599. /**
  600. * Gets the name of the default primary column.
  601. *
  602. * @since 4.3.0
  603. *
  604. * @return string Name of the default primary column, in this case, 'title'.
  605. */
  606. protected function get_default_primary_column_name() {
  607. return 'title';
  608. }
  609. /**
  610. * @param WP_Post $post
  611. * @param string $att_title
  612. * @return array
  613. */
  614. private function _get_row_actions( $post, $att_title ) {
  615. $actions = array();
  616. if ( $this->detached ) {
  617. if ( current_user_can( 'edit_post', $post->ID ) ) {
  618. $actions['edit'] = sprintf(
  619. '<a href="%s" aria-label="%s">%s</a>',
  620. get_edit_post_link( $post->ID ),
  621. /* translators: %s: Attachment title. */
  622. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
  623. __( 'Edit' )
  624. );
  625. }
  626. if ( current_user_can( 'delete_post', $post->ID ) ) {
  627. if ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  628. $actions['trash'] = sprintf(
  629. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  630. wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
  631. /* translators: %s: Attachment title. */
  632. esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
  633. _x( 'Trash', 'verb' )
  634. );
  635. } else {
  636. $delete_ays = ! MEDIA_TRASH ? " onclick='return showNotice.warn();'" : '';
  637. $actions['delete'] = sprintf(
  638. '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
  639. wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
  640. $delete_ays,
  641. /* translators: %s: Attachment title. */
  642. esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
  643. __( 'Delete Permanently' )
  644. );
  645. }
  646. }
  647. $actions['view'] = sprintf(
  648. '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
  649. get_permalink( $post->ID ),
  650. /* translators: %s: Attachment title. */
  651. esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
  652. __( 'View' )
  653. );
  654. if ( current_user_can( 'edit_post', $post->ID ) ) {
  655. $actions['attach'] = sprintf(
  656. '<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>',
  657. $post->ID,
  658. /* translators: %s: Attachment title. */
  659. esc_attr( sprintf( __( 'Attach &#8220;%s&#8221; to existing content' ), $att_title ) ),
  660. __( 'Attach' )
  661. );
  662. }
  663. } else {
  664. if ( current_user_can( 'edit_post', $post->ID ) && ! $this->is_trash ) {
  665. $actions['edit'] = sprintf(
  666. '<a href="%s" aria-label="%s">%s</a>',
  667. get_edit_post_link( $post->ID ),
  668. /* translators: %s: Attachment title. */
  669. esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $att_title ) ),
  670. __( 'Edit' )
  671. );
  672. }
  673. if ( current_user_can( 'delete_post', $post->ID ) ) {
  674. if ( $this->is_trash ) {
  675. $actions['untrash'] = sprintf(
  676. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  677. wp_nonce_url( "post.php?action=untrash&amp;post=$post->ID", 'untrash-post_' . $post->ID ),
  678. /* translators: %s: Attachment title. */
  679. esc_attr( sprintf( __( 'Restore &#8220;%s&#8221; from the Trash' ), $att_title ) ),
  680. __( 'Restore' )
  681. );
  682. } elseif ( EMPTY_TRASH_DAYS && MEDIA_TRASH ) {
  683. $actions['trash'] = sprintf(
  684. '<a href="%s" class="submitdelete aria-button-if-js" aria-label="%s">%s</a>',
  685. wp_nonce_url( "post.php?action=trash&amp;post=$post->ID", 'trash-post_' . $post->ID ),
  686. /* translators: %s: Attachment title. */
  687. esc_attr( sprintf( __( 'Move &#8220;%s&#8221; to the Trash' ), $att_title ) ),
  688. _x( 'Trash', 'verb' )
  689. );
  690. }
  691. if ( $this->is_trash || ! EMPTY_TRASH_DAYS || ! MEDIA_TRASH ) {
  692. $delete_ays = ( ! $this->is_trash && ! MEDIA_TRASH ) ? " onclick='return showNotice.warn();'" : '';
  693. $actions['delete'] = sprintf(
  694. '<a href="%s" class="submitdelete aria-button-if-js"%s aria-label="%s">%s</a>',
  695. wp_nonce_url( "post.php?action=delete&amp;post=$post->ID", 'delete-post_' . $post->ID ),
  696. $delete_ays,
  697. /* translators: %s: Attachment title. */
  698. esc_attr( sprintf( __( 'Delete &#8220;%s&#8221; permanently' ), $att_title ) ),
  699. __( 'Delete Permanently' )
  700. );
  701. }
  702. }
  703. if ( ! $this->is_trash ) {
  704. $actions['view'] = sprintf(
  705. '<a href="%s" aria-label="%s" rel="bookmark">%s</a>',
  706. get_permalink( $post->ID ),
  707. /* translators: %s: Attachment title. */
  708. esc_attr( sprintf( __( 'View &#8220;%s&#8221;' ), $att_title ) ),
  709. __( 'View' )
  710. );
  711. $actions['copy'] = sprintf(
  712. '<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>',
  713. esc_url( wp_get_attachment_url( $post->ID ) ),
  714. /* translators: %s: Attachment title. */
  715. esc_attr( sprintf( __( 'Copy &#8220;%s&#8221; URL to clipboard' ), $att_title ) ),
  716. __( 'Copy URL to clipboard' ),
  717. __( 'Copied!' )
  718. );
  719. }
  720. }
  721. /**
  722. * Filters the action links for each attachment in the Media list table.
  723. *
  724. * @since 2.8.0
  725. *
  726. * @param string[] $actions An array of action links for each attachment.
  727. * Default 'Edit', 'Delete Permanently', 'View'.
  728. * @param WP_Post $post WP_Post object for the current attachment.
  729. * @param bool $detached Whether the list table contains media not attached
  730. * to any posts. Default true.
  731. */
  732. return apply_filters( 'media_row_actions', $actions, $post, $this->detached );
  733. }
  734. /**
  735. * Generates and displays row action links.
  736. *
  737. * @since 4.3.0
  738. * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support.
  739. *
  740. * @param WP_Post $item Attachment being acted upon.
  741. * @param string $column_name Current column name.
  742. * @param string $primary Primary column name.
  743. * @return string Row actions output for media attachments, or an empty string
  744. * if the current column is not the primary column.
  745. */
  746. protected function handle_row_actions( $item, $column_name, $primary ) {
  747. if ( $primary !== $column_name ) {
  748. return '';
  749. }
  750. $att_title = _draft_or_post_title();
  751. $actions = $this->_get_row_actions(
  752. $item, // WP_Post object for an attachment.
  753. $att_title
  754. );
  755. return $this->row_actions( $actions );
  756. }
  757. }