/**
  * Construct Upload parameters.
  *
  * @since 2.3.0
  *
  * @see  BP_Attachment::__construct() for list of parameters
  * @uses bp_core_avatar_original_max_filesize()
  * @uses BP_Attachment::__construct()
  */
 public function __construct()
 {
     // Allowed avatar types.
     $allowed_types = bp_core_get_allowed_avatar_types();
     parent::__construct(array('action' => 'bp_avatar_upload', 'file_input' => 'file', 'original_max_filesize' => bp_core_avatar_original_max_filesize(), 'upload_error_strings' => array(9 => sprintf(__('That photo is too big. Please upload one smaller than %s', 'buddypress'), size_format(bp_core_avatar_original_max_filesize())), 10 => sprintf(_n('Please upload only this file type: %s.', 'Please upload only these file types: %s.', count($allowed_types), 'buddypress'), self::get_avatar_types($allowed_types)))));
 }
Example #2
0
/**
 * Is the file size of the current avatar upload permitted?
 *
 * @param array $file The $_FILES array.
 *
 * @return bool True if the avatar is under the size limit, otherwise false.
 */
function bp_core_check_avatar_size($file)
{
    if ($file['file']['size'] > bp_core_avatar_original_max_filesize()) {
        return false;
    }
    return true;
}
function tv_bp_core_avatar_handle_upload($file, $upload_dir_filter)
{
    /***
     * You may want to hook into this filter if you want to override this function.
     * Make sure you return false.
     */
    if (!apply_filters('bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter)) {
        return true;
    }
    require_once ABSPATH . '/wp-admin/includes/file.php';
    $uploadErrors = array(0 => __('The image was uploaded successfully', 'buddypress'), 1 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 2 => __('The image exceeds the maximum allowed file size of: ', 'buddypress') . size_format(bp_core_avatar_original_max_filesize()), 3 => __('The uploaded file was only partially uploaded.', 'buddypress'), 4 => __('The image was not uploaded.', 'buddypress'), 6 => __('Missing a temporary folder.', 'buddypress'));
    if (!bp_core_check_avatar_upload($file)) {
        bp_core_add_message(sprintf(__('Your upload failed, please try again. Error was: %s', 'buddypress'), $uploadErrors[$file['file']['error']]), 'error');
        return false;
    }
    if (!bp_core_check_avatar_size($file)) {
        bp_core_add_message(sprintf(__('The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format(bp_core_avatar_original_max_filesize())), 'error');
        return false;
    }
    if (!bp_core_check_avatar_type($file)) {
        bp_core_add_message(__('Please upload only JPG, GIF or PNG photos.', 'buddypress'), 'error');
        return false;
    }
    // Filter the upload location
    add_filter('upload_dir', $upload_dir_filter, 10, 0);
    $upload_dir = wp_upload_dir();
    emptyDirectory($upload_dir['path']);
    $bp = buddypress();
    $bp->avatar_admin->original = wp_handle_upload($file['file'], array('test_form' => false));
    // Remove the upload_dir filter, so that other upload URLs on the page
    // don't break
    remove_filter('upload_dir', $upload_dir_filter, 10, 0);
    // Move the file to the correct upload location.
    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;
    }
    // Get image size
    $size = @getimagesize($bp->avatar_admin->original['file']);
    $error = false;
    // Check image size and shrink if too large
    if ($size[0] > bp_core_avatar_original_max_width()) {
        $editor = wp_get_image_editor($bp->avatar_admin->original['file']);
        if (!is_wp_error($editor)) {
            $editor->set_quality(100);
            $resized = $editor->resize(bp_core_avatar_original_max_width(), bp_core_avatar_original_max_width(), false);
            if (!is_wp_error($resized)) {
                $thumb = $editor->save($editor->generate_filename());
            } else {
                $error = $resized;
            }
            // Check for thumbnail creation errors
            if (false === $error && is_wp_error($thumb)) {
                $error = $thumb;
            }
            // Thumbnail is good so proceed
            if (false === $error) {
                $bp->avatar_admin->resized = $thumb;
            }
        } else {
            $error = $editor;
        }
        if (false !== $error) {
            bp_core_add_message(sprintf(__('Upload Failed! Error was: %s', 'buddypress'), $error->get_error_message()), 'error');
            return false;
        }
    }
    if (!isset($bp->avatar_admin->image)) {
        $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->dir = str_replace(bp_core_avatar_upload_path(), '', $bp->avatar_admin->original['file']);
    } else {
        $bp->avatar_admin->image->dir = str_replace(bp_core_avatar_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;
    }
    // Set the url value for the image
    $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir;
    return true;
}