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 {
22 var $pending_count = array();
24 function __construct( $args = array() ) {
27 $post_id = isset( $_REQUEST['p'] ) ? absint( $_REQUEST['p'] ) : 0;
29 if ( get_option('show_avatars') )
30 add_filter( 'comment_author', 'floated_admin_avatar' );
32 parent::__construct( array(
33 'plural' => 'comments',
34 'singular' => 'comment',
36 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
40 function ajax_user_can() {
41 return current_user_can('edit_posts');
44 function prepare_items() {
45 global $post_id, $comment_status, $search, $comment_type;
47 $comment_status = isset( $_REQUEST['comment_status'] ) ? $_REQUEST['comment_status'] : 'all';
48 if ( !in_array( $comment_status, array( 'all', 'moderated', 'approved', 'spam', 'trash' ) ) )
49 $comment_status = 'all';
51 $comment_type = !empty( $_REQUEST['comment_type'] ) ? $_REQUEST['comment_type'] : '';
53 $search = ( isset( $_REQUEST['s'] ) ) ? $_REQUEST['s'] : '';
55 $post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_key( $_REQUEST['post_type'] ) : '';
57 $user_id = ( isset( $_REQUEST['user_id'] ) ) ? $_REQUEST['user_id'] : '';
59 $orderby = ( isset( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : '';
60 $order = ( isset( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : '';
62 $comments_per_page = $this->get_per_page( $comment_status );
64 $doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX;
66 if ( isset( $_REQUEST['number'] ) ) {
67 $number = (int) $_REQUEST['number'];
70 $number = $comments_per_page + min( 8, $comments_per_page ); // Grab a few extra
73 $page = $this->get_pagenum();
75 if ( isset( $_REQUEST['start'] ) ) {
76 $start = $_REQUEST['start'];
78 $start = ( $page - 1 ) * $comments_per_page;
81 if ( $doing_ajax && isset( $_REQUEST['offset'] ) ) {
82 $start += $_REQUEST['offset'];
86 'moderated' => 'hold',
87 'approved' => 'approve',
92 'status' => isset( $status_map[$comment_status] ) ? $status_map[$comment_status] : $comment_status,
94 'user_id' => $user_id,
97 'post_id' => $post_id,
98 'type' => $comment_type,
99 'orderby' => $orderby,
101 'post_type' => $post_type,
104 $_comments = get_comments( $args );
106 update_comment_cache( $_comments );
108 $this->items = array_slice( $_comments, 0, $comments_per_page );
109 $this->extra_items = array_slice( $_comments, $comments_per_page );
111 $total_comments = get_comments( array_merge( $args, array('count' => true, 'offset' => 0, 'number' => 0) ) );
113 $_comment_post_ids = array();
114 foreach ( $_comments as $_c ) {
115 $_comment_post_ids[] = $_c->comment_post_ID;
118 $_comment_post_ids = array_unique( $_comment_post_ids );
120 $this->pending_count = get_pending_comments_num( $_comment_post_ids );
122 $this->set_pagination_args( array(
123 'total_items' => $total_comments,
124 'per_page' => $comments_per_page,
128 function get_per_page( $comment_status = 'all' ) {
129 $comments_per_page = $this->get_items_per_page( 'edit_comments_per_page' );
131 * Filter the number of comments listed per page in the comments list table.
135 * @param int $comments_per_page The number of comments to list per page.
136 * @param string $comment_status The comment status name. Default 'All'.
138 $comments_per_page = apply_filters( 'comments_per_page', $comments_per_page, $comment_status );
139 return $comments_per_page;
142 function no_items() {
143 global $comment_status;
145 if ( 'moderated' == $comment_status )
146 _e( 'No comments awaiting moderation.' );
148 _e( 'No comments found.' );
151 function get_views() {
152 global $post_id, $comment_status, $comment_type;
154 $status_links = array();
155 $num_comments = ( $post_id ) ? wp_count_comments( $post_id ) : wp_count_comments();
156 //, number_format_i18n($num_comments->moderated) ), "<span class='comment-count'>" . number_format_i18n($num_comments->moderated) . "</span>"),
157 //, number_format_i18n($num_comments->spam) ), "<span class='spam-comment-count'>" . number_format_i18n($num_comments->spam) . "</span>")
159 'all' => _nx_noop('All', 'All', 'comments'), // singular not used
160 '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>'),
161 'approved' => _n_noop('Approved', 'Approved'), // singular not used
162 '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>'),
163 '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>')
166 if ( !EMPTY_TRASH_DAYS )
167 unset($stati['trash']);
169 $link = 'edit-comments.php';
170 if ( !empty($comment_type) && 'all' != $comment_type )
171 $link = add_query_arg( 'comment_type', $comment_type, $link );
173 foreach ( $stati as $status => $label ) {
174 $class = ( $status == $comment_status ) ? ' class="current"' : '';
176 if ( !isset( $num_comments->$status ) )
177 $num_comments->$status = 10;
178 $link = add_query_arg( 'comment_status', $status, $link );
180 $link = add_query_arg( 'p', absint( $post_id ), $link );
182 // I toyed with this, but decided against it. Leaving it in here in case anyone thinks it is a good idea. ~ Mark
183 if ( !empty( $_REQUEST['s'] ) )
184 $link = add_query_arg( 's', esc_attr( wp_unslash( $_REQUEST['s'] ) ), $link );
186 $status_links[$status] = "<a href='$link'$class>" . sprintf(
187 translate_nooped_plural( $label, $num_comments->$status ),
188 number_format_i18n( $num_comments->$status )
193 * Filter the comment status links.
197 * @param array $status_links An array of fully-formed status links. Default 'All'.
198 * Accepts 'All', 'Pending', 'Approved', 'Spam', and 'Trash'.
200 $status_links = apply_filters( 'comment_status_links', $status_links );
201 return $status_links;
204 function get_bulk_actions() {
205 global $comment_status;
208 if ( in_array( $comment_status, array( 'all', 'approved' ) ) )
209 $actions['unapprove'] = __( 'Unapprove' );
210 if ( in_array( $comment_status, array( 'all', 'moderated' ) ) )
211 $actions['approve'] = __( 'Approve' );
212 if ( in_array( $comment_status, array( 'all', 'moderated', 'approved' ) ) )
213 $actions['spam'] = _x( 'Mark as Spam', 'comment' );
215 if ( 'trash' == $comment_status )
216 $actions['untrash'] = __( 'Restore' );
217 elseif ( 'spam' == $comment_status )
218 $actions['unspam'] = _x( 'Not Spam', 'comment' );
220 if ( in_array( $comment_status, array( 'trash', 'spam' ) ) || !EMPTY_TRASH_DAYS )
221 $actions['delete'] = __( 'Delete Permanently' );
223 $actions['trash'] = __( 'Move to Trash' );
228 function extra_tablenav( $which ) {
229 global $comment_status, $comment_type;
231 <div class="alignleft actions">
233 if ( 'top' == $which ) {
235 <select name="comment_type">
236 <option value=""><?php _e( 'All comment types' ); ?></option>
239 * Filter the comment types dropdown menu.
243 * @param array $comment_types An array of comment types. Accepts 'Comments', 'Pings'.
245 $comment_types = apply_filters( 'admin_comment_types_dropdown', array(
246 'comment' => __( 'Comments' ),
247 'pings' => __( 'Pings' ),
250 foreach ( $comment_types as $type => $label )
251 echo "\t<option value='" . esc_attr( $type ) . "'" . selected( $comment_type, $type, false ) . ">$label</option>\n";
256 * Fires just before the Filter submit button for comment types.
260 do_action( 'restrict_manage_comments' );
261 submit_button( __( 'Filter' ), 'button', false, false, array( 'id' => 'post-query-submit' ) );
264 if ( ( 'spam' == $comment_status || 'trash' == $comment_status ) && current_user_can( 'moderate_comments' ) ) {
265 wp_nonce_field( 'bulk-destroy', '_destroy_nonce' );
266 $title = ( 'spam' == $comment_status ) ? esc_attr__( 'Empty Spam' ) : esc_attr__( 'Empty Trash' );
267 submit_button( $title, 'apply', 'delete_all', false );
270 * Fires after the Filter submit button for comment types.
274 * @param string $comment_status The comment status name. Default 'All'.
276 do_action( 'manage_comments_nav', $comment_status );
280 function current_action() {
281 if ( isset( $_REQUEST['delete_all'] ) || isset( $_REQUEST['delete_all2'] ) )
284 return parent::current_action();
287 function get_columns() {
292 if ( $this->checkbox )
293 $columns['cb'] = '<input type="checkbox" />';
295 $columns['author'] = __( 'Author' );
296 $columns['comment'] = _x( 'Comment', 'column name' );
299 $columns['response'] = _x( 'In Response To', 'column name' );
304 function get_sortable_columns() {
306 'author' => 'comment_author',
307 'response' => 'comment_post_ID'
312 extract( $this->_args );
314 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
316 $this->display_tablenav( 'top' );
319 <table class="<?php echo implode( ' ', $this->get_table_classes() ); ?>">
322 <?php $this->print_column_headers(); ?>
328 <?php $this->print_column_headers( false ); ?>
332 <tbody id="the-comment-list" data-wp-lists="list:comment">
333 <?php $this->display_rows_or_placeholder(); ?>
336 <tbody id="the-extra-comment-list" data-wp-lists="list:comment" style="display: none;">
337 <?php $this->items = $this->extra_items; $this->display_rows(); ?>
342 $this->display_tablenav( 'bottom' );
345 function single_row( $a_comment ) {
346 global $post, $comment;
348 $comment = $a_comment;
349 $the_comment_class = wp_get_comment_status( $comment->comment_ID );
350 $the_comment_class = join( ' ', get_comment_class( $the_comment_class, $comment->comment_ID, $comment->comment_post_ID ) );
352 $post = get_post( $comment->comment_post_ID );
354 $this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
356 echo "<tr id='comment-$comment->comment_ID' class='$the_comment_class'>";
357 $this->single_row_columns( $comment );
361 function column_cb( $comment ) {
362 if ( $this->user_can ) { ?>
363 <label class="screen-reader-text" for="cb-select-<?php echo $comment->comment_ID; ?>"><?php _e( 'Select comment' ); ?></label>
364 <input id="cb-select-<?php echo $comment->comment_ID; ?>" type="checkbox" name="delete_comments[]" value="<?php echo $comment->comment_ID; ?>" />
369 function column_comment( $comment ) {
370 global $comment_status;
373 $user_can = $this->user_can;
375 $comment_url = esc_url( get_comment_link( $comment->comment_ID ) );
376 $the_comment_status = wp_get_comment_status( $comment->comment_ID );
379 $del_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "delete-comment_$comment->comment_ID" ) );
380 $approve_nonce = esc_html( '_wpnonce=' . wp_create_nonce( "approve-comment_$comment->comment_ID" ) );
382 $url = "comment.php?c=$comment->comment_ID";
384 $approve_url = esc_url( $url . "&action=approvecomment&$approve_nonce" );
385 $unapprove_url = esc_url( $url . "&action=unapprovecomment&$approve_nonce" );
386 $spam_url = esc_url( $url . "&action=spamcomment&$del_nonce" );
387 $unspam_url = esc_url( $url . "&action=unspamcomment&$del_nonce" );
388 $trash_url = esc_url( $url . "&action=trashcomment&$del_nonce" );
389 $untrash_url = esc_url( $url . "&action=untrashcomment&$del_nonce" );
390 $delete_url = esc_url( $url . "&action=deletecomment&$del_nonce" );
393 echo '<div class="comment-author">';
394 $this->column_author( $comment );
397 echo '<div class="submitted-on">';
398 /* translators: 2: comment date, 3: comment time */
399 printf( __( 'Submitted on <a href="%1$s">%2$s at %3$s</a>' ), $comment_url,
400 /* translators: comment date format. See http://php.net/date */
401 get_comment_date( __( 'Y/m/d' ) ),
402 get_comment_date( get_option( 'time_format' ) )
405 if ( $comment->comment_parent ) {
406 $parent = get_comment( $comment->comment_parent );
407 $parent_link = esc_url( get_comment_link( $comment->comment_parent ) );
408 $name = get_comment_author( $parent->comment_ID );
409 printf( ' | '.__( 'In reply to <a href="%1$s">%2$s</a>.' ), $parent_link, $name );
414 if ( $user_can ) { ?>
415 <div id="inline-<?php echo $comment->comment_ID; ?>" class="hidden">
416 <textarea class="comment" rows="1" cols="1"><?php
417 /** This filter is documented in wp-admin/includes/comment.php */
418 echo esc_textarea( apply_filters( 'comment_edit_pre', $comment->comment_content ) );
420 <div class="author-email"><?php echo esc_attr( $comment->comment_author_email ); ?></div>
421 <div class="author"><?php echo esc_attr( $comment->comment_author ); ?></div>
422 <div class="author-url"><?php echo esc_attr( $comment->comment_author_url ); ?></div>
423 <div class="comment_status"><?php echo $comment->comment_approved; ?></div>
429 // preorder it: Approve | Reply | Quick Edit | Edit | Spam | Trash
431 'approve' => '', 'unapprove' => '',
435 'spam' => '', 'unspam' => '',
436 'trash' => '', 'untrash' => '', 'delete' => ''
439 if ( $comment_status && 'all' != $comment_status ) { // not looking at all comments
440 if ( 'approved' == $the_comment_status )
441 $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>';
442 else if ( 'unapproved' == $the_comment_status )
443 $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>';
445 $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>';
446 $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>';
449 if ( 'spam' != $the_comment_status && 'trash' != $the_comment_status ) {
450 $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>';
451 } elseif ( 'spam' == $the_comment_status ) {
452 $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>';
453 } elseif ( 'trash' == $the_comment_status ) {
454 $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>';
457 if ( 'spam' == $the_comment_status || 'trash' == $the_comment_status || !EMPTY_TRASH_DAYS ) {
458 $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>';
460 $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>';
463 if ( 'spam' != $the_comment_status && 'trash' != $the_comment_status ) {
464 $actions['edit'] = "<a href='comment.php?action=editcomment&c={$comment->comment_ID}' title='" . esc_attr__( 'Edit comment' ) . "'>". __( 'Edit' ) . '</a>';
465 $actions['quickedit'] = '<a onclick="window.commentReply && commentReply.open( \''.$comment->comment_ID.'\',\''.$post->ID.'\',\'edit\' );return false;" class="vim-q" title="'.esc_attr__( 'Quick Edit' ).'" href="#">' . __( 'Quick Edit' ) . '</a>';
466 $actions['reply'] = '<a onclick="window.commentReply && commentReply.open( \''.$comment->comment_ID.'\',\''.$post->ID.'\' );return false;" class="vim-r" title="'.esc_attr__( 'Reply to this comment' ).'" href="#">' . __( 'Reply' ) . '</a>';
469 /** This filter is documented in wp-admin/includes/dashboard.php */
470 $actions = apply_filters( 'comment_row_actions', array_filter( $actions ), $comment );
473 echo '<div class="row-actions">';
474 foreach ( $actions as $action => $link ) {
476 ( ( ( 'approve' == $action || 'unapprove' == $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | ';
478 // Reply and quickedit need a hide-if-no-js span when not added with ajax
479 if ( ( 'reply' == $action || 'quickedit' == $action ) && ! defined('DOING_AJAX') )
480 $action .= ' hide-if-no-js';
481 elseif ( ( $action == 'untrash' && $the_comment_status == 'trash' ) || ( $action == 'unspam' && $the_comment_status == 'spam' ) ) {
482 if ( '1' == get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) )
483 $action .= ' approve';
485 $action .= ' unapprove';
488 echo "<span class='$action'>$sep$link</span>";
494 function column_author( $comment ) {
495 global $comment_status;
497 $author_url = get_comment_author_url();
498 if ( 'http://' == $author_url )
500 $author_url_display = preg_replace( '|http://(www\.)?|i', '', $author_url );
501 if ( strlen( $author_url_display ) > 50 )
502 $author_url_display = substr( $author_url_display, 0, 49 ) . '…';
504 echo "<strong>"; comment_author(); echo '</strong><br />';
505 if ( !empty( $author_url ) )
506 echo "<a title='$author_url' href='$author_url'>$author_url_display</a><br />";
508 if ( $this->user_can ) {
509 if ( !empty( $comment->comment_author_email ) ) {
510 comment_author_email_link();
513 echo '<a href="edit-comments.php?s=';
515 echo '&mode=detail';
516 if ( 'spam' == $comment_status )
517 echo '&comment_status=spam';
524 function column_date( $comment ) {
525 return get_comment_date( __( 'Y/m/d \a\t g:ia' ) );
528 function column_response( $comment ) {
531 if ( isset( $this->pending_count[$post->ID] ) ) {
532 $pending_comments = $this->pending_count[$post->ID];
534 $_pending_count_temp = get_pending_comments_num( array( $post->ID ) );
535 $pending_comments = $this->pending_count[$post->ID] = $_pending_count_temp[$post->ID];
538 if ( current_user_can( 'edit_post', $post->ID ) ) {
539 $post_link = "<a href='" . get_edit_post_link( $post->ID ) . "'>";
540 $post_link .= get_the_title( $post->ID ) . '</a>';
542 $post_link = get_the_title( $post->ID );
545 echo '<div class="response-links"><span class="post-com-count-wrapper">';
546 echo $post_link . '<br />';
547 $this->comments_bubble( $post->ID, $pending_comments );
549 $post_type_object = get_post_type_object( $post->post_type );
550 echo "<a href='" . get_permalink( $post->ID ) . "'>" . $post_type_object->labels->view_item . '</a>';
552 if ( 'attachment' == $post->post_type && ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) )
556 function column_default( $comment, $column_name ) {
558 * Fires when the default column output is displayed for a single row.
562 * @param string $column_name The custom column's name.
563 * @param int $comment->comment_ID The custom column's unique ID number.
565 do_action( 'manage_comments_custom_column', $column_name, $comment->comment_ID );
570 * Post Comments List Table class.
573 * @subpackage List_Table
577 * @see WP_Comments_Table
579 class WP_Post_Comments_List_Table extends WP_Comments_List_Table {
581 function get_column_info() {
582 $this->_column_headers = array(
584 'author' => __( 'Author' ),
585 'comment' => _x( 'Comment', 'column name' ),
591 return $this->_column_headers;
594 function get_table_classes() {
595 $classes = parent::get_table_classes();
596 $classes[] = 'comments-box';
600 function display( $output_empty = false ) {
601 extract( $this->_args );
603 wp_nonce_field( "fetch-list-" . get_class( $this ), '_ajax_fetch_list_nonce' );
605 <table class="<?php echo implode( ' ', $this->get_table_classes() ); ?>" style="display:none;">
606 <tbody id="the-comment-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
607 <?php if ( ! $output_empty ) $this->display_rows_or_placeholder(); ?>
613 function get_per_page( $comment_status = false ) {