bp_cover_group_handle_upload

The BuddyPress cover bp cover group handle upload function.

Description

bp_cover_group_handle_upload( $activity_id ); 

Parameters (1)

0. $activity_id
The activity id.

Usage

  1. if ( !function_exists( 'bp_cover_group_handle_upload' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-cover/bp-cover-group.php'; 
  3.  
  4. // The activity id. 
  5. $activity_id = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_cover_group_handle_upload($activity_id); 
  9.  

Defined (1)

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

/bp-cover-group.php  
  1. function bp_cover_group_handle_upload($activity_id) { 
  2. global $bp, $wpdb; 
  3. $group_id=bp_get_current_group_id(); 
  4. $activity_table = $wpdb->prefix."bp_activity"; 
  5. $activity_meta_table = $wpdb->prefix."bp_activity_meta";  
  6. $sql = "SELECT COUNT(*) as photo_count FROM $activity_table a INNER JOIN $activity_meta_table am ON a.id = am.activity_id WHERE a.item_id = %d AND meta_key = 'all_bp_cover_group'"; 
  7. $sql = $wpdb->prepare( $sql, $group_id ); 
  8. $cnt = $wpdb->get_var( $sql ); 
  9. $max_cnt = bp_cover_get_max_total(); 
  10. if( $cnt < $max_cnt ) {  
  11. if( $_POST['encodedimg'] ) {  
  12. $file = $_POST['imgsize'] ; 
  13. $max_upload_size=bp_cover_get_max_media_size();  
  14. if( $max_upload_size > $file) {  
  15. $group_id=$bp->groups->current_group->id;  
  16. $imgresponse = array(); 
  17. $uploaddir =wp_upload_dir();  
  18. /** let's decode the base64 encoded image sent */ 
  19. $img = $_POST['encodedimg']; 
  20. $img = str_replace('data:'.$_POST['imgtype'].';base64, ', '', $img); 
  21. $img = str_replace(' ', '+', $img); 
  22. $data = base64_decode($img); 
  23.  
  24. $imgname = wp_unique_filename( $uploaddir['path'], $_POST['imgname'] );  
  25. $filepath = $uploaddir['path'] . '/' . $imgname; 
  26. $fileurl = $uploaddir['url'] . '/' . $imgname; 
  27.  
  28. /** now we write the image in dir */ 
  29.  
  30. $success = file_put_contents($filepath, $data); 
  31.  
  32. if($success) { 
  33. $imgresponse[0] = "1"; 
  34. $imgresponse[1] = $fileurl;  
  35. $size = @getimagesize( $filepath ); 
  36. if ( $size[0] > 100 ) { 
  37. $thumb = image_resize( $filepath, 100, 100, true ); 
  38.  
  39. // Check for thumbnail creation errors  
  40. if ( is_wp_error( $thumb ) ) { 
  41. $imgresponse[0] = "0"; 
  42. $imgresponse[1] = sprintf( __( 'Upload Failed! Error was: %s', 'bp-cover' ), $thumb->get_error_message() ); 
  43. die(); 
  44.  
  45. // Thumbnail is good so proceed  
  46. $array_filepath = explode('/', $thumb); 
  47. $image_resized = $array_filepath[count($array_filepath)-1]; 
  48.  
  49. if ( !empty( $image_resized ) ) { 
  50. $imgresponse[2] = $uploaddir['url'] . '/'. $image_resized; 
  51. $attachment = array( 
  52. 'post_mime_type' => $_POST['imgtype'],  
  53. 'guid' => $fileurl,  
  54. 'post_title' => $imgname,  
  55. );  
  56. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  57. $attachment_id = wp_insert_attachment($attachment, $imgname);  
  58. groups_update_groupmeta($group_id, 'bp_cover_group', $fileurl);  
  59. groups_update_groupmeta($group_id, 'bp_cover_group_position', 0);  
  60. $group = groups_get_group ( array ( "group_id" => $group_id ) ); 
  61. $activity_id = groups_record_activity( array( 
  62. 'action' => sprintf( __( '%s uploaded a new cover picture to the group %s', 'bp-cover' ),  
  63. bp_core_get_userlink( $bp->loggedin_user->id ),  
  64. '<a href="' . bp_get_group_permalink( $group ) . '">' . esc_attr( $group->name ) . '</a>') ,  
  65. 'type' => 'cover_added',  
  66. 'item_id' => $group_id,  
  67. 'content' =>bp_cover_group_get_image_scr() ,  
  68. 'item_id' => $group_id,  
  69. ) ); 
  70.  
  71. bp_activity_update_meta( $activity_id, 'all_bp_cover_group', $attachment_id ); 
  72. update_post_meta($attachment_id, 'bp_cover_group_thumb', $imgresponse[2]);  
  73. } else { 
  74. $imgresponse[0] = "0"; 
  75. $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover'); 
  76.  
  77. } else { 
  78. $imgresponse[0] = "0"; 
  79. $imgresponse[1] = sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size) ); 
  80.  
  81. }  
  82. }else { 
  83. $imgresponse[0] = "0"; 
  84. $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover'); 
  85.  
  86. }else { 
  87. $imgresponse[0] = "0"; 
  88. $imgresponse[1] = sprintf( __('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt); 
  89.  
  90. /** if everything is ok, we send back url to thumbnail and to full image */ 
  91. echo json_encode( $imgresponse ); 
  92. die();