3 * Comments and Post Comments List Table classes.
6 * @subpackage List_Table
11 * Comments List Table class.
14 * @subpackage List_Table
18 class WP_Comments_List_Table extends WP_List_Table {
20 public $checkbox = true;
22 public $pending_count = array();
34 * @see WP_List_Table::__construct() for more information on default arguments.
36 * @global int $post_id
38 * @param array $args An associative array of arguments.
40 public function __construct( $args = array() ) {
43 $post_id = isset( $_REQUEST['p'] ) ? absint( $_REQUEST['p'] ) : 0;
45 if ( get_option('show_avatars') )
46 add_filter( 'comment_author', 'floated_admin_avatar' );
48 parent::__construct( array(
49 'plural' => 'comments',
50 'singular' => 'comment',
52 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
59 public function ajax_user_can() {
60 return current_user_can('edit_posts');
65 * @global int $post_id
66 * @global string $comment_status
67 * @global string $search
68 * @global string $comment_type
70 public function prepare_items() {
71 global $post_id, $comment_status, $search, $comment_type;
73 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
74 if ( !in_array( $comment_status, array( 'all', 'moderated', 'approved', 'spam', 'trash' ) ) )
75 $comment_status = 'all';
77 $comment_type = !empty( $_REQUEST['comment_type'] ) ? $_REQUEST['comment_type'] : '';
79 $search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : '';
81 $post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_key( $_REQUEST['post_type'] ) : '';
83 $user_id = ( isset( $_REQUEST['user_id'] ) ) ? $_REQUEST['user_id'] : '';
85 $orderby = ( isset( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : '';
86 $order = ( isset( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : '';
88 $comments_per_page = $this->get_per_page( $comment_status );
90 $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
92 if ( isset( $_REQUEST['number'] ) ) {
93 $number = (int) $_REQUEST['number'];
96 $number = $comments_per_page + min( 8, $comments_per_page ); // Grab a few extra
99 $page = $this->get_pagenum();
101 if ( isset( $_REQUEST['start'] ) ) {
102 $start = $_REQUEST['start'];
104 $start = ( $page - 1 ) * $comments_per_page;
107 if ( $doing_ajax && isset( $_REQUEST['offset'] ) ) {
108 $start += $_REQUEST['offset'];
112 'moderated' => 'hold',
113 'approved' => 'approve',
118 'status' => isset( $status_map[$comment_status] ) ? $status_map[$comment_status] : $comment_status,
120 'user_id' => $user_id,
123 'post_id' => $post_id,
124 'type' => $comment_type,
125 'orderby' => $orderby,
127 'post_type' => $post_type,
130 $_comments = get_comments( $args );
131 if ( is_array( $_comments ) ) {
132 update_comment_cache( $_comments );
134 $this->items = array_slice( $_comments, 0, $comments_per_page );
135 $this->extra_items = array_slice( $_comments, $comments_per_page );
137 $_comment_post_ids = array_unique( wp_list_pluck( $_comments, 'comment_post_ID' ) );
139 $this->pending_count = get_pending_comments_num( $_comment_post_ids );
142 $total_comments = get_comments( array_merge( $args, array(
148 $this->set_pagination_args( array(
149 'total_items' => $total_comments,
150 'per_page' => $comments_per_page,
156 * @param string $comment_status
159 public function get_per_page( $comment_status = 'all' ) {
160 $comments_per_page = $this->get_items_per_page( 'edit_comments_per_page' );
162 * Filter the number of comments listed per page in the comments list table.
166 * @param int $comments_per_page The number of comments to list per page.
167 * @param string $comment_status The comment status name. Default 'All'.
169 return apply_filters( 'comments_per_page', $comments_per_page, $comment_status );
174 * @global string $comment_status
176 public function no_items() {
177 global $comment_status;
179 if ( 'moderated' == $comment_status )
180 _e( 'No comments awaiting moderation.' );
182 _e( 'No comments found.' );
187 * @global int $post_id
188 * @global string $comment_status
189 * @global string $comment_type
191 protected function get_views() {
192 global $post_id, $comment_status, $comment_type;
194 $status_links = array();
195 $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
196 //, number_format_i18n($num_comments->moderated) ), "<span class='comment-count'>" . number_format_i18n($num_comments->moderated) . "</span>"),
197 //, number_format_i18n($num_comments->spam) ), "<span class='spam-comment-count'>" . number_format_i18n($num_comments->spam) . "</span>")
199 'all' => _nx_noop('All', 'All', 'comments'), // singular not used
200 'moderated' => _n_noop('Pending <span class="count">(<span class="pending-count">%s</span>)</span>', 'Pending <span class="count">(<span class="pending-count">%s</span>)</span>'),
201 'approved' => _n_noop('Approved', 'Approved'), // singular not used
202 'spam' => _n_noop('Spam <span class="count">(<span class="spam-count">%s</span>)</span>', 'Spam <span class="count">(<span class="spam-count">%s</span>)</span>'),
203 'trash' => _n_noop('Trash <span class="count">(<span class="trash-count">%s</span>)</span>', 'Trash <span class="count">(<span class="trash-count">%s</span>)</span>')
206 if ( !EMPTY_TRASH_DAYS )
207 unset($stati['trash']);
209 $link = 'edit-comments.php';
210 if ( !empty($comment_type) && 'all' != $comment_type )
211 $link = add_query_arg( 'comment_type', $comment_type, $link );
213 foreach ( $stati as $status => $label ) {
214 $class = ( $status == $comment_status ) ? ' class="current"' : '';
216 if ( !isset( $num_comments->$status ) )
217 $num_comments->$status = 10;
218 $link = add_query_arg( 'comment_status', $status, $link );
220 $link = add_query_arg( 'p', absint( $post_id ), $link );
222 // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
223 if ( !empty( $_REQUEST['s'] ) )
224 $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
226 $status_links[$status] = "<a href='$link'$class>" . sprintf(
227 translate_nooped_plural( $label, $num_comments->$status ),
228 number_format_i18n( $num_comments->$status )
233 * Filter the comment status links.
237 * @param array $status_links An array of fully-formed status links. Default 'All'.
238 * Accepts 'All', 'Pending', 'Approved', 'Spam', and 'Trash'.
240 return apply_filters( 'comment_status_links', $status_links );
245 * @global string $comment_status
249 protected function get_bulk_actions() {
250 global $comment_status;
253 if ( in_array( $comment_status, array( 'all', 'approved' ) ) )
254 $actions['unapprove'] = __( 'Unapprove' );
255 if ( in_array( $comment_status, array( 'all', 'moderated' ) ) )
256 $actions['approve'] = __( 'Approve' );
257 if ( in_array( $comment_status, array( 'all', 'moderated', 'approved', 'trash' ) ) )
258 $actions['spam'] = _x( 'Mark as Spam', 'comment' );
260 if ( 'trash' == $comment_status )
261 $actions['untrash'] = __( 'Restore' );
262 elseif ( 'spam' == $comment_status )
263 $actions['unspam'] = _x( 'Not Spam', 'comment' );
265 if ( in_array( $comment_status, array( 'trash', 'spam' ) ) || !EMPTY_TRASH_DAYS )
266 $actions['delete'] = __( 'Delete Permanently' );
268 $actions['trash'] = __( 'Move to Trash' );
275 * @global string $comment_status
276 * @global string $comment_type
278 * @param string $which
280 protected function extra_tablenav( $which ) {
281 global $comment_status, $comment_type;
283 <div class="alignleft actions">
285 if ( 'top' == $which ) {
287 <label class="screen-reader-text" for="filter-by-comment-type"><?php _e( 'Filter by comment type' ); ?></label>
288 <select id="filter-by-comment-type" name="comment_type">
289 <option value=""><?php _e( 'All comment types' ); ?></option>
292 * Filter the comment types dropdown menu.
296 * @param array $comment_types An array of comment types. Accepts 'Comments', 'Pings'.
298 $comment_types = apply_filters( 'admin_comment_types_dropdown', array(
299 'comment' => __( 'Comments' ),
300 'pings' => __( 'Pings' ),
303 foreach ( $comment_types as $type => $label )
304 echo "\t" . '<option value="' . esc_attr( $type ) . '"' . selected( $comment_type, $type, false ) . ">$label</option>\n";
309 * Fires just before the Filter submit button for comment types.
313 do_action( 'restrict_manage_comments' );
314 submit_button( __( 'Filter' ), 'button', 'filter_action', false, array( 'id' => 'post-query-submit' ) );
317 if ( ( 'spam' == $comment_status || 'trash' == $comment_status ) && current_user_can( 'moderate_comments' ) ) {
318 wp_nonce_field( 'bulk-destroy', '_destroy_nonce' );
319 $title = ( 'spam' == $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' );
320 submit_button( $title, 'apply', 'delete_all', false );
323 * Fires after the Filter submit button for comment types.
327 * @param string $comment_status The comment status name. Default 'All'.
329 do_action( 'manage_comments_nav', $comment_status );
334 * @return string|false
336 public function current_action() {
337 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
340 return parent::current_action();
345 * @global int $post_id
349 public function get_columns() {
354 if ( $this->checkbox )
355 $columns['cb'] = '<input type="checkbox" />';
357 $columns['author'] = __( 'Author' );
358 $columns['comment'] = _x( 'Comment', 'column name' );
361 /* translators: column name or table row header */
362 $columns['response'] = __( 'In Response To' );
372 protected function get_sortable_columns() {
374 'author' => 'comment_author',
375 'response' => 'comment_post_ID'
380 * Get the name of the default primary column.
385 * @return string Name of the default primary column, in this case, 'comment'.
387 protected function get_default_primary_column_name() {
394 public function display() {
395 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
397 $this->display_tablenav( 'top' );
400 <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>">
403 <?php $this->print_column_headers(); ?>
407 <tbody id="the-comment-list" data-wp-lists="list:comment">
408 <?php $this->display_rows_or_placeholder(); ?>
411 <tbody id="the-extra-comment-list" data-wp-lists="list:comment" style="display: none;">
412 <?php $this->items = $this->extra_items; $this->display_rows(); ?>
417 <?php $this->print_column_headers( false ); ?>
424 $this->display_tablenav( 'bottom' );
429 * @global WP_Post $post
430 * @global object $comment
432 * @param object $a_comment
434 public function single_row( $a_comment ) {
435 global $post, $comment;
437 $comment = $a_comment;
438 $the_comment_class = wp_get_comment_status( $comment->comment_ID );
439 if ( ! $the_comment_class ) {
440 $the_comment_class = '';
442 $the_comment_class = join( ' ', get_comment_class( $the_comment_class, $comment->comment_ID, $comment->comment_post_ID ) );
444 $post = get_post( $comment->comment_post_ID );
446 $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
448 echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
449 $this->single_row_columns( $comment );
454 * Generate and display row actions links.
459 * @param object $comment Comment being acted upon.
460 * @param string $column_name Current column name.
461 * @param string $primary Primary column name.
462 * @return string|void Comment row actions output.
464 protected function handle_row_actions( $comment, $column_name, $primary ) {
465 global $comment_status;
467 if ( $primary !== $column_name ) {
471 if ( ! $this->user_can ) {
475 $the_comment_status = wp_get_comment_status( $comment->comment_ID );
479 $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-comment_$comment->comment_ID" ) );
480 $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "approve-comment_$comment->comment_ID" ) );
482 $url = "comment.php?c=$comment->comment_ID";
484 $approve_url = esc_url( $url . "&action=approvecomment&$approve_nonce" );
485 $unapprove_url = esc_url( $url . "&action=unapprovecomment&$approve_nonce" );
486 $spam_url = esc_url( $url . "&action=spamcomment&$del_nonce" );
487 $unspam_url = esc_url( $url . "&action=unspamcomment&$del_nonce" );
488 $trash_url = esc_url( $url . "&action=trashcomment&$del_nonce" );
489 $untrash_url = esc_url( $url . "&action=untrashcomment&$del_nonce" );
490 $delete_url = esc_url( $url . "&action=deletecomment&$del_nonce" );
492 // Preorder it: Approve | Reply | Quick Edit | Edit | Spam | Trash.
494 'approve' => '', 'unapprove' => '',
498 'spam' => '', 'unspam' => '',
499 'trash' => '', 'untrash' => '', 'delete' => ''
502 // Not looking at all comments.
503 if ( $comment_status && 'all' != $comment_status ) {
504 if ( 'approved' == $the_comment_status ) {
505 $actions['unapprove'] = "<a href='$unapprove_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID:e7e7d3:action=dim-comment&new=unapproved' class='vim-u vim-destructive' title='" . esc_attr__( 'Unapprove this comment' ) . "'>" . __( 'Unapprove' ) . '</a>';
506 } elseif ( 'unapproved' == $the_comment_status ) {
507 $actions['approve'] = "<a href='$approve_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID:e7e7d3:action=dim-comment&new=approved' class='vim-a vim-destructive' title='" . esc_attr__( 'Approve this comment' ) . "'>" . __( 'Approve' ) . '</a>';
510 $actions['approve'] = "<a href='$approve_url' data-wp-lists='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3:new=approved' class='vim-a' title='" . esc_attr__( 'Approve this comment' ) . "'>" . __( 'Approve' ) . '</a>';
511 $actions['unapprove'] = "<a href='$unapprove_url' data-wp-lists='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3:new=unapproved' class='vim-u' title='" . esc_attr__( 'Unapprove this comment' ) . "'>" . __( 'Unapprove' ) . '</a>';
514 if ( 'spam' != $the_comment_status ) {
515 $actions['spam'] = "<a href='$spam_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID::spam=1' class='vim-s vim-destructive' title='" . esc_attr__( 'Mark this comment as spam' ) . "'>" . /* translators: mark as spam link */ _x( 'Spam', 'verb' ) . '</a>';
516 } elseif ( 'spam' == $the_comment_status ) {
517 $actions['unspam'] = "<a href='$unspam_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID:66cc66:unspam=1' class='vim-z vim-destructive'>" . _x( 'Not Spam', 'comment' ) . '</a>';
520 if ( 'trash' == $the_comment_status ) {
521 $actions['untrash'] = "<a href='$untrash_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID:66cc66:untrash=1' class='vim-z vim-destructive'>" . __( 'Restore' ) . '</a>';
524 if ( 'spam' == $the_comment_status || 'trash' == $the_comment_status || !EMPTY_TRASH_DAYS ) {
525 $actions['delete'] = "<a href='$delete_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID::delete=1' class='delete vim-d vim-destructive'>" . __( 'Delete Permanently' ) . '</a>';
527 $actions['trash'] = "<a href='$trash_url' data-wp-lists='delete:the-comment-list:comment-$comment->comment_ID::trash=1' class='delete vim-d vim-destructive' title='" . esc_attr__( 'Move this comment to the trash' ) . "'>" . _x( 'Trash', 'verb' ) . '</a>';
530 if ( 'spam' != $the_comment_status && 'trash' != $the_comment_status ) {
531 $actions['edit'] = "<a href='comment.php?action=editcomment&c={$comment->comment_ID}' title='" . esc_attr__( 'Edit comment' ) . "'>". __( 'Edit' ) . '</a>';
533 $format = '<a data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s" title="%s" href="#">%s</a>';
535 $actions['quickedit'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'edit', 'vim-q comment-inline',esc_attr__( 'Edit this item inline' ), __( 'Quick Edit' ) );
537 $actions['reply'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'replyto', 'vim-r comment-inline', esc_attr__( 'Reply to this comment' ), __( 'Reply' ) );
540 /** This filter is documented in wp-admin/includes/dashboard.php */
541 $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
544 $out .= '<div class="row-actions">';
545 foreach ( $actions as $action => $link ) {
547 ( ( ( 'approve' == $action || 'unapprove' == $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | ';
549 // Reply and quickedit need a hide-if-no-js span when not added with ajax
550 if ( ( 'reply' == $action || 'quickedit' == $action ) && ! defined('DOING_AJAX') )
551 $action .= ' hide-if-no-js';
552 elseif ( ( $action == 'untrash' && $the_comment_status == 'trash' ) || ( $action == 'unspam' && $the_comment_status == 'spam' ) ) {
553 if ( '1' == get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) )
554 $action .= ' approve';
556 $action .= ' unapprove';
559 $out .= "<span class='$action'>$sep$link</span>";
563 $out .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
570 * @param object $comment
572 public function column_cb( $comment ) {
573 if ( $this->user_can ) { ?>
574 <label class="screen-reader-text" for="cb-select-<?php echo $comment->comment_ID; ?>"><?php _e( 'Select comment' ); ?></label>
575 <input id="cb-select-<?php echo $comment->comment_ID; ?>" type="checkbox" name="delete_comments[]" value="<?php echo $comment->comment_ID; ?>" />
581 * @param object $comment
583 public function column_comment( $comment ) {
584 $comment_url = esc_url( get_comment_link( $comment->comment_ID ) );
586 echo '<div class="comment-author">';
587 $this->column_author( $comment );
590 echo '<div class="submitted-on">';
591 /* translators: 2: comment date, 3: comment time */
592 printf( __( 'Submitted on <a href="%1$s">%2$s at %3$s</a>' ), $comment_url,
593 /* translators: comment date format. See http://php.net/date */
594 get_comment_date( __( 'Y/m/d' ) ),
595 get_comment_date( get_option( 'time_format' ) )
598 if ( $comment->comment_parent ) {
599 $parent = get_comment( $comment->comment_parent );
600 $parent_link = esc_url( get_comment_link( $comment->comment_parent ) );
601 $name = get_comment_author( $parent->comment_ID );
602 printf( ' | '.__( 'In reply to <a href="%1$s">%2$s</a>.' ), $parent_link, $name );
607 if ( $this->user_can ) { ?>
608 <div id="inline-<?php echo $comment->comment_ID; ?>" class="hidden">
609 <textarea class="comment" rows="1" cols="1"><?php
610 /** This filter is documented in wp-admin/includes/comment.php */
611 echo esc_textarea( apply_filters( 'comment_edit_pre', $comment->comment_content ) );
613 <div class="author-email"><?php echo esc_attr( $comment->comment_author_email ); ?></div>
614 <div class="author"><?php echo esc_attr( $comment->comment_author ); ?></div>
615 <div class="author-url"><?php echo esc_attr( $comment->comment_author_url ); ?></div>
616 <div class="comment_status"><?php echo $comment->comment_approved; ?></div>
624 * @global string $comment_status
626 * @param object $comment
628 public function column_author( $comment ) {
629 global $comment_status;
631 $author_url = get_comment_author_url();
632 if ( 'http://' == $author_url )
634 $author_url_display = preg_replace( '|http://(www\.)?|i', '', $author_url );
635 if ( strlen( $author_url_display ) > 50 )
636 $author_url_display = substr( $author_url_display, 0, 49 ) . '…';
638 echo "<strong>"; comment_author(); echo '</strong><br />';
639 if ( !empty( $author_url ) )
640 echo "<a title='$author_url' href='$author_url'>$author_url_display</a><br />";
642 if ( $this->user_can ) {
643 if ( !empty( $comment->comment_author_email ) ) {
644 comment_author_email_link();
648 $author_ip = get_comment_author_IP();
650 $author_ip_url = add_query_arg( array( 's' => $author_ip, 'mode' => 'detail' ), 'edit-comments.php' );
651 if ( 'spam' == $comment_status ) {
652 $author_ip_url = add_query_arg( 'comment_status', 'spam', $author_ip_url );
654 printf( '<a href="%s">%s</a>', esc_url( $author_ip_url ), $author_ip );
663 public function column_date() {
664 return get_comment_date( __( 'Y/m/d \a\t g:i a' ) );
670 public function column_response() {
677 if ( isset( $this->pending_count[$post->ID] ) ) {
678 $pending_comments = $this->pending_count[$post->ID];
680 $_pending_count_temp = get_pending_comments_num( array( $post->ID ) );
681 $pending_comments = $this->pending_count[$post->ID] = $_pending_count_temp[$post->ID];
684 if ( current_user_can( 'edit_post', $post->ID ) ) {
685 $post_link = "<a href='" . get_edit_post_link( $post->ID ) . "' class='comments-edit-item-link'>";
686 $post_link .= esc_html( get_the_title( $post->ID ) ) . '</a>';
688 $post_link = esc_html( get_the_title( $post->ID ) );
691 echo '<div class="response-links">';
692 if ( 'attachment' == $post->post_type && ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) ) {
696 $post_type_object = get_post_type_object( $post->post_type );
697 echo "<a href='" . get_permalink( $post->ID ) . "' class='comments-view-item-link'>" . $post_type_object->labels->view_item . '</a>';
698 echo '<span class="post-com-count-wrapper">';
699 $this->comments_bubble( $post->ID, $pending_comments );
706 * @param object $comment
707 * @param string $column_name
709 public function column_default( $comment, $column_name ) {
711 * Fires when the default column output is displayed for a single row.
715 * @param string $column_name The custom column's name.
716 * @param int $comment->comment_ID The custom column's unique ID number.
718 do_action( 'manage_comments_custom_column', $column_name, $comment->comment_ID );
723 * Post Comments List Table class.
726 * @subpackage List_Table
730 * @see WP_Comments_Table
732 class WP_Post_Comments_List_Table extends WP_Comments_List_Table {
738 protected function get_column_info() {
741 'author' => __( 'Author' ),
742 'comment' => _x( 'Comment', 'column name' ),
754 protected function get_table_classes() {
755 $classes = parent::get_table_classes();
756 $classes[] = 'wp-list-table';
757 $classes[] = 'comments-box';
763 * @param bool $output_empty
765 public function display( $output_empty = false ) {
766 $singular = $this->_args['singular'];
768 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
770 <table class="<?php echo implode( ' ', $this->get_table_classes() ); ?>" style="display:none;">
771 <tbody id="the-comment-list"<?php
773 echo " data-wp-lists='list:$singular'";
775 <?php if ( ! $output_empty ) {
776 $this->display_rows_or_placeholder();
785 * @param bool $comment_status
788 public function get_per_page( $comment_status = false ) {