WP_Comments_Table cleanup:

* use column_*() methods
	* remove extra arguments from single_row()
	* introduce derived class WP_Post_Comments_Table for handling the post metabox comments
	* use $pending_count property instead of $_comment_pending_count global
See #14579



git-svn-id: http://svn.automattic.com/wordpress/trunk@15630 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-09-18 17:46:29 +00:00
parent a927a1665c
commit ce723858c2
3 changed files with 223 additions and 213 deletions

View File

@ -624,6 +624,8 @@ case 'add-comment' :
die('-1'); die('-1');
$wp_list_table = get_list_table('comments'); $wp_list_table = get_list_table('comments');
$wp_list_table->from_ajax = true;
$wp_list_table->prepare_items(); $wp_list_table->prepare_items();
if ( !$wp_list_table->has_items() ) if ( !$wp_list_table->has_items() )
@ -631,9 +633,8 @@ case 'add-comment' :
$x = new WP_Ajax_Response(); $x = new WP_Ajax_Response();
foreach ( $wp_list_table->items as $comment ) { foreach ( $wp_list_table->items as $comment ) {
get_comment( $comment );
ob_start(); ob_start();
$wp_list_table->single_row( $comment->comment_ID, $mode, $comment_status, true, true ); $wp_list_table->single_row( $comment );
$comment_list_item = ob_get_contents(); $comment_list_item = ob_get_contents();
ob_end_clean(); ob_end_clean();
$x->add( array( $x->add( array(
@ -651,7 +652,8 @@ case 'get-comments' :
if ( !current_user_can( 'edit_post', $post_ID ) ) if ( !current_user_can( 'edit_post', $post_ID ) )
die('-1'); die('-1');
$wp_list_table = get_list_table('comments'); $wp_list_table = get_list_table('post-comments');
$wp_list_table->prepare_items(); $wp_list_table->prepare_items();
if ( !$wp_list_table->has_items() ) if ( !$wp_list_table->has_items() )
@ -662,7 +664,7 @@ case 'get-comments' :
foreach ( $wp_list_table->items as $comment ) { foreach ( $wp_list_table->items as $comment ) {
get_comment( $comment ); get_comment( $comment );
ob_start(); ob_start();
$wp_list_table->single_row( $comment->comment_ID, 'single', false, false ); $wp_list_table->single_row( $comment );
$comment_list_item .= ob_get_contents(); $comment_list_item .= ob_get_contents();
ob_end_clean(); ob_end_clean();
} }
@ -676,6 +678,7 @@ case 'replyto-comment' :
check_ajax_referer( $action, '_ajax_nonce-replyto-comment' ); check_ajax_referer( $action, '_ajax_nonce-replyto-comment' );
$wp_list_table = get_list_table('comments'); $wp_list_table = get_list_table('comments');
$wp_list_table->checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
$comment_post_ID = (int) $_POST['comment_post_ID']; $comment_post_ID = (int) $_POST['comment_post_ID'];
if ( !current_user_can( 'edit_post', $comment_post_ID ) ) if ( !current_user_can( 'edit_post', $comment_post_ID ) )
@ -715,16 +718,15 @@ case 'replyto-comment' :
if ( ! $comment ) die('1'); if ( ! $comment ) die('1');
$position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
$checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
$x = new WP_Ajax_Response(); $x = new WP_Ajax_Response();
ob_start(); ob_start();
if ( 'dashboard' == $mode ) { if ( 'dashboard' == $mode ) {
require_once( ABSPATH . 'wp-admin/includes/dashboard.php' ); require_once( ABSPATH . 'wp-admin/includes/dashboard.php' );
_wp_dashboard_recent_comments_row( $comment, false ); _wp_dashboard_recent_comments_row( $comment );
} else { } else {
$wp_list_table->single_row( $comment->comment_ID, $mode, false, $checkbox ); $wp_list_table->single_row( $comment );
} }
$comment_list_item = ob_get_contents(); $comment_list_item = ob_get_contents();
ob_end_clean(); ob_end_clean();
@ -753,13 +755,13 @@ case 'edit-comment' :
edit_comment(); edit_comment();
$position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1'; $position = ( isset($_POST['position']) && (int) $_POST['position']) ? (int) $_POST['position'] : '-1';
$checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0; $comments_status = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
$comments_listing = isset($_POST['comments_listing']) ? $_POST['comments_listing'] : '';
$wp_list_table = get_list_table('comments'); $checkbox = ( isset($_POST['checkbox']) && true == $_POST['checkbox'] ) ? 1 : 0;
$wp_list_table = get_list_table( $checkbox ? 'comments' : 'post-comments' );
ob_start(); ob_start();
$wp_list_table->single_row( $comment_id, $mode, $comments_listing, $checkbox ); $wp_list_table->single_row( get_comment( $comment_id ) );
$comment_list_item = ob_get_contents(); $comment_list_item = ob_get_contents();
ob_end_clean(); ob_end_clean();

View File

@ -2015,6 +2015,11 @@ class WP_Users_Table extends WP_List_Table {
class WP_Comments_Table extends WP_List_Table { class WP_Comments_Table extends WP_List_Table {
var $checkbox = true;
var $from_ajax = false;
var $pending_count = array();
function WP_Comments_Table() { function WP_Comments_Table() {
global $mode; global $mode;
@ -2091,7 +2096,7 @@ class WP_Comments_Table extends WP_List_Table {
$_comment_post_ids[] = $_c->comment_post_ID; $_comment_post_ids[] = $_c->comment_post_ID;
} }
$_comment_pending_count = get_pending_comments_num( $_comment_post_ids ); $this->pending_count = get_pending_comments_num( $_comment_post_ids );
$this->set_pagination_args( array( $this->set_pagination_args( array(
'total_items' => $total_comments, 'total_items' => $total_comments,
@ -2205,13 +2210,20 @@ class WP_Comments_Table extends WP_List_Table {
} }
function get_columns() { function get_columns() {
return array( global $mode;
'cb' => '<input type="checkbox" />',
'author' => __( 'Author' ), $columns = array();
/* translators: column name */
'comment' => _x( 'Comment', 'column name' ), if ( $this->checkbox )
'response' => __( 'In Response To' ) $columns['cb'] = '<input type="checkbox" />';
);
$columns['author'] = __( 'Author' );
$columns['comment'] = _x( 'Comment', 'column name' );
if ( 'single' !== $mode )
$columns['response'] = _x( 'Comment', 'column name' );
return $columns;
} }
function get_sortable_columns() { function get_sortable_columns() {
@ -2242,11 +2254,11 @@ class WP_Comments_Table extends WP_List_Table {
</tfoot> </tfoot>
<tbody id="the-comment-list" class="list:comment"> <tbody id="the-comment-list" class="list:comment">
<?php $this->display_rows( $this->items ); ?> <?php $this->display_rows(); ?>
</tbody> </tbody>
<tbody id="the-extra-comment-list" class="list:comment" style="display: none;"> <tbody id="the-extra-comment-list" class="list:comment" style="display: none;">
<?php $this->display_rows( $this->extra_items ); ?> <?php $this->items = $this->extra_items; $this->display_rows(); ?>
</tbody> </tbody>
</table> </table>
<?php <?php
@ -2254,30 +2266,32 @@ class WP_Comments_Table extends WP_List_Table {
$this->display_tablenav( 'bottom' ); $this->display_tablenav( 'bottom' );
} }
function display_rows( $items = false ) { function single_row( $a_comment ) {
global $mode, $comment_status; global $post, $comment, $the_comment_status;
if ( false === $items ) $comment = $a_comment;
$items = $this->items; $the_comment_status = wp_get_comment_status( $comment->comment_ID );
foreach ( $items as $comment ) $post = get_post( $comment->comment_post_ID );
$this->single_row( $comment->comment_ID, $mode, $comment_status );
$this->user_can = current_user_can( 'edit_comment', $comment->comment_ID );
echo "<tr id='comment-$comment->comment_ID' class='$the_comment_status'>";
echo $this->single_row_columns( $comment );
echo "</tr>\n";
} }
function single_row( $comment_id, $mode, $comment_status, $checkbox = true, $from_ajax = false ) { function column_cb( $comment ) {
global $comment, $post, $_comment_pending_count; if ( $this->user_can )
$comment = get_comment( $comment_id ); echo "<input type='checkbox' name='delete_comments[]' value='$comment->comment_ID' />";
$post = get_post( $comment->comment_post_ID ); }
$the_comment_status = wp_get_comment_status( $comment->comment_ID );
$user_can = current_user_can( 'edit_comment', $comment_id ); function column_comment( $comment ) {
global $post, $comment_status, $the_comment_status;
$user_can = $this->user_can;
$comment_url = esc_url( get_comment_link( $comment->comment_ID ) ); $comment_url = esc_url( get_comment_link( $comment->comment_ID ) );
$author_url = get_comment_author_url();
if ( 'http://' == $author_url )
$author_url = '';
$author_url_display = preg_replace( '|http://(www\.)?|i', '', $author_url );
if ( strlen( $author_url_display ) > 50 )
$author_url_display = substr( $author_url_display, 0, 49 ) . '...';
$ptime = date( 'G', strtotime( $comment->comment_date ) ); $ptime = date( 'G', strtotime( $comment->comment_date ) );
if ( ( abs( time() - $ptime ) ) < 86400 ) if ( ( abs( time() - $ptime ) ) < 86400 )
@ -2300,28 +2314,6 @@ class WP_Comments_Table extends WP_List_Table {
$delete_url = esc_url( $url . "&action=deletecomment&$del_nonce" ); $delete_url = esc_url( $url . "&action=deletecomment&$del_nonce" );
} }
echo "<tr id='comment-$comment->comment_ID' class='$the_comment_status'>";
list( $columns, $hidden ) = $this->get_column_headers();
foreach ( $columns as $column_name => $column_display_name ) {
$class = "class=\"$column_name column-$column_name\"";
$style = '';
if ( in_array( $column_name, $hidden ) )
$style = ' style="display:none;"';
$attributes = "$class$style";
switch ( $column_name ) {
case 'cb':
if ( !$checkbox ) break;
echo '<th scope="row" class="check-column">';
if ( $user_can ) echo "<input type='checkbox' name='delete_comments[]' value='$comment->comment_ID' />";
echo '</th>';
break;
case 'comment':
echo "<td $attributes>";
echo '<div id="submitted-on">'; echo '<div id="submitted-on">';
/* translators: 2: comment date, 3: comment time */ /* translators: 2: comment date, 3: comment time */
printf( __( '<a href="%1$s">%2$s at %3$s</a>' ), $comment_url, printf( __( '<a href="%1$s">%2$s at %3$s</a>' ), $comment_url,
@ -2399,7 +2391,7 @@ class WP_Comments_Table extends WP_List_Table {
( ( ( 'approve' == $action || 'unapprove' == $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | '; ( ( ( 'approve' == $action || 'unapprove' == $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | ';
// Reply and quickedit need a hide-if-no-js span when not added with ajax // Reply and quickedit need a hide-if-no-js span when not added with ajax
if ( ( 'reply' == $action || 'quickedit' == $action ) && ! $from_ajax ) if ( ( 'reply' == $action || 'quickedit' == $action ) && ! $this->from_ajax )
$action .= ' hide-if-no-js'; $action .= ' hide-if-no-js';
elseif ( ( $action == 'untrash' && $the_comment_status == 'trash' ) || ( $action == 'unspam' && $the_comment_status == 'spam' ) ) { elseif ( ( $action == 'untrash' && $the_comment_status == 'trash' ) || ( $action == 'unspam' && $the_comment_status == 'spam' ) ) {
if ( '1' == get_comment_meta( $comment_id, '_wp_trash_meta_status', true ) ) if ( '1' == get_comment_meta( $comment_id, '_wp_trash_meta_status', true ) )
@ -2412,14 +2404,23 @@ class WP_Comments_Table extends WP_List_Table {
} }
echo '</div>'; echo '</div>';
} }
}
echo '</td>'; function column_author( $comment ) {
break; global $comment_status;
case 'author':
echo "<td $attributes><strong>"; comment_author(); echo '</strong><br />'; $author_url = get_comment_author_url();
if ( 'http://' == $author_url )
$author_url = '';
$author_url_display = preg_replace( '|http://(www\.)?|i', '', $author_url );
if ( strlen( $author_url_display ) > 50 )
$author_url_display = substr( $author_url_display, 0, 49 ) . '...';
echo "<strong>"; comment_author(); echo '</strong><br />';
if ( !empty( $author_url ) ) if ( !empty( $author_url ) )
echo "<a title='$author_url' href='$author_url'>$author_url_display</a><br />"; echo "<a title='$author_url' href='$author_url'>$author_url_display</a><br />";
if ( $user_can ) {
if ( $this->user_can ) {
if ( !empty( $comment->comment_author_email ) ) { if ( !empty( $comment->comment_author_email ) ) {
comment_author_email_link(); comment_author_email_link();
echo '<br />'; echo '<br />';
@ -2432,27 +2433,30 @@ class WP_Comments_Table extends WP_List_Table {
echo '">'; echo '">';
comment_author_IP(); comment_author_IP();
echo '</a>'; echo '</a>';
} //current_user_can
echo '</td>';
break;
case 'date':
echo "<td $attributes>" . get_comment_date( __( 'Y/m/d \a\t g:ia' ) ) . '</td>';
break;
case 'response':
if ( 'single' !== $mode ) {
if ( isset( $_comment_pending_count[$post->ID] ) ) {
$pending_comments = $_comment_pending_count[$post->ID];
} else {
$_comment_pending_count_temp = get_pending_comments_num( array( $post->ID ) );
$pending_comments = $_comment_pending_count[$post->ID] = $_comment_pending_count_temp[$post->ID];
} }
if ( $user_can ) { }
function column_date( $comment ) {
return get_comment_date( __( 'Y/m/d \a\t g:ia' ) );
}
function column_response( $comment ) {
global $post;
if ( isset( $this->pending_count[$post->ID] ) ) {
$pending_comments = $this->pending_count[$post->ID];
} else {
$_pending_count_temp = get_pending_comments_num( array( $post->ID ) );
$pending_comments = $this->pending_count[$post->ID] = $_pending_count_temp[$post->ID];
}
if ( current_user_can( 'edit_post', $post->ID ) ) {
$post_link = "<a href='" . get_edit_post_link( $post->ID ) . "'>"; $post_link = "<a href='" . get_edit_post_link( $post->ID ) . "'>";
$post_link .= get_the_title( $post->ID ) . '</a>'; $post_link .= get_the_title( $post->ID ) . '</a>';
} else { } else {
$post_link = get_the_title( $post->ID ); $post_link = get_the_title( $post->ID );
} }
echo "<td $attributes>\n";
echo '<div class="response-links"><span class="post-com-count-wrapper">'; echo '<div class="response-links"><span class="post-com-count-wrapper">';
echo $post_link . '<br />'; echo $post_link . '<br />';
$this->comments_bubble( $post->ID, $pending_comments ); $this->comments_bubble( $post->ID, $pending_comments );
@ -2461,17 +2465,24 @@ class WP_Comments_Table extends WP_List_Table {
echo '</div>'; echo '</div>';
if ( 'attachment' == $post->post_type && ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) ) if ( 'attachment' == $post->post_type && ( $thumb = wp_get_attachment_image( $post->ID, array( 80, 60 ), true ) ) )
echo $thumb; echo $thumb;
echo '</td>';
} }
break;
default: function column_default( $comment, $column_name ) {
echo "<td $attributes>\n";
do_action( 'manage_comments_custom_column', $column_name, $comment->comment_ID ); do_action( 'manage_comments_custom_column', $column_name, $comment->comment_ID );
echo "</td>\n";
break;
} }
} }
echo "</tr>\n";
class WP_Post_Comments_Table extends WP_Comments_Table {
function get_columns() {
return array(
'author' => __( 'Author' ),
'comment' => _x( 'Comment', 'column name' ),
);
}
function get_sortable_columns() {
return array();
} }
} }

View File

@ -459,10 +459,8 @@ function post_comment_meta_box($post) {
} }
wp_nonce_field( 'get-comments', 'add_comment_nonce', false ); wp_nonce_field( 'get-comments', 'add_comment_nonce', false );
add_filter('manage_edit-comments_columns', 'post_comment_meta_box_thead', 8, 1);
add_filter('manage_edit-comments_sortable_columns', '__return_empty_array', 8);
$wp_list_table = get_list_table('comments'); $wp_list_table = get_list_table('post-comments');
?> ?>
<table class="widefat comments-box fixed" cellspacing="0" style="display:none;"> <table class="widefat comments-box fixed" cellspacing="0" style="display:none;">
@ -478,7 +476,6 @@ function post_comment_meta_box($post) {
<script type="text/javascript">jQuery(document).ready(function(){commentsBox.get(<?php echo $total; ?>, 10);});</script> <script type="text/javascript">jQuery(document).ready(function(){commentsBox.get(<?php echo $total; ?>, 10);});</script>
<?php <?php
} }
remove_filter('manage_edit-comments_columns', 'post_comment_meta_box_thead');
wp_comment_trashnotice(); wp_comment_trashnotice();
} }