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, $album_id = 0, $group = 0)
 {
     do_action('bp_media_before_add_media');
     global $bp, $wpdb, $bp_media_count;
     include_once ABSPATH . 'wp-admin/includes/file.php';
     include_once ABSPATH . 'wp-admin/includes/image.php';
     $create_new_album_flag = false;
     if ($album_id != 0) {
         $album = get_post($album_id);
         if ($album->post_author != get_current_user_id()) {
             $create_new_album_flag = true;
         } else {
             $post_id = $album->ID;
         }
     } else {
         $create_new_album_flag = true;
     }
     if ($create_new_album_flag) {
         $post_id = $wpdb->get_var("SELECT ID FROM {$wpdb->posts} WHERE post_title = 'Wall Posts' AND post_author = '" . get_current_user_id() . "' AND post_type='bp_media_album'");
         if ($post_id == null) {
             $album = new BP_Media_Album();
             $album->add_album('Wall Posts');
             $post_id = $album->get_id();
         }
     }
     $file = wp_handle_upload($_FILES['bp_media_file']);
     if (isset($file['error']) || $file === null) {
         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':
         case 'video/quicktime':
             $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) {
                 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')) {
                         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 {
                     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 {
                 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':
             include_once trailingslashit(BP_MEDIA_PLUGIN_DIR) . 'includes/lib/getid3/getid3.php';
             try {
                 $getID3 = new getID3();
                 $file_info = $getID3->analyze($file);
             } catch (Exception $e) {
                 unlink($file);
                 $activity_content = false;
                 throw new Exception(__('MP3 file you have uploaded is currupt.', 'bp-media'));
             }
             if (is_array($file_info)) {
                 if (!array_key_exists('error', $file_info) && array_key_exists('fileformat', $file_info) && array_key_exists('audio', $file_info) && array_key_exists('dataformat', $file_info['audio'])) {
                     if (!($file_info['fileformat'] == 'mp3' && $file_info['audio']['dataformat'] == 'mp3')) {
                         unlink($file);
                         $activity_content = false;
                         throw new Exception(__('The MP3 file you have uploaded is using an unsupported audio format. Supported audio format is MP3.', 'bp-media'));
                     }
                 } else {
                     unlink($file);
                     $activity_content = false;
                     throw new Exception(__('The MP3 file you have uploaded is using an unsupported audio format. Supported audio format is MP3.', 'bp-media'));
                 }
             } else {
                 unlink($file);
                 $activity_content = false;
                 throw new Exception(__('The MP3 file you have uploaded is not an audio file.', 'bp-media'));
             }
             $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);
             $activity_content = false;
             throw new Exception(__('Media File you have tried to upload is not supported. Supported media files are .jpg, .png, .gif, .mp3, .mov 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 {
         unlink($file);
         throw new Exception(__('Error creating attachment for the media file, please try again', 'bp-media'));
     }
     $this->id = $attachment_id;
     $this->name = $name;
     $this->description = $description;
     $this->type = $type;
     $this->owner = get_current_user_id();
     $this->album_id = $post_id;
     switch ($this->type) {
         case 'video':
             $this->url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_VIDEOS_SLUG . '/' . $this->id);
             $this->edit_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_VIDEOS_SLUG . '/' . BP_MEDIA_VIDEOS_EDIT_SLUG . '/' . $this->id);
             $this->delete_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_VIDEOS_SLUG . '/' . BP_MEDIA_DELETE_SLUG . '/' . $this->id);
             $this->thumbnail_id = get_post_meta($this->id, 'bp_media_thumbnail', true);
             break;
         case 'audio':
             $this->url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_AUDIO_SLUG . '/' . $this->id);
             $this->edit_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_AUDIO_SLUG . '/' . BP_MEDIA_AUDIO_EDIT_SLUG . '/' . $this->id);
             $this->delete_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_AUDIO_SLUG . '/' . BP_MEDIA_DELETE_SLUG . '/' . $this->id);
             $this->thumbnail_id = get_post_meta($this->id, 'bp_media_thumbnail', true);
             break;
         case 'image':
             $this->url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_IMAGES_SLUG . '/' . $this->id);
             $this->edit_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_IMAGES_SLUG . '/' . BP_MEDIA_IMAGES_EDIT_SLUG . '/' . $this->id);
             $this->delete_url = trailingslashit(bp_core_get_user_domain($this->owner) . BP_MEDIA_IMAGES_SLUG . '/' . BP_MEDIA_DELETE_SLUG . '/' . $this->id);
             $image_array = image_downsize($this->id, 'bp_media_single_image');
             $this->thumbnail_id = $this->id;
             break;
         default:
             //return false;
     }
     if ($group == 0) {
         update_post_meta($attachment_id, 'bp-media-key', get_current_user_id());
     } else {
         update_post_meta($attachment_id, 'bp-media-key', -$group);
     }
     bp_update_user_meta(bp_loggedin_user_id(), 'bp_media_count', $bp_media_count);
     do_action('bp_media_after_add_media', $this);
 }
/**
 * Function to create new album called via ajax request
 */
function bp_media_add_album()
{
    if (isset($_POST['bp_media_album_name']) && $_POST['bp_media_album_name'] != '') {
        $album = new BP_Media_Album();
        try {
            $album->add_album($_POST['bp_media_album_name']);
            echo $album->get_id();
        } catch (exception $e) {
            echo '0';
        }
    } else {
        echo '0';
    }
    die;
}
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_the_content($id = 0)
{
    if (is_object($id)) {
        $album = $id;
    } else {
        $album =& get_post($id);
    }
    if (empty($album->ID)) {
        return false;
    }
    if (!$album->post_type == 'bp_media_album') {
        return false;
    }
    try {
        $album = new BP_Media_Album($album->ID);
        echo $album->get_album_gallery_content();
    } catch (Exception $e) {
        echo '';
    }
}