<!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>[12148] trunk: Trash comments when trashing a post.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12148">12148</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2009-11-05 21:03:09 +0000 (Thu, 05 Nov 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>Trash comments when trashing a post. see <a href="http://trac.wordpress.org/ticket/11073">#11073</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpcommentspostphp">trunk/wp-comments-post.php</a></li>
<li><a href="#trunkwpincludescommentphp">trunk/wp-includes/comment.php</a></li>
<li><a href="#trunkwpincludespostphp">trunk/wp-includes/post.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpcommentspostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-comments-post.php (12147 => 12148)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-comments-post.php        2009-11-05 20:28:46 UTC (rev 12147)
+++ trunk/wp-comments-post.php        2009-11-05 21:03:09 UTC (rev 12148)
</span><span class="lines">@@ -30,6 +30,9 @@
</span><span class="cx"> } elseif ( in_array($status-&gt;post_status, array('draft', 'pending') ) ) {
</span><span class="cx">         do_action('comment_on_draft', $comment_post_ID);
</span><span class="cx">         exit;
</span><ins>+} elseif ( 'trash' == $status-&gt;post_status ) {
+        do_action('comment_on_trash', $comment_post_ID);
+        exit;
</ins><span class="cx"> } else {
</span><span class="cx">         do_action('pre_comment_on_post', $comment_post_ID);
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkwpincludescommentphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/comment.php (12147 => 12148)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/comment.php        2009-11-05 20:28:46 UTC (rev 12147)
+++ trunk/wp-includes/comment.php        2009-11-05 21:03:09 UTC (rev 12148)
</span><span class="lines">@@ -773,10 +773,12 @@
</span><span class="cx">         $count = $wpdb-&gt;get_results( &quot;SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb-&gt;comments} {$where} GROUP BY comment_approved&quot;, ARRAY_A );
</span><span class="cx"> 
</span><span class="cx">         $total = 0;
</span><del>-        $approved = array('0' =&gt; 'moderated', '1' =&gt; 'approved', 'spam' =&gt; 'spam', 'trash' =&gt; 'trash');
</del><ins>+        $approved = array('0' =&gt; 'moderated', '1' =&gt; 'approved', 'spam' =&gt; 'spam', 'trash' =&gt; 'trash', 'post-trashed' =&gt; 'post-trashed');
</ins><span class="cx">         $known_types = array_keys( $approved );
</span><span class="cx">         foreach( (array) $count as $row_num =&gt; $row ) {
</span><del>-                $total += $row['num_comments'];
</del><ins>+                // Don't count post-trashed toward totals
+                if ( 'post-trashed' != $row['comment_approved'] )
+                        $total += $row['num_comments'];
</ins><span class="cx">                 if ( in_array( $row['comment_approved'], $known_types ) )
</span><span class="cx">                         $stats[$approved[$row['comment_approved']]] = $row['num_comments'];
</span><span class="cx">         }
</span><span class="lines">@@ -1178,9 +1180,13 @@
</span><span class="cx">         $status = '0';
</span><span class="cx">         switch ( $comment_status ) {
</span><span class="cx">                 case 'hold':
</span><ins>+                case '0':
+                case 0:
</ins><span class="cx">                         $status = '0';
</span><span class="cx">                         break;
</span><span class="cx">                 case 'approve':
</span><ins>+                case '1':
+                case 1:
</ins><span class="cx">                         $status = '1';
</span><span class="cx">                         if ( get_option('comments_notify') ) {
</span><span class="cx">                                 $comment = get_comment($comment_id);
</span></span></pre></div>
<a id="trunkwpincludespostphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/post.php (12147 => 12148)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/post.php        2009-11-05 20:28:46 UTC (rev 12147)
+++ trunk/wp-includes/post.php        2009-11-05 21:03:09 UTC (rev 12148)
</span><span class="lines">@@ -1268,6 +1268,8 @@
</span><span class="cx">         $post['post_status'] = 'trash';
</span><span class="cx">         wp_insert_post($post);
</span><span class="cx"> 
</span><ins>+        wp_trash_post_comments($post_id);
+
</ins><span class="cx">         do_action('trashed_post', $post_id);
</span><span class="cx"> 
</span><span class="cx">         return $post;
</span><span class="lines">@@ -1301,12 +1303,101 @@
</span><span class="cx"> 
</span><span class="cx">         wp_insert_post($post);
</span><span class="cx"> 
</span><ins>+        wp_untrash_post_comments($post_id);
+
</ins><span class="cx">         do_action('untrashed_post', $post_id);
</span><span class="cx"> 
</span><span class="cx">         return $post;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /**
</span><ins>+ * Moves comments for a post to the trash
+ *
+ * @since 2.9.0
+ * @uses do_action() on 'trash_post_comments' before trashing
+ * @uses do_action() on 'trashed_post_comments' after trashing
+ *
+ * @param int $post Post ID or object.
+ * @return mixed False on failure
+ */
+function wp_trash_post_comments($post = null) {
+        global $wpdb;
+
+        $post = get_post($post);
+        if ( empty($post) )
+                return;
+
+        $post_id = $post-&gt;ID;
+
+        do_action('trash_post_comments', $post_id);
+
+        $comments = $wpdb-&gt;get_results( $wpdb-&gt;prepare(&quot;SELECT comment_ID, comment_approved FROM $wpdb-&gt;comments WHERE comment_post_ID = %d&quot;, $post_id) );
+        if ( empty($comments) )
+                return;
+
+        // Cache current status for each comment
+        $statuses = array();
+        foreach ( $comments as $comment )
+                $statuses[$comment-&gt;comment_ID] = $comment-&gt;comment_approved;
+        add_post_meta($post_id, '_wp_trash_meta_comments_status', $statuses);
+
+        // Set status for all comments to post-trashed
+        $result = $wpdb-&gt;update($wpdb-&gt;comments, array('comment_approved' =&gt; 'post-trashed'), array('comment_post_ID' =&gt; $post_id));
+
+        clean_comment_cache( array_keys($statuses) );
+
+        do_action('trashed_post_comments', $post_id, $statuses);
+
+        return $result;
+}
+
+/**
+ * Restore comments for a post from the trash
+ *
+ * @since 2.9.0
+ * @uses do_action() on 'untrash_post_comments' before trashing
+ * @uses do_action() on 'untrashed_post_comments' after trashing
+ *
+ * @param int $post Post ID or object.
+ * @return mixed False on failure
+ */
+function wp_untrash_post_comments($post = null) {
+        global $wpdb;
+
+        $post = get_post($post);
+        if ( empty($post) )
+                return;
+
+        $post_id = $post-&gt;ID;
+
+        $statuses = get_post_meta($post_id, '_wp_trash_meta_comments_status', true);
+
+        if ( empty($statuses) )
+                return true;
+
+        do_action('untrash_post_comments', $post_id);
+
+        // Restore each comment to its original status
+        $group_by_status = array();
+        foreach ( $statuses as $comment_id =&gt; $comment_status )
+                $group_by_status[$comment_status][] = $comment_id;
+
+        foreach ( $group_by_status as $status =&gt; $comments ) {
+                // Sanity check. This shouldn't happen.
+                if ( 'post-trashed' == $status )
+                        $status = '0';
+                $comments_in = implode( &quot;', '&quot;, $comments );
+                $wpdb-&gt;query( &quot;UPDATE $wpdb-&gt;comments SET comment_approved = '$status' WHERE comment_ID IN ('&quot; . $comments_in . &quot;')&quot; );
+        }
+
+        clean_comment_cache( array_keys($statuses) );
+
+        delete_post_meta($post_id, '_wp_trash_meta_comments_status');
+
+        do_action('untrashed_post_comments', $post_id);
+}
+
+/**
</ins><span class="cx">  * Retrieve the list of categories for a post.
</span><span class="cx">  *
</span><span class="cx">  * Compatibility layer for themes and plugins. Also an easy layer of abstraction
</span></span></pre>
</div>
</div>

</body>
</html>