/**
  * Post a gallery or media Main comment on single page
  * 
  * @return type
  */
 public function post_comment()
 {
     // Bail if not a POST action
     if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) {
         return;
     }
     // Check the nonce
     check_admin_referer('post_update', '_wpnonce_post_update');
     if (!is_user_logged_in()) {
         exit('-1');
     }
     $mpp_type = $_POST['mpp-type'];
     $mpp_id = $_POST['mpp-id'];
     if (empty($_POST['content'])) {
         exit('-1<div id="message" class="error"><p>' . __('Please enter some content to post.', 'mediapress') . '</p></div>');
     }
     $activity_id = 0;
     if (empty($_POST['object']) && bp_is_active('activity')) {
         //we are preventing this comment to be set as the user's lastes_update
         $user_id = bp_loggedin_user_id();
         $old_latest_update = bp_get_user_meta($user_id, 'bp_latest_update', true);
         $activity_id = bp_activity_post_update(array('content' => $_POST['content']));
         //restore
         if (!empty($old_latest_update)) {
             bp_update_user_meta($user_id, 'bp_latest_update', $old_latest_update);
         }
     } elseif ($_POST['object'] == 'groups') {
         if (!empty($_POST['item_id']) && bp_is_active('groups')) {
             $activity_id = groups_post_update(array('content' => $_POST['content'], 'group_id' => $_POST['item_id']));
         }
     } else {
         $activity_id = apply_filters('bp_activity_custom_update', $_POST['object'], $_POST['item_id'], $_POST['content']);
     }
     if (empty($activity_id)) {
         exit('-1<div id="message" class="error"><p>' . __('There was a problem posting your update, please try again.', 'mediapress') . '</p></div>');
     }
     //if we have got activity id, let us add a meta key
     if ($mpp_type == 'gallery') {
         mpp_activity_update_gallery_id($activity_id, $mpp_id);
     } elseif ($mpp_type == 'media') {
         mpp_activity_update_media_id($activity_id, $mpp_id);
     }
     $activity = new BP_Activity_Activity($activity_id);
     // $activity->component = buddypress()->mediapress->id;
     $activity->type = 'mpp_media_upload';
     $activity->save();
     if (bp_has_activities('include=' . $activity_id)) {
         while (bp_activities()) {
             bp_the_activity();
             mpp_locate_template(array('activity/entry.php'), true);
         }
     }
     exit;
 }
function mpp_upgrade_legacy_1_0_b1_activity()
{
    if (get_option('mpp_upgraded_1_0_b1')) {
        return;
        //already upgraded
    }
    add_option('mpp_upgraded_1_0_b1', 1);
    if (!get_option('mpp-settings')) {
        return;
        //mediapress was not installed earlier
    }
    if (!function_exists('buddypress')) {
        return;
    }
    global $wpdb;
    $activity_table = bp_core_get_table_prefix() . 'bp_activity_meta';
    //rename _mpp_attached_media_ids key tp _mpp_attached_media_id
    $sql = "UPDATE {$activity_table} SET meta_key = '_mpp_attached_media_id' WHERE meta_key = '_mpp_attached_media_ids'";
    $wpdb->query($sql);
    //add context to all Media comments
    $update_query = "INSERT INTO {$activity_table} (activity_id, meta_key, meta_value) \n\t\t\tSELECT  activity_id, %s as meta_key, %s as meta_value FROM {$activity_table} where meta_key ='_mpp_galery_id'";
    //update the context? should we?
    $wpdb->query($wpdb->prepare($update_query, '_mpp_context', 'gallery'));
    //update type
    $wpdb->query($wpdb->prepare($update_query, '_mpp_activity_type', 'media_upload'));
    //for media comments
    //$entries = $wpdb->get_col( "SELECT activity_id, meta_value FROM {$activity_table} WHERE meta_key = '_mpp_media_id'" );
    $entries = $wpdb->get_results("SELECT activity_id, meta_value FROM {$activity_table} WHERE meta_key = '_mpp_media_id'");
    $media_ids = wp_list_pluck($entries, 'meta_value');
    //comments are there
    if (!empty($media_ids)) {
        _prime_post_caches($media_ids, false, false);
        //add parent gallery id for each of the media
        foreach ($entries as $entry) {
            $media = get_post($entry->meta_value);
            mpp_activity_update_gallery_id($entry->activity_id, $media->post_parent);
        }
        //update context to 'media'
        $update_query = "INSERT INTO {$activity_table} (activity_id, meta_key, meta_value) \n\t\t\tSELECT  activity_id, %s as meta_key, %s as meta_value FROM {$activity_table} WHERE meta_key ='_mpp_media_id'";
        $wpdb->query($wpdb->prepare($update_query, '_mpp_activity_type', 'media_comment'));
        $wpdb->query($wpdb->prepare($update_query, '_mpp_activity_type', 'media'));
    }
}
/**
 * Record Media Activity
 * 
 * It does not actually records activity, simply simulates the activity update and rest are done by the actions.php functions
 * 
 * It will be removed in future for a better record_activity method
 * @param type $args
 * @return boolean
 */
function mpp_record_activity($args = null)
{
    //if activity module is not active, why bother
    if (!bp_is_active('activity')) {
        return false;
    }
    $default = array('gallery_id' => 0, 'media_id' => 0, 'media_ids' => null, 'action' => '', 'content' => '', 'type' => '', 'component' => mpp_get_current_component(), 'component_id' => mpp_get_current_component_id(), 'user_id' => get_current_user_id(), 'status' => '');
    $args = wp_parse_args($args, $default);
    //atleast a gallery id or a media id should be given
    if (!$args['gallery_id'] && !$args['media_id'] || !mpp_is_active_component($args['component']) || !$args['component_id']) {
        return false;
    }
    $gallery_id = absint($args['gallery_id']);
    $media_id = absint($args['media_id']);
    $type = $args['type'];
    //should we validate type too?
    $hide_sitewide = 0;
    $status_object = null;
    if ($args['status']) {
        $status_object = mpp_get_status_object($args['status']);
        if ($status_object && ($status_object->activity_privacy == 'hidden' || $status_object->activity_privacy == 'onlyme')) {
            $hide_sitewide = 1;
        }
    }
    $media_ids = $args['media_ids'];
    if (!empty($media_ids) && !is_array($media_ids)) {
        $media_ids = explode(',', $media_ids);
    }
    $component = $args['component'];
    if ($component == buddypress()->members->id) {
        $component = buddypress()->activity->id;
        //for user gallery updates, let it be simple activity , do not set the component to 'members'
    }
    $activity_id = bp_activity_add(array('id' => false, 'user_id' => $args['user_id'], 'action' => $args['action'], 'content' => $args['content'], 'component' => $component, 'type' => 'mpp_media_upload', 'item_id' => absint($args['component_id']), 'secondary_item_id' => false, 'recorded_time' => bp_core_current_time(), 'hide_sitewide' => $hide_sitewide));
    if (!$activity_id) {
        return false;
        //there was a problem
    }
    //store the type of gallery activity in meta
    if ($type) {
        mpp_activity_update_activity_type($activity_id, $type);
    }
    if ($media_ids) {
        $media_ids = wp_parse_id_list($media_ids);
        mpp_activity_update_attached_media_ids($activity_id, $media_ids);
    }
    if ($gallery_id) {
        mpp_activity_update_gallery_id($activity_id, $gallery_id);
    }
    if ($media_id) {
        mpp_activity_update_media_id($activity_id, $media_id);
    }
    mpp_activity_update_context($activity_id, 'gallery');
    //save activity privacy
    if ($status_object) {
        bp_activity_update_meta($activity_id, 'activity-privacy', $status_object->activity_privacy);
    }
    return $activity_id;
}
/**
 * Record Media Activity
 * 
 * It does not actually records activity, simply simulates the activity update and rest are done by the actions.php functions
 * 
 * It will be removed in future for a better record_activity method
 * @param type $args
 * @return boolean
 */
function mpp_record_activity($args = null)
{
    //if activity module is not active, why bother
    if (!bp_is_active('activity')) {
        return false;
    }
    $default = array('id' => false, 'gallery_id' => 0, 'media_id' => 0, 'media_ids' => null, 'action' => '', 'content' => '', 'type' => '', 'component' => mpp_get_current_component(), 'component_id' => mpp_get_current_component_id(), 'user_id' => get_current_user_id(), 'status' => '');
    $args = wp_parse_args($args, $default);
    //atleast a gallery id or a media id should be given
    if (!$args['gallery_id'] && !$args['media_id'] || !mpp_is_enabled($args['component'], $args['component_id']) || !$args['component_id']) {
        return false;
    }
    $gallery_id = absint($args['gallery_id']);
    $media_id = absint($args['media_id']);
    $type = $args['type'];
    //should we validate type too?
    $hide_sitewide = 0;
    $status_object = null;
    if ($args['status']) {
        $status_object = mpp_get_status_object($args['status']);
        if ($status_object && ($status_object->activity_privacy == 'hidden' || $status_object->activity_privacy == 'onlyme')) {
            $hide_sitewide = 1;
        }
        //if BuddyPress Activity privacy plugin is not active, revert back to hiding all non public activity
        if (!function_exists('bp_activity_privacy_check_config')) {
            $hide_sitewide = $args['status'] == 'public' ? 0 : 1;
            //overwrite privacy
        }
    }
    $media_ids = $args['media_ids'];
    if (!empty($media_ids) && !is_array($media_ids)) {
        $media_ids = explode(',', $media_ids);
    }
    $component = $args['component'];
    if ($component == buddypress()->members->id) {
        $component = buddypress()->activity->id;
        //for user gallery updates, let it be simple activity , do not set the component to 'members'
    }
    $activity_args = array('id' => $args['id'], 'user_id' => $args['user_id'], 'action' => $args['action'], 'content' => $args['content'], 'component' => $component, 'type' => 'mpp_media_upload', 'item_id' => absint($args['component_id']), 'secondary_item_id' => false, 'hide_sitewide' => $hide_sitewide);
    //only update record time if this is a new activity
    if (empty($args['id'])) {
        $activity_args['recorded_time'] = bp_core_current_time();
    }
    //let us give an opportunity to customize the activity args
    //use this filter to work with the activity privacy
    $activity_args = apply_filters('mpp_record_activity_args', $activity_args, $default);
    $activity_id = bp_activity_add($activity_args);
    if (!$activity_id) {
        return false;
        //there was a problem
    }
    //store the type of gallery activity in meta
    if ($type) {
        mpp_activity_update_activity_type($activity_id, $type);
    }
    if ($media_ids) {
        $media_ids = wp_parse_id_list($media_ids);
        mpp_activity_update_attached_media_ids($activity_id, $media_ids);
    }
    if ($gallery_id) {
        mpp_activity_update_gallery_id($activity_id, $gallery_id);
    }
    if ($media_id) {
        mpp_activity_update_media_id($activity_id, $media_id);
    }
    mpp_activity_update_context($activity_id, 'gallery');
    //save activity privacy
    if ($status_object) {
        bp_activity_update_meta($activity_id, 'activity-privacy', $status_object->activity_privacy);
    }
    return $activity_id;
}