Exemple #1
0
function bp_cover_group_handle_upload($activity_id)
{
    global $bp, $wpdb;
    $group_id = bp_get_current_group_id();
    $activity_table = $wpdb->prefix . "bp_activity";
    $activity_meta_table = $wpdb->prefix . "bp_activity_meta";
    $sql = "SELECT COUNT(*) as photo_count FROM {$activity_table} a INNER JOIN {$activity_meta_table} am ON a.id = am.activity_id WHERE a.item_id = %d AND meta_key = 'all_bp_cover_group'";
    $sql = $wpdb->prepare($sql, $group_id);
    $cnt = $wpdb->get_var($sql);
    $max_cnt = bp_cover_get_max_total();
    if ($cnt < $max_cnt) {
        if ($_POST['encodedimg']) {
            $file = $_POST['imgsize'];
            $max_upload_size = bp_cover_get_max_media_size();
            if ($max_upload_size > $file) {
                $group_id = $bp->groups->current_group->id;
                $imgresponse = array();
                $uploaddir = wp_upload_dir();
                /* let's decode the base64 encoded image sent */
                $img = $_POST['encodedimg'];
                $img = str_replace('data:' . $_POST['imgtype'] . ';base64,', '', $img);
                $img = str_replace(' ', '+', $img);
                $data = base64_decode($img);
                $imgname = wp_unique_filename($uploaddir['path'], $_POST['imgname']);
                $filepath = $uploaddir['path'] . '/' . $imgname;
                $fileurl = $uploaddir['url'] . '/' . $imgname;
                /* now we write the image in dir */
                $success = file_put_contents($filepath, $data);
                if ($success) {
                    $imgresponse[0] = "1";
                    $imgresponse[1] = $fileurl;
                    $size = @getimagesize($filepath);
                    $attachment = array('post_mime_type' => $_POST['imgtype'], 'guid' => $fileurl, 'post_title' => $imgname);
                    require_once ABSPATH . 'wp-admin/includes/image.php';
                    $attachment_id = wp_insert_attachment($attachment, $filepath);
                    $attach_data = wp_generate_attachment_metadata($attachment_id, $filepath);
                    wp_update_attachment_metadata($attachment_id, $attach_data);
                    groups_update_groupmeta($group_id, 'bp_cover_group', $fileurl);
                    groups_update_groupmeta($group_id, 'bp_cover_group_position', 0);
                    $group = groups_get_group(array("group_id" => $group_id));
                    $activity_id = groups_record_activity(array('action' => sprintf(__('%s uploaded a new cover picture to the group %s', 'bp-cover'), bp_core_get_userlink($bp->loggedin_user->id), '<a href="' . bp_get_group_permalink($group) . '">' . esc_attr($group->name) . '</a>'), 'type' => 'cover_added', 'item_id' => $group_id, 'content' => bp_cover_group_get_image_scr(), 'item_id' => $group_id));
                    bp_activity_update_meta($activity_id, 'all_bp_cover_group', $attachment_id);
                    update_post_meta($attachment_id, 'bp_cover_group_thumb', $imgresponse[2]);
                } else {
                    $imgresponse[0] = "0";
                    $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover');
                }
            } else {
                $imgresponse[0] = "0";
                $imgresponse[1] = sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size));
            }
        } else {
            $imgresponse[0] = "0";
            $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover');
        }
    } else {
        $imgresponse[0] = "0";
        $imgresponse[1] = sprintf(__('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt);
    }
    /* if everything is ok, we send back url to thumbnail and to full image */
    echo json_encode($imgresponse);
    die;
}
Exemple #2
0
function bp_cover_handle_upload()
{
    global $bp, $wpdb;
    if (!$user_id && $bp->displayed_user->id) {
        $user_id = $bp->displayed_user->id;
    }
    if (empty($user_id)) {
        return false;
    }
    $activity_table = $wpdb->prefix . "bp_activity";
    $activity_meta_table = $wpdb->prefix . "bp_activity_meta";
    $sql = "SELECT COUNT(*) as photo_count FROM {$activity_table} a INNER JOIN {$activity_meta_table} am ON a.id = am.activity_id WHERE a.user_id = %d AND meta_key = 'bp_cover_activity'";
    $sql = $wpdb->prepare($sql, $user_id);
    $cnt = $wpdb->get_var($sql);
    $max_cnt = bp_cover_get_max_total();
    if ($cnt < $max_cnt) {
        if ($_POST['encodedimg']) {
            $file = $_POST['imgsize'];
            $max_upload_size = bp_cover_get_max_media_size();
            if ($max_upload_size > $file) {
                $imgresponse = array();
                $uploaddir = wp_upload_dir();
                /* let's decode the base64 encoded image sent */
                $img = $_POST['encodedimg'];
                $img = str_replace('data:' . $_POST['imgtype'] . ';base64,', '', $img);
                $img = str_replace(' ', '+', $img);
                $data = base64_decode($img);
                $imgname = wp_unique_filename($uploaddir['path'], $_POST['imgname']);
                $filepath = $uploaddir['path'] . '/' . $imgname;
                $fileurl = $uploaddir['url'] . '/' . $imgname;
                /* now we write the image in dir */
                $success = file_put_contents($filepath, $data);
                if ($success) {
                    $imgresponse[0] = "1";
                    $imgresponse[1] = $fileurl;
                    update_user_meta(bp_loggedin_user_id(), 'bp_cover', $fileurl);
                    delete_user_meta(bp_loggedin_user_id(), 'bp_cover_position');
                    do_action('bp_cover_uploaded', $fileurl);
                } else {
                    $imgresponse[0] = "0";
                    $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover');
                }
            } else {
                $imgresponse[0] = "0";
                $imgresponse[1] = sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size));
            }
        } else {
            $imgresponse[0] = "0";
            $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover');
        }
    } else {
        $imgresponse[0] = "0";
        $imgresponse[1] = sprintf(__('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt);
    }
    /* if everything is ok, we send back url to thumbnail and to full image */
    echo json_encode($imgresponse);
    die;
}