Skip to:
Content

BuddyPress.org

Ticket #5097: 5097.02.patch

File 5097.02.patch, 6.6 KB (added by r-a-y, 12 years ago)
  • bp-activity/bp-activity-classes.php

    class BP_Activity_Activity { 
    391391                return $wpdb->get_var( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    392392        }
    393393
     394        /**
     395         * Delete activity items depending on the arguments passed.
     396         *
     397         * @since BuddyPress (1.2)
     398         *
     399         * @todo Make 'id' accept comma-delimited list
     400         * @todo Make 'date_recorded' accept a range
     401         *
     402         * @param array|string $args The parameters to delete notifications either by array or querystring {
     403         *      @type int $id The activity ID.
     404         *      @type string $action The activity action to filter by.
     405         *      @type string $content The activity content to filter by
     406         *      @type string $type The activity type to filter by
     407         *      @type string $primary_link The primary link to filter by
     408         *      @type int $user_id The user ID to filter by
     409         *      @type int $item_id The activity item ID to filter by
     410         *      @type int $secondary_item_id The secondary item ID to filter by
     411         *      @type string $date_recorded The date the activity item was recorded.
     412         *      @type int $hide_sitewide Whether to filter by hidden sitewide items. Either 1 or 0.
     413         * }
     414         */
    394415        public static function delete( $args = array() ) {
    395416                global $wpdb, $bp;
    396417
    class BP_Activity_Activity { 
    453474                // Fetch the activity IDs so we can delete any comments for this activity item
    454475                $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
    455476
    456                 if ( !$wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) )
     477                // Delete the activity item
     478                if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
    457479                        return false;
     480                }
    458481
     482                // Handle accompanying activity comments and meta deletion
    459483                if ( $activity_ids ) {
    460                         BP_Activity_Activity::delete_activity_item_comments( $activity_ids );
    461                         BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
     484                        $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
     485                        $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
    462486
    463                         return $activity_ids;
     487                        // Fetch the activity comment IDs for our deleted activity items
     488                        $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     489
     490                        // We have activity comments!
     491                        if ( ! empty( $activity_comment_ids ) ) {
     492                                // Delete activity comments
     493                                $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     494
     495                                // Merge activity IDs with activity comment IDs
     496                                $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
     497                        }
     498
     499                        // Delete all activity meta entries for activity items and activity comments
     500                        BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
    464501                }
    465502
    466503                return $activity_ids;
    467504        }
    468505
    469         public static function delete_activity_item_comments( $activity_ids = array() ) {
     506        /**
     507         * Delete the activity comments for a given parent activity item or items.
     508         *
     509         * @since BuddyPress (1.2)
     510         *
     511         * @todo Mark as deprecated?  Method is no longer used internally.
     512         *
     513         * @param array $activity_ids Array of parent activity item IDs to check for activity comments.
     514         * @param bool $delete_cache Should we delete the activity meta cache for these comments?
     515         */
     516        public static function delete_activity_item_comments( $activity_ids = array(), $delete_cache = true ) {
    470517                global $bp, $wpdb;
    471518
     519                $delete_cache = (bool) $delete_cache;
     520
    472521                $activity_ids = implode( ',', wp_parse_id_list( $activity_ids ) );
    473522
     523                if ( $delete_cache ) {
     524                        // Fetch the activity comment IDs for our deleted activity items
     525                        $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" );
     526
     527                        if ( ! empty( $activity_comment_ids ) ) {
     528                                self::delete_activity_meta_entries( $activity_comment_ids );
     529                        }
     530                }
     531
    474532                return $wpdb->query( "DELETE FROM {$bp->activity->table_name} WHERE type = 'activity_comment' AND item_id IN ({$activity_ids})" );
    475533        }
    476534
     535        /**
     536         * Deletes the activity meta entries for a given list of activity IDs.
     537         *
     538         * @since BuddyPress (1.2)
     539         *
     540         * @param array $activity_ids Array of activity item IDs to delete meta cache for.
     541         */
    477542        public static function delete_activity_meta_entries( $activity_ids = array() ) {
    478543                global $bp, $wpdb;
    479544
  • tests/testcases/activity/functions.php

    class BP_Tests_Activity_Functions extends BP_UnitTestCase { 
    3434                $this->assertEquals( bp_activity_thumbnail_content_images( $post_content ), '<img src="http://example.com/foo.jpg" width="40" height="40" alt="Thumbnail" class="align-left thumbnail" /> Awesome.' );
    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}