bp_attachments_cover_image_ajax_delete()
Ajax delete a cover image for a given object and item id.
Description Description
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 = 'xprofile';
$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' ) ) ) {
/**
* Fires if the cover image was successfully deleted.
*
* The dynamic portion of the hook will be xprofile in case of a user's
* cover image, groups in case of a group's cover image. For instance:
* Use add_action( 'xprofile_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", (int) $args['item_id'] );
$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,
) );
}
}
Changelog Changelog
| Version | Description |
|---|---|
| 2.4.0 | Introduced. |