bp_core_avatar_handle_upload( array $file, string $upload_dir_filter )
Handle avatar uploading.
Description
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 also
Parameters
- $file
-
(Required) The appropriate entry the from $_FILES superglobal.
- $upload_dir_filter
-
(Required) A filter to be applied to 'upload_dir'.
Return
(bool) True on success, false on failure.
Source
File: bp-core/bp-core-avatars.php
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 | 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 BuddyPress 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' , 'buddyboss' ), $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 Error: %s' , 'buddyboss' ), $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( __( 'For best results, upload an image that is %1$spx by %2$spx or larger.' , 'buddyboss' ), 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; } |
Changelog
Version | Description |
---|---|
BuddyPress 1.1.0 | Introduced. |
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.