<!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" />
<title>[BuddyPress][9652] trunk/src/bp-activity/classes/class-bp-activity-activity.php: Activity: Improvements to `BP_Activity_Activity::delete()`:</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { 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 #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#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>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/9652">9652</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/9652","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnjamesjacoby</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-03-26 22:20:02 +0000 (Thu, 26 Mar 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Activity: Improvements to `BP_Activity_Activity::delete()`:

* Select `*` instead of `id` on about-to-be-deleted activity items
* `wp_list_pluck()` the ID's out of the total results for backwards compatibility
* Introduce before/after actions on array of activity items about to be deleted
* Recursively call `BP_Activity_Activity::delete()` on activity comments so new actions are fired on them
* Remove `extract()` usage in place of `$r` array convention

See <a href="http://buddypress.trac.wordpress.org/ticket/6323">#6323</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpactivityclassesclassbpactivityactivityphp">trunk/src/bp-activity/classes/class-bp-activity-activity.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpactivityclassesclassbpactivityactivityphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-activity/classes/class-bp-activity-activity.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-activity/classes/class-bp-activity-activity.php      2015-03-26 22:05:12 UTC (rev 9651)
+++ trunk/src/bp-activity/classes/class-bp-activity-activity.php        2015-03-26 22:20:02 UTC (rev 9652)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1058,8 +1058,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                global $wpdb;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $bp = buddypress();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               $defaults = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $r  = wp_parse_args( $args, array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'id'                => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'action'            => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'content'           => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1071,76 +1070,136 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'secondary_item_id' => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'date_recorded'     => false,
</span><span class="cx" style="display: block; padding: 0 10px">                        'hide_sitewide'     => false
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                );
-               $params = wp_parse_args( $args, $defaults );
-               extract( $params );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $where_args = false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Setup empty array fro where query arguments
+               $where_args = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $id ) )
-                       $where_args[] = $wpdb->prepare( "id = %d", $id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // ID
+               if ( ! empty( $r['id'] ) ) {
+                       $where_args[] = $wpdb->prepare( "id = %d", $r['id'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $user_id ) )
-                       $where_args[] = $wpdb->prepare( "user_id = %d", $user_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // User ID
+               if ( ! empty( $r['user_id'] ) ) {
+                       $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $action ) )
-                       $where_args[] = $wpdb->prepare( "action = %s", $action );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Action
+               if ( ! empty( $r['action'] ) ) {
+                       $where_args[] = $wpdb->prepare( "action = %s", $r['action'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $content ) )
-                       $where_args[] = $wpdb->prepare( "content = %s", $content );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Content
+               if ( ! empty( $r['content'] ) ) {
+                       $where_args[] = $wpdb->prepare( "content = %s", $r['content'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $component ) )
-                       $where_args[] = $wpdb->prepare( "component = %s", $component );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Component
+               if ( ! empty( $r['component'] ) ) {
+                       $where_args[] = $wpdb->prepare( "component = %s", $r['component'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $type ) )
-                       $where_args[] = $wpdb->prepare( "type = %s", $type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Type
+               if ( ! empty( $r['type'] ) ) {
+                       $where_args[] = $wpdb->prepare( "type = %s", $r['type'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $primary_link ) )
-                       $where_args[] = $wpdb->prepare( "primary_link = %s", $primary_link );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Primary Link
+               if ( ! empty( $r['primary_link'] ) ) {
+                       $where_args[] = $wpdb->prepare( "primary_link = %s", $r['primary_link'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $item_id ) )
-                       $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Item ID
+               if ( ! empty( $r['item_id'] ) ) {
+                       $where_args[] = $wpdb->prepare( "item_id = %d", $r['item_id'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $secondary_item_id ) )
-                       $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Secondary item ID
+               if ( ! empty( $r['secondary_item_id'] ) ) {
+                       $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $r['secondary_item_id'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $date_recorded ) )
-                       $where_args[] = $wpdb->prepare( "date_recorded = %s", $date_recorded );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Date Recorded
+               if ( ! empty( $r['date_recorded'] ) ) {
+                       $where_args[] = $wpdb->prepare( "date_recorded = %s", $r['date_recorded'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $hide_sitewide ) )
-                       $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $hide_sitewide );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Hidden sitewide
+               if ( ! empty( $r['hide_sitewide'] ) ) {
+                       $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $r['hide_sitewide'] );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( !empty( $where_args ) )
-                       $where_sql = 'WHERE ' . join( ' AND ', $where_args );
-               else
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Bail if no where arguments
+               if ( empty( $where_args ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Fetch the activity IDs so we can delete any comments for this activity item
-               $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Join the where arguments for querying
+               $where_sql = 'WHERE ' . join( ' AND ', $where_args );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Fetch all activities being deleted so we can perform more actions
+               $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} {$where_sql}" );
+
+               /**
+                * Action to allow intercepting activity items to be deleted
+                *
+                * @since BuddyPress (2.3.0)
+                *
+                * @param array $activities Array of activities
+                * @param array $r          Array of parsed arguments
+                */
+               do_action_ref_array( 'bp_activity_before_delete', array( $activities, $r ) );
+
+               // Attempt to delete activities from the database
+               $deleted = $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" );
+
+               // Bail if nothing was deleted
+               if ( empty( $deleted ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         return false;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /**
+                * Action to allow intercepting activity items just deleted
+                *
+                * @since BuddyPress (2.3.0)
+                *
+                * @param array $activities Array of activities
+                * @param array $r          Array of parsed arguments
+                */
+               do_action_ref_array( 'bp_activity_after_delete', array( $activities, $r ) );
+
+               // Pluck the activity ID's out of the $activities array
+               $activity_ids = wp_parse_id_list( wp_list_pluck( $activities, 'id' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Handle accompanying activity comments and meta deletion
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( $activity_ids ) {
-                       $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
-                       $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! empty( $activity_ids ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Fetch the activity comment IDs for our deleted activity items
-                       $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Delete all activity meta entries for activity items
+                       BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // We have activity comments!
-                       if ( ! empty( $activity_comment_ids ) ) {
-                               // Delete activity comments
-                               $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Setup empty array for comments
+                       $comment_ids = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                // Merge activity IDs with activity comment IDs
-                               $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Loop through activity ids and attempt to delete comments
+                       foreach ( $activity_ids as $activity_id ) {
+
+                               // Attempt to delete comments
+                               $comments = BP_Activity_Activity::delete( array(
+                                       'type'    => 'activity_comment',
+                                       'item_id' => $activity_id
+                               ) );
+
+                               // Merge ID's together
+                               if ( ! empty( $comments ) ) {
+                                       $comment_ids = array_merge( $comment_ids, $comments );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Delete all activity meta entries for activity items and activity comments
-                       BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Merge activity IDs with any deleted comment IDs
+                       if ( ! empty( $comment_ids ) ) {
+                               $activity_ids = array_unique( array_merge( $activity_ids, $comment_ids ) );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                return $activity_ids;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1149,10 +1208,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Delete the comments associated with a set of activity items.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * This method is no longer used by BuddyPress, and it is recommended not to
+        * use it going forward, and use BP_Activity_Activity::delete() instead.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @since BuddyPress (1.2.0)
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @deprecated BuddyPress (2.3.0)
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @todo Mark as deprecated?  Method is no longer used internally.
-        *
</del><span class="cx" style="display: block; padding: 0 10px">          * @param array $activity_ids Activity IDs whose comments should be deleted.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param bool $delete_meta Should we delete the activity meta items for these comments?
</span><span class="cx" style="display: block; padding: 0 10px">         * @return bool True on success.
</span></span></pre>
</div>
</div>

</body>
</html>