Skip to:
Content

BuddyPress.org

Ticket #6323: 6323.01.patch

File 6323.01.patch, 7.0 KB (added by johnjamesjacoby, 11 years ago)
  • src/bp-activity/classes/class-bp-activity-activity.php

     
    10581058                global $wpdb;
    10591059
    10601060                $bp = buddypress();
    1061 
    1062                 $defaults = array(
     1061                $r  = wp_parse_args( $args, array(
    10631062                        'id'                => false,
    10641063                        'action'            => false,
    10651064                        'content'           => false,
     
    10711070                        'secondary_item_id' => false,
    10721071                        'date_recorded'     => false,
    10731072                        'hide_sitewide'     => false
    1074                 );
    1075                 $params = wp_parse_args( $args, $defaults );
    1076                 extract( $params );
     1073                ) );
    10771074
    1078                 $where_args = false;
     1075                // Setup empty array fro where query arguments
     1076                $where_args = array();
    10791077
    1080                 if ( !empty( $id ) )
    1081                         $where_args[] = $wpdb->prepare( "id = %d", $id );
     1078                // ID
     1079                if ( ! empty( $r['id'] ) ) {
     1080                        $where_args[] = $wpdb->prepare( "id = %d", $r['id'] );
     1081                }
    10821082
    1083                 if ( !empty( $user_id ) )
    1084                         $where_args[] = $wpdb->prepare( "user_id = %d", $user_id );
     1083                // User ID
     1084                if ( ! empty( $r['user_id'] ) ) {
     1085                        $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] );
     1086                }
    10851087
    1086                 if ( !empty( $action ) )
    1087                         $where_args[] = $wpdb->prepare( "action = %s", $action );
     1088                // Action
     1089                if ( ! empty( $r['action'] ) ) {
     1090                        $where_args[] = $wpdb->prepare( "action = %s", $r['action'] );
     1091                }
    10881092
    1089                 if ( !empty( $content ) )
    1090                         $where_args[] = $wpdb->prepare( "content = %s", $content );
     1093                // Content
     1094                if ( ! empty( $r['content'] ) ) {
     1095                        $where_args[] = $wpdb->prepare( "content = %s", $r['content'] );
     1096                }
    10911097
    1092                 if ( !empty( $component ) )
    1093                         $where_args[] = $wpdb->prepare( "component = %s", $component );
     1098                // Component
     1099                if ( ! empty( $r['component'] ) ) {
     1100                        $where_args[] = $wpdb->prepare( "component = %s", $r['component'] );
     1101                }
    10941102
    1095                 if ( !empty( $type ) )
    1096                         $where_args[] = $wpdb->prepare( "type = %s", $type );
     1103                // Type
     1104                if ( ! empty( $r['type'] ) ) {
     1105                        $where_args[] = $wpdb->prepare( "type = %s", $r['type'] );
     1106                }
    10971107
    1098                 if ( !empty( $primary_link ) )
    1099                         $where_args[] = $wpdb->prepare( "primary_link = %s", $primary_link );
     1108                // Primary Link
     1109                if ( ! empty( $r['primary_link'] ) ) {
     1110                        $where_args[] = $wpdb->prepare( "primary_link = %s", $r['primary_link'] );
     1111                }
    11001112
    1101                 if ( !empty( $item_id ) )
    1102                         $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
     1113                // Item ID
     1114                if ( ! empty( $r['item_id'] ) ) {
     1115                        $where_args[] = $wpdb->prepare( "item_id = %d", $r['item_id'] );
     1116                }
    11031117
    1104                 if ( !empty( $secondary_item_id ) )
    1105                         $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
     1118                // Secondary item ID
     1119                if ( ! empty( $r['secondary_item_id'] ) ) {
     1120                        $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $r['secondary_item_id'] );
     1121                }
    11061122
    1107                 if ( !empty( $date_recorded ) )
    1108                         $where_args[] = $wpdb->prepare( "date_recorded = %s", $date_recorded );
     1123                // Date Recorded
     1124                if ( ! empty( $r['date_recorded'] ) ) {
     1125                        $where_args[] = $wpdb->prepare( "date_recorded = %s", $r['date_recorded'] );
     1126                }
    11091127
    1110                 if ( !empty( $hide_sitewide ) )
    1111                         $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $hide_sitewide );
     1128                // Hidden sitewide
     1129                if ( ! empty( $r['hide_sitewide'] ) ) {
     1130                        $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $r['hide_sitewide'] );
     1131                }
    11121132
    1113                 if ( !empty( $where_args ) )
    1114                         $where_sql = 'WHERE ' . join( ' AND ', $where_args );
    1115                 else
     1133                // Bail if no where arguments
     1134                if ( empty( $where_args ) ) {
    11161135                        return false;
     1136                }
    11171137
    1118                 // Fetch the activity IDs so we can delete any comments for this activity item
    1119                 $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" );
     1138                // Join the where arguments for querying
     1139                $where_sql = 'WHERE ' . join( ' AND ', $where_args );
    11201140
    1121                 if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
     1141                // Fetch all activities being deleted so we can perform more actions
     1142                $activities = $wpdb->get_results( "SELECT * FROM {$bp->activity->table_name} {$where_sql}" );
     1143
     1144                // Give plugins a chance to see the activity items being deleted
     1145                do_action_ref_array( 'bp_activity_before_delete', array( $activities, $r ) );
     1146
     1147                // Attempt to delete activities from the database
     1148                $deleted = $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" );
     1149
     1150                // Bail if nothing was deleted
     1151                if ( empty( $deleted ) ) {
    11221152                        return false;
    11231153                }
    11241154
     1155                // Give plugins a chance to see the activity items being deleted
     1156                do_action_ref_array( 'bp_activity_after_delete', array( $activities, $r ) );
     1157
     1158                // Pluck the activity ID's out of the $activities array
     1159                $activity_ids = wp_parse_id_list( wp_list_pluck( $activities, 'id' ) );
     1160
    11251161                // Handle accompanying activity comments and meta deletion
    1126                 if ( $activity_ids ) {
    1127                         $activity_ids_comma          = implode( ',', wp_parse_id_list( $activity_ids ) );
    1128                         $activity_comments_where_sql = "WHERE type = 'activity_comment' AND item_id IN ({$activity_ids_comma})";
     1162                if ( ! empty( $activity_ids ) ) {
    11291163
    1130                         // Fetch the activity comment IDs for our deleted activity items
    1131                         $activity_comment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     1164                        // Delete all activity meta entries for activity items
     1165                        BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
    11321166
    1133                         // We have activity comments!
    1134                         if ( ! empty( $activity_comment_ids ) ) {
    1135                                 // Delete activity comments
    1136                                 $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );
     1167                        // Setup empty array for comments
     1168                        $comment_ids = array();
    11371169
    1138                                 // Merge activity IDs with activity comment IDs
    1139                                 $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
     1170                        // Loop through activity ids and attempt to delete comments
     1171                        foreach ( $activity_ids as $activity_id ) {
     1172
     1173                                // Attempt to delete comments
     1174                                $comments = BP_Activity_Activity::delete( array(
     1175                                        'type'    => 'activity_comment',
     1176                                        'item_id' => $activity_id
     1177                                ) );
     1178
     1179                                // Merge ID's together
     1180                                if ( ! empty( $comments ) ) {
     1181                                        $comment_ids = array_merge( $comment_ids, $comments );
     1182                                }
    11401183                        }
    11411184
    1142                         // Delete all activity meta entries for activity items and activity comments
    1143                         BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
     1185                        // Merge activity IDs with any deleted comment IDs
     1186                        if ( ! empty( $comment_ids ) ) {
     1187                                $activity_ids = array_unique( array_merge( $activity_ids, $comment_ids ) );
     1188                        }
    11441189                }
    11451190
    11461191                return $activity_ids;
     
    11491194        /**
    11501195         * Delete the comments associated with a set of activity items.
    11511196         *
     1197         * This method is no longer used by BuddyPress, and it is recommended not to
     1198         * use it going forward, and use BP_Activity_Activity::delete() instead.
     1199         *
    11521200         * @since BuddyPress (1.2.0)
     1201         * @deprecated BuddyPress (2.3.0)
    11531202         *
    1154          * @todo Mark as deprecated?  Method is no longer used internally.
    1155          *
    11561203         * @param array $activity_ids Activity IDs whose comments should be deleted.
    11571204         * @param bool $delete_meta Should we delete the activity meta items for these comments?
    11581205         * @return bool True on success.