<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[13246] trunk/wp-admin: Comments moderation code cleanup.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/13246">13246</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2010-02-20 11:18:25 +0000 (Sat, 20 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Comments moderation code cleanup. Dry up some code, add screen icon to AYS, fix notice in comment_footer_die(). Redirect to edit-comments.php on an error, instead of stranding them with dull comment_footer_die() message. See <a href="http://trac.wordpress.org/ticket/11441">#11441</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincommentphp">trunk/wp-admin/comment.php</a></li>
<li><a href="#trunkwpadmineditcommentsphp">trunk/wp-admin/edit-comments.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/comment.php (13245 => 13246)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/comment.php        2010-02-20 10:50:57 UTC (rev 13245)
+++ trunk/wp-admin/comment.php        2010-02-20 11:18:25 UTC (rev 13246)
</span><span class="lines">@@ -35,6 +35,7 @@
</span><span class="cx"> * @param string $msg Error Message. Assumed to contain HTML and be sanitized.
</span><span class="cx"> */
</span><span class="cx"> function comment_footer_die( $msg ) {
</span><ins>+        global $hook_suffix;
</ins><span class="cx">         echo "<div class='wrap'><p>$msg</p></div>";
</span><span class="cx">         include('admin-footer.php');
</span><span class="cx">         die;
</span><span class="lines">@@ -70,22 +71,32 @@
</span><span class="cx"> case 'trash' :
</span><span class="cx"> case 'spam' :
</span><span class="cx">
</span><ins>+        $comment_id = absint( $_GET['c'] );
+
+        if ( !$comment = get_comment_to_edit( $comment_id ) ) {
+                wp_redirect( admin_url('edit-comments.php?error=1') );
+                die();
+        }
+
+        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) ) {
+                wp_redirect( admin_url('edit-comments.php?error=2') );
+                die();
+        }
+
</ins><span class="cx">         require_once('admin-header.php');
</span><span class="cx">
</span><del>-        $comment_id = absint( $_GET['c'] );
</del><span class="cx">         $formaction = $action . 'comment';
</span><span class="cx">         $nonce_action = 'approve' == $action ? 'approve-comment_' : 'delete-comment_';
</span><span class="cx">         $nonce_action .= $comment_id;
</span><span class="cx">
</span><del>-        if ( !$comment = get_comment_to_edit( $comment_id ) )
-                comment_footer_die( __('Oops, no comment with this ID.') . sprintf(' <a href="%s">'.__('Go back').'</a>!', 'edit.php') );
-
-        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) )
-                comment_footer_die( 'approve' != $action ? __('You are not allowed to delete comments on this post.') : __('You are not allowed to edit comments on this post, so you cannot approve this comment.') );
</del><span class="cx"> ?>
</span><span class="cx"> <div class='wrap'>
</span><span class="cx">
</span><span class="cx"> <div class="narrow">
</span><ins>+
+<?php screen_icon(); ?>
+<h2><?php esc_html_e( 'Moderate Comment' ); ?></h2>
+
</ins><span class="cx"> <?php
</span><span class="cx"> switch ( $action ) {
</span><span class="cx">         case 'spam' :
</span><span class="lines">@@ -155,14 +166,20 @@
</span><span class="cx"> <?php
</span><span class="cx">         break;
</span><span class="cx">
</span><del>-case 'deletecomment' :
-case 'trashcomment' :
-case 'untrashcomment' :
-case 'spamcomment' :
-case 'unspamcomment' :
</del><ins>+case 'deletecomment' :
+case 'trashcomment' :
+case 'untrashcomment' :
+case 'spamcomment' :
+case 'unspamcomment' :
+case 'approvecomment' :
+case 'unapprovecomment' :
</ins><span class="cx">         $comment_id = absint( $_REQUEST['c'] );
</span><del>-        check_admin_referer( 'delete-comment_' . $comment_id );
</del><span class="cx">
</span><ins>+        if ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ) ) )
+                check_admin_referer( 'approve-comment_' . $comment_id );
+        else
+                check_admin_referer( 'delete-comment_' . $comment_id );
+
</ins><span class="cx">         $noredir = isset($_REQUEST['noredir']);
</span><span class="cx">
</span><span class="cx">         if ( !$comment = get_comment($comment_id) )
</span><span class="lines">@@ -170,14 +187,16 @@
</span><span class="cx">         if ( !current_user_can('edit_post', $comment->comment_post_ID ) )
</span><span class="cx">                 comment_footer_die( __('You are not allowed to edit comments on this post.') );
</span><span class="cx">
</span><del>-        if ( '' != wp_get_referer() && false == $noredir && false === strpos(wp_get_referer(), 'comment.php') )
</del><ins>+        if ( '' != wp_get_referer() && ! $noredir && false === strpos(wp_get_referer(), 'comment.php') )
</ins><span class="cx">                 $redir = wp_get_referer();
</span><del>-        elseif ( '' != wp_get_original_referer() && false == $noredir )
</del><ins>+        elseif ( '' != wp_get_original_referer() && ! $noredir )
</ins><span class="cx">                 $redir = wp_get_original_referer();
</span><ins>+        elseif ( in_array( $action, array( 'approvecomment', 'unapprovecomment' ) ) )
+                $redir = admin_url('edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
</ins><span class="cx">         else
</span><span class="cx">                 $redir = admin_url('edit-comments.php');
</span><span class="cx">
</span><del>-        $redir = remove_query_arg( array('spammed', 'unspammed', 'trashed', 'untrashed', 'deleted', 'ids'), $redir );
</del><ins>+        $redir = remove_query_arg( array('spammed', 'unspammed', 'trashed', 'untrashed', 'deleted', 'ids', 'approved', 'unapproved'), $redir );
</ins><span class="cx">
</span><span class="cx">         switch ( $action ) {
</span><span class="cx">                 case 'deletecomment' :
</span><span class="lines">@@ -200,48 +219,20 @@
</span><span class="cx">                         wp_unspam_comment($comment_id);
</span><span class="cx">                         $redir = add_query_arg( array('unspammed' => '1'), $redir );
</span><span class="cx">                         break;
</span><del>-        }
</del><ins>+                case 'approvecomment' :
+                        wp_set_comment_status( $comment_id, 'approve' );
+                        $redir = add_query_arg( array( 'approved' => 1 ), $redir );
+                        break;
+                case 'unapprovecomment' :
+                        wp_set_comment_status( $comment_id, 'hold' );
+                        $redir = add_query_arg( array( 'unapproved' => 1 ), $redir );
+                        break;
+        }        
</ins><span class="cx">
</span><span class="cx">         wp_redirect( $redir );
</span><del>-
</del><span class="cx">         die;
</span><span class="cx">         break;
</span><span class="cx">
</span><del>-case 'approvecomment' :
-case 'unapprovecomment' :
-        $comment_id = absint( $_GET['c'] );
-        check_admin_referer( 'approve-comment_' . $comment_id );
-
-        $noredir = isset( $_GET['noredir'] );
-
-        if ( !$comment = get_comment( $comment_id ) )
-                comment_footer_die( __('Oops, no comment with this ID.') . sprintf(' <a href="%s">'.__('Go back').'</a>!', 'edit.php') );
-
-        if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) ) {
-                if ( 'approvecomment' == $action )
-                        comment_footer_die( __('You are not allowed to edit comments on this post, so you cannot approve this comment.') );
-                else
-                        comment_footer_die( __('You are not allowed to edit comments on this post, so you cannot disapprove this comment.') );
-        }
-
-        if ( '' != wp_get_referer() && false == $noredir )
-                $redir = remove_query_arg( array('approved', 'unapproved'), wp_get_referer() );
-        else
-                $redir = admin_url('edit-comments.php?p=' . absint( $comment->comment_post_ID ) );
-
-        if ( 'approvecomment' == $action ) {
-                wp_set_comment_status( $comment_id, 'approve' );
-                $redir = add_query_arg( array( 'approved' => 1 ), $redir );
-        } else {
-                wp_set_comment_status( $comment_id, 'hold' );
-                $redir = add_query_arg( array( 'unapproved' => 1 ), $redir );
-        }
-
-        wp_redirect( $redir );
-
-        exit();
-        break;
-
</del><span class="cx"> case 'editedcomment' :
</span><span class="cx">
</span><span class="cx">         $comment_id = absint( $_POST['comment_ID'] );
</span></span></pre></div>
<a id="trunkwpadmineditcommentsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/edit-comments.php (13245 => 13246)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/edit-comments.php        2010-02-20 10:50:57 UTC (rev 13245)
+++ trunk/wp-admin/edit-comments.php        2010-02-20 11:18:25 UTC (rev 13246)
</span><span class="lines">@@ -126,45 +126,53 @@
</span><span class="cx"> </h2>
</span><span class="cx">
</span><span class="cx"> <?php
</span><ins>+if ( isset( $_GET['error'] ) ) {
+        $error = (int) $_GET['error'];
+        $error_msg = '';
+        switch ( $error ) {
+                case 1 :
+                        $error_msg = __( 'Oops, no comment with this ID.' );
+                        break;
+                case 2 :
+                        $error_msg = __( 'You are not allowed to edit comments on this post.' );
+                        break;
+        }
+        if ( $error_msg )
+                echo '<div id="moderated" class="error"><p>' . $error_msg . '</p></div>';
+}
+
</ins><span class="cx"> if ( isset($_GET['approved']) || isset($_GET['deleted']) || isset($_GET['trashed']) || isset($_GET['untrashed']) || isset($_GET['spammed']) || isset($_GET['unspammed']) ) {
</span><del>-        $approved = isset($_GET['approved']) ? (int) $_GET['approved'] : 0;
-        $deleted = isset($_GET['deleted']) ? (int) $_GET['deleted'] : 0;
-        $trashed = isset($_GET['trashed']) ? (int) $_GET['trashed'] : 0;
-        $untrashed = isset($_GET['untrashed']) ? (int) $_GET['untrashed'] : 0;
-        $spammed = isset($_GET['spammed']) ? (int) $_GET['spammed'] : 0;
-        $unspammed = isset($_GET['unspammed']) ? (int) $_GET['unspammed'] : 0;
</del><ins>+        $approved = isset( $_GET['approved'] ) ? (int) $_GET['approved'] : 0;
+        $deleted = isset( $_GET['deleted'] ) ? (int) $_GET['deleted'] : 0;
+        $trashed = isset( $_GET['trashed'] ) ? (int) $_GET['trashed'] : 0;
+        $untrashed = isset( $_GET['untrashed'] ) ? (int) $_GET['untrashed'] : 0;
+        $spammed = isset( $_GET['spammed'] ) ? (int) $_GET['spammed'] : 0;
+        $unspammed = isset( $_GET['unspammed'] ) ? (int) $_GET['unspammed'] : 0;
</ins><span class="cx">
</span><span class="cx">         if ( $approved > 0 || $deleted > 0 || $trashed > 0 || $untrashed > 0 || $spammed > 0 || $unspammed > 0 ) {
</span><del>-                echo '<div id="moderated" class="updated"><p>';
</del><ins>+                if ( $approved > 0 )
+                        $messages[] = sprintf( _n( '%s comment approved', '%s comments approved', $approved ), $approved );
</ins><span class="cx">
</span><del>-                if ( $approved > 0 ) {
-                        printf( _n( '%s comment approved', '%s comments approved', $approved ), $approved );
-                        echo '<br />';
-                }
</del><span class="cx">                 if ( $spammed > 0 ) {
</span><del>-                        printf( _n( '%s comment marked as spam.', '%s comments marked as spam.', $spammed ), $spammed );
</del><span class="cx">                         $ids = isset($_GET['ids']) ? $_GET['ids'] : 0;
</span><del>-                        echo ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=unspam&ids=$ids", "bulk-comments" ) ) . '">' . __('Undo') . '</a><br />';
</del><ins>+                        $messages[] = sprintf( _n( '%s comment marked as spam.', '%s comments marked as spam.', $spammed ), $spammed ) . ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=unspam&ids=$ids", "bulk-comments" ) ) . '">' . __('Undo') . '</a><br />';
</ins><span class="cx">                 }
</span><del>-                if ( $unspammed > 0 ) {
-                        printf( _n( '%s comment restored from the spam', '%s comments restored from the spam', $unspammed ), $unspammed );
-                        echo '<br />';
-                }
</del><ins>+
+                if ( $unspammed > 0 )
+                        $messages = sprintf( _n( '%s comment restored from the spam', '%s comments restored from the spam', $unspammed ), $unspammed );
+
</ins><span class="cx">                 if ( $trashed > 0 ) {
</span><del>-                        printf( _n( '%s comment moved to the trash.', '%s comments moved to the trash.', $trashed ), $trashed );
</del><span class="cx">                         $ids = isset($_GET['ids']) ? $_GET['ids'] : 0;
</span><del>-                        echo ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=untrash&ids=$ids", "bulk-comments" ) ) . '">' . __('Undo') . '</a><br />';
</del><ins>+                        $messages[] = sprintf( _n( '%s comment moved to the trash.', '%s comments moved to the trash.', $trashed ), $trashed ) . ' <a href="' . esc_url( wp_nonce_url( "edit-comments.php?doaction=undo&action=untrash&ids=$ids", "bulk-comments" ) ) . '">' . __('Undo') . '</a><br />';
</ins><span class="cx">                 }
</span><del>-                if ( $untrashed > 0 ) {
-                        printf( _n( '%s comment restored from the trash', '%s comments restored from the trash', $untrashed ), $untrashed );
-                        echo '<br />';
-                }
-                if ( $deleted > 0 ) {
-                        printf( _n( '%s comment permanently deleted', '%s comments permanently deleted', $deleted ), $deleted );
-                        echo '<br />';
-                }
</del><span class="cx">
</span><del>-                echo '</p></div>';
</del><ins>+                if ( $untrashed > 0 )
+                        $messages[] = sprintf( _n( '%s comment restored from the trash', '%s comments restored from the trash', $untrashed ), $untrashed );
+
+                if ( $deleted > 0 )
+                        $messages[] = sprintf( _n( '%s comment permanently deleted', '%s comments permanently deleted', $deleted ), $deleted );
+
+                echo '<div id="moderated" class="updated"><p>' . implode( "<br/>\n", $messages ) . '</p></div>';
</ins><span class="cx">         }
</span><span class="cx"> }
</span><span class="cx"> ?>
</span></span></pre>
</div>
</div>
</body>
</html>