bp_core_delete_existing_avatar( array|string $args = '' )

Delete an existing avatar.


Description Description


Parameters Parameters

$args

(Optional) Array of function parameters.

  • 'item_id'
    (bool|int) ID of the item whose avatar you're deleting. Defaults to the current item of type $object.
  • 'object'
    (string) Object type of the item whose avatar you're deleting. 'user', 'group', 'blog', or custom. Default: 'user'.
  • 'avatar_dir'
    (bool|string) Subdirectory where avatar is located. Default: false, which falls back on the default location corresponding to the $object.

Default value: ''


Top ↑

Return Return

(bool) True on success, false on failure.


Top ↑

Source Source

File: bp-core/bp-core-avatars.php

function bp_core_delete_existing_avatar( $args = '' ) {

	$defaults = array(
		'item_id'    => false,
		'object'     => 'user', // User OR group OR blog OR custom type (if you use filters).
		'avatar_dir' => false
	);

	$args = wp_parse_args( $args, $defaults );

	/**
	 * Filters whether or not to handle deleting an existing avatar.
	 *
	 * If you want to override this function, make sure you return false.
	 *
	 * @since 2.5.1
	 *
	 * @param bool  $value Whether or not to delete the avatar.
	 * @param array $args {
	 *     Array of function parameters.
	 *
	 *     @type bool|int    $item_id    ID of the item whose avatar you're deleting.
	 *                                   Defaults to the current item of type $object.
	 *     @type string      $object     Object type of the item whose avatar you're
	 *                                   deleting. 'user', 'group', 'blog', or custom.
	 *                                   Default: 'user'.
	 *     @type bool|string $avatar_dir Subdirectory where avatar is located.
	 *                                   Default: false, which falls back on the default location
	 *                                   corresponding to the $object.
	 * }
	 */
	if ( ! apply_filters( 'bp_core_pre_delete_existing_avatar', true, $args ) ) {
		return true;
	}

	if ( empty( $args['item_id'] ) ) {
		if ( 'user' === $args['object'] ) {
			$args['item_id'] = bp_displayed_user_id();
		} elseif ( 'group' === $args['object'] ) {
			$args['item_id'] = buddypress()->groups->current_group->id;
		} elseif ( 'blog' === $args['object'] ) {
			$args['item_id'] = $current_blog->id;
		}

		/** This filter is documented in bp-core/bp-core-avatars.php */
		$item_id = apply_filters( 'bp_core_avatar_item_id', $args['item_id'], $args['object'] );
	} else {
		$item_id = $args['item_id'];
	}

	if ( $item_id && ( ctype_digit( $item_id ) || is_int( $item_id ) ) ) {
		$item_id = (int) $item_id;
	} else {
		return false;
	}

	if ( empty( $args['avatar_dir'] ) ) {
		if ( 'user' === $args['object'] ) {
			$args['avatar_dir'] = 'avatars';
		} elseif ( 'group' === $args['object'] ) {
			$args['avatar_dir'] = 'group-avatars';
		} elseif ( 'blog' === $args['object'] ) {
			$args['avatar_dir'] = 'blog-avatars';
		}

		/** This filter is documented in bp-core/bp-core-avatars.php */
		$avatar_dir = apply_filters( 'bp_core_avatar_dir', $args['avatar_dir'], $args['object'] );
	} else {
		$avatar_dir = $args['avatar_dir'];
	}

	if ( ! $avatar_dir ) {
		return false;
	}

	/** This filter is documented in bp-core/bp-core-avatars.php */
	$avatar_folder_dir = apply_filters( 'bp_core_avatar_folder_dir', bp_core_avatar_upload_path() . '/' . $avatar_dir . '/' . $item_id, $item_id, $args['object'], $avatar_dir );

	if ( ! is_dir( $avatar_folder_dir ) ) {
		return false;
	}

	if ( $av_dir = opendir( $avatar_folder_dir ) ) {
		while ( false !== ( $avatar_file = readdir( $av_dir ) ) ) {
			if ( ( preg_match( "/-bpfull/", $avatar_file ) || preg_match( "/-bpthumb/", $avatar_file ) ) && '.' != $avatar_file && '..' != $avatar_file ) {
				@unlink( $avatar_folder_dir . '/' . $avatar_file );
			}
		}
	}
	closedir( $av_dir );

	@rmdir( $avatar_folder_dir );

	/**
	 * Fires after deleting an existing avatar.
	 *
	 * @since 1.1.0
	 *
	 * @param array $args Array of arguments used for avatar deletion.
	 */
	do_action( 'bp_core_delete_existing_avatar', $args );

	return true;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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