class-wp-comments-list-table.php 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. <?php
  2. /**
  3. * List Table API: WP_Comments_List_Table class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 3.1.0
  8. */
  9. /**
  10. * Core class used to implement displaying comments in a list table.
  11. *
  12. * @since 3.1.0
  13. *
  14. * @see WP_List_Table
  15. */
  16. class WP_Comments_List_Table extends WP_List_Table {
  17. public $checkbox = true;
  18. public $pending_count = array();
  19. public $extra_items;
  20. private $user_can;
  21. /**
  22. * Constructor.
  23. *
  24. * @since 3.1.0
  25. *
  26. * @see WP_List_Table::__construct() for more information on default arguments.
  27. *
  28. * @global int $post_id
  29. *
  30. * @param array $args An associative array of arguments.
  31. */
  32. public function __construct( $args = array() ) {
  33. global $post_id;
  34. $post_id = isset( $_REQUEST['p'] ) ? absint( $_REQUEST['p'] ) : 0;
  35. if ( get_option( 'show_avatars' ) ) {
  36. add_filter( 'comment_author', array( $this, 'floated_admin_avatar' ), 10, 2 );
  37. }
  38. parent::__construct(
  39. array(
  40. 'plural' => 'comments',
  41. 'singular' => 'comment',
  42. 'ajax' => true,
  43. 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  44. )
  45. );
  46. }
  47. /**
  48. * Adds avatars to comment author names.
  49. *
  50. * @since 3.1.0
  51. *
  52. * @param string $name Comment author name.
  53. * @param int $comment_id Comment ID.
  54. * @return string Avatar with the user name.
  55. */
  56. public function floated_admin_avatar( $name, $comment_id ) {
  57. $comment = get_comment( $comment_id );
  58. $avatar = get_avatar( $comment, 32, 'mystery' );
  59. return "$avatar $name";
  60. }
  61. /**
  62. * @return bool
  63. */
  64. public function ajax_user_can() {
  65. return current_user_can( 'edit_posts' );
  66. }
  67. /**
  68. * @global string $mode List table view mode.
  69. * @global int $post_id
  70. * @global string $comment_status
  71. * @global string $comment_type
  72. * @global string $search
  73. */
  74. public function prepare_items() {
  75. global $mode, $post_id, $comment_status, $comment_type, $search;
  76. if ( ! empty( $_REQUEST['mode'] ) ) {
  77. $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
  78. set_user_setting( 'posts_list_mode', $mode );
  79. } else {
  80. $mode = get_user_setting( 'posts_list_mode', 'list' );
  81. }
  82. $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
  83. if ( ! in_array( $comment_status, array( 'all', 'mine', 'moderated', 'approved', 'spam', 'trash' ), true ) ) {
  84. $comment_status = 'all';
  85. }
  86. $comment_type = ! empty( $_REQUEST['comment_type'] ) ? $_REQUEST['comment_type'] : '';
  87. $search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : '';
  88. $post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_key( $_REQUEST['post_type'] ) : '';
  89. $user_id = ( isset( $_REQUEST['user_id'] ) ) ? $_REQUEST['user_id'] : '';
  90. $orderby = ( isset( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : '';
  91. $order = ( isset( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : '';
  92. $comments_per_page = $this->get_per_page( $comment_status );
  93. $doing_ajax = wp_doing_ajax();
  94. if ( isset( $_REQUEST['number'] ) ) {
  95. $number = (int) $_REQUEST['number'];
  96. } else {
  97. $number = $comments_per_page + min( 8, $comments_per_page ); // Grab a few extra.
  98. }
  99. $page = $this->get_pagenum();
  100. if ( isset( $_REQUEST['start'] ) ) {
  101. $start = $_REQUEST['start'];
  102. } else {
  103. $start = ( $page - 1 ) * $comments_per_page;
  104. }
  105. if ( $doing_ajax && isset( $_REQUEST['offset'] ) ) {
  106. $start += $_REQUEST['offset'];
  107. }
  108. $status_map = array(
  109. 'mine' => '',
  110. 'moderated' => 'hold',
  111. 'approved' => 'approve',
  112. 'all' => '',
  113. );
  114. $args = array(
  115. 'status' => isset( $status_map[ $comment_status ] ) ? $status_map[ $comment_status ] : $comment_status,
  116. 'search' => $search,
  117. 'user_id' => $user_id,
  118. 'offset' => $start,
  119. 'number' => $number,
  120. 'post_id' => $post_id,
  121. 'type' => $comment_type,
  122. 'orderby' => $orderby,
  123. 'order' => $order,
  124. 'post_type' => $post_type,
  125. );
  126. /**
  127. * Filters the arguments for the comment query in the comments list table.
  128. *
  129. * @since 5.1.0
  130. *
  131. * @param array $args An array of get_comments() arguments.
  132. */
  133. $args = apply_filters( 'comments_list_table_query_args', $args );
  134. $_comments = get_comments( $args );
  135. if ( is_array( $_comments ) ) {
  136. update_comment_cache( $_comments );
  137. $this->items = array_slice( $_comments, 0, $comments_per_page );
  138. $this->extra_items = array_slice( $_comments, $comments_per_page );
  139. $_comment_post_ids = array_unique( wp_list_pluck( $_comments, 'comment_post_ID' ) );
  140. $this->pending_count = get_pending_comments_num( $_comment_post_ids );
  141. }
  142. $total_comments = get_comments(
  143. array_merge(
  144. $args,
  145. array(
  146. 'count' => true,
  147. 'offset' => 0,
  148. 'number' => 0,
  149. )
  150. )
  151. );
  152. $this->set_pagination_args(
  153. array(
  154. 'total_items' => $total_comments,
  155. 'per_page' => $comments_per_page,
  156. )
  157. );
  158. }
  159. /**
  160. * @param string $comment_status
  161. * @return int
  162. */
  163. public function get_per_page( $comment_status = 'all' ) {
  164. $comments_per_page = $this->get_items_per_page( 'edit_comments_per_page' );
  165. /**
  166. * Filters the number of comments listed per page in the comments list table.
  167. *
  168. * @since 2.6.0
  169. *
  170. * @param int $comments_per_page The number of comments to list per page.
  171. * @param string $comment_status The comment status name. Default 'All'.
  172. */
  173. return apply_filters( 'comments_per_page', $comments_per_page, $comment_status );
  174. }
  175. /**
  176. * @global string $comment_status
  177. */
  178. public function no_items() {
  179. global $comment_status;
  180. if ( 'moderated' === $comment_status ) {
  181. _e( 'No comments awaiting moderation.' );
  182. } elseif ( 'trash' === $comment_status ) {
  183. _e( 'No comments found in Trash.' );
  184. } else {
  185. _e( 'No comments found.' );
  186. }
  187. }
  188. /**
  189. * @global int $post_id
  190. * @global string $comment_status
  191. * @global string $comment_type
  192. */
  193. protected function get_views() {
  194. global $post_id, $comment_status, $comment_type;
  195. $status_links = array();
  196. $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
  197. $stati = array(
  198. /* translators: %s: Number of comments. */
  199. 'all' => _nx_noop(
  200. 'All <span class="count">(%s)</span>',
  201. 'All <span class="count">(%s)</span>',
  202. 'comments'
  203. ), // Singular not used.
  204. /* translators: %s: Number of comments. */
  205. 'mine' => _nx_noop(
  206. 'Mine <span class="count">(%s)</span>',
  207. 'Mine <span class="count">(%s)</span>',
  208. 'comments'
  209. ),
  210. /* translators: %s: Number of comments. */
  211. 'moderated' => _nx_noop(
  212. 'Pending <span class="count">(%s)</span>',
  213. 'Pending <span class="count">(%s)</span>',
  214. 'comments'
  215. ),
  216. /* translators: %s: Number of comments. */
  217. 'approved' => _nx_noop(
  218. 'Approved <span class="count">(%s)</span>',
  219. 'Approved <span class="count">(%s)</span>',
  220. 'comments'
  221. ),
  222. /* translators: %s: Number of comments. */
  223. 'spam' => _nx_noop(
  224. 'Spam <span class="count">(%s)</span>',
  225. 'Spam <span class="count">(%s)</span>',
  226. 'comments'
  227. ),
  228. /* translators: %s: Number of comments. */
  229. 'trash' => _nx_noop(
  230. 'Trash <span class="count">(%s)</span>',
  231. 'Trash <span class="count">(%s)</span>',
  232. 'comments'
  233. ),
  234. );
  235. if ( ! EMPTY_TRASH_DAYS ) {
  236. unset( $stati['trash'] );
  237. }
  238. $link = admin_url( 'edit-comments.php' );
  239. if ( ! empty( $comment_type ) && 'all' !== $comment_type ) {
  240. $link = add_query_arg( 'comment_type', $comment_type, $link );
  241. }
  242. foreach ( $stati as $status => $label ) {
  243. if ( 'mine' === $status ) {
  244. $current_user_id = get_current_user_id();
  245. $num_comments->mine = get_comments(
  246. array(
  247. 'post_id' => $post_id ? $post_id : 0,
  248. 'user_id' => $current_user_id,
  249. 'count' => true,
  250. )
  251. );
  252. $link = add_query_arg( 'user_id', $current_user_id, $link );
  253. } else {
  254. $link = remove_query_arg( 'user_id', $link );
  255. }
  256. if ( ! isset( $num_comments->$status ) ) {
  257. $num_comments->$status = 10;
  258. }
  259. $link = add_query_arg( 'comment_status', $status, $link );
  260. if ( $post_id ) {
  261. $link = add_query_arg( 'p', absint( $post_id ), $link );
  262. }
  263. /*
  264. // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
  265. if ( !empty( $_REQUEST['s'] ) )
  266. $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
  267. */
  268. $status_links[ $status ] = array(
  269. 'url' => esc_url( $link ),
  270. 'label' => sprintf(
  271. translate_nooped_plural( $label, $num_comments->$status ),
  272. sprintf(
  273. '<span class="%s-count">%s</span>',
  274. ( 'moderated' === $status ) ? 'pending' : $status,
  275. number_format_i18n( $num_comments->$status )
  276. )
  277. ),
  278. 'current' => $status === $comment_status,
  279. );
  280. }
  281. /**
  282. * Filters the comment status links.
  283. *
  284. * @since 2.5.0
  285. * @since 5.1.0 The 'Mine' link was added.
  286. *
  287. * @param string[] $status_links An associative array of fully-formed comment status links. Includes 'All', 'Mine',
  288. * 'Pending', 'Approved', 'Spam', and 'Trash'.
  289. */
  290. return apply_filters( 'comment_status_links', $this->get_views_links( $status_links ) );
  291. }
  292. /**
  293. * @global string $comment_status
  294. *
  295. * @return array
  296. */
  297. protected function get_bulk_actions() {
  298. global $comment_status;
  299. $actions = array();
  300. if ( in_array( $comment_status, array( 'all', 'approved' ), true ) ) {
  301. $actions['unapprove'] = __( 'Unapprove' );
  302. }
  303. if ( in_array( $comment_status, array( 'all', 'moderated' ), true ) ) {
  304. $actions['approve'] = __( 'Approve' );
  305. }
  306. if ( in_array( $comment_status, array( 'all', 'moderated', 'approved', 'trash' ), true ) ) {
  307. $actions['spam'] = _x( 'Mark as spam', 'comment' );
  308. }
  309. if ( 'trash' === $comment_status ) {
  310. $actions['untrash'] = __( 'Restore' );
  311. } elseif ( 'spam' === $comment_status ) {
  312. $actions['unspam'] = _x( 'Not spam', 'comment' );
  313. }
  314. if ( in_array( $comment_status, array( 'trash', 'spam' ), true ) || ! EMPTY_TRASH_DAYS ) {
  315. $actions['delete'] = __( 'Delete permanently' );
  316. } else {
  317. $actions['trash'] = __( 'Move to Trash' );
  318. }
  319. return $actions;
  320. }
  321. /**
  322. * @global string $comment_status
  323. * @global string $comment_type
  324. *
  325. * @param string $which
  326. */
  327. protected function extra_tablenav( $which ) {
  328. global $comment_status, $comment_type;
  329. static $has_items;
  330. if ( ! isset( $has_items ) ) {
  331. $has_items = $this->has_items();
  332. }
  333. echo '<div class="alignleft actions">';
  334. if ( 'top' === $which ) {
  335. ob_start();
  336. $this->comment_type_dropdown( $comment_type );
  337. /**
  338. * Fires just before the Filter submit button for comment types.
  339. *
  340. * @since 3.5.0
  341. */
  342. do_action( 'restrict_manage_comments' );
  343. $output = ob_get_clean();
  344. if ( ! empty( $output ) && $this->has_items() ) {
  345. echo $output;
  346. submit_button( __( 'Filter' ), '', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
  347. }
  348. }
  349. if ( ( 'spam' === $comment_status || 'trash' === $comment_status ) && $has_items
  350. && current_user_can( 'moderate_comments' )
  351. ) {
  352. wp_nonce_field( 'bulk-destroy', '_destroy_nonce' );
  353. $title = ( 'spam' === $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' );
  354. submit_button( $title, 'apply', 'delete_all', false );
  355. }
  356. /**
  357. * Fires after the Filter submit button for comment types.
  358. *
  359. * @since 2.5.0
  360. * @since 5.6.0 The `$which` parameter was added.
  361. *
  362. * @param string $comment_status The comment status name. Default 'All'.
  363. * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
  364. */
  365. do_action( 'manage_comments_nav', $comment_status, $which );
  366. echo '</div>';
  367. }
  368. /**
  369. * @return string|false
  370. */
  371. public function current_action() {
  372. if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) ) {
  373. return 'delete_all';
  374. }
  375. return parent::current_action();
  376. }
  377. /**
  378. * @global int $post_id
  379. *
  380. * @return array
  381. */
  382. public function get_columns() {
  383. global $post_id;
  384. $columns = array();
  385. if ( $this->checkbox ) {
  386. $columns['cb'] = '<input type="checkbox" />';
  387. }
  388. $columns['author'] = __( 'Author' );
  389. $columns['comment'] = _x( 'Comment', 'column name' );
  390. if ( ! $post_id ) {
  391. /* translators: Column name or table row header. */
  392. $columns['response'] = __( 'In response to' );
  393. }
  394. $columns['date'] = _x( 'Submitted on', 'column name' );
  395. return $columns;
  396. }
  397. /**
  398. * Displays a comment type drop-down for filtering on the Comments list table.
  399. *
  400. * @since 5.5.0
  401. * @since 5.6.0 Renamed from `comment_status_dropdown()` to `comment_type_dropdown()`.
  402. *
  403. * @param string $comment_type The current comment type slug.
  404. */
  405. protected function comment_type_dropdown( $comment_type ) {
  406. /**
  407. * Filters the comment types shown in the drop-down menu on the Comments list table.
  408. *
  409. * @since 2.7.0
  410. *
  411. * @param string[] $comment_types Array of comment type labels keyed by their name.
  412. */
  413. $comment_types = apply_filters(
  414. 'admin_comment_types_dropdown',
  415. array(
  416. 'comment' => __( 'Comments' ),
  417. 'pings' => __( 'Pings' ),
  418. )
  419. );
  420. if ( $comment_types && is_array( $comment_types ) ) {
  421. printf( '<label class="screen-reader-text" for="filter-by-comment-type">%s</label>', __( 'Filter by comment type' ) );
  422. echo '<select id="filter-by-comment-type" name="comment_type">';
  423. printf( "\t<option value=''>%s</option>", __( 'All comment types' ) );
  424. foreach ( $comment_types as $type => $label ) {
  425. if ( get_comments(
  426. array(
  427. 'number' => 1,
  428. 'type' => $type,
  429. )
  430. ) ) {
  431. printf(
  432. "\t<option value='%s'%s>%s</option>\n",
  433. esc_attr( $type ),
  434. selected( $comment_type, $type, false ),
  435. esc_html( $label )
  436. );
  437. }
  438. }
  439. echo '</select>';
  440. }
  441. }
  442. /**
  443. * @return array
  444. */
  445. protected function get_sortable_columns() {
  446. return array(
  447. 'author' => 'comment_author',
  448. 'response' => 'comment_post_ID',
  449. 'date' => 'comment_date',
  450. );
  451. }
  452. /**
  453. * Gets the name of the default primary column.
  454. *
  455. * @since 4.3.0
  456. *
  457. * @return string Name of the default primary column, in this case, 'comment'.
  458. */
  459. protected function get_default_primary_column_name() {
  460. return 'comment';
  461. }
  462. /**
  463. * Displays the comments table.
  464. *
  465. * Overrides the parent display() method to render extra comments.
  466. *
  467. * @since 3.1.0
  468. */
  469. public function display() {
  470. wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
  471. static $has_items;
  472. if ( ! isset( $has_items ) ) {
  473. $has_items = $this->has_items();
  474. if ( $has_items ) {
  475. $this->display_tablenav( 'top' );
  476. }
  477. }
  478. $this->screen->render_screen_reader_content( 'heading_list' );
  479. ?>
  480. <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
  481. <thead>
  482. <tr>
  483. <?php $this->print_column_headers(); ?>
  484. </tr>
  485. </thead>
  486. <tbody id="the-comment-list" data-wp-lists="list:comment">
  487. <?php $this->display_rows_or_placeholder(); ?>
  488. </tbody>
  489. <tbody id="the-extra-comment-list" data-wp-lists="list:comment" style="display: none;">
  490. <?php
  491. /*
  492. * Back up the items to restore after printing the extra items markup.
  493. * The extra items may be empty, which will prevent the table nav from displaying later.
  494. */
  495. $items = $this->items;
  496. $this->items = $this->extra_items;
  497. $this->display_rows_or_placeholder();
  498. $this->items = $items;
  499. ?>
  500. </tbody>
  501. <tfoot>
  502. <tr>
  503. <?php $this->print_column_headers( false ); ?>
  504. </tr>
  505. </tfoot>
  506. </table>
  507. <?php
  508. $this->display_tablenav( 'bottom' );
  509. }
  510. /**
  511. * @global WP_Post $post Global post object.
  512. * @global WP_Comment $comment Global comment object.
  513. *
  514. * @param WP_Comment $item
  515. */
  516. public function single_row( $item ) {
  517. global $post, $comment;
  518. $comment = $item;
  519. $the_comment_class = wp_get_comment_status( $comment );
  520. if ( ! $the_comment_class ) {
  521. $the_comment_class = '';
  522. }
  523. $the_comment_class = implode( ' ', get_comment_class( $the_comment_class, $comment, $comment->comment_post_ID ) );
  524. if ( $comment->comment_post_ID > 0 ) {
  525. $post = get_post( $comment->comment_post_ID );
  526. }
  527. $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
  528. echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
  529. $this->single_row_columns( $comment );
  530. echo "</tr>\n";
  531. unset( $GLOBALS['post'], $GLOBALS['comment'] );
  532. }
  533. /**
  534. * Generates and displays row actions links.
  535. *
  536. * @since 4.3.0
  537. * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
  538. *
  539. * @global string $comment_status Status for the current listed comments.
  540. *
  541. * @param WP_Comment $item The comment object.
  542. * @param string $column_name Current column name.
  543. * @param string $primary Primary column name.
  544. * @return string Row actions output for comments. An empty string
  545. * if the current column is not the primary column,
  546. * or if the current user cannot edit the comment.
  547. */
  548. protected function handle_row_actions( $item, $column_name, $primary ) {
  549. global $comment_status;
  550. if ( $primary !== $column_name ) {
  551. return '';
  552. }
  553. if ( ! $this->user_can ) {
  554. return '';
  555. }
  556. // Restores the more descriptive, specific name for use within this method.
  557. $comment = $item;
  558. $the_comment_status = wp_get_comment_status( $comment );
  559. $output = '';
  560. $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-comment_$comment->comment_ID" ) );
  561. $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "approve-comment_$comment->comment_ID" ) );
  562. $url = "comment.php?c=$comment->comment_ID";
  563. $approve_url = esc_url( $url . "&action=approvecomment&$approve_nonce" );
  564. $unapprove_url = esc_url( $url . "&action=unapprovecomment&$approve_nonce" );
  565. $spam_url = esc_url( $url . "&action=spamcomment&$del_nonce" );
  566. $unspam_url = esc_url( $url . "&action=unspamcomment&$del_nonce" );
  567. $trash_url = esc_url( $url . "&action=trashcomment&$del_nonce" );
  568. $untrash_url = esc_url( $url . "&action=untrashcomment&$del_nonce" );
  569. $delete_url = esc_url( $url . "&action=deletecomment&$del_nonce" );
  570. // Preorder it: Approve | Reply | Quick Edit | Edit | Spam | Trash.
  571. $actions = array(
  572. 'approve' => '',
  573. 'unapprove' => '',
  574. 'reply' => '',
  575. 'quickedit' => '',
  576. 'edit' => '',
  577. 'spam' => '',
  578. 'unspam' => '',
  579. 'trash' => '',
  580. 'untrash' => '',
  581. 'delete' => '',
  582. );
  583. // Not looking at all comments.
  584. if ( $comment_status && 'all' !== $comment_status ) {
  585. if ( 'approved' === $the_comment_status ) {
  586. $actions['unapprove'] = sprintf(
  587. '<a href="%s" data-wp-lists="%s" class="vim-u vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  588. $unapprove_url,
  589. "delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=unapproved",
  590. esc_attr__( 'Unapprove this comment' ),
  591. __( 'Unapprove' )
  592. );
  593. } elseif ( 'unapproved' === $the_comment_status ) {
  594. $actions['approve'] = sprintf(
  595. '<a href="%s" data-wp-lists="%s" class="vim-a vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  596. $approve_url,
  597. "delete:the-comment-list:comment-{$comment->comment_ID}:e7e7d3:action=dim-comment&amp;new=approved",
  598. esc_attr__( 'Approve this comment' ),
  599. __( 'Approve' )
  600. );
  601. }
  602. } else {
  603. $actions['approve'] = sprintf(
  604. '<a href="%s" data-wp-lists="%s" class="vim-a aria-button-if-js" aria-label="%s">%s</a>',
  605. $approve_url,
  606. "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=approved",
  607. esc_attr__( 'Approve this comment' ),
  608. __( 'Approve' )
  609. );
  610. $actions['unapprove'] = sprintf(
  611. '<a href="%s" data-wp-lists="%s" class="vim-u aria-button-if-js" aria-label="%s">%s</a>',
  612. $unapprove_url,
  613. "dim:the-comment-list:comment-{$comment->comment_ID}:unapproved:e7e7d3:e7e7d3:new=unapproved",
  614. esc_attr__( 'Unapprove this comment' ),
  615. __( 'Unapprove' )
  616. );
  617. }
  618. if ( 'spam' !== $the_comment_status ) {
  619. $actions['spam'] = sprintf(
  620. '<a href="%s" data-wp-lists="%s" class="vim-s vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  621. $spam_url,
  622. "delete:the-comment-list:comment-{$comment->comment_ID}::spam=1",
  623. esc_attr__( 'Mark this comment as spam' ),
  624. /* translators: "Mark as spam" link. */
  625. _x( 'Spam', 'verb' )
  626. );
  627. } elseif ( 'spam' === $the_comment_status ) {
  628. $actions['unspam'] = sprintf(
  629. '<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  630. $unspam_url,
  631. "delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:unspam=1",
  632. esc_attr__( 'Restore this comment from the spam' ),
  633. _x( 'Not Spam', 'comment' )
  634. );
  635. }
  636. if ( 'trash' === $the_comment_status ) {
  637. $actions['untrash'] = sprintf(
  638. '<a href="%s" data-wp-lists="%s" class="vim-z vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  639. $untrash_url,
  640. "delete:the-comment-list:comment-{$comment->comment_ID}:66cc66:untrash=1",
  641. esc_attr__( 'Restore this comment from the Trash' ),
  642. __( 'Restore' )
  643. );
  644. }
  645. if ( 'spam' === $the_comment_status || 'trash' === $the_comment_status || ! EMPTY_TRASH_DAYS ) {
  646. $actions['delete'] = sprintf(
  647. '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  648. $delete_url,
  649. "delete:the-comment-list:comment-{$comment->comment_ID}::delete=1",
  650. esc_attr__( 'Delete this comment permanently' ),
  651. __( 'Delete Permanently' )
  652. );
  653. } else {
  654. $actions['trash'] = sprintf(
  655. '<a href="%s" data-wp-lists="%s" class="delete vim-d vim-destructive aria-button-if-js" aria-label="%s">%s</a>',
  656. $trash_url,
  657. "delete:the-comment-list:comment-{$comment->comment_ID}::trash=1",
  658. esc_attr__( 'Move this comment to the Trash' ),
  659. _x( 'Trash', 'verb' )
  660. );
  661. }
  662. if ( 'spam' !== $the_comment_status && 'trash' !== $the_comment_status ) {
  663. $actions['edit'] = sprintf(
  664. '<a href="%s" aria-label="%s">%s</a>',
  665. "comment.php?action=editcomment&amp;c={$comment->comment_ID}",
  666. esc_attr__( 'Edit this comment' ),
  667. __( 'Edit' )
  668. );
  669. $format = '<button type="button" data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s button-link" aria-expanded="false" aria-label="%s">%s</button>';
  670. $actions['quickedit'] = sprintf(
  671. $format,
  672. $comment->comment_ID,
  673. $comment->comment_post_ID,
  674. 'edit',
  675. 'vim-q comment-inline',
  676. esc_attr__( 'Quick edit this comment inline' ),
  677. __( 'Quick&nbsp;Edit' )
  678. );
  679. $actions['reply'] = sprintf(
  680. $format,
  681. $comment->comment_ID,
  682. $comment->comment_post_ID,
  683. 'replyto',
  684. 'vim-r comment-inline',
  685. esc_attr__( 'Reply to this comment' ),
  686. __( 'Reply' )
  687. );
  688. }
  689. /** This filter is documented in wp-admin/includes/dashboard.php */
  690. $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
  691. $always_visible = false;
  692. $mode = get_user_setting( 'posts_list_mode', 'list' );
  693. if ( 'excerpt' === $mode ) {
  694. $always_visible = true;
  695. }
  696. $output .= '<div class="' . ( $always_visible ? 'row-actions visible' : 'row-actions' ) . '">';
  697. $i = 0;
  698. foreach ( $actions as $action => $link ) {
  699. ++$i;
  700. if ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i )
  701. || 1 === $i
  702. ) {
  703. $separator = '';
  704. } else {
  705. $separator = ' | ';
  706. }
  707. // Reply and quickedit need a hide-if-no-js span when not added with Ajax.
  708. if ( ( 'reply' === $action || 'quickedit' === $action ) && ! wp_doing_ajax() ) {
  709. $action .= ' hide-if-no-js';
  710. } elseif ( ( 'untrash' === $action && 'trash' === $the_comment_status )
  711. || ( 'unspam' === $action && 'spam' === $the_comment_status )
  712. ) {
  713. if ( '1' === get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) ) {
  714. $action .= ' approve';
  715. } else {
  716. $action .= ' unapprove';
  717. }
  718. }
  719. $output .= "<span class='$action'>{$separator}{$link}</span>";
  720. }
  721. $output .= '</div>';
  722. $output .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
  723. return $output;
  724. }
  725. /**
  726. * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
  727. *
  728. * @param WP_Comment $item The comment object.
  729. */
  730. public function column_cb( $item ) {
  731. // Restores the more descriptive, specific name for use within this method.
  732. $comment = $item;
  733. if ( $this->user_can ) {
  734. ?>
  735. <label class="screen-reader-text" for="cb-select-<?php echo $comment->comment_ID; ?>"><?php _e( 'Select comment' ); ?></label>
  736. <input id="cb-select-<?php echo $comment->comment_ID; ?>" type="checkbox" name="delete_comments[]" value="<?php echo $comment->comment_ID; ?>" />
  737. <?php
  738. }
  739. }
  740. /**
  741. * @param WP_Comment $comment The comment object.
  742. */
  743. public function column_comment( $comment ) {
  744. echo '<div class="comment-author">';
  745. $this->column_author( $comment );
  746. echo '</div>';
  747. if ( $comment->comment_parent ) {
  748. $parent = get_comment( $comment->comment_parent );
  749. if ( $parent ) {
  750. $parent_link = esc_url( get_comment_link( $parent ) );
  751. $name = get_comment_author( $parent );
  752. printf(
  753. /* translators: %s: Comment link. */
  754. __( 'In reply to %s.' ),
  755. '<a href="' . $parent_link . '">' . $name . '</a>'
  756. );
  757. }
  758. }
  759. comment_text( $comment );
  760. if ( $this->user_can ) {
  761. /** This filter is documented in wp-admin/includes/comment.php */
  762. $comment_content = apply_filters( 'comment_edit_pre', $comment->comment_content );
  763. ?>
  764. <div id="inline-<?php echo $comment->comment_ID; ?>" class="hidden">
  765. <textarea class="comment" rows="1" cols="1"><?php echo esc_textarea( $comment_content ); ?></textarea>
  766. <div class="author-email"><?php echo esc_html( $comment->comment_author_email ); ?></div>
  767. <div class="author"><?php echo esc_html( $comment->comment_author ); ?></div>
  768. <div class="author-url"><?php echo esc_url( $comment->comment_author_url ); ?></div>
  769. <div class="comment_status"><?php echo $comment->comment_approved; ?></div>
  770. </div>
  771. <?php
  772. }
  773. }
  774. /**
  775. * @global string $comment_status
  776. *
  777. * @param WP_Comment $comment The comment object.
  778. */
  779. public function column_author( $comment ) {
  780. global $comment_status;
  781. $author_url = get_comment_author_url( $comment );
  782. $author_url_display = untrailingslashit( preg_replace( '|^http(s)?://(www\.)?|i', '', $author_url ) );
  783. if ( strlen( $author_url_display ) > 50 ) {
  784. $author_url_display = wp_html_excerpt( $author_url_display, 49, '&hellip;' );
  785. }
  786. echo '<strong>';
  787. comment_author( $comment );
  788. echo '</strong><br />';
  789. if ( ! empty( $author_url_display ) ) {
  790. // Print link to author URL, and disallow referrer information (without using target="_blank").
  791. printf(
  792. '<a href="%s" rel="noopener noreferrer">%s</a><br />',
  793. esc_url( $author_url ),
  794. esc_html( $author_url_display )
  795. );
  796. }
  797. if ( $this->user_can ) {
  798. if ( ! empty( $comment->comment_author_email ) ) {
  799. /** This filter is documented in wp-includes/comment-template.php */
  800. $email = apply_filters( 'comment_email', $comment->comment_author_email, $comment );
  801. if ( ! empty( $email ) && '@' !== $email ) {
  802. printf( '<a href="%1$s">%2$s</a><br />', esc_url( 'mailto:' . $email ), esc_html( $email ) );
  803. }
  804. }
  805. $author_ip = get_comment_author_IP( $comment );
  806. if ( $author_ip ) {
  807. $author_ip_url = add_query_arg(
  808. array(
  809. 's' => $author_ip,
  810. 'mode' => 'detail',
  811. ),
  812. admin_url( 'edit-comments.php' )
  813. );
  814. if ( 'spam' === $comment_status ) {
  815. $author_ip_url = add_query_arg( 'comment_status', 'spam', $author_ip_url );
  816. }
  817. printf( '<a href="%1$s">%2$s</a>', esc_url( $author_ip_url ), esc_html( $author_ip ) );
  818. }
  819. }
  820. }
  821. /**
  822. * @param WP_Comment $comment The comment object.
  823. */
  824. public function column_date( $comment ) {
  825. $submitted = sprintf(
  826. /* translators: 1: Comment date, 2: Comment time. */
  827. __( '%1$s at %2$s' ),
  828. /* translators: Comment date format. See https://www.php.net/manual/datetime.format.php */
  829. get_comment_date( __( 'Y/m/d' ), $comment ),
  830. /* translators: Comment time format. See https://www.php.net/manual/datetime.format.php */
  831. get_comment_date( __( 'g:i a' ), $comment )
  832. );
  833. echo '<div class="submitted-on">';
  834. if ( 'approved' === wp_get_comment_status( $comment ) && ! empty( $comment->comment_post_ID ) ) {
  835. printf(
  836. '<a href="%s">%s</a>',
  837. esc_url( get_comment_link( $comment ) ),
  838. $submitted
  839. );
  840. } else {
  841. echo $submitted;
  842. }
  843. echo '</div>';
  844. }
  845. /**
  846. * @param WP_Comment $comment The comment object.
  847. */
  848. public function column_response( $comment ) {
  849. $post = get_post();
  850. if ( ! $post ) {
  851. return;
  852. }
  853. if ( isset( $this->pending_count[ $post->ID ] ) ) {
  854. $pending_comments = $this->pending_count[ $post->ID ];
  855. } else {
  856. $_pending_count_temp = get_pending_comments_num( array( $post->ID ) );
  857. $pending_comments = $_pending_count_temp[ $post->ID ];
  858. $this->pending_count[ $post->ID ] = $pending_comments;
  859. }
  860. if ( current_user_can( 'edit_post', $post->ID ) ) {
  861. $post_link = "<a href='" . get_edit_post_link( $post->ID ) . "' class='comments-edit-item-link'>";
  862. $post_link .= esc_html( get_the_title( $post->ID ) ) . '</a>';
  863. } else {
  864. $post_link = esc_html( get_the_title( $post->ID ) );
  865. }
  866. echo '<div class="response-links">';
  867. if ( 'attachment' === $post->post_type ) {
  868. $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true );
  869. if ( $thumb ) {
  870. echo $thumb;
  871. }
  872. }
  873. echo $post_link;
  874. $post_type_object = get_post_type_object( $post->post_type );
  875. echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';
  876. echo '<span class="post-com-count-wrapper post-com-count-', $post->ID, '">';
  877. $this->comments_bubble( $post->ID, $pending_comments );
  878. echo '</span> ';
  879. echo '</div>';
  880. }
  881. /**
  882. * @since 5.9.0 Renamed `$comment` to `$item` to match parent class for PHP 8 named parameter support.
  883. *
  884. * @param WP_Comment $item The comment object.
  885. * @param string $column_name The custom column's name.
  886. */
  887. public function column_default( $item, $column_name ) {
  888. /**
  889. * Fires when the default column output is displayed for a single row.
  890. *
  891. * @since 2.8.0
  892. *
  893. * @param string $column_name The custom column's name.
  894. * @param string $comment_id The comment ID as a numeric string.
  895. */
  896. do_action( 'manage_comments_custom_column', $column_name, $item->comment_ID );
  897. }
  898. }