Skip to:
Content

BuddyPress.org

Changeset 7422


Ignore:
Timestamp:
10/13/2013 02:36:04 AM (13 years ago)
Author:
r-a-y
Message:

Delete activity comment meta when deleting parent activity items.

Previously, when deleting parent activity items, BP would delete the
related activity comments, but not the activity comment metadata.

To solve this, activity comments are now queried and removed in the
main BP_Activity_Activity::delete() method and the activity comment
IDs are now properly passed to the delete_activity_meta_entries()
method.

Commit also includes a unit test for this issue.

Hat-tip boonebgorges for feedback.

Fixes #5097.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-classes.php

    r7403 r7422  
    551551         * Otherwise use the filters.
    552552         *
     553         * @since BuddyPress (1.2)
     554         *
    553555         * @param array $args {
    554556         *     @int $id Optional. The ID of a specific item to delete.
     
    628630                $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    629631
    630                 if ( !$wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) )
     632                if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
    631633                        return false;
    632 
     634                }
     635
     636                // Handle accompanying activity comments and meta deletion
    633637                if ( $activity_ids ) {
    634                         BP_Activity_Activity::delete_activity_item_comments( $activity_ids );
     638                        $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
     639                        $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
     640
     641                        // Fetch the activity comment IDs for our deleted activity items
     642                        $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     643
     644                        // We have activity comments!
     645                        if ( ! empty( $activity_comment_ids ) ) {
     646                                // Delete activity comments
     647                                $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     648
     649                                // Merge activity IDs with activity comment IDs
     650                                $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
     651                        }
     652
     653                        // Delete all activity meta entries for activity items and activity comments
    635654                        BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
    636 
    637                         return $activity_ids;
    638655                }
    639656
     
    644661         * Delete the comments associated with a set of activity items.
    645662         *
     663         * @since BuddyPress (1.2)
     664         *
     665         * @todo Mark as deprecated?  Method is no longer used internally.
     666         *
    646667         * @param array $activity_ids Activity IDs whose comments should be deleted.
     668         * @param bool $delete_meta Should we delete the activity meta items for these comments?
    647669         * @return bool True on success.
    648670         */
    649         public static function delete_activity_item_comments( $activity_ids = array() ) {
     671        public static function delete_activity_item_comments( $activity_ids = array(), $delete_meta = true ) {
    650672                global $bp, $wpdb;
    651673
     674                $delete_meta = (bool) $delete_meta;
     675
    652676                $activity_ids = implode( ',', wp_parse_id_list( $activity_ids ) );
    653677
     678                if ( $delete_meta ) {
     679                        // Fetch the activity comment IDs for our deleted activity items
     680                        $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" );
     681
     682                        if ( ! empty( $activity_comment_ids ) ) {
     683                                self::delete_activity_meta_entries( $activity_comment_ids );
     684                        }
     685                }
     686
    654687                return $wpdb->query( "DELETE FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" );
    655688        }
     
    657690        /**
    658691         * Delete the meta entries associated with a set of activity items.
     692         *
     693         * @since BuddyPress (1.2)
    659694         *
    660695         * @param array $activity_ids Activity IDs whose meta should be deleted.
  • trunk/tests/testcases/activity/functions.php

    r7039 r7422  
    3535        }
    3636
     37        /**
     38         * @group delete
     39         */
     40        public function test_delete_activity_and_meta() {
     41                // create an activity update
     42                $parent_activity = $this->factory->activity->create( array(
     43                        'type' => 'activity_update',
     44                ) );
     45
     46                // create some activity comments
     47                $comment_one = $this->factory->activity->create( array(
     48                        'type'              => 'activity_comment',
     49                        'item_id'           => $parent_activity,
     50                        'secondary_item_id' => $parent_activity,
     51                ) );
     52
     53                $comment_two = $this->factory->activity->create( array(
     54                        'type'              => 'activity_comment',
     55                        'item_id'           => $parent_activity,
     56                        'secondary_item_id' => $parent_activity,
     57                ) );
     58
     59                // add some meta to the activity items
     60                bp_activity_update_meta( $parent_activity, 'foo', 'bar' );
     61                bp_activity_update_meta( $comment_one,     'foo', 'bar' );
     62                bp_activity_update_meta( $comment_two,     'foo', 'bar' );
     63
     64                // now delete the parent activity item
     65                // this should hopefully delete the associated comments and meta entries
     66                bp_activity_delete( array(
     67                        'id' => $parent_activity
     68                ) );
     69
     70                // now fetch the deleted activity entries
     71                $get = bp_activity_get( array(
     72                        'in'               => array( $parent_activity, $comment_one, $comment_two ),
     73                        'display_comments' => 'stream'
     74                ) );
     75
     76                // activities should equal zero
     77                $this->assertEquals( 0, $get['total'] );
     78
     79                // now fetch activity meta for the deleted activity entries
     80                $m1 = bp_activity_get_meta( $parent_activity );
     81                $m2 = bp_activity_get_meta( $comment_one );
     82                $m3 = bp_activity_get_meta( $comment_two );
     83
     84                // test if activity meta entries still exist
     85                $this->assertEquals( false, $m1 );
     86                $this->assertEquals( false, $m2 );
     87                $this->assertEquals( false, $m3 );
     88        }
    3789}
Note: See TracChangeset for help on using the changeset viewer.