<!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>[BuddyPress] [2644] trunk:
Fixes #1862 - remove all child comments when deleting a comment.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd>2644</dd>
<dt>Author</dt> <dd>apeatling</dd>
<dt>Date</dt> <dd>2010-02-10 15:15:48 +0000 (Wed, 10 Feb 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Fixes #1862 - remove all child comments when deleting a comment.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkbpactivitybpactivityclassesphp">trunk/bp-activity/bp-activity-classes.php</a></li>
<li><a href="#trunkbpactivityphp">trunk/bp-activity.php</a></li>
<li><a href="#trunkbpthemesbpdefault_incajaxphp">trunk/bp-themes/bp-default/_inc/ajax.php</a></li>
<li><a href="#trunkbpthemesbpdefault_incglobaljs">trunk/bp-themes/bp-default/_inc/global.js</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkbpactivitybpactivityclassesphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity/bp-activity-classes.php (2643 => 2644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity/bp-activity-classes.php        2010-02-10 14:11:07 UTC (rev 2643)
+++ trunk/bp-activity/bp-activity-classes.php        2010-02-10 15:15:48 UTC (rev 2644)
</span><span class="lines">@@ -38,6 +38,8 @@
</span><span class="cx">                         $this->content = $row->content;
</span><span class="cx">                         $this->date_recorded = $row->date_recorded;
</span><span class="cx">                         $this->hide_sitewide = $row->hide_sitewide;
</span><ins>+                        $this->mptt_left = $row->mptt_left;
+                        $this->mptt_right = $row->mptt_right;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">
</span><span class="lines">@@ -373,7 +375,7 @@
</span><span class="cx">                 $right = $left + 1;
</span><span class="cx">
</span><span class="cx">                 /* Get all descendants of this node */
</span><del>-                $descendants = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND secondary_item_id = %d", $parent_id ) );
</del><ins>+                $descendants = BP_Activity_Activity::get_child_comments( $parent_id );
</ins><span class="cx">
</span><span class="cx">                 /* Loop the descendants and recalculate the left and right values */
</span><span class="cx">                 foreach ( (array)$descendants as $descendant )
</span><span class="lines">@@ -389,6 +391,12 @@
</span><span class="cx">                 return $right + 1;
</span><span class="cx">         }
</span><span class="cx">
</span><ins>+        function get_child_comments( $parent_id ) {
+                global $bp, $wpdb;
+
+                return $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND secondary_item_id = %d", $parent_id ) );
+        }
+
</ins><span class="cx">         function get_recorded_components() {
</span><span class="cx">                 global $wpdb, $bp;
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpactivityphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-activity.php (2643 => 2644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-activity.php        2010-02-10 14:11:07 UTC (rev 2643)
+++ trunk/bp-activity.php        2010-02-10 15:15:48 UTC (rev 2644)
</span><span class="lines">@@ -686,6 +686,8 @@
</span><span class="cx"> *
</span><span class="cx"> * The action passes one parameter that is a single activity ID or an
</span><span class="cx"> * array of activity IDs depending on the number deleted.
</span><ins>+ *
+ * If you are deleting an activity comment please use bp_activity_delete_comment();
</ins><span class="cx"> */
</span><span class="cx">
</span><span class="cx"> function bp_activity_delete( $args = '' ) {
</span><span class="lines">@@ -754,6 +756,24 @@
</span><span class="cx">         }
</span><span class="cx">         /* End deprecation */
</span><span class="cx">
</span><ins>+function bp_activity_delete_comment( $activity_id, $comment_id ) {
+        /* Recursively delete all children of this comment. */
+        if ( $children = BP_Activity_Activity::get_child_comments( $comment_id ) ) {
+                foreach( (array)$children as $child )
+                        bp_activity_delete_comment( $activity_id, $child->id );
+        }
+        bp_activity_delete( array( 'secondary_item_id' => $comment_id, 'type' => 'activity_comment', 'item_id' => $activity_id ) );
+
+        /* Delete the actual comment */
+        if ( !bp_activity_delete( array( 'id' => $comment_id, 'type' => 'activity_comment' ) ) )
+                return false;
+
+        /* Recalculate the comment tree */
+        BP_Activity_Activity::rebuild_activity_comment_tree( $activity_id );
+
+        return true;
+}
+
</ins><span class="cx"> function bp_activity_get_permalink( $activity_id, $activity_obj = false ) {
</span><span class="cx">         global $bp;
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpthemesbpdefault_incajaxphp"></a>
<div class="modfile"><h4>Modified: trunk/bp-themes/bp-default/_inc/ajax.php (2643 => 2644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-themes/bp-default/_inc/ajax.php        2010-02-10 14:11:07 UTC (rev 2643)
+++ trunk/bp-themes/bp-default/_inc/ajax.php        2010-02-10 15:15:48 UTC (rev 2644)
</span><span class="lines">@@ -225,9 +225,34 @@
</span><span class="cx">
</span><span class="cx">         return true;
</span><span class="cx"> }
</span><del>-add_action( 'wp_ajax_delete_activity_comment', 'bp_dtheme_delete_activity' );
</del><span class="cx"> add_action( 'wp_ajax_delete_activity', 'bp_dtheme_delete_activity' );
</span><span class="cx">
</span><ins>+function bp_dtheme_delete_activity_comment() {
+        global $bp;
+
+        /* Check the nonce */
+        check_admin_referer( 'bp_activity_delete_link' );
+
+        if ( !is_user_logged_in() ) {
+                echo '-1';
+                return false;
+        }
+
+        $comment = new BP_Activity_Activity( $_POST['id'] );
+
+        /* Check access */
+        if ( !is_site_admin() && $comment->user_id != $bp->loggedin_user->id )
+                return false;
+
+        if ( empty( $_POST['id'] ) || !is_numeric( $_POST['id'] ) || !bp_activity_delete_comment( $comment->item_id, $comment->id ) ) {
+                echo '-1<div id="message" class="error"><p>' . __( 'There was a problem when deleting. Please try again.', 'buddypress' ) . '</p></div>';
+                return false;
+        }
+
+        return true;
+}
+add_action( 'wp_ajax_delete_activity_comment', 'bp_dtheme_delete_activity_comment' );
+
</ins><span class="cx"> function bp_dtheme_mark_activity_favorite() {
</span><span class="cx">         global $bp;
</span><span class="cx">
</span></span></pre></div>
<a id="trunkbpthemesbpdefault_incglobaljs"></a>
<div class="modfile"><h4>Modified: trunk/bp-themes/bp-default/_inc/global.js (2643 => 2644)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/bp-themes/bp-default/_inc/global.js        2010-02-10 14:11:07 UTC (rev 2643)
+++ trunk/bp-themes/bp-default/_inc/global.js        2010-02-10 15:15:48 UTC (rev 2644)
</span><span class="lines">@@ -372,6 +372,7 @@
</span><span class="cx">                 if ( target.hasClass('acomment-delete') ) {
</span><span class="cx">                         var link_href = target.attr('href');
</span><span class="cx">                         var comment_li = target.parent().parent();
</span><ins>+                        var form = comment_li.parents('div.activity-comments').children('form');
</ins><span class="cx">
</span><span class="cx">                         var nonce = link_href.split('_wpnonce=');
</span><span class="cx">                                 nonce = nonce[1];
</span><span class="lines">@@ -385,8 +386,11 @@
</span><span class="cx">                         /* Remove any error messages */
</span><span class="cx">                         j('div.activity-comments ul div.error').remove();
</span><span class="cx">
</span><ins>+                        /* Reset the form position */
+                        comment_li.parents('div.activity-comments').append(form);
+
</ins><span class="cx">                         j.post( ajaxurl, {
</span><del>-                                action: 'delete_activity',
</del><ins>+                                action: 'delete_activity_comment',
</ins><span class="cx">                                 'cookie': encodeURIComponent(document.cookie),
</span><span class="cx">                                 '_wpnonce': nonce,
</span><span class="cx">                                 'id': comment_id
</span><span class="lines">@@ -397,17 +401,12 @@
</span><span class="cx">                                 if ( response[0] + response[1] == '-1' ) {
</span><span class="cx">                                         comment_li.prepend( response.substr( 2, response.length ) ).hide().fadeIn( 200 );
</span><span class="cx">                                 } else {
</span><del>-                                        comment_li.fadeOut( 200, function() {
-                                                var children = j( 'li#' + comment_li.attr('id') + ' ul:first' );
</del><ins>+                                        var children = j( 'li#' + comment_li.attr('id') + ' ul' ).children('li');
+                                        comment_li.fadeOut(200);
</ins><span class="cx">
</span><del>-                                                /* Fade in sub comments if any were found. */
-                                                if ( children.length )
-                                                        comment_li.parent().append( children.html() ).hide().fadeIn( 200 );
-                                         });
-
</del><span class="cx">                                         /* Decrease the "Reply (X)" button count */
</span><span class="cx">                                         var parent_li = comment_li.parents('ul#activity-stream > li');
</span><del>-                                        j('li#' + parent_li.attr('id') + ' a.acomment-reply span').html( j('li#' + parent_li.attr('id') + ' a.acomment-reply span').html() - 1 );
</del><ins>+                                        j('li#' + parent_li.attr('id') + ' a.acomment-reply span').html( j('li#' + parent_li.attr('id') + ' a.acomment-reply span').html() - ( 1 + children.length ) );
</ins><span class="cx">                                 }
</span><span class="cx">                         });
</span><span class="cx">
</span></span></pre>
</div>
</div>
</body>
</html>