bp_core_avatar_handle_upload

Handle avatar uploading.

Description

bp_core_avatar_handle_upload( (array) $file, (string) $upload_dir_filter ); 

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.

Parameters (2)

0. $file (array)
The appropriate entry the from $_FILES superglobal.
1. $upload_dir_filter (string)
A filter to be applied to upload_dir..

Usage

  1. if ( !function_exists( 'bp_core_avatar_handle_upload' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-avatars.php'; 
  3.  
  4. // The appropriate entry the from $_FILES superglobal. 
  5. $file = array(); 
  6.  
  7. // A filter to be applied to 'upload_dir'. 
  8. $upload_dir_filter = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_core_avatar_handle_upload($file, $upload_dir_filter); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-avatars.php  
  1. function bp_core_avatar_handle_upload( $file, $upload_dir_filter ) { 
  2.  
  3. /** 
  4. * Filters whether or not to handle uploading. 
  5. * If you want to override this function, make sure you return false. 
  6. * @since 1.2.4 
  7. * @param bool $value Whether or not to crop. 
  8. * @param array $file Appropriate entry from $_FILES superglobal. 
  9. * @parma string $upload_dir_filter A filter to be applied to 'upload_dir'. 
  10. */ 
  11. if ( ! apply_filters( 'bp_core_pre_avatar_handle_upload', true, $file, $upload_dir_filter ) ) { 
  12. return true; 
  13.  
  14. // Setup some variables. 
  15. $bp =buddypress); 
  16. $upload_path = bp_core_avatar_upload_path(); 
  17.  
  18. // Upload the file. 
  19. $avatar_attachment = new BP_Attachment_Avatar(); 
  20. $bp->avatar_admin->original = $avatar_attachment->upload( $file, $upload_dir_filter ); 
  21.  
  22. // In case of an error, stop the process and display a feedback to the user. 
  23. if ( ! empty( $bp->avatar_admin->original[error] ) ) { 
  24. bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', buddypress ), $bp->avatar_admin->original[error] ), error ); 
  25. return false; 
  26.  
  27. // The Avatar UI available width. 
  28. $ui_available_width = 0; 
  29.  
  30. // Try to set the ui_available_width using the avatar_admin global. 
  31. if ( isset( $bp->avatar_admin->ui_available_width ) ) { 
  32. $ui_available_width = $bp->avatar_admin->ui_available_width; 
  33.  
  34. // Maybe resize. 
  35. $bp->avatar_admin->resized = $avatar_attachment->shrink( $bp->avatar_admin->original['file'], $ui_available_width ); 
  36. $bp->avatar_admin->image = new stdClass(); 
  37.  
  38. // We only want to handle one image after resize. 
  39. if ( empty( $bp->avatar_admin->resized ) ) { 
  40. $bp->avatar_admin->image->file = $bp->avatar_admin->original['file']; 
  41. $bp->avatar_admin->image->dir = str_replace( $upload_path, '', $bp->avatar_admin->original['file'] ); 
  42. } else { 
  43. $bp->avatar_admin->image->file = $bp->avatar_admin->resized['path']; 
  44. $bp->avatar_admin->image->dir = str_replace( $upload_path, '', $bp->avatar_admin->resized['path'] ); 
  45. @unlink( $bp->avatar_admin->original['file'] ); 
  46.  
  47. // Check for WP_Error on what should be an image. 
  48. if ( is_wp_error( $bp->avatar_admin->image->dir ) ) { 
  49. bp_core_add_message( sprintf( __( 'Upload failed! Error was: %s', buddypress ), $bp->avatar_admin->image->dir->get_error_message() ), error ); 
  50. return false; 
  51.  
  52. // If the uploaded image is smaller than the "full" dimensions, throw a warning. 
  53. if ( $avatar_attachment->is_too_small( $bp->avatar_admin->image->file ) ) { 
  54. 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 ); 
  55.  
  56. // Set the url value for the image. 
  57. $bp->avatar_admin->image->url = bp_core_avatar_url() . $bp->avatar_admin->image->dir; 
  58.  
  59. return true;