/bp-cover.php

  1. <?php 
  2. /** 
  3. Plugin Name: BuddyPress cover 
  4. Plugin URI: http://webcaffe.ir 
  5. Description: Adds cover image in profile page and groups buddypress. 
  6. Version: 2.0.0 
  7. Author: asghar hatampoor 
  8. Author URI: http://webcaffe.ir 
  9.   
  10.   
  11. */ 
  12. if ( !defined( 'ABSPATH' ) ) exit; 
  13.  
  14. if ( !defined( 'BPCO_PLUGIN_VERSION' ) ) 
  15. define( 'BPCO_PLUGIN_VERSION', '2.0.0' ); 
  16.  
  17. if ( !defined( 'BPCO_PLUGIN_DIRNAME' ) ) 
  18. define( 'BPCO_PLUGIN_DIRNAME', basename( dirname( __FILE__ ) ) ); 
  19.  
  20.  
  21. if ( !defined( 'BPCO_PLUGIN_DIR' ) ) 
  22. define( 'BPCO_PLUGIN_DIR', trailingslashit( WP_PLUGIN_DIR . '/' . BPCO_PLUGIN_DIRNAME ) ); 
  23.  
  24. if ( !defined( 'BPCO_PLUGIN_URL' ) ) { 
  25. $plugin_url = trailingslashit( plugins_url( BPCO_PLUGIN_DIRNAME ) ); 
  26. define( 'BPCO_PLUGIN_URL', $plugin_url ); 
  27. if ( !defined('BP_COVER_TEMPLATES_DIR') ) { 
  28. define( 'BP_COVER_TEMPLATES_DIR', BPCO_PLUGIN_DIR . 'templates/'); 
  29. if ( !defined('BP_COVER_TEMPLATES_URL') ) { 
  30. define( 'BP_COVER_TEMPLATES_URL', BPCO_PLUGIN_URL . 'templates/'); 
  31.  
  32. require_once ( BPCO_PLUGIN_DIR . 'bp-cover-group.php' ); 
  33. require_once ( BPCO_PLUGIN_DIR . 'bp-cover-admin.php' );  
  34.  
  35. function bp_cover_load_textdomain() { 
  36. load_plugin_textdomain('bp-cover', false, dirname(plugin_basename(__FILE__)) . "/languages/"); 
  37. add_action('init', 'bp_cover_load_textdomain'); 
  38.  
  39. function bp_cover_scripts() {  
  40. wp_enqueue_script( 'jquery-ui-js', BPCO_PLUGIN_URL . 'js/jquery-ui.min.js'); 
  41. wp_enqueue_style( 'bp-cover-css', BPCO_PLUGIN_URL . 'css/bp-cover.css'); 
  42. add_action( 'wp_head', 'bp_cover_scripts' ); 
  43.  
  44. function bp_cover_enqueue_scripts() {  
  45. wp_enqueue_script( 'bp-cover-js', BPCO_PLUGIN_URL . 'js/bp-cover.js', $dep = array(), $version = BPCO_PLUGIN_VERSION );  
  46.  
  47. add_action( 'bp_after_member_home_content', 'bp_cover_enqueue_scripts' ); 
  48.  
  49. function bp_cover_load_template () { 
  50. global $bp; 
  51. $theme=get_option( 'bp_cover_skin' ); 
  52. if (!empty($theme)) { 
  53. require_once ( BPCO_PLUGIN_DIR .'templates/'. $theme . '/theme.php' ); 
  54. }else{ 
  55. require_once ( BPCO_PLUGIN_DIR .'templates/default/theme.php' ); 
  56. }  
  57. add_action('bp_located_template', 'bp_cover_load_template'); 
  58.  
  59. function bp_cover_avatar_box($args = '') { 
  60. global $bp; 
  61. return bp_core_fetch_avatar( $args );  
  62. extract( $args );  
  63.  
  64. function select_pic_for_cover($activity_id ) { 
  65. global $bp;  
  66. $user_id=bp_loggedin_user_id();  
  67. $activity_id = $_POST['activity_id'];  
  68. $attachment_id = bp_activity_get_meta( $activity_id, 'bp_cover_activity', true ); 
  69. $fileurl = wp_get_attachment_image_src( $attachment_id ); 
  70. update_user_meta($user_id, 'bp_cover', $fileurl[0]);  
  71. delete_user_meta($user_id, 'bp_cover_position');  
  72.  
  73. add_action('wp_ajax_select_pic_for_cover', 'select_pic_for_cover'); 
  74.  
  75. function delete_pic_cover($activity_id ) { 
  76. global $bp;  
  77. $user_id=bp_loggedin_user_id();  
  78. $activity_id = $_POST['activity_id'];  
  79. $attachment_id = bp_activity_get_meta( $activity_id, 'bp_cover_activity', true );  
  80. wp_delete_attachment( $attachment_id, true); 
  81. delete_user_meta($user_id, 'bp_cover');  
  82. delete_user_meta($user_id, 'bp_cover_position');  
  83. bp_activity_delete( array( 'id' => $activity_id, 'user_id' => $bp->loggedin_user->id ) ); 
  84. BP_Activity_Activity::delete_activity_meta_entries( $activity_id ); 
  85. add_action('wp_ajax_delete_pic_cover', 'delete_pic_cover'); 
  86.  
  87. function bp_cover_position( ) { 
  88. $user_id=bp_loggedin_user_id();  
  89. $id = ( isset( $_POST['id'] ) ) ? $_POST['id'] : ''; 
  90. update_user_meta( $user_id, 'bp_cover_position', $id ); 
  91. if( empty( $id ) ) 
  92. return; 
  93. echo $id; 
  94. add_action('wp_ajax_bp_cover_position', 'bp_cover_position');  
  95. add_action('wp_ajax_nopriv_bp_cover_position', 'bp_cover_position'); 
  96.  
  97.  
  98. function core_get_user_displayname_box( $user_id ) {  
  99. return bp_core_get_user_displayname( $user_id ); 
  100.  
  101. $user_info = get_userdata( $user_id ); 
  102. return $user_info->user_nicename; 
  103. function core_get_user_id_box( $user_id ) {  
  104. $user_info = get_userdata( $user_id ); 
  105. return $user_info->ID; 
  106. function core_get_user_email_box( $user_id ) {  
  107. $user_info = get_userdata( $user_id ); 
  108. return $user_info->user_email; 
  109. }  
  110. function core_get_user_description_box( $user_id ) { 
  111. $user_description = get_userdata( $user_id ); 
  112. return $user_description->description; 
  113. function core_get_user_description_limit( $user_id ) { 
  114. $user_bio = core_get_user_description_box( $user_id ); 
  115. $biography = wp_trim_words( $user_bio, 15, '...' ); 
  116. return $biography; 
  117. function bp_cover_delete($activity_id) { 
  118. global $bp; 
  119. delete_user_meta(bp_loggedin_user_id(), 'bp_cover');  
  120. delete_user_meta(bp_loggedin_user_id(), 'bp_cover_position');  
  121. die(); 
  122. add_action('wp_ajax_bp_cover_delete', 'bp_cover_delete');  
  123.  
  124. function bp_cover_refresh() { 
  125. global $bp; 
  126. $user_id=$bp->displayed_user->id;  
  127. if(empty($user_id)) 
  128. return false;  
  129. $image=get_user_meta($user_id, 'bp_cover', true);  
  130. $filter = " <div class='image-upload-container'> 
  131. <img class='img-profile-header-background' id='user-banner-image' 
  132. src='$image'width='100%' style='width: 100%;'> </div>";  
  133. echo $filter;  
  134. die(); 
  135. }  
  136. add_action('wp_ajax_bp_cover_refresh', 'bp_cover_refresh');  
  137.  
  138. function bp_avatar_refresh() { 
  139. global $bp;  
  140. $avatar_url = bp_core_fetch_avatar( array( 'type' => 'full', 'html' => false, 'item_id' => $user_id ) );  
  141. $filter = " <div class='ava'> <img class='img-rounded profile-user-photo' id='user-profile-image' 
  142. src=".$avatar_url."></div>";  
  143. echo $filter;  
  144. die(); 
  145. }  
  146. add_action('wp_ajax_bp_avatar_refresh', 'bp_avatar_refresh');  
  147.  
  148. function bp_cover_handle_upload() { 
  149. global $bp, $wpdb;  
  150. if(!$user_id&&$bp->displayed_user->id) 
  151. $user_id=$bp->displayed_user->id;  
  152. if(empty($user_id)) 
  153. return false; 
  154. $activity_table = $wpdb->prefix."bp_activity"; 
  155. $activity_meta_table = $wpdb->prefix."bp_activity_meta";  
  156. $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'"; 
  157. $sql = $wpdb->prepare( $sql, $user_id ); 
  158. $cnt = $wpdb->get_var( $sql ); 
  159. $max_cnt =bp_cover_get_max_total(); 
  160. if( $cnt < $max_cnt ) {  
  161. if( $_POST['encodedimg'] ) {  
  162. $file = $_POST['imgsize'] ; 
  163. $max_upload_size=bp_cover_get_max_media_size();  
  164. if( $max_upload_size > $file) {  
  165. $imgresponse = array(); 
  166. $uploaddir =wp_upload_dir();  
  167. /** let's decode the base64 encoded image sent */ 
  168. $img = $_POST['encodedimg']; 
  169. $img = str_replace('data:'.$_POST['imgtype'].';base64, ', '', $img); 
  170. $img = str_replace(' ', '+', $img); 
  171. $data = base64_decode($img); 
  172.  
  173. $imgname = wp_unique_filename( $uploaddir['path'], $_POST['imgname'] );  
  174. $filepath = $uploaddir['path'] . '/' . $imgname; 
  175. $fileurl = $uploaddir['url'] . '/' . $imgname; 
  176.  
  177. /** now we write the image in dir */ 
  178.  
  179. $success = file_put_contents($filepath, $data); 
  180.  
  181. if($success) { 
  182. $imgresponse[0] = "1"; 
  183. $imgresponse[1] = $fileurl;  
  184.  
  185. update_user_meta(bp_loggedin_user_id(), 'bp_cover', $fileurl);  
  186. delete_user_meta(bp_loggedin_user_id(), 'bp_cover_position');  
  187.  
  188. do_action('bp_cover_uploaded', $fileurl); 
  189.  
  190. } else { 
  191. $imgresponse[0] = "0"; 
  192. $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover'); 
  193.  
  194. }  
  195. } else { 
  196. $imgresponse[0] = "0"; 
  197. $imgresponse[1] = sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size) ); 
  198.  
  199. }  
  200.  
  201. }else { 
  202. $imgresponse[0] = "0"; 
  203. $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover'); 
  204.  
  205.  
  206. }else { 
  207. $imgresponse[0] = "0"; 
  208. $imgresponse[1] = sprintf( __('Max total images allowed %d in a cover gallery', 'bp-cover'), $max_cnt); 
  209.  
  210. /** if everything is ok, we send back url to thumbnail and to full image */ 
  211. echo json_encode( $imgresponse ); 
  212. die(); 
  213.  
  214. add_action('wp_ajax_bp_cover_handle_upload', 'bp_cover_handle_upload'); 
  215. add_action( 'wp_ajax_nopriv_bp_cover_handle_upload', 'bp_cover_handle_upload' ); 
  216.  
  217. function bp_cover_get_max_total() { 
  218. $total=get_option('bp_cover_profie_item'); 
  219. $size_in_kb=20; 
  220. if(empty ($total)) 
  221. $total=$size_in_kb;  
  222.  
  223. return apply_filters("bp_cover_get_max_total", $total); 
  224.  
  225. function bp_cover_get_max_media_size($converted=true) { 
  226. $size_in_kb=get_option('bp_cover_max_upload_size'); 
  227. if(empty ($size_in_kb)) 
  228. $size_in_kb=2000; 
  229. if(!$converted) 
  230. return apply_filters("bp_cover_get_max_size_kb", $size_in_kb); 
  231. $allowed_size=$size_in_kb*1024; 
  232. return apply_filters("bp_cover_get_max_size_bytes", $allowed_size); 
  233.  
  234.  
  235. function bp_caver_avatar_handle_upload() { 
  236. global $bp; 
  237. if( $_POST['encodedimg'] ) { 
  238. $user_id = !empty( $_POST['user_id'] ) ? $_POST['user_id'] : bp_displayed_user_id() ;  
  239. $imgresponse = array(); 
  240. $uploaddir = bp_core_avatar_upload_path() . '/avatars';  
  241. if( ! file_exists( $uploaddir ) ) {  
  242. mkdir( $uploaddir );  
  243. }  
  244. $img = $_POST['encodedimg']; 
  245. $img = str_replace('data:'.$_POST['imgtype'].';base64, ', '', $img); 
  246. $img = str_replace(' ', '+', $img); 
  247. $data = base64_decode($img);  
  248. $filepath = $uploaddir . '/' . $user_id; 
  249. if( ! file_exists( $filepath ) ) {  
  250. mkdir( $filepath );  
  251. $imgname = wp_unique_filename( $uploaddir, $_POST['imgname'] );  
  252. $fileurl = $filepath . '/' . $imgname; 
  253. $siteurl = trailingslashit( get_blog_option( 1, 'siteurl' ) ); 
  254. $url = str_replace(ABSPATH, $siteurl, $fileurl);  
  255. $success = file_put_contents($fileurl, $data); 
  256. $file = $_POST['imgsize'] ; 
  257. $max_upload_size=bp_cover_get_max_media_size();  
  258. if( $max_upload_size > $file) {  
  259. if($success) { 
  260. $imgresponse[0] = "1"; 
  261. $imgresponse[1] = $fileurl; 
  262.  
  263. $size = getimagesize( $fileurl ); 
  264.  
  265. /** Check image size and shrink if too large */ 
  266. if ( $size[0] > 150 ) { 
  267. $original_file = image_resize( $fileurl, 150, 150, true ); 
  268. //$ava_file = image_resize( $fileurl, 250, 250, true ); 
  269. /** Check for thumbnail creation errors */ 
  270. if ( is_wp_error( $original_file ) ) { 
  271. $imgresponse[0] = "0"; 
  272. $imgresponse[1] = sprintf( __( 'Upload Failed! Error was: %s', 'bp-cover' ), $original_file->get_error_message() ); 
  273. die(); 
  274.  
  275. $avatar_to_crop = str_replace( bp_core_avatar_upload_path(), '', $original_file ); 
  276. bp_core_delete_existing_avatar( array( 'item_id' => $user_id, 'avatar_path' => bp_core_avatar_upload_path() .'/avatars/' . $user_id ) ); 
  277. $crop_args = array( 'item_id' => $user_id, 'original_file' => $avatar_to_crop, 'crop_w' => 0, 'crop_h' => 0);  
  278. bp_core_avatar_handle_crop( $crop_args ) ;  
  279. //$url = str_replace(ABSPATH, $siteurl, $ava_file);  
  280. update_user_meta(bp_loggedin_user_id(), 'profile_avatar', $url); 
  281. do_action( 'xprofile_avatar_uploaded' ); 
  282. } else{  
  283.  
  284. $imgresponse[0] = "0"; 
  285. $imgresponse[1] = __('Upload Failed! Your photo must be larger than 150px', 'bp-cover'); 
  286.  
  287.  
  288. } else { 
  289. $imgresponse[0] = "0"; 
  290. $imgresponse[1] = __('Upload Failed! Unable to write the image on server', 'bp-cover'); 
  291.  
  292. } else { 
  293. $imgresponse[0] = "0"; 
  294. $imgresponse[1] = sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'bp-cover'), size_format($max_upload_size) ); 
  295.  
  296. }  
  297. }else { 
  298. $imgresponse[0] = "0"; 
  299. $imgresponse[1] = __('Upload Failed! No image sent', 'bp-cover'); 
  300.  
  301. /** if everything is ok, we send back url to thumbnail and to full image */ 
  302. echo json_encode( $imgresponse ); 
  303. die(); 
  304.  
  305. add_action('wp_ajax_bp_caver_avatar_handle_upload', 'bp_caver_avatar_handle_upload'); 
  306. add_action( 'wp_ajax_nopriv_bp_caver_avatar_handle_upload', 'bp_caver_avatar_handle_upload' ); 
  307.  
  308. function bp_cover_get_image_scr($user_id=false) { 
  309. global $bp; 
  310. if(!$user_id&&$bp->displayed_user->id) 
  311. $user_id=$bp->displayed_user->id;  
  312. if(empty($user_id)) 
  313. return false; 
  314. $image=get_user_meta($user_id, 'bp_cover', true);  
  315. $filter .= "<img class='bp-cover' src='$image' />";  
  316. return $filter;  
  317.  
  318. function bp_cover_get_avatar_scr($user_id=false) { 
  319. global $bp; 
  320. if(!$user_id&&$bp->displayed_user->id) 
  321. $user_id=$bp->displayed_user->id; 
  322.  
  323. if(empty($user_id)) 
  324. return false; 
  325. $image=get_user_meta($user_id, 'profile_avatar', true);  
  326. $filter .= "<img class='bp-ava' src='$image' />";  
  327. return $filter;  
  328.  
  329. function bp_cover_record_activity() { 
  330. global $bp; 
  331.  
  332. if ( !function_exists( 'bp_activity_add' ) ) 
  333. return false; 
  334. $r = array( 
  335. 'user_id' => $bp->loggedin_user->id,  
  336. 'content' =>bp_cover_get_image_scr() ,  
  337. 'action'=>sprintf( __( '%s uploaded a new cover picture', 'bp-cover' ), bp_core_get_userlink( $bp->loggedin_user->id )),  
  338. 'primary_link' => bp_core_get_userlink( $bp->loggedin_user->id ),  
  339. 'component_name' => $bp->profile->id,  
  340. 'component_action' =>"change_cover",  
  341. 'item_id' => $bp->loggedin_user->id,  
  342. 'secondary_item_id' => false,  
  343. 'recorded_time' => gmdate( "Y-m-d H:i:s" ),  
  344. 'hide_sitewide' => false 
  345. ); 
  346.  
  347. extract( $r, EXTR_SKIP );  
  348. $activity_id = bp_activity_add( $r ); 
  349. $uploaddir =wp_upload_dir();  
  350. $name = $_POST['imgname'];  
  351. $attachment = array(); 
  352. $type = $_POST['imgtype']; 
  353. $filename = $uploaddir['path']. '/' . $name; 
  354. $title = wp_unique_filename( $uploaddir['path'], $_POST['imgname'] );  
  355. $url = $uploaddir['url'] . '/' . $title;  
  356. $attachment = array( 
  357. 'post_mime_type' => $type,  
  358. 'guid' => $url,  
  359. 'post_title' => $title,  
  360. ); 
  361. $size = @getimagesize( $filename ); 
  362.  
  363. /** Check image size and shrink if too large */ 
  364. if ( $size[0] > 100 ) { 
  365. $thumb = image_resize( $filename, 100, 100, true ); 
  366.  
  367. // Check for thumbnail creation errors  
  368. if ( is_wp_error( $thumb ) ) { 
  369. $imgresponse[0] = "0"; 
  370. $imgresponse[1] = sprintf( __( 'Upload Failed! Error was: %s', 'bp-cover' ), $thumb->get_error_message() ); 
  371. die(); 
  372.  
  373. // Thumbnail is good so proceed  
  374. $array_filepath = explode('/', $thumb); 
  375. $image_resized = $array_filepath[count($array_filepath)-1]; 
  376.  
  377. if ( !empty( $image_resized ) ) { 
  378. $imgresponse[2] = $uploaddir['url'] . '/'. $image_resized; 
  379. require_once( ABSPATH . 'wp-admin/includes/image.php' ); 
  380. $attachment_id = wp_insert_attachment($attachment, $title);  
  381. bp_activity_update_meta( $activity_id, 'bp_cover_activity', $attachment_id ); 
  382. update_post_meta($attachment_id, 'bp_cover_thumb', $imgresponse[2]);  
  383. add_action("bp_cover_uploaded", "bp_cover_record_activity"); 
  384.  
  385.  
  386. function xprofile_new_avatar_activity_new() { 
  387. global $bp; 
  388. if ( !function_exists( 'bp_activity_add' ) ) 
  389. return false; 
  390. $user_id = apply_filters( 'xprofile_new_avatar_user_id', $bp->displayed_user->id ); 
  391. $userlink = bp_core_get_userlink( $user_id ); 
  392. bp_activity_add( array( 
  393. 'user_id' => $user_id,  
  394. 'content' =>bp_cover_get_avatar_scr() ,  
  395. 'action' => apply_filters( 'xprofile_new_avatar_action', sprintf( __( '%s uploaded a new profile picture', 'buddypress' ), $userlink ), $user_id ),  
  396. 'component' => 'profile',  
  397. 'type' => 'new_avatar' 
  398. )); 
  399. add_action( 'xprofile_avatar_uploaded', 'xprofile_new_avatar_activity_new' ); 
  400.  
  401. function bn_new_mysteryman () { 
  402. $default_avatar = get_option('bp_cover_avatar'); 
  403. if ( ! empty( $default_avatar ) ) { 
  404. $avatar = $default_avatar; 
  405. }else{ 
  406. $avatar = BPCO_PLUGIN_URL ."/images/default_user.jpg"; 
  407. }  
  408.  
  409. return apply_filters( 'bp_cover_get_default_avatar', $avatar ); 
  410. add_filter('bp_core_mysteryman_src', 'bn_new_mysteryman' ); 
  411. function bp_cover_actions() { 
  412. global $bp;  
  413. remove_action( 'xprofile_avatar_uploaded', 'bp_xprofile_new_avatar_activity' );  
  414.  
  415. add_action( "init", "bp_cover_actions", 5 ); 
.