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

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