Upload attachments to BuddyDrive
-
I’m currently trying to hack together a way for attachments to forum posts (made with GD bbPress attachments) to be automatically added to BuddyDrive, and I’m hitting a wall. I’ve tried to do a custom implementation of buddydrive_upload_item() by passing the $_FILES array and the user id using get_current_user_id():
buddydrive_upload_item($_FILES, get_current_user_id());
But this just returns an “Invalid Form Submission” error. My guess is that this is because I’m using buddydrive_upload_item() from a weird place, and I’m really bad at understanding classes. Is there a way that I can either successfully call buddydrive_upload_item() with a custom file or otherwise get my file added into the BuddyDrive?
-
Ok, so I’ve realized there’s more to this. Here’s where I’m at, at this point I’m just straight up editing core files to see what needs to change, later I’ll work on integration.
I’ve customized the save_reply function in front.php of GD bbPress Attachments (as part of its gdbbAtt_Front class) to call a custom function located in the same file (for now):
$this->double_attach_bd($_FILES)
So far, double_attach_bd($files) is a copy and paste of buddydrive_upload_file(), but with help. I realized I needed to reformat the $_FILES variable to match buddydrive instead of GD bbPress, so I’ve done that with simple reassignment:
$to_bd_files = array( 'buddyfile-upload' => array( 'name' => $files['d4p_attachment']['name'][0], 'type' =>$files['d4p_attachment']['type'][0], 'tmp_name' =>$files['d4p_attachment']['tmp_name'][0], 'error' =>$files['d4p_attachment']['error'][0], 'size' =>$files['d4p_attachment']['size'][0] ) );
I’ve also manually initialized bp_params:
$bp_params = array( 'object' => 'buddydrive-file', 'item_id' => bp_loggedin_user_id(), 'parent_folder_id' => 0, 'privacy' => 'groups', 'privacy_item_id' => 3 );
And by writing out all of these variables to a .txt file, I can see the structure matches perfectly for both $_FILES and bp_params.
Here’s the issue I’m currently working on: I call:
$bd_file = buddydrive_upload_item( $to_bd_files, $bp_params['item_id'] );
And $bd_file contains [‘error’][‘Invalid form submission’]. I know this is an error with the wp_handle_upload as called by upload function of BuddyPress’ BP_Attachment class (because BuddyDrive_Attachment extends it but doesn’t override that function).
According to wp_handle_upload, $_POST[‘action’] must equal $overrides[‘action’] (or ‘wp_handle_upload’ if not set).
…
…Ok, figured it out, I’ll post this anyway. BuddyDrive sets $overrides[‘action’] to ‘buddydrive_upload’. If I add this line in GD bbPress Attachments, it works!
$_POST['action'] = 'buddydrive_upload';
Except, of course, that now it doesn’t get correctly added to the media library. The URL is listed as MYSITE.COM/groups/MYGROUP/forum/reply/1592/attachment/1594/ or similar. But that’s a problem for later.
So, I ended up just “forking” the plugin and creating my own. I only had to modify one file, code/attachments/front.php. You can see the changes at github.com/alexalexalex09/gd-bbpress-attachments.
I customized the save_reply() function by inserting the following on line 103 (after $errors = new gdbbp_Error();) :
//-----------------------------------------// //----Prepare Attachment for BuddyDrive----// //-----------------------------------------// //--Derived from save_reply in GD bbPress--// //-----------------------------------------// //Change $overrides from GD bbPress Attachments' version //$overrides = array('test_form' => false, 'upload_error_handler' => 'd4p_bbattachment_handle_upload_error'); $tmppath = $_FILES['d4p_attachment']['tmp_name'][0]; $tmpcopypath = __DIR__ . '/' . $_FILES['d4p_attachment']['name'][0]; if (copy($tmppath, $tmpcopypath)) { // Copied } else { // Not copied echo ('File not copied'); } $overrides = array('test_form' => false); //Attach to BuddyDrive using custom function $this->double_attach_bd($_FILES); if (copy($tmpcopypath, $tmppath)) { //$txt .= "Copied back \r\n"; } else { //$txt .= "Not copied back \r\n"; return "Not copied back"; } // Reset overrides $overrides = array('test_form' => false, 'upload_error_handler' => 'd4p_bbattachment_handle_upload_error'); //-----------------------------------------// //-------------End Custom Edits------------// //-----------------------------------------//
I also added a new function to go along with this, double_attach_bd_files($files) :
public function double_attach_bd($files) { //-----------------------------------------// //------Save Attachment in BuddyDrive------// //-----------------------------------------// //--Derived from buddydrive_upload_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; } // (Don't) Use deprecated function for the deprecated UI (because it shouldn't be allowed) //if ( empty( $_POST['bp_params'] ) && buddydrive_use_deprecated_ui() ) { // buddydrive_save_new_buddyfile(); // return; //} //TODO // (Don't) Check the nonce (because it will fail - but why? And does it matter?) // check_admin_referer( 'bp-uploader' ); //Init the BuddyPress parameters $bp_params = array( 'object' => 'buddydrive-file', 'item_id' => bp_loggedin_user_id(), 'parent_folder_id' => 0, 'privacy' => 'groups', 'privacy_item_id' => 3 ); $to_bd_files = array( 'buddyfile-upload' => array( 'name' => $files['d4p_attachment']['name'][0], 'type' =>$files['d4p_attachment']['type'][0], 'tmp_name' =>$files['d4p_attachment']['tmp_name'][0], 'error' =>$files['d4p_attachment']['error'][0], 'size' =>$files['d4p_attachment']['size'][0] ) ); //Stop wp_handle_upload from giving an "Invalid Forum Submission" error $_POST['action'] = 'buddydrive_upload'; //I don't know if bpcustom_attachments_json_response actually works in this context //Check params if ( empty( $bp_params['item_id'] ) ) { $this->bpcustom_attachments_json_response( false, $is_html4 ); } // Capability check if ( ! is_user_logged_in() || ( (int) bp_loggedin_user_id() !== (int) $bp_params['item_id'] && ! bp_current_user_can( 'bp_moderate' ) ) ) { $this->bpcustom_attachments_json_response( false, $is_html4 ); } // Upload the File! $bd_file = buddydrive_upload_item( $to_bd_files, $bp_params['item_id'] ); // Error while trying to upload the file if ( ! empty( $bd_file['error'] ) ) { $this->bpcustom_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']; /** * @todo check it has no impact on BuddyDrive Editor */ $privacy = buddydrive_get_default_privacy(); $groups = array(); // Set folder //TODO //Make folder selectable by user $parent_folder_id = 0; //-----------------------------------------// //-------------End Custom Edits------------// //-----------------------------------------// if ( ! empty( $bp_params['parent_folder_id'] ) ) { $parent_folder_id = (int) $bp_params['parent_folder_id']; } if ( ! empty( $bp_params['privacy'] ) ) { $privacy = $bp_params['privacy']; if ( ! empty( $bp_params['privacy_item_id'] ) && 'groups' === $privacy ) { $groups = (array) $bp_params['privacy_item_id']; } } $buddyfile_id = buddydrive_add_item( array( 'user_id' => $bp_params['item_id'], 'type' => buddydrive_get_file_post_type(), 'guid' => $url, 'title' => $title, 'mime_type' => $mime, 'privacy' => $privacy, 'groups' => $groups, 'parent_folder_id' => $parent_folder_id, ) ); if ( empty( $buddyfile_id ) ) { $this->bpcustom_attachments_json_response( false, $is_html4, array( 'type' => 'upload_error', 'message' => __( 'Error while creating the file, sorry.', 'buddydrive' ), ) ); } else { // Try to create a thumbnail if it's an image and a public file if ( 'public' === $privacy ) { buddydrive_set_thumbnail( $buddyfile_id, $bd_file ); } } $response = buddydrive_prepare_for_js( $buddyfile_id ); $response['buddydrive_id'] = $response['id']; $response['url'] = $response['link']; $response['uploaded'] = true; unset( $response['id'] ); // Finally return file to the editor $this->bpcustom_attachments_json_response( true, $is_html4, $response ); }
So this creates a copy of the uploaded file, formats it and uploads it via BuddyDrive, and then moves the copied file back into place so that GD bbPress Attachments can continue as normal.
Glad you were able to customize the plugin to your liking. Be aware that future updates will overwrite this customization.
- The topic ‘Upload attachments to BuddyDrive’ is closed to new replies.