/** * @group delete_activity_item_comments */ public function test_delete_activity_item_comments() { $parent_activity = $this->factory->activity->create(array('type' => 'activity_update')); $comments = $this->factory->activity->create_many(3, array('item_id' => $parent_activity, 'type' => 'activity_comment')); BP_Activity_Activity::delete_activity_item_comments($parent_activity); $result = BP_Activity_Activity::get(array('in' => wp_list_pluck($comments, 'id'))); $this->assertEmpty($result['activities']); }
function delete($args) { global $nxtdb, $bp; $defaults = array('id' => false, 'action' => false, 'content' => false, 'component' => false, 'type' => false, 'primary_link' => false, 'user_id' => false, 'item_id' => false, 'secondary_item_id' => false, 'date_recorded' => false, 'hide_sitewide' => false); $params = nxt_parse_args($args, $defaults); extract($params); $where_args = false; if (!empty($id)) { $where_args[] = $nxtdb->prepare("id = %d", $id); } if (!empty($user_id)) { $where_args[] = $nxtdb->prepare("user_id = %d", $user_id); } if (!empty($action)) { $where_args[] = $nxtdb->prepare("action = %s", $action); } if (!empty($content)) { $where_args[] = $nxtdb->prepare("content = %s", $content); } if (!empty($component)) { $where_args[] = $nxtdb->prepare("component = %s", $component); } if (!empty($type)) { $where_args[] = $nxtdb->prepare("type = %s", $type); } if (!empty($primary_link)) { $where_args[] = $nxtdb->prepare("primary_link = %s", $primary_link); } if (!empty($item_id)) { $where_args[] = $nxtdb->prepare("item_id = %s", $item_id); } if (!empty($secondary_item_id)) { $where_args[] = $nxtdb->prepare("secondary_item_id = %s", $secondary_item_id); } if (!empty($date_recorded)) { $where_args[] = $nxtdb->prepare("date_recorded = %s", $date_recorded); } if (!empty($hide_sitewide)) { $where_args[] = $nxtdb->prepare("hide_sitewide = %d", $hide_sitewide); } if (!empty($where_args)) { $where_sql = 'WHERE ' . join(' AND ', $where_args); } else { return false; } // Fetch the activity IDs so we can delete any comments for this activity item $activity_ids = $nxtdb->get_col($nxtdb->prepare("SELECT id FROM {$bp->activity->table_name} {$where_sql}")); if (!$nxtdb->query($nxtdb->prepare("DELETE FROM {$bp->activity->table_name} {$where_sql}"))) { return false; } if ($activity_ids) { BP_Activity_Activity::delete_activity_item_comments($activity_ids); BP_Activity_Activity::delete_activity_meta_entries($activity_ids); return $activity_ids; } return $activity_ids; }
/** * CLEANUP DATABASE AND RECONCILE WITH WP MEDIA LIBRARY */ function buddy_boss_pics_cleanup_db() { global $wpdb; $activity_table = $wpdb->prefix . "bp_activity"; $activity_meta_table = $wpdb->prefix . "bp_activity_meta"; $posts_table = $wpdb->prefix . "posts"; // Prepare a SQL query to retrieve the activity posts // that have pictures associated with them $all_aids_sql = "SELECT am.meta_value, am.activity_id FROM {$activity_table} a \r\n\t\t\t\t\t\t\t\t\t\t INNER JOIN {$activity_meta_table} am ON a.id = am.activity_id \r\n\t\t\t\t\t\t\t\t\t\t WHERE am.meta_key = 'bboss_pics_aid'"; // Now perpare a SQL query to retrieve all attachments // that are BuddyBoss wall pictures AND are published in the media library $existing_sql = "SELECT am.meta_value FROM {$activity_table} a \r\n\t\t\t\t\t\t\t\t\t\t INNER JOIN {$activity_meta_table} am ON a.id = am.activity_id \r\n\t\t\t\t\t\t\t\t\t\t INNER JOIN {$posts_table} p ON am.meta_value = p.ID \r\n\t\t\t\t\t\t\t\t\t\t WHERE am.meta_key = 'bboss_pics_aid'\r\n\t\t\t\t\t\t\t\t\t\t AND p.post_status = 'inherit'\r\n\t\t\t\t\t\t\t\t\t\t AND p.post_parent = 0"; // Query the database for all attachment IDS $all_aids = (array) $wpdb->get_results($all_aids_sql, ARRAY_A); // Query the database for all pics in the media library that are BuddyBoss pics $existing_aids = (array) $wpdb->get_col($existing_sql); // If we have a result set if (!empty($all_aids)) { // Prepare array $attachment_ids = $activity_ids = $aids2activity = array(); foreach ($all_aids as $aid) { $attachment_ids[] = $aid['meta_value']; $aids2activity[$aid['meta_value']] = $activity_ids[] = $aid['activity_id']; } // Lets get the difference of our published vs. orphaned IDs $orphans = array_diff($attachment_ids, $existing_aids); // Delete related activity stream posts if (!empty($orphans)) { $orphan_acitivity_ids = array(); foreach ($orphans as $orphan) { if (isset($aids2activity[$orphan])) { $orphan_acitivity_ids[] = $aids2activity[$orphan]; } } $orphan_acitivity_ids_string = implode(',', $orphan_acitivity_ids); $sql = $wpdb->prepare("DELETE FROM {$activity_table} WHERE id IN ({$orphan_acitivity_ids_string})"); $deleted = $wpdb->query($sql); BP_Activity_Activity::delete_activity_item_comments($orphan_acitivity_ids); BP_Activity_Activity::delete_activity_meta_entries($orphan_acitivity_ids); } } }