bp_avatar_ajax_set()
Ajax set an avatar for a given object and item id.
Description
Return
(string|null) A JSON object containing success data if the crop/capture succeeded error message otherwise.
Source
File: bp-core/bp-core-avatars.php
function bp_avatar_ajax_set() { if ( ! bp_is_post_request() ) { wp_send_json_error(); } // Check the nonce. check_admin_referer( 'bp_avatar_cropstore', 'nonce' ); $avatar_data = wp_parse_args( $_POST, array( 'crop_w' => bp_core_avatar_full_width(), 'crop_h' => bp_core_avatar_full_height(), 'crop_x' => 0, 'crop_y' => 0 ) ); if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) || empty( $avatar_data['original_file'] ) ) { wp_send_json_error(); } // Capability check. if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) { wp_send_json_error(); } if ( ! empty( $avatar_data['type'] ) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object'] ) { $webcam_avatar = false; if ( ! empty( $avatar_data['original_file'] ) ) { $webcam_avatar = str_replace( array( 'data:image/png;base64,', ' ' ), array( '', '+' ), $avatar_data['original_file'] ); $webcam_avatar = base64_decode( $webcam_avatar ); } if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) { wp_send_json_error( array( 'feedback_code' => 1 ) ); } else { $return = array( 'avatar' => esc_url( bp_core_fetch_avatar( array( 'object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full', ) ) ), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id'], ); /** * Fires if the new avatar was successfully captured. * * @since BuddyPress 1.1.0 Used to inform the avatar was successfully cropped * @since BuddyPress 2.3.4 Add two new parameters to inform about the user id and * about the way the avatar was set (eg: 'crop' or 'camera') * Move the action at the right place, once the avatar is set * @since BuddyPress 2.8.0 Added the `$avatar_data` parameter. * * @param string $item_id Inform about the user id the avatar was set for. * @param string $type Inform about the way the avatar was set ('camera'). * @param array $avatar_data Array of parameters passed to the avatar handler. */ do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data ); wp_send_json_success( $return ); } return; } $original_file = str_replace( bp_core_avatar_url(), '', $avatar_data['original_file'] ); // Set avatars dir & feedback part. if ( 'user' === $avatar_data['object'] ) { $avatar_dir = 'avatars'; // Defaults to object-avatars dir. } else { $avatar_dir = sanitize_key( $avatar_data['object'] ) . '-avatars'; } // Crop args. $r = array( 'item_id' => $avatar_data['item_id'], 'object' => $avatar_data['object'], 'avatar_dir' => $avatar_dir, 'original_file' => $original_file, 'crop_w' => $avatar_data['crop_w'], 'crop_h' => $avatar_data['crop_h'], 'crop_x' => $avatar_data['crop_x'], 'crop_y' => $avatar_data['crop_y'] ); // Handle crop. if ( bp_core_avatar_handle_crop( $r ) ) { $return = array( 'avatar' => esc_url( bp_core_fetch_avatar( array( 'object' => $avatar_data['object'], 'item_id' => $avatar_data['item_id'], 'html' => false, 'type' => 'full', ) ) ), 'feedback_code' => 2, 'item_id' => $avatar_data['item_id'], ); if ( 'user' === $avatar_data['object'] ) { /** This action is documented in bp-core/bp-core-avatars.php */ do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r ); } elseif ( 'group' === $avatar_data['object'] ) { /** This action is documented in bp-groups/bp-groups-screens.php */ do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r ); } wp_send_json_success( $return ); } else { wp_send_json_error( array( 'feedback_code' => 1, ) ); } }
Changelog
Version | Description |
---|---|
BuddyPress 2.3.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.