bp_attachments_cover_image_ajax_delete()

Ajax delete a cover image for a given object and item id.


Return Return

(string|null) A json object containing success data if the cover image was deleted error message otherwise.


Source Source

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

function bp_attachments_cover_image_ajax_delete() {
	if ( ! bp_is_post_request() ) {
		wp_send_json_error();
	}

	if ( empty( $_POST['object'] ) || empty( $_POST['item_id'] ) || ( ! ctype_digit( $_POST['item_id'] ) && ! is_int( $_POST['item_id'] ) ) ) {
		wp_send_json_error();
	}

	$args = array(
		'object'  => sanitize_text_field( $_POST['object'] ),
		'item_id' => (int) $_POST['item_id'],
	);

	// Check permissions.
	check_admin_referer( 'bp_delete_cover_image', 'nonce' );
	if ( ! bp_attachments_current_user_can( 'edit_cover_image', $args ) ) {
		wp_send_json_error();
	}

	// Set object for the user's case.
	if ( 'user' === $args['object'] ) {
		$component = 'members';
		$dir       = 'members';

	// Set it for any other cases.
	} else {
		$component = $args['object'] . 's';
		$dir       = $component;
	}

	// Handle delete.
	if ( bp_attachments_delete_file( array( 'item_id' => $args['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) {
		$item_id = (int) $args['item_id'];

		/**
		 * Fires if the cover image was successfully deleted.
		 *
		 * The dynamic portion of the hook will be members in case of a user's
		 * cover image, groups in case of a group's cover image. For instance:
		 * Use add_action( 'members_cover_image_deleted' ) to run your specific
		 * code once the user has deleted his cover image.
		 *
		 * @since 2.8.0
		 *
		 * @param int $item_id Inform about the item id the cover image was deleted for.
		 */
		do_action( "{$component}_cover_image_deleted", $item_id );

		// Handle deprecated xProfile action.
		if ( 'members' === $component ) {
			/** This filter is documented in wp-includes/deprecated.php */
			do_action_deprecated( 'xprofile_cover_image_deleted', array( $item_id ), '6.0.0', 'members_cover_image_deleted' );
		}

		$response = array(
			'reset_url'     => '',
			'feedback_code' => 3,
		);

		// Get cover image settings in case there's a default header.
		$cover_params = bp_attachments_get_cover_image_settings( $component );

		// Check if there's a default cover.
		if ( ! empty( $cover_params['default_cover'] ) ) {
			$response['reset_url'] = $cover_params['default_cover'];
		}

		wp_send_json_success( $response );

	} else {
		wp_send_json_error( array(
			'feedback_code' => 2,
		) );
	}
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

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