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

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