コード例 #1
0
/**
 * Handle avatar uploading.
 *
 * The functions starts off by checking that the file has been uploaded
 * properly using bp_core_check_avatar_upload(). It then checks that the file
 * size is within limits, and that it has an accepted file extension (jpg, gif,
 * png). If everything checks out, crop the image and move it to its real
 * location.
 *
 * @see bp_core_check_avatar_upload()
 * @see bp_core_check_avatar_type()
 *
 * @param array  $file              The appropriate entry the from $_FILES superglobal.
 * @param string $upload_dir_filter A filter to be applied to 'upload_dir'.
 *
 * @return bool True on success, false on failure.
 */
function bp_core_avatar_handle_upload($file, $upload_dir_filter)
{
    /**
     * Filters whether or not to handle uploading.
     *
     * If you want to override this function, make sure you return false.
     *
     * @since 1.2.4
     *
     * @param bool   $value             Whether or not to crop.
     * @param array  $file              Appropriate entry from $_FILES superglobal.
     * @parma string $upload_dir_filter A filter to be applied to 'upload_dir'.
     */
    if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) {
        return true;
    }
    // Setup some variables.
    $bp = buddypress();
    $upload_path = bp_core_avatar_upload_path();
    // Upload the file.
    $avatar_attachment = new BP_Attachment_Avatar();
    $bp->avatar_admin->original = $avatar_attachment->upload($file, $upload_dir_filter);
    // In case of an error, stop the process and display a feedback to the user.
    if (!empty($bp->avatar_admin->original['error'])) {
        bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $bp->avatar_admin->original['error']), 'error');
        return false;
    }
    // The Avatar UI available width
    $ui_available_width = 0;
    // Try to set the ui_available_width using the avatar_admin global
    if (isset($bp->avatar_admin->ui_available_width)) {
        $ui_available_width = $bp->avatar_admin->ui_available_width;
    }
    // Maybe resize.
    $bp->avatar_admin->resized = $avatar_attachment->shrink($bp->avatar_admin->original['file'], $ui_available_width);
    $bp->avatar_admin->image = new stdClass();
    // We only want to handle one image after resize.
    if (empty($bp->avatar_admin->resized)) {
        $bp->avatar_admin->image->file = $bp->avatar_admin->original['file'];
        $bp->avatar_admin->image->dir = str_replace($upload_path, '', $bp->avatar_admin->original['file']);
    } else {
        $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path'];
        $bp->avatar_admin->image->dir = str_replace($upload_path, '', $bp->avatar_admin->resized['path']);
        @unlink($bp->avatar_admin->original['file']);
    }
    // Check for WP_Error on what should be an image.
    if (is_wp_error($bp->avatar_admin->image->dir)) {
        bp_core_add_message(sprintf(__('Upload failed! Error was: %s', 'buddypress'), $bp->avatar_admin->image->dir->get_error_message()), 'error');
        return false;
    }
    // If the uploaded image is smaller than the "full" dimensions, throw a warning.
    if ($avatar_attachment->is_too_small($bp->avatar_admin->image->file)) {
        bp_core_add_message(sprintf(__('You have selected an image that is smaller than recommended. For best results, upload a picture larger than %d x %d pixels.', 'buddypress'), bp_core_avatar_full_width(), bp_core_avatar_full_height()), 'error');
    }
    // Set the url value for the image.
    $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
    return true;
}
コード例 #2
0
 /**
  * @group upload
  * @group avatar
  */
 public function test_bp_attachment_avatar_group_upload()
 {
     $bp = buddypress();
     $reset_files = $_FILES;
     $reset_post = $_POST;
     $reset_current_group = $bp->groups->current_group;
     $g = $this->factory->group->create();
     $bp->groups->current_group = groups_get_group(array('group_id' => $g, 'populate_extras' => true));
     // Upload the file
     $avatar_attachment = new BP_Attachment_Avatar();
     $_POST['action'] = $avatar_attachment->action;
     $_FILES[$avatar_attachment->file_input] = array('tmp_name' => $this->image_file, 'name' => 'mystery-man.jpg', 'type' => 'image/jpeg', 'error' => 0, 'size' => filesize($this->image_file));
     $group_avatar = $avatar_attachment->upload($_FILES, 'groups_avatar_upload_dir');
     $this->assertEquals($group_avatar['file'], $bp->avatar->upload_path . '/group-avatars/' . $g . '/mystery-man.jpg');
     // clean up!
     $this->clean_avatars('group');
     $bp->groups->current_group = $reset_current_group;
     $_FILES = $reset_files;
     $_POST = $reset_post;
 }