bp_attachments_cover_image_ajax_delete

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

Description

(string|null) bp_attachments_cover_image_ajax_delete(); 

Returns (string|null)

A json object containing success data if the cover image was deleted error message otherwise.


Usage

  1. if ( !function_exists( 'bp_attachments_cover_image_ajax_delete' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-attachments.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_attachments_cover_image_ajax_delete(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-core/bp-core-attachments.php  
  1. function bp_attachments_cover_image_ajax_delete() { 
  2. // Bail if not a POST action. 
  3. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  4. wp_send_json_error(); 
  5.  
  6. $cover_image_data = $_POST; 
  7.  
  8. if ( empty( $cover_image_data['object'] ) || empty( $cover_image_data['item_id'] ) ) { 
  9. wp_send_json_error(); 
  10.  
  11. // Check the nonce. 
  12. check_admin_referer( 'bp_delete_cover_image', 'nonce' ); 
  13.  
  14. // Capability check. 
  15. if ( ! bp_attachments_current_user_can( 'edit_cover_image', $cover_image_data ) ) { 
  16. wp_send_json_error(); 
  17.  
  18. // Set object for the user's case. 
  19. if ( 'user' === $cover_image_data['object'] ) { 
  20. $component = 'xprofile'; 
  21. $dir = 'members'; 
  22.  
  23. // Set it for any other cases. 
  24. } else { 
  25. $component = $cover_image_data['object'] . 's'; 
  26. $dir = $component; 
  27.  
  28. // Handle delete. 
  29. if ( bp_attachments_delete_file( array( 'item_id' => $cover_image_data['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) { 
  30. /** 
  31. * Fires if the cover image was successfully deleted. 
  32. * The dynamic portion of the hook will be xprofile in case of a user's 
  33. * cover image, groups in case of a group's cover image. For instance: 
  34. * Use add_action( 'xprofile_cover_image_deleted' ) to run your specific 
  35. * code once the user has deleted his cover image. 
  36. * @since 2.8.0 
  37. * @param int $item_id Inform about the item id the cover image was deleted for. 
  38. */ 
  39. do_action( "{$component}_cover_image_deleted", (int) $cover_image_data['item_id'] ); 
  40.  
  41. // Defaults no cover image. 
  42. $response = array( 
  43. 'reset_url' => '',  
  44. 'feedback_code' => 3 ,  
  45. ); 
  46.  
  47. // Get cover image settings in case there's a default header. 
  48. $cover_params = bp_attachments_get_cover_image_settings( $component ); 
  49.  
  50. // Check if there's a default cover. 
  51. if ( ! empty( $cover_params['default_cover'] ) ) { 
  52. $response['reset_url'] = $cover_params['default_cover']; 
  53.  
  54. // Finally send the reset url. 
  55. wp_send_json_success( $response ); 
  56.  
  57. } else { 
  58. wp_send_json_error( array( 
  59. 'feedback_code' => 2,  
  60. ) );