Skip to:
Content

BuddyPress.org

Changeset 9652


Ignore:
Timestamp:
03/26/2015 10:20:02 PM (11 years ago)
Author:
johnjamesjacoby
Message:

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 #6323.

File:
1 edited

Legend:

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

    r9644 r9652  
    10591059
    10601060                $bp = buddypress();
    1061 
    1062                 $defaults = array(
     1061                $r  = wp_parse_args( $args, array(
    10631062                        'id'                => false,
    10641063                        'action'            => false,
     
    10721071                        'date_recorded'     => false,
    10731072                        'hide_sitewide'     => false
    1074                 );
    1075                 $params = wp_parse_args( $args, $defaults );
    1076                 extract( $params );
    1077 
    1078                 $where_args = false;
    1079 
    1080                 if ( !empty( $id ) )
    1081                         $where_args[] = $wpdb->prepare( "id = %d", $id );
    1082 
    1083                 if ( !empty( $user_id ) )
    1084                         $where_args[] = $wpdb->prepare( "user_id = %d", $user_id );
    1085 
    1086                 if ( !empty( $action ) )
    1087                         $where_args[] = $wpdb->prepare( "action = %s", $action );
    1088 
    1089                 if ( !empty( $content ) )
    1090                         $where_args[] = $wpdb->prepare( "content = %s", $content );
    1091 
    1092                 if ( !empty( $component ) )
    1093                         $where_args[] = $wpdb->prepare( "component = %s", $component );
    1094 
    1095                 if ( !empty( $type ) )
    1096                         $where_args[] = $wpdb->prepare( "type = %s", $type );
    1097 
    1098                 if ( !empty( $primary_link ) )
    1099                         $where_args[] = $wpdb->prepare( "primary_link = %s", $primary_link );
    1100 
    1101                 if ( !empty( $item_id ) )
    1102                         $where_args[] = $wpdb->prepare( "item_id = %d", $item_id );
    1103 
    1104                 if ( !empty( $secondary_item_id ) )
    1105                         $where_args[] = $wpdb->prepare( "secondary_item_id = %d", $secondary_item_id );
    1106 
    1107                 if ( !empty( $date_recorded ) )
    1108                         $where_args[] = $wpdb->prepare( "date_recorded = %s", $date_recorded );
    1109 
    1110                 if ( !empty( $hide_sitewide ) )
    1111                         $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $hide_sitewide );
    1112 
    1113                 if ( !empty( $where_args ) )
    1114                         $where_sql = 'WHERE ' . join( ' AND ', $where_args );
    1115                 else
     1073                ) );
     1074
     1075                // Setup empty array fro where query arguments
     1076                $where_args = array();
     1077
     1078                // ID
     1079                if ( ! empty( $r['id'] ) ) {
     1080                        $where_args[] = $wpdb->prepare( "id = %d", $r['id'] );
     1081                }
     1082
     1083                // User ID
     1084                if ( ! empty( $r['user_id'] ) ) {
     1085                        $where_args[] = $wpdb->prepare( "user_id = %d", $r['user_id'] );
     1086                }
     1087
     1088                // Action
     1089                if ( ! empty( $r['action'] ) ) {
     1090                        $where_args[] = $wpdb->prepare( "action = %s", $r['action'] );
     1091                }
     1092
     1093                // Content
     1094                if ( ! empty( $r['content'] ) ) {
     1095                        $where_args[] = $wpdb->prepare( "content = %s", $r['content'] );
     1096                }
     1097
     1098                // Component
     1099                if ( ! empty( $r['component'] ) ) {
     1100                        $where_args[] = $wpdb->prepare( "component = %s", $r['component'] );
     1101                }
     1102
     1103                // Type
     1104                if ( ! empty( $r['type'] ) ) {
     1105                        $where_args[] = $wpdb->prepare( "type = %s", $r['type'] );
     1106                }
     1107
     1108                // Primary Link
     1109                if ( ! empty( $r['primary_link'] ) ) {
     1110                        $where_args[] = $wpdb->prepare( "primary_link = %s", $r['primary_link'] );
     1111                }
     1112
     1113                // Item ID
     1114                if ( ! empty( $r['item_id'] ) ) {
     1115                        $where_args[] = $wpdb->prepare( "item_id = %d", $r['item_id'] );
     1116                }
     1117
     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                }
     1122
     1123                // Date Recorded
     1124                if ( ! empty( $r['date_recorded'] ) ) {
     1125                        $where_args[] = $wpdb->prepare( "date_recorded = %s", $r['date_recorded'] );
     1126                }
     1127
     1128                // Hidden sitewide
     1129                if ( ! empty( $r['hide_sitewide'] ) ) {
     1130                        $where_args[] = $wpdb->prepare( "hide_sitewide = %d", $r['hide_sitewide'] );
     1131                }
     1132
     1133                // Bail if no where arguments
     1134                if ( empty( $where_args ) ) {
    11161135                        return false;
    1117 
    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}" );
    1120 
    1121                 if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) {
     1136                }
     1137
     1138                // Join the where arguments for querying
     1139                $where_sql = 'WHERE ' . join( ' AND ', $where_args );
     1140
     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                /**
     1145                 * Action to allow intercepting activity items to be deleted
     1146                 *
     1147                 * @since BuddyPress (2.3.0)
     1148                 *
     1149                 * @param array $activities Array of activities
     1150                 * @param array $r          Array of parsed arguments
     1151                 */
     1152                do_action_ref_array( 'bp_activity_before_delete', array( $activities, $r ) );
     1153
     1154                // Attempt to delete activities from the database
     1155                $deleted = $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" );
     1156
     1157                // Bail if nothing was deleted
     1158                if ( empty( $deleted ) ) {
    11221159                        return false;
    11231160                }
    11241161
     1162                /**
     1163                 * Action to allow intercepting activity items just deleted
     1164                 *
     1165                 * @since BuddyPress (2.3.0)
     1166                 *
     1167                 * @param array $activities Array of activities
     1168                 * @param array $r          Array of parsed arguments
     1169                 */
     1170                do_action_ref_array( 'bp_activity_after_delete', array( $activities, $r ) );
     1171
     1172                // Pluck the activity ID's out of the $activities array
     1173                $activity_ids = wp_parse_id_list( wp_list_pluck( $activities, 'id' ) );
     1174
    11251175                // 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})";
    1129 
    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}" );
    1132 
    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}" );
    1137 
    1138                                 // Merge activity IDs with activity comment IDs
    1139                                 $activity_ids = array_merge( $activity_ids, $activity_comment_ids );
    1140                         }
    1141 
    1142                         // Delete all activity meta entries for activity items and activity comments
     1176                if ( ! empty( $activity_ids ) ) {
     1177
     1178                        // Delete all activity meta entries for activity items
    11431179                        BP_Activity_Activity::delete_activity_meta_entries( $activity_ids );
     1180
     1181                        // Setup empty array for comments
     1182                        $comment_ids = array();
     1183
     1184                        // Loop through activity ids and attempt to delete comments
     1185                        foreach ( $activity_ids as $activity_id ) {
     1186
     1187                                // Attempt to delete comments
     1188                                $comments = BP_Activity_Activity::delete( array(
     1189                                        'type'    => 'activity_comment',
     1190                                        'item_id' => $activity_id
     1191                                ) );
     1192
     1193                                // Merge ID's together
     1194                                if ( ! empty( $comments ) ) {
     1195                                        $comment_ids = array_merge( $comment_ids, $comments );
     1196                                }
     1197                        }
     1198
     1199                        // Merge activity IDs with any deleted comment IDs
     1200                        if ( ! empty( $comment_ids ) ) {
     1201                                $activity_ids = array_unique( array_merge( $activity_ids, $comment_ids ) );
     1202                        }
    11441203                }
    11451204
     
    11501209         * Delete the comments associated with a set of activity items.
    11511210         *
     1211         * This method is no longer used by BuddyPress, and it is recommended not to
     1212         * use it going forward, and use BP_Activity_Activity::delete() instead.
     1213         *
    11521214         * @since BuddyPress (1.2.0)
    1153          *
    1154          * @todo Mark as deprecated?  Method is no longer used internally.
     1215         * @deprecated BuddyPress (2.3.0)
    11551216         *
    11561217         * @param array $activity_ids Activity IDs whose comments should be deleted.
Note: See TracChangeset for help on using the changeset viewer.