bp_avatar_ajax_set()

Ajax set an avatar for a given object and item id.


Return Return

(string|null) A JSON object containing success data if the crop/capture succeeded error message otherwise.


Source 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'],
			);

			/** This action is documented in wp-includes/deprecated.php */
			do_action_deprecated( 'xprofile_avatar_uploaded', array( (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data ), '6.0.0', 'bp_members_avatar_uploaded' );

			/**
			 * Fires if the new avatar was successfully captured.
			 *
			 * @since 6.0.0
			 *
			 * @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( 'bp_members_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 wp-includes/deprecated.php */
			do_action_deprecated( 'xprofile_avatar_uploaded', array( (int) $avatar_data['item_id'], $avatar_data['type'], $r ), '6.0.0', 'bp_members_avatar_uploaded' );

			/** This action is documented in bp-core/bp-core-avatars.php */
			do_action( 'bp_members_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,
		) );
	}
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.