/** * Ajax upload an avatar. * * @since 2.3.0 * * @return string|null A json object containing success data if the upload succeeded * error message otherwise. */ function bp_avatar_ajax_upload() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { wp_die(); } /** * 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(); // We need it to carry on if (!empty($_POST['bp_params'])) { $bp_params = $_POST['bp_params']; } else { bp_attachments_json_response(false, $is_html4); } // We need the object to set the uploads dir filter if (empty($bp_params['object'])) { bp_attachments_json_response(false, $is_html4); } // Capability check if (!bp_attachments_current_user_can('edit_avatar', $bp_params)) { bp_attachments_json_response(false, $is_html4); } $bp = buddypress(); $bp_params['upload_dir_filter'] = ''; $needs_reset = array(); if ('user' === $bp_params['object'] && bp_is_active('xprofile')) { $bp_params['upload_dir_filter'] = 'xprofile_avatar_upload_dir'; if (!bp_displayed_user_id() && !empty($bp_params['item_id'])) { $needs_reset = array('key' => 'displayed_user', 'value' => $bp->displayed_user); $bp->displayed_user->id = $bp_params['item_id']; } } elseif ('group' === $bp_params['object'] && bp_is_active('groups')) { $bp_params['upload_dir_filter'] = 'groups_avatar_upload_dir'; if (!bp_get_current_group_id() && !empty($bp_params['item_id'])) { $needs_reset = array('component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group); $bp->groups->current_group = groups_get_group(array('group_id' => $bp_params['item_id'], 'populate_extras' => false)); } } else { /** * Filter here to deal with other components. * * @since 2.3.0 * * @var array $bp_params the BuddyPress Ajax parameters. */ $bp_params = apply_filters('bp_core_avatar_ajax_upload_params', $bp_params); } if (!isset($bp->avatar_admin)) { $bp->avatar_admin = new stdClass(); } /** * The BuddyPress upload parameters is including the Avatar UI Available width, * add it to the avatar_admin global for a later use. */ if (isset($bp_params['ui_available_width'])) { $bp->avatar_admin->ui_available_width = (int) $bp_params['ui_available_width']; } // Upload the avatar $avatar = bp_core_avatar_handle_upload($_FILES, $bp_params['upload_dir_filter']); // Reset objects if (!empty($needs_reset)) { if (!empty($needs_reset['component'])) { $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value']; } else { $bp->{$needs_reset['key']} = $needs_reset['value']; } } // Init the feedback message $feedback_message = false; if (!empty($bp->template_message)) { $feedback_message = $bp->template_message; // Remove template message. $bp->template_message = false; $bp->template_message_type = false; @setcookie('bp-message', false, time() - 1000, COOKIEPATH); @setcookie('bp-message-type', false, time() - 1000, COOKIEPATH); } if (empty($avatar)) { // Default upload error $message = __('Upload failed.', 'buddypress'); // Use the template message if set if (!empty($feedback_message)) { $message = $feedback_message; } // Upload error reply bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => $message)); } if (empty($bp->avatar_admin->image->file)) { bp_attachments_json_response(false, $is_html4); } $uploaded_image = @getimagesize($bp->avatar_admin->image->file); // Set the name of the file $name = $_FILES['file']['name']; $name_parts = pathinfo($name); $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension'])))); // Finally return the avatar to the editor bp_attachments_json_response(true, $is_html4, array('name' => $name, 'url' => $bp->avatar_admin->image->url, 'width' => $uploaded_image[0], 'height' => $uploaded_image[1], 'feedback' => $feedback_message)); }
/** * 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))); }
/** * Ajax Upload and set a cover image * * @since 2.4.0 * * @return string|null A json object containing success data if the upload succeeded * error message otherwise. */ function bp_attachments_cover_image_ajax_upload() { // Bail if not a POST action if ('POST' !== strtoupper($_SERVER['REQUEST_METHOD'])) { wp_die(); } /** * 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(); // We need it to carry on if (!empty($_POST['bp_params'])) { $bp_params = bp_parse_args($_POST['bp_params'], array('object' => 'user', 'item_id' => bp_loggedin_user_id()), 'attachments_cover_image_ajax_upload'); } else { bp_attachments_json_response(false, $is_html4); } // We need the object to set the uploads dir filter if (empty($bp_params['object'])) { bp_attachments_json_response(false, $is_html4); } // Capability check if (!bp_attachments_current_user_can('edit_cover_image', $bp_params)) { bp_attachments_json_response(false, $is_html4); } $bp = buddypress(); $needs_reset = array(); // Member's cover image if ('user' === $bp_params['object']) { $object_data = array('dir' => 'members', 'component' => 'xprofile'); if (!bp_displayed_user_id() && !empty($bp_params['item_id'])) { $needs_reset = array('key' => 'displayed_user', 'value' => $bp->displayed_user); $bp->displayed_user->id = $bp_params['item_id']; } // Group's cover image } elseif ('group' === $bp_params['object']) { $object_data = array('dir' => 'groups', 'component' => 'groups'); if (!bp_get_current_group_id() && !empty($bp_params['item_id'])) { $needs_reset = array('component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group); $bp->groups->current_group = groups_get_group(array('group_id' => $bp_params['item_id'], 'populate_extras' => false)); } // Other object's cover image } else { $object_data = apply_filters('bp_attachments_cover_image_object_dir', array(), $bp_params['object']); } // Stop here in case of a missing parameter for the object if (empty($object_data['dir']) || empty($object_data['component'])) { bp_attachments_json_response(false, $is_html4); } $cover_image_attachment = new BP_Attachment_Cover_Image(); $uploaded = $cover_image_attachment->upload($_FILES); // Reset objects if (!empty($needs_reset)) { if (!empty($needs_reset['component'])) { $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value']; } else { $bp->{$needs_reset['key']} = $needs_reset['value']; } } if (!empty($uploaded['error'])) { // Upload error response bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $uploaded['error']))); } // Default error message $error_message = __('There was a problem uploading the cover image.', 'buddypress'); // Get BuddyPress Attachments Uploads Dir datas $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); // The BP Attachments Uploads Dir is not set, stop. if (!$bp_attachments_uploads_dir) { bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => $error_message)); } $cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image'; $cover_dir = trailingslashit($bp_attachments_uploads_dir['basedir']) . $cover_subdir; if (!is_dir($cover_dir)) { // Upload error response bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => $error_message)); } /** * Generate the cover image so that it fit to feature's dimensions * * Unlike the Avatar, Uploading and generating the cover image is happening during * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image * class, let's use it. */ $cover = bp_attachments_cover_image_generate_file(array('file' => $uploaded['file'], 'component' => $object_data['component'], 'cover_image_dir' => $cover_dir), $cover_image_attachment); if (!$cover) { // Upload error response bp_attachments_json_response(false, $is_html4, array('type' => 'upload_error', 'message' => $error_message)); } // Build the url to the file $cover_url = trailingslashit($bp_attachments_uploads_dir['baseurl']) . $cover_subdir . '/' . $cover['cover_basename']; // Init Feedback code, 1 is success $feedback_code = 1; // 0 is the size warning if ($cover['is_too_small']) { $feedback_code = 0; } // Set the name of the file $name = $_FILES['file']['name']; $name_parts = pathinfo($name); $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension'])))); /** * Fires if the new cover image was successfully uploaded. * * The dynamic portion of the hook will be xprofile in case of a user's * cover image, groups in case of a group's cover image. For instance: * Use add_action( 'xprofile_cover_image_uploaded' ) to run your specific * code once the user has set his cover image. * * @since 2.4.0 * * @param int $item_id Inform about the item id the cover image was set for. */ do_action($object_data['component'] . '_cover_image_uploaded', (int) $bp_params['item_id']); // Finally return the cover image url to the UI bp_attachments_json_response(true, $is_html4, array('name' => $name, 'url' => $cover_url, 'feedback_code' => $feedback_code)); }
/** * Assuming you have defined your attachment class */ function front_end_attachments_handle_upload() { $is_html4 = false; if (!empty($_POST['html4'])) { $is_html4 = true; } if (empty($_POST['bp_params']) || empty($_POST['bp_params']['item_id'])) { return; } // Init the BuddyPress parameters $bp_params = (array) $_POST['bp_params']; // Check the nonce check_admin_referer('bp-uploader'); // Capability check if (!current_user_can('upload_files')) { bp_attachments_json_response(false, $is_html4); } // Let's get ready to upload a new front end attachment $front_end_attachment = new Front_End_Attachment(); $file = $front_end_attachment->upload($_FILES); /** * If there's an error during the upload process * stop.. */ if (!empty($result['error'])) { bp_attachments_json_response(false, $is_html4); } else { $name_parts = pathinfo($file['file']); // Construct the attachment array $attachment = array('post_mime_type' => $file['type'], 'guid' => $file['url'], 'post_title' => $name_parts['filename'], 'post_status' => 'front_end_public'); // Force the status of the Attachment's post type to be our custom one add_filter('wp_insert_attachment_data', 'front_end_attachments_set_status', 10, 2); // Save the data $id = wp_insert_attachment($attachment, $file['file'], 0); // Remove the filter remove_filter('wp_insert_attachment_data', 'front_end_attachments_set_status', 10, 2); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file['file'])); // Finally return file to the editor bp_attachments_json_response(true, $is_html4, array('name' => esc_html($name_parts['filename']), 'icon' => wp_get_attachment_thumb_url($id), 'url' => esc_url_raw($file['url']))); } } }