bp_avatar_ajax_upload

Ajax upload an avatar.

Description

(string|null) bp_avatar_ajax_upload(); 

Returns (string|null)

A JSON object containing success data if the upload succeeded error message otherwise.


Usage

  1. if ( !function_exists( 'bp_avatar_ajax_upload' ) ) { 
  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_upload(); 
  7.  

Defined (1)

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

/bp-core/bp-core-avatars.php  
  1. function bp_avatar_ajax_upload() { 
  2. // Bail if not a POST action. 
  3. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  4. wp_die(); 
  5.  
  6. /** 
  7. * Sending the json response will be different if 
  8. * the current Plupload runtime is html4. 
  9. */ 
  10. $is_html4 = false; 
  11. if ( ! empty( $_POST['html4' ] ) ) { 
  12. $is_html4 = true; 
  13.  
  14. // Check the nonce. 
  15. check_admin_referer( 'bp-uploader' ); 
  16.  
  17. // Init the BuddyPress parameters. 
  18. $bp_params = array(); 
  19.  
  20. // We need it to carry on. 
  21. if ( ! empty( $_POST['bp_params' ] ) ) { 
  22. $bp_params = $_POST['bp_params' ]; 
  23. } else { 
  24. bp_attachments_json_response( false, $is_html4 ); 
  25.  
  26. // We need the object to set the uploads dir filter. 
  27. if ( empty( $bp_params['object'] ) ) { 
  28. bp_attachments_json_response( false, $is_html4 ); 
  29.  
  30. // Capability check. 
  31. if ( ! bp_attachments_current_user_can( 'edit_avatar', $bp_params ) ) { 
  32. bp_attachments_json_response( false, $is_html4 ); 
  33.  
  34. $bp =buddypress); 
  35. $bp_params['upload_dir_filter'] = ''; 
  36. $needs_reset = array(); 
  37.  
  38. if ( 'user' === $bp_params['object'] && bp_is_active( 'xprofile' ) ) { 
  39. $bp_params['upload_dir_filter'] = 'xprofile_avatar_upload_dir'; 
  40.  
  41. if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) { 
  42. $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user ); 
  43. $bp->displayed_user->id = $bp_params['item_id']; 
  44. } elseif ( 'group' === $bp_params['object'] && bp_is_active( 'groups' ) ) { 
  45. $bp_params['upload_dir_filter'] = 'groups_avatar_upload_dir'; 
  46.  
  47. if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) { 
  48. $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group ); 
  49. $bp->groups->current_group = groups_get_group( $bp_params['item_id'] ); 
  50. } else { 
  51. /** 
  52. * Filter here to deal with other components. 
  53. * @since 2.3.0 
  54. * @var array $bp_params the BuddyPress Ajax parameters. 
  55. */ 
  56. $bp_params = apply_filters( 'bp_core_avatar_ajax_upload_params', $bp_params ); 
  57.  
  58. if ( ! isset( $bp->avatar_admin ) ) { 
  59. $bp->avatar_admin = new stdClass(); 
  60.  
  61. /** 
  62. * The BuddyPress upload parameters is including the Avatar UI Available width,  
  63. * add it to the avatar_admin global for a later use. 
  64. */ 
  65. if ( isset( $bp_params['ui_available_width'] ) ) { 
  66. $bp->avatar_admin->ui_available_width = (int) $bp_params['ui_available_width']; 
  67.  
  68. // Upload the avatar. 
  69. $avatar = bp_core_avatar_handle_upload( $_FILES, $bp_params['upload_dir_filter'] ); 
  70.  
  71. // Reset objects. 
  72. if ( ! empty( $needs_reset ) ) { 
  73. if ( ! empty( $needs_reset['component'] ) ) { 
  74. $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value']; 
  75. } else { 
  76. $bp->{$needs_reset['key']} = $needs_reset['value']; 
  77.  
  78. // Init the feedback message. 
  79. $feedback_message = false; 
  80.  
  81. if ( ! empty( $bp->template_message ) ) { 
  82. $feedback_message = $bp->template_message; 
  83.  
  84. // Remove template message. 
  85. $bp->template_message = false; 
  86. $bp->template_message_type = false; 
  87.  
  88. @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  89. @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() ); 
  90.  
  91. if ( empty( $avatar ) ) { 
  92. // Default upload error. 
  93. $message = __( 'Upload failed.', buddypress ); 
  94.  
  95. // Use the template message if set. 
  96. if ( ! empty( $feedback_message ) ) { 
  97. $message = $feedback_message; 
  98.  
  99. // Uploaderrorreply. 
  100. bp_attachments_json_response( false, $is_html4, array( 
  101. 'type' => 'upload_error',  
  102. 'message' => $message,  
  103. ) ); 
  104.  
  105. if ( empty( $bp->avatar_admin->image->file ) ) { 
  106. bp_attachments_json_response( false, $is_html4 ); 
  107.  
  108. $uploaded_image = @getimagesize( $bp->avatar_admin->image->file ); 
  109.  
  110. // Set the name of the file. 
  111. $name = $_FILES['file']['name']; 
  112. $name_parts = pathinfo( $name ); 
  113. $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); 
  114.  
  115. // Finally return the avatar to the editor. 
  116. bp_attachments_json_response( true, $is_html4, array( 
  117. 'name' => $name,  
  118. 'url' => $bp->avatar_admin->image->url,  
  119. 'width' => $uploaded_image[0],  
  120. 'height' => $uploaded_image[1],  
  121. 'feedback' => $feedback_message,  
  122. ) );