From f24f716775d6b11b5b8e5b60dea038e6cb0fadcc Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 27 Mar 2007 21:20:16 +0000 Subject: [PATCH] Dynamically refill comment list when a comment is deleted. Props mdawaffe. fixes #4038 git-svn-id: http://svn.automattic.com/wordpress/trunk@5118 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 25 ++++++ wp-admin/admin-functions.php | 62 +++++++++++++ wp-admin/edit-comments.js | 41 ++++++++- wp-admin/edit-comments.php | 158 +++++++++------------------------- wp-includes/script-loader.php | 4 +- 5 files changed, 166 insertions(+), 124 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 2ad2469c3..ad5064fd7 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -151,6 +151,31 @@ case 'add-cat' : // From Manage->Categories ) ); $x->send(); break; +case 'add-comment' : + if ( !current_user_can( 'edit_post', $id ) ) + die('-1'); + $search = isset($_POST['s']) ? $_POST['s'] : false; + $start = isset($_POST['page']) ? intval($_POST['page']) * 25 : 25; + + list($comments, $total) = _wp_get_comment_list( $search, $start, 1 ); + + if ( !$comments ) + die('1'); + $x = new WP_Ajax_Response(); + foreach ( (array) $comments as $comment ) { + get_comment( $comment ); + ob_start(); + _wp_comment_list_item( $comment->comment_ID ); + $comment_list_item = ob_get_contents(); + ob_end_clean(); + $x->add( array( + 'what' => 'comment', + 'id' => $comment->comment_ID, + 'data' => $comment_list_item + ) ); + } + $x->send(); + break; case 'add-meta' : if ( !current_user_can( 'edit_post', $id ) ) die('-1'); diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 0a50ee0cc..c07f0c4b6 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -909,6 +909,68 @@ function user_row( $user_object, $style = '' ) { return $r; } +function _wp_get_comment_list( $s = false, $start, $num ) { + global $wpdb; + + $start = (int) $start; + $num = (int) $num; + + if ( $s ) { + $s = $wpdb->escape($s); + $comments = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->comments WHERE + (comment_author LIKE '%$s%' OR + comment_author_email LIKE '%$s%' OR + comment_author_url LIKE ('%$s%') OR + comment_author_IP LIKE ('%$s%') OR + comment_content LIKE ('%$s%') ) AND + comment_approved != 'spam' + ORDER BY comment_date DESC LIMIT $start, $num"); + } else { + $comments = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->comments WHERE comment_approved = '0' OR comment_approved = '1' ORDER BY comment_date DESC LIMIT $start, $num" ); + } + + $total = $wpdb->get_var( "SELECT FOUND_ROWS()" ); + + return array($comments, $total); +} + +function _wp_comment_list_item( $id, $alt = 0 ) { + global $authordata, $comment, $wpdb; + $id = (int) $id; + $comment =& get_comment( $id ); + $class = ''; + $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID")); + $comment_status = wp_get_comment_status($comment->comment_ID); + if ( 'unapproved' == $comment_status ) + $class .= ' unapproved'; + if ( $alt % 2 ) + $class .= ' alternate'; + echo "
  • "; +?> +

    comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | |

    + + + +

    — [ +comment_post_ID) ) { + echo " " . __('Edit') . ''; + echo ' | comment_author)) . "', theCommentList );\">" . __('Delete') . ' '; + if ( ('none' != $comment_status) && ( current_user_can('moderate_comments') ) ) { + echo ' | ' . __('Unapprove') . ' '; + echo ' | ' . __('Approve') . ' '; + } + echo " | comment_post_ID . "&c=" . $comment->comment_ID, 'delete-comment_' . $comment->comment_ID) . "\" onclick=\"return deleteSomething( 'comment-as-spam', $comment->comment_ID, '" . js_escape(sprintf(__("You are about to mark as spam this comment by '%s'.\n'Cancel' to stop, 'OK' to mark as spam."), $comment->comment_author)) . "', theCommentList );\">" . __('Spam') . " "; +} +$post = get_post($comment->comment_post_ID); +$post_title = wp_specialchars( $post->post_title, 'double' ); +$post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; +?> + ] —

    +
  • +'; endif; -if (isset($_GET['s'])) { - $s = $wpdb->escape($_GET['s']); - $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE - (comment_author LIKE '%$s%' OR - comment_author_email LIKE '%$s%' OR - comment_author_url LIKE ('%$s%') OR - comment_author_IP LIKE ('%$s%') OR - comment_content LIKE ('%$s%') ) AND - comment_approved != 'spam' - ORDER BY comment_date DESC"); -} else { - if ( isset( $_GET['apage'] ) ) - $page = (int) $_GET['apage']; - else - $page = 1; - $start = $offset = ( $page - 1 ) * 20; +if ( isset( $_GET['apage'] ) ) + $page = (int) $_GET['apage']; +else + $page = 1; +$start = $offset = ( $page - 1 ) * 20; - $comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '0' OR comment_approved = '1' ORDER BY comment_date DESC LIMIT $start, 20" ); - $total = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0' OR comment_approved = '1'" ); -} -?> - 20 ) { -$total_pages = ceil( $total / 20 ); -$r = ''; -if ( 1 < $page ) { - $args['apage'] = ( 1 == $page - 1 ) ? FALSE : $page - 1; - $r .= '' . "\n"; -} -if ( ( $total_pages = ceil( $total / 20 ) ) > 1 ) { - for ( $page_num = 1; $page_num <= $total_pages; $page_num++ ) : - if ( $page == $page_num ) : - $r .= "$page_num\n"; - else : - $p = false; - if ( $page_num < 3 || ( $page_num >= $page - 3 && $page_num <= $page + 3 ) || $page_num > $total_pages - 3 ) : - $args['apage'] = ( 1 == $page_num ) ? FALSE : $page_num; - $r .= '' . ( $page_num ) . "\n"; - $in = true; - elseif ( $in == true ) : - $r .= "...\n"; - $in = false; - endif; - endif; - endfor; -} -if ( ( $page ) * 20 < $total || -1 == $total ) { - $args['apage'] = $page + 1; - $r .= '' . "\n"; -} -echo ""; -?> +list($_comments, $total) = _wp_get_comment_list( isset($_GET['s']) ? $_GET['s'] : false, $start, 25 ); // Grab a few extra - +$comments = array_slice($_comments, 0, 20); +$extra_comments = array_slice($_comments, 20); + +$page_links = paginate_links( array( + 'base' => clean_url( add_query_arg( 'apage', '%#%' ) ), + 'format' => '', + 'total' => ceil($total / 20), + 'current' => $page +)); + +if ( $page_links ) + echo ""; - -"; + echo "
      \n"; $i = 0; - foreach ($comments as $comment) { - ++$i; $class = ''; - $authordata = get_userdata($wpdb->get_var("SELECT post_author FROM $wpdb->posts WHERE ID = $comment->comment_post_ID")); - $comment_status = wp_get_comment_status($comment->comment_ID); - if ('unapproved' == $comment_status) - $class .= ' unapproved'; - if ($i % 2) - $class .= ' alternate'; - echo "
    1. "; -?> -

      comment_author_email) { ?>| comment_author_url && 'http://' != $comment->comment_author_url) { ?> | |

      + foreach ( $comments as $comment ) { + get_comment( $comment ); // Cache it + _wp_comment_list_item( $comment->comment_ID, ++$i ); + } + echo "
    \n\n"; - - -

    — [ +if ( $extra_comments ) : ?> +

    +
    @@ -242,39 +190,11 @@ $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; - 20 ) { -$total_pages = ceil( $total / 20 ); -$r = ''; -if ( 1 < $page ) { - $args['apage'] = ( 1 == $page - 1 ) ? FALSE : $page - 1; - $r .= '' . "\n"; -} -if ( ( $total_pages = ceil( $total / 20 ) ) > 1 ) { - for ( $page_num = 1; $page_num <= $total_pages; $page_num++ ) : - if ( $page == $page_num ) : - $r .= "$page_num\n"; - else : - $p = false; - if ( $page_num < 3 || ( $page_num >= $page - 3 && $page_num <= $page + 3 ) || $page_num > $total_pages - 3 ) : - $args['apage'] = ( 1 == $page_num ) ? FALSE : $page_num; - $r .= '' . ( $page_num ) . "\n"; - $in = true; - elseif ( $in == true ) : - $r .= "...\n"; - $in = false; - endif; - endif; - endfor; -} -if ( ( $page ) * 20 < $total || -1 == $total ) { - $args['apage'] = $page + 1; - $r .= '' . "\n"; -} -echo ""; -?> - +if ( $page_links ) + echo ""; + +?> diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 20dadfd13..9351dc8da 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -34,7 +34,7 @@ class WP_Scripts { 'strangeText' => __("Something strange happened. Try refreshing the page."), 'whoaText' => __("Slow down, I'm still sending your data!") ) ); - $this->add( 'listman', '/wp-includes/js/list-manipulation.js', array('wp-ajax', 'fat'), '20070306'); + $this->add( 'listman', '/wp-includes/js/list-manipulation.js', array('wp-ajax', 'fat'), '20070306' ); $this->localize( 'listman', 'listManL10n', array( 'jumpText' => __('Jump to new item'), 'delText' => __('Are you sure you want to delete this %thing%?') @@ -84,7 +84,7 @@ class WP_Scripts { ) ); $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' ); $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' ); - $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman'), '3847' ); + $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman'), '20070327' ); $this->add( 'admin-users', '/wp-admin/users.js', array('listman'), '4583' ); $this->add( 'xfn', '/wp-admin/xfn.js', false, '3517' ); $this->add( 'upload', '/wp-admin/upload.js', array('prototype'), '20070306' );