function bp_media_upgrade_to_2_2()
{
    global $wpdb;
    remove_filter('bp_activity_get_user_join_filter', 'bp_media_activity_query_filter', 10);
    /* @var $wpdb wpdb */
    $media_files = new WP_Query(array('post_type' => 'bp_media', 'posts_per_page' => -1));
    $media_files = $media_files->posts;
    $wall_posts_album_ids = array();
    if (is_array($media_files) && count($media_files)) {
        foreach ($media_files as $media_file) {
            $attachment_id = get_post_meta($media_file->ID, 'bp_media_child_attachment', true);
            $child_activity = get_post_meta($media_file->ID, 'bp_media_child_activity', true);
            update_post_meta($attachment_id, 'bp_media_child_activity', $child_activity);
            $attachment = get_post($attachment_id, ARRAY_A);
            if (isset($wall_posts_album_ids[$media_file->post_author])) {
                $wall_posts_id = $wall_posts_album_ids[$media_file->post_author];
            } else {
                $wall_posts_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_title = 'Wall Posts' AND post_author = '" . $media_file->post_author . "' AND post_type='bp_media_album'");
                if ($wall_posts_id == null) {
                    $album = new BP_Media_Album();
                    $album->add_album('Wall Posts', $media_file->post_author);
                    $wall_posts_id = $album->get_id();
                }
                if (!$wall_posts_id) {
                    continue;
                    //This condition should never be encountered
                }
                $wall_posts_album_ids[$media_file->post_author] = $wall_posts_id;
            }
            $attachment['post_parent'] = $wall_posts_id;
            wp_update_post($attachment);
            update_post_meta($attachment_id, 'bp-media-key', $media_file->post_author);
            $activity = bp_activity_get(array('in' => intval($child_activity)));
            if (isset($activity['activities'][0]->id)) {
                $activity = $activity['activities'][0];
            }
            $bp_media = new BP_Media_Host_Wordpress($attachment_id);
            $args = array('content' => $bp_media->get_media_activity_content(), 'id' => $child_activity, 'type' => 'media_upload', 'action' => apply_filters('bp_media_added_media', sprintf(__('%1$s added a %2$s', 'bp-media'), bp_core_get_userlink($media_file->post_author), '<a href="' . $bp_media->get_url() . '">' . $bp_media->get_media_activity_type() . '</a>')), 'primary_link' => $bp_media->get_url(), 'item_id' => $attachment_id, 'recorded_time' => $activity->date_recorded);
            $act_id = bp_media_record_activity($args);
            bp_activity_delete_meta($child_activity, 'bp_media_parent_post');
            wp_delete_post($media_file->ID);
        }
    }
    update_option('bp_media_db_version', BP_MEDIA_DB_VERSION);
    add_action('admin_notices', 'bp_media_database_updated_notice');
    wp_cache_flush();
}
	/**
	 * Handles the uploaded media file and creates attachment post for the file.
	 * 
	 * @since BP Media 2.0
	 */
	function add_media($name, $description) {
		global $bp, $wpdb, $bp_media_count;
		include_once(ABSPATH . 'wp-admin/includes/file.php');
		include_once(ABSPATH . 'wp-admin/includes/image.php');
		//media_handle_upload('async-upload', $_REQUEST['post_id']);
		$postarr = array(
			'post_status' => 'draft',
			'post_type' => 'bp_media',
			'post_content' => $description,
			'post_title' => $name
		);
		$post_id = wp_insert_post($postarr);
		$file = wp_handle_upload($_FILES['bp_media_file']);
		if (isset($file['error']) || $file === null) {
			wp_delete_post($post_id, true);
			throw new Exception(__('Error Uploading File', 'bp-media'));
		}
		$attachment = array();
		$url = $file['url'];
		$type = $file['type'];
		$file = $file['file'];
		$title = $name;
		$content = $description;
		$attachment = array(
			'post_mime_type' => $type,
			'guid' => $url,
			'post_title' => $title,
			'post_content' => $content,
			'post_parent' => $post_id,
		);
		bp_media_init_count(bp_loggedin_user_id());
		switch ($type) {
			case 'video/mp4' :
				$type = 'video';
				include_once(trailingslashit(BP_MEDIA_PLUGIN_DIR) . 'includes/lib/getid3/getid3.php');
				try {
					$getID3 = new getID3;
					$vid_info = $getID3->analyze($file);
				} catch (Exception $e) {
					wp_delete_post($post_id, true);
					unlink($file);
					$activity_content = false;
					throw new Exception(__('MP4 file you have uploaded is currupt.', 'bp-media'));
				}
				if (is_array($vid_info)) {
					if (!array_key_exists('error',$vid_info)&& array_key_exists('fileformat', $vid_info) && array_key_exists('video', $vid_info)&&array_key_exists('fourcc',$vid_info['video'])) {
						if (!($vid_info['fileformat']=='mp4'&&$vid_info['video']['fourcc']=='avc1')) {
							wp_delete_post($post_id, true);
							unlink($file);
							$activity_content = false;
							throw new Exception(__('The MP4 file you have uploaded is using an unsupported video codec. Supported video codec is H.264.', 'bp-media'));
						}
					} else {
						wp_delete_post($post_id, true);
						unlink($file);
						$activity_content = false;
						throw new Exception(__('The MP4 file you have uploaded is using an unsupported video codec. Supported video codec is H.264.', 'bp-media'));
					}
				} else {
					wp_delete_post($post_id, true);
					unlink($file);
					$activity_content = false;
					throw new Exception(__('The MP4 file you have uploaded is not a video file.', 'bp-media'));
				}
				$bp_media_count['videos'] = intval($bp_media_count['videos']) + 1;
				break;
			case 'audio/mpeg' :
				$type = 'audio';
				$bp_media_count['audio'] = intval($bp_media_count['audio']) + 1;
				break;
			case 'image/gif' :
			case 'image/jpeg' :
			case 'image/png' :
				$type = 'image';
				$bp_media_count['images'] = intval($bp_media_count['images']) + 1;
				break;
			default : unlink($file);
				wp_delete_post($post_id, true);
				unlink($file);
				$activity_content = false;
				throw new Exception(__('Media File you have tried to upload is not supported. Supported media files are .jpg, .png, .gif, .mp3 and .mp4.', 'bp-media'));
		}
		$attachment_id = wp_insert_attachment($attachment, $file, $post_id);
		if (!is_wp_error($attachment_id)) {
			wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $file));
		} else {
			wp_delete_post($post_id, true);
			unlink($file);
			throw new Exception(__('Error creating activity for the media file, please try again', 'bp-media'));
		}
		$postarr['ID'] = $post_id;
		$postarr['post_mime_type'] = $type;
		$postarr['post_status'] = 'publish';
		wp_insert_post($postarr);
		$activity_content = '[bp_media_content id="' . $post_id . '"]';
		$activity_id = bp_media_record_activity(array(
			'action' => '[bp_media_action id="' . $post_id . '"]',
			'content' => $activity_content,
			'primary_link' => '[bp_media_url id="' . $post_id . '"]',
			'type' => 'media_upload'
		));
		bp_activity_update_meta($activity_id, 'bp_media_parent_post', $post_id);
		update_post_meta($post_id, 'bp_media_child_activity', $activity_id);
		update_post_meta($post_id, 'bp_media_child_attachment', $attachment_id);
		update_post_meta($post_id, 'bp_media_type', $type);
		update_post_meta($post_id, 'bp_media_hosting', 'wordpress');
		$this->id = $post_id;
		$this->name = $name;
		$this->description = $description;
		$this->owner = bp_loggedin_user_id();
		$this->type = $type;
		$this->url = $url;
		bp_update_user_meta(bp_loggedin_user_id(), 'bp_media_count', $bp_media_count);
	}
function bp_media_update_album_activity($album, $current_time = true, $delete_media_id = null)
{
    if (!is_object($album)) {
        $album = new BP_Media_Album($album);
    }
    $args = array('post_parent' => $album->get_id(), 'numberposts' => 4, 'post_type' => 'attachment');
    if ($delete_media_id) {
        $args['exclude'] = $delete_media_id;
    }
    $attachments = get_posts($args);
    if (is_array($attachments)) {
        $content = '<ul>';
        foreach ($attachments as $media) {
            $bp_media = new BP_Media_Host_Wordpress($media->ID);
            $content .= $bp_media->get_album_activity_content();
        }
        $content .= '</ul>';
        $activity_id = get_post_meta($album->get_id(), 'bp_media_child_activity');
        if ($activity_id) {
            $args = array('in' => $activity_id);
            $activity = @bp_activity_get($args);
            if (isset($activity['activities'][0]->id)) {
                $args = array('content' => $content, 'id' => $activity_id, 'type' => 'album_updated', 'user_id' => $activity['activities'][0]->user_id, 'action' => apply_filters('bp_media_filter_album_updated', sprintf(__('%1$s added new media in album %2$s', 'bp-media'), bp_core_get_userlink($activity['activities'][0]->user_id), '<a href="' . $album->get_url() . '">' . $album->get_title() . '</a>')), 'component' => BP_MEDIA_SLUG, 'primary_link' => $activity['activities'][0]->primary_link, 'item_id' => $activity['activities'][0]->item_id, 'secondary_item_id' => $activity['activities'][0]->secondary_item_id, 'recorded_time' => $current_time ? bp_core_current_time() : $activity['activities'][0]->date_recorded, 'hide_sitewide' => $activity['activities'][0]->hide_sitewide);
                bp_media_record_activity($args);
            }
        }
    }
}
function bp_media_album_create_activity($album)
{
    /* @var $album BP_Media_Album */
    $args = array('action' => apply_filters('bp_media_album_created', sprintf(__('%1$s created an album %2$s', 'bp-media'), bp_core_get_userlink($album->get_owner()), '<a href="' . $album->get_url() . '">' . $album->get_title() . '</a>')), 'component' => BP_MEDIA_SLUG, 'type' => 'album_created', 'primary_link' => $album->get_url(), 'user_id' => $album->get_owner(), 'item_id' => $album->get_id());
    $activity_id = bp_media_record_activity($args);
    update_post_meta($album->get_id(), 'bp_media_child_activity', $activity_id);
}