bp_attachments_cover_image_ajax_upload

Ajax Upload and set a cover image.

Description

(string|null) bp_attachments_cover_image_ajax_upload(); 

Returns (string|null)

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


Usage

  1. if ( !function_exists( 'bp_attachments_cover_image_ajax_upload' ) ) { 
  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_upload(); 
  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_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 = bp_parse_args( $_POST['bp_params'], array( 
  23. 'object' => 'user',  
  24. 'item_id' => bp_loggedin_user_id(),  
  25. ), 'attachments_cover_image_ajax_upload' ); 
  26. } else { 
  27. bp_attachments_json_response( false, $is_html4 ); 
  28.  
  29. // We need the object to set the uploads dir filter. 
  30. if ( empty( $bp_params['object'] ) ) { 
  31. bp_attachments_json_response( false, $is_html4 ); 
  32.  
  33. // Capability check. 
  34. if ( ! bp_attachments_current_user_can( 'edit_cover_image', $bp_params ) ) { 
  35. bp_attachments_json_response( false, $is_html4 ); 
  36.  
  37. $bp =buddypress); 
  38. $needs_reset = array(); 
  39.  
  40. // Member's cover image. 
  41. if ( 'user' === $bp_params['object'] ) { 
  42. $object_data = array( 'dir' => 'members', 'component' => 'xprofile' ); 
  43.  
  44. if ( ! bp_displayed_user_id() && ! empty( $bp_params['item_id'] ) ) { 
  45. $needs_reset = array( 'key' => 'displayed_user', 'value' => $bp->displayed_user ); 
  46. $bp->displayed_user->id = $bp_params['item_id']; 
  47.  
  48. // Group's cover image. 
  49. } elseif ( 'group' === $bp_params['object'] ) { 
  50. $object_data = array( 'dir' => 'groups', 'component' => 'groups' ); 
  51.  
  52. if ( ! bp_get_current_group_id() && ! empty( $bp_params['item_id'] ) ) { 
  53. $needs_reset = array( 'component' => 'groups', 'key' => 'current_group', 'value' => $bp->groups->current_group ); 
  54. $bp->groups->current_group = groups_get_group( $bp_params['item_id'] ); 
  55.  
  56. // Other object's cover image. 
  57. } else { 
  58. $object_data = apply_filters( 'bp_attachments_cover_image_object_dir', array(), $bp_params['object'] ); 
  59.  
  60. // Stop here in case of a missing parameter for the object. 
  61. if ( empty( $object_data['dir'] ) || empty( $object_data['component'] ) ) { 
  62. bp_attachments_json_response( false, $is_html4 ); 
  63.  
  64. /** 
  65. * Filters whether or not to handle cover image uploading. 
  66. * If you want to override this function, make sure you return an array with the 'result' key set. 
  67. * @since 2.5.1 
  68. * @param array $value 
  69. * @param array $bp_params 
  70. * @param array $needs_reset Stores original value of certain globals we need to revert to later. 
  71. * @param array $object_data 
  72. */ 
  73. $pre_filter = apply_filters( 'bp_attachments_pre_cover_image_ajax_upload', array(), $bp_params, $needs_reset, $object_data ); 
  74. if ( isset( $pre_filter['result'] ) ) { 
  75. bp_attachments_json_response( $pre_filter['result'], $is_html4, $pre_filter ); 
  76.  
  77. $cover_image_attachment = new BP_Attachment_Cover_Image(); 
  78. $uploaded = $cover_image_attachment->upload( $_FILES ); 
  79.  
  80. // Reset objects. 
  81. if ( ! empty( $needs_reset ) ) { 
  82. if ( ! empty( $needs_reset['component'] ) ) { 
  83. $bp->{$needs_reset['component']}->{$needs_reset['key']} = $needs_reset['value']; 
  84. } else { 
  85. $bp->{$needs_reset['key']} = $needs_reset['value']; 
  86.  
  87. if ( ! empty( $uploaded[error] ) ) { 
  88. // Uploaderrorresponse. 
  89. bp_attachments_json_response( false, $is_html4, array( 
  90. 'type' => 'upload_error',  
  91. 'message' => sprintf( __( 'Upload Failed! Error was: %s', buddypress ), $uploaded[error] ),  
  92. ) ); 
  93.  
  94. // Defaulterrormessage. 
  95. $error_message = __( 'There was a problem uploading the cover image.', buddypress ); 
  96.  
  97. // Get BuddyPress Attachments Uploads Dir datas. 
  98. $bp_attachments_uploads_dir = bp_attachments_uploads_dir_get(); 
  99.  
  100. // The BP Attachments Uploads Dir is not set, stop. 
  101. if ( ! $bp_attachments_uploads_dir ) { 
  102. bp_attachments_json_response( false, $is_html4, array( 
  103. 'type' => 'upload_error',  
  104. 'message' => $error_message,  
  105. ) ); 
  106.  
  107. $cover_subdir = $object_data['dir'] . '/' . $bp_params['item_id'] . '/cover-image'; 
  108. $cover_dir = trailingslashit( $bp_attachments_uploads_dir['basedir'] ) . $cover_subdir; 
  109.  
  110. if ( ! is_dir( $cover_dir ) ) { 
  111. // Uploaderrorresponse. 
  112. bp_attachments_json_response( false, $is_html4, array( 
  113. 'type' => 'upload_error',  
  114. 'message' => $error_message,  
  115. ) ); 
  116.  
  117. /** 
  118. * Generate the cover image so that it fit to feature's dimensions 
  119. * Unlike the Avatar, Uploading and generating the cover image is happening during 
  120. * the same Ajax request, as we already instantiated the BP_Attachment_Cover_Image 
  121. * class, let's use it. 
  122. */ 
  123. 'file' => $uploaded['file'],  
  124. 'component' => $object_data['component'],  
  125. 'cover_image_dir' => $cover_dir 
  126. ), $cover_image_attachment ); 
  127.  
  128. if ( ! $cover ) { 
  129. // Uploaderrorresponse. 
  130. bp_attachments_json_response( false, $is_html4, array( 
  131. 'type' => 'upload_error',  
  132. 'message' => $error_message,  
  133. ) ); 
  134.  
  135. // Build the url to the file. 
  136. $cover_url = trailingslashit( $bp_attachments_uploads_dir['baseurl'] ) . $cover_subdir . '/' . $cover['cover_basename']; 
  137.  
  138. // Init Feedback code, 1 is success. 
  139. $feedback_code = 1; 
  140.  
  141. // 0 is the size warning. 
  142. if ( $cover['is_too_small'] ) { 
  143. $feedback_code = 0; 
  144.  
  145. // Set the name of the file. 
  146. $name = $_FILES['file']['name']; 
  147. $name_parts = pathinfo( $name ); 
  148. $name = trim( substr( $name, 0, - ( 1 + strlen( $name_parts['extension'] ) ) ) ); 
  149.  
  150. /** 
  151. * Fires if the new cover image was successfully uploaded. 
  152. * The dynamic portion of the hook will be xprofile in case of a user's 
  153. * cover image, groups in case of a group's cover image. For instance: 
  154. * Use add_action( 'xprofile_cover_image_uploaded' ) to run your specific 
  155. * code once the user has set his cover image. 
  156. * @since 2.4.0 
  157. * @param int $item_id Inform about the item id the cover image was set for. 
  158. */ 
  159.  
  160. // Finally return the cover image url to the UI. 
  161. bp_attachments_json_response( true, $is_html4, array( 
  162. 'name' => $name,  
  163. 'url' => $cover_url,  
  164. 'feedback_code' => $feedback_code,  
  165. ) );