bp_avatar_ajax_set

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

Description

(string|null) bp_avatar_ajax_set(); 

Returns (string|null)

A JSON object containing success data if the crop/capture succeeded error message otherwise.


Usage

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

Defined (1)

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

/bp-core/bp-core-avatars.php  
  1. function bp_avatar_ajax_set() { 
  2. // Bail if not a POST action. 
  3. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  4. wp_send_json_error(); 
  5.  
  6. // Check the nonce. 
  7. check_admin_referer( 'bp_avatar_cropstore', 'nonce' ); 
  8.  
  9. $avatar_data = wp_parse_args( $_POST, array( 
  10. 'crop_w' => bp_core_avatar_full_width(),  
  11. 'crop_h' => bp_core_avatar_full_height(),  
  12. 'crop_x' => 0,  
  13. 'crop_y' => 0 
  14. ) ); 
  15.  
  16. if ( empty( $avatar_data['object'] ) || empty( $avatar_data['item_id'] ) || empty( $avatar_data['original_file'] ) ) { 
  17. wp_send_json_error(); 
  18.  
  19. // Capability check. 
  20. if ( ! bp_attachments_current_user_can( 'edit_avatar', $avatar_data ) ) { 
  21. wp_send_json_error(); 
  22.  
  23. if ( ! empty( $avatar_data['type'] ) && 'camera' === $avatar_data['type'] && 'user' === $avatar_data['object'] ) { 
  24. $webcam_avatar = false; 
  25.  
  26. if ( ! empty( $avatar_data['original_file'] ) ) { 
  27. $webcam_avatar = str_replace( array( 'data:image/png;base64, ', ' ' ), array( '', '+' ), $avatar_data['original_file'] ); 
  28. $webcam_avatar = base64_decode( $webcam_avatar ); 
  29.  
  30. if ( ! bp_avatar_handle_capture( $webcam_avatar, $avatar_data['item_id'] ) ) { 
  31. wp_send_json_error( array( 
  32. 'feedback_code' => 1 
  33. ) ); 
  34.  
  35. } else { 
  36. $return = array( 
  37. 'avatar' => html_entity_decode( bp_core_fetch_avatar( array( 
  38. 'object' => $avatar_data['object'],  
  39. 'item_id' => $avatar_data['item_id'],  
  40. 'html' => false,  
  41. 'type' => 'full',  
  42. ) ) ),  
  43. 'feedback_code' => 2,  
  44. 'item_id' => $avatar_data['item_id'],  
  45. ); 
  46.  
  47. /** 
  48. * Fires if the new avatar was successfully captured. 
  49. * @since 1.1.0 Used to inform the avatar was successfully cropped 
  50. * @since 2.3.4 Add two new parameters to inform about the user id and 
  51. * about the way the avatar was set (eg: 'crop' or 'camera') 
  52. * Move the action at the right place, once the avatar is set 
  53. * @since 2.8.0 Added the `$avatar_data` parameter. 
  54. * @param string $item_id Inform about the user id the avatar was set for. 
  55. * @param string $type Inform about the way the avatar was set ('camera'). 
  56. * @param array $avatar_data Array of parameters passed to the avatar handler. 
  57. */ 
  58. do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $avatar_data ); 
  59.  
  60. wp_send_json_success( $return ); 
  61.  
  62. return; 
  63.  
  64. $original_file = str_replace( bp_core_avatar_url(), '', $avatar_data['original_file'] ); 
  65.  
  66. // Set avatars dir & feedback part. 
  67. if ( 'user' === $avatar_data['object'] ) { 
  68. $avatar_dir = 'avatars'; 
  69.  
  70. // Defaults to object-avatars dir. 
  71. } else { 
  72. $avatar_dir = sanitize_key( $avatar_data['object'] ) . '-avatars'; 
  73.  
  74. // Crop args. 
  75. $r = array( 
  76. 'item_id' => $avatar_data['item_id'],  
  77. 'object' => $avatar_data['object'],  
  78. 'avatar_dir' => $avatar_dir,  
  79. 'original_file' => $original_file,  
  80. 'crop_w' => $avatar_data['crop_w'],  
  81. 'crop_h' => $avatar_data['crop_h'],  
  82. 'crop_x' => $avatar_data['crop_x'],  
  83. 'crop_y' => $avatar_data['crop_y'] 
  84. ); 
  85.  
  86. // Handle crop. 
  87. if ( bp_core_avatar_handle_crop( $r ) ) { 
  88. $return = array( 
  89. 'avatar' => html_entity_decode( bp_core_fetch_avatar( array( 
  90. 'object' => $avatar_data['object'],  
  91. 'item_id' => $avatar_data['item_id'],  
  92. 'html' => false,  
  93. 'type' => 'full',  
  94. ) ) ),  
  95. 'feedback_code' => 2,  
  96. 'item_id' => $avatar_data['item_id'],  
  97. ); 
  98.  
  99. if ( 'user' === $avatar_data['object'] ) { 
  100. /** This action is documented in bp-core/bp-core-avatars.php */ 
  101. do_action( 'xprofile_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r ); 
  102. } elseif ( 'group' === $avatar_data['object'] ) { 
  103. /** This action is documented in bp-groups/bp-groups-screens.php */ 
  104. do_action( 'groups_avatar_uploaded', (int) $avatar_data['item_id'], $avatar_data['type'], $r ); 
  105.  
  106. wp_send_json_success( $return ); 
  107. } else { 
  108. wp_send_json_error( array( 
  109. 'feedback_code' => 1,  
  110. ) );