Sin descripción

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

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