bp_cover_handle_upload

The BuddyPress cover bp cover handle upload function.

Description

bp_cover_handle_upload(); 

Usage

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

Defined (1)

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

/bp-cover.php  
  1. function bp_cover_handle_upload() { 
  2. global $bp, $wpdb;  
  3. if(!$user_id&&$bp->displayed_user->id) 
  4. $user_id=$bp->displayed_user->id;  
  5. if(empty($user_id)) 
  6. return false; 
  7. $activity_table = $wpdb->prefix."bp_activity"; 
  8. $activity_meta_table = $wpdb->prefix."bp_activity_meta";  
  9. $sql = "SELECT COUNT(*) as photo_count FROM $activity_table a INNER JOIN $activity_meta_table am ON a.id = am.activity_id WHERE a.user_id = %d AND meta_key = 'bp_cover_activity'"; 
  10. $sql = $wpdb->prepare( $sql, $user_id ); 
  11. $cnt = $wpdb->get_var( $sql ); 
  12. $max_cnt =bp_cover_get_max_total(); 
  13. if( $cnt < $max_cnt ) {  
  14. if( $_POST['encodedimg'] ) {  
  15. $file = $_POST['imgsize'] ; 
  16. $max_upload_size=bp_cover_get_max_media_size();  
  17. if( $max_upload_size > $file) {  
  18. $imgresponse = array(); 
  19. $uploaddir =wp_upload_dir();  
  20. /** let's decode the base64 encoded image sent */ 
  21. $img = $_POST['encodedimg']; 
  22. $img = str_replace('data:'.$_POST['imgtype'].';base64, ', '', $img); 
  23. $img = str_replace(' ', '+', $img); 
  24. $data = base64_decode($img); 
  25.  
  26. $imgname = wp_unique_filename( $uploaddir['path'], $_POST['imgname'] );  
  27. $filepath = $uploaddir['path'] . '/' . $imgname; 
  28. $fileurl = $uploaddir['url'] . '/' . $imgname; 
  29.  
  30. /** now we write the image in dir */ 
  31.  
  32. $success = file_put_contents($filepath, $data); 
  33.  
  34. if($success) { 
  35. $imgresponse[0] = "1"; 
  36. $imgresponse[1] = $fileurl;  
  37.  
  38. update_user_meta(bp_loggedin_user_id(), 'bp_cover', $fileurl);  
  39. delete_user_meta(bp_loggedin_user_id(), 'bp_cover_position');  
  40.  
  41. do_action('bp_cover_uploaded', $fileurl); 
  42.  
  43. } else { 
  44. $imgresponse[0] = "0"; 
  45. $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover'); 
  46.  
  47. }  
  48. } else { 
  49. $imgresponse[0] = "0"; 
  50. $imgresponse[1] = sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size) ); 
  51.  
  52. }  
  53.  
  54. }else { 
  55. $imgresponse[0] = "0"; 
  56. $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover'); 
  57.  
  58.  
  59. }else { 
  60. $imgresponse[0] = "0"; 
  61. $imgresponse[1] = sprintf( __('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt); 
  62.  
  63. /** if everything is ok, we send back url to thumbnail and to full image */ 
  64. echo json_encode( $imgresponse ); 
  65. die();