Exemplo n.º 1
0
/**
 * Handle public file uploaded using buddydrive_editor
 *
 * @since 1.3.0
 */
function buddydrive_add_public_file()
{
    /**
     * Sending the json response will be different if
     * the current Plupload runtime is html4
     */
    $is_html4 = false;
    if (!empty($_POST['html4'])) {
        $is_html4 = true;
    }
    // Check the nonce
    check_admin_referer('bp-uploader');
    // Init the BuddyPress parameters
    $bp_params = (array) $_POST['bp_params'];
    // Check params
    if (empty($bp_params['item_id'])) {
        bp_attachments_json_response(false, $is_html4);
    }
    // Capability check
    if (!is_user_logged_in() || (int) bp_loggedin_user_id() !== (int) $bp_params['item_id']) {
        bp_attachments_json_response(false, $is_html4);
    }
    $bd_file = buddydrive_upload_item($_FILES, $bp_params['item_id']);
    // Error while trying to upload the file
    if (!empty($bd_file['error'])) {
        bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => $bd_file['error']));
    }
    $name_parts = pathinfo($bd_file['file']);
    $url = $bd_file['url'];
    $mime = $bd_file['type'];
    $file = $bd_file['file'];
    $title = $name_parts['filename'];
    if (is_numeric($title)) {
        $title = 'f-' . $title;
    }
    $meta = new stdClass();
    // Defaults to public.
    $meta->privacy = 'public';
    if (!empty($bp_params['privacy'])) {
        $meta->privacy = $bp_params['privacy'];
        if (!empty($bp_params['privacy_item_id']) && 'groups' === $meta->privacy) {
            $meta->groups = $bp_params['privacy_item_id'];
        }
    }
    $buddyfile_id = buddydrive_save_item(array('type' => buddydrive_get_file_post_type(), 'guid' => $url, 'title' => $title, 'mime_type' => $mime, 'metas' => $meta));
    if (empty($buddyfile_id)) {
        bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => __('Error while creating the file, sorry.', 'buddydrive')));
    } else {
        $icon = wp_mime_type_icon($buddyfile_id);
        // Try to create a thumbnail if it's an image and a public file
        if (!empty($buddyfile_id) && 'public' === $meta->privacy) {
            $thumbnail = buddydrive_set_thumbnail($buddyfile_id, $bd_file);
            if (!empty($thumbnail)) {
                $icon = $thumbnail;
            }
        }
    }
    $response = buddydrive_get_buddyfile($buddyfile_id);
    // Finally return file to the editor
    bp_attachments_json_response(true, $is_html4, array('name' => esc_html($response->title), 'icon' => $icon, 'url' => esc_url_raw($response->link)));
}
Exemplo n.º 2
0
 /**
  * @group upload
  * @group check_upload_limit
  */
 public function test_buddydrive_upload_item_upload_limit()
 {
     $reset_files = $_FILES;
     $reset_post = $_POST;
     $tmp_name = wp_tempnam($this->file);
     copy($this->file, $tmp_name);
     $_POST['action'] = 'buddydrive_upload';
     $_FILES['buddyfile-upload'] = array('tmp_name' => $tmp_name, 'name' => 'screenshot-1.png', 'type' => 'image/png', 'error' => 0, 'size' => filesize($this->file));
     add_filter('upload_size_limit', '__return_zero');
     // Upload the file
     $upload = buddydrive_upload_item($_FILES, bp_loggedin_user_id());
     remove_filter('upload_size_limit', '__return_zero');
     $this->assertTrue(!empty($upload['error']));
     // clean up!
     $_FILES = $reset_files;
     $_POST = $reset_post;
 }