Advertisement
Guest User

BP Profile Header

a guest
Jul 8th, 2011
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: BP Custom Header for User Profile
  4. * Version:1.0.1
  5. * Author: Brajesh Singh
  6. * Author URI: http://buddydev.com/members/sbrajesh/
  7. * Plugin URI: http://buddydev.com/plugins/bp-custom-header-for-user-profile/
  8. * License: GPL
  9. *
  10. * Description: Allows Users to upload custom header for their profile pages
  11. */
  12.  
  13. //let us use the class to aboid name space collision with ever growing bp plugins
  14.  
  15. class BPProfileHChanger{
  16.  
  17. //php4 constructor
  18. function BPProfileBGChanger(){
  19. $this->__construct();
  20. }
  21. //php5 constructor
  22. function __construct() {
  23.  
  24. //load textdomain
  25. add_action ( 'bp_loaded', array(&$this,'load_textdomain'), 2 );
  26. //setup nav
  27. add_action( 'xprofile_setup_nav',array(&$this,'setup_nav' ));
  28.  
  29. //inject custom css class to body
  30. add_filter("body_class",array(&$this,'get_body_class'),30);
  31. //add css for background change
  32. add_action("wp_head",array(&$this,'inject_css'));
  33.  
  34. }
  35.  
  36. //translation
  37. function load_textdomain(){
  38.  
  39. $locale = apply_filters( 'bp_custom_header_for_profile_load_textdomain_get_locale', get_locale() );
  40.  
  41. // if load .mo file
  42. if ( !empty( $locale ) ) {
  43. $mofile_default = sprintf( '%slanguages/%s.mo', plugin_dir_path(__FILE__), $locale );
  44.  
  45. $mofile = apply_filters( 'bp_custom_header_for_profile_load_textdomain_mofile', $mofile_default );
  46.  
  47. if ( file_exists( $mofile ) ) {
  48. // make sure file exists, and load it
  49. load_textdomain( "bpph", $mofile );
  50. }
  51. }
  52.  
  53. }
  54. //adda sub nav to My profile for chaging Background
  55. function setup_nav(){
  56. global $bp;
  57. $profile_link = $bp->loggedin_user->domain . $bp->profile->slug . '/';
  58. bp_core_new_subnav_item( array( 'name' => __( 'Change Header', 'bpph' ), 'slug' => 'change-header', 'parent_url' => $profile_link, 'parent_slug' => $bp->profile->slug, 'screen_function' =>array(&$this,'screen_change_header'), 'position' => 40 ) );
  59.  
  60. }
  61.  
  62. //screen function
  63.  
  64.  
  65. function screen_change_header(){
  66. global $bp;
  67. //if the form was submitted, update here
  68. if(!empty($_POST['bpprofbg_save_submit'])){
  69. if(!wp_verify_nonce($_POST['_wpnonce'],"bp_upload_profile_header"))
  70. die(__('Security check failed','bppbh'));
  71. //handle the upload
  72. if( $this->handle_upload())
  73. bp_core_add_message(__("Header uploaded successfully!","bpph"));
  74.  
  75. if($_POST['bpph_keep_bh']=="no"){
  76. //delete the associated image and send a message
  77. $old_file_path=get_user_meta(bp_loggedin_user_id(), "profile_header_file_path",true);
  78. if($old_file_path)
  79. @unlink ($old_file_path);//remove old files with each new upload
  80. delete_user_meta(bp_loggedin_user_id(),"profile_header_file_path");
  81. delete_user_meta(bp_loggedin_user_id(),"profile_header");
  82. bp_core_add_message(__("Header deleted successfully!","bpph"));
  83. }
  84. }
  85.  
  86. //hook the content
  87. add_action( 'bp_template_title', array(&$this,'page_title' ));
  88. add_action( 'bp_template_content',array(&$this, 'page_content') );
  89. bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
  90. }
  91. //Change Background Page title
  92. function page_title(){
  93. echo "<h3>".__("Profile Header","bppbh")."</h3>";
  94. }
  95. //Upload page content
  96. function page_content(){
  97.  
  98. ?>
  99. <form name="bpprofbph_change" method="post" class="standard-form" enctype="multipart/form-data">
  100. <label for="bpph_keep_header">
  101. <input type="radio" name="bpph_keep_header" id="bpph_keep_header" checked="checked" value="yes"><?php _e("Keep Header","bpph");?>
  102.  
  103.  
  104. </label>
  105. <label for="bpph_delete_bh">
  106. <input type="radio" name="bpph_keep_header" id="bpph_delete_header" value="no"><?php _e("Delete Header","bpph");?>
  107. </label>
  108.  
  109. <p><?php _e("If you want to change your profile header, please upload a new image.","bpph");?></p>
  110. <label for="bprpgbh_upload">
  111. <input type="file" name="file" id="bprpgbh_upload" class="settings-input" />
  112. </label>
  113.  
  114. <?php wp_nonce_field("bp_upload_profile_header");?>
  115. <input type="hidden" name="action" id="action" value="bp_upload_profile_header" />
  116. <p class="submit"><input type="submit" id="bpprofbh_save_submit" name="bpprofbh_save_submit" class="button" value="<?php _e('Save','bppbh') ?>" /></p>
  117. </form>
  118. <?php
  119. }
  120.  
  121. //handles upload, a modified version of bp_core_avatar_handle_upload(from bp-core/bp-core-avatars.php)
  122. function handle_upload( ) {
  123. global $bp;
  124.  
  125. //include core files
  126. require_once( ABSPATH . '/wp-admin/includes/file.php' );
  127. $max_upload_size=$this->get_max_upload_size();
  128. $max_upload_size=$max_upload_size*1024;//convert kb to bytes
  129. $file=$_FILES;
  130.  
  131. //I am not changing the domain of erro messages as these are same as bp, so you should have a translation for this
  132. $uploadErrors = array(
  133. 0 => __("There is no error, the file uploaded with success", 'buddypress'),
  134. 1 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format($max_upload_size),
  135. 2 => __("Your image was bigger than the maximum allowed file size of: ", 'buddypress') . size_format($max_upload_size),
  136. 3 => __("The uploaded file was only partially uploaded", 'buddypress'),
  137. 4 => __("No file was uploaded", 'buddypress'),
  138. 6 => __("Missing a temporary folder", 'buddypress')
  139. );
  140.  
  141. if ( $file['error'] ) {
  142. bp_core_add_message( sprintf( __( 'Your upload failed, please try again. Error was: %s', 'buddypress' ), $uploadErrors[$file['file']['error']] ), 'error' );
  143. return false;
  144. }
  145.  
  146. if ( ! ($file['file']['size']<$max_upload_size) ) {
  147. bp_core_add_message( sprintf( __( 'The file you uploaded is too big. Please upload a file under %s', 'buddypress'), size_format($max_upload_size) ), 'error' );
  148. return false;
  149. }
  150.  
  151. if ( ( !empty( $file['file']['type'] ) && !preg_match('/(jpe?g|gif|png)$/i', $file['file']['type'] ) ) || !preg_match( '/(jpe?g|gif|png)$/i', $file['file']['name'] ) )
  152. {
  153. bp_core_add_message( __( 'Please upload only JPG, GIF or PNG photos.', 'buddypress' ), 'error' );
  154. return false;
  155. }
  156.  
  157.  
  158. $uploaded_file = wp_handle_upload( $file['file'], array( 'action'=> 'bp_upload_profile_bg' ) );
  159.  
  160. //if file was not uploaded correctly
  161. if ( !empty($uploaded_file['error'] ) ) {
  162. bp_core_add_message( sprintf( __( 'Upload Failed! Error was: %s', 'buddypress' ), $uploaded_file['error'] ), 'error' );
  163. return false;
  164. }
  165.  
  166. //assume that the file uploaded succesfully
  167. //delete any previous uploaded image
  168. $old_file_path=get_user_meta(bp_loggedin_user_id(), "profile_header_file_path",true);
  169. if($old_file_path)
  170. @unlink ($old_file_path);//remove old files with each new upload
  171.  
  172. //save in usermeta
  173. update_user_meta(bp_loggedin_user_id(),"profile_header",$uploaded_file['url']);
  174. update_user_meta(bp_loggedin_user_id(),"profile_header_file_path",$uploaded_file['file']);
  175.  
  176. return true;
  177. }
  178.  
  179. //get the allowed upload size
  180. //there is no setting on single wp, on multisite, there is a setting, we will adhere to both
  181. function get_max_upload_size(){
  182. $max_file_sizein_kb=get_site_option("fileupload_maxk");//it wil be empty for standard wordpress
  183.  
  184.  
  185. if(empty($max_file_sizein_kb)){//check for the server limit since we are on single wp
  186.  
  187. $max_upload_size = (int)(ini_get('upload_max_filesize'));
  188. $max_post_size = (int)(ini_get('post_max_size'));
  189. $memory_limit = (int)(ini_get('memory_limit'));
  190. $max_file_sizein_mb= min($max_upload_size, $max_post_size, $memory_limit);
  191. $max_file_sizein_kb=$max_file_sizein_mb*1024;//convert mb to kb
  192. }
  193. return apply_filters('bpph_max_upload_size',$max_file_sizein_kb);
  194.  
  195.  
  196.  
  197. }
  198. //inject css
  199. function inject_css(){
  200. $image_url= bpph_get_image();
  201. if(empty($image_url)|| apply_filters("bpph_iwilldo_it_myself",false))
  202. return;
  203. ?>
  204. <style type="text/css">
  205. body.is-user-profile{
  206. background:url(<?php echo $image_url;?>);
  207. }
  208. </style>
  209. <?php
  210.  
  211. }
  212.  
  213. //inject custom class for profile pages
  214.  
  215. function get_body_class($classes){
  216. if(!bp_is_member ())
  217. return $classes;
  218. else
  219. $classes[]="is-user-profile";
  220.  
  221. return $classes;
  222.  
  223.  
  224. }
  225.  
  226. }
  227.  
  228. /*public function for your use*/
  229. /**
  230. *
  231. * @global type $bp
  232. * @param type $user_id
  233. * @return string url of the image associated with current user or false
  234. */
  235.  
  236. function bpph_get_image($user_id=false){
  237. global $bp;
  238. if(!$user_id&&$bp->displayed_user->id)
  239. $user_id=$bp->displayed_user->id;
  240.  
  241. if(empty($user_id))
  242. return false;
  243. $image_url=get_user_meta($user_id, "profile_header", true);
  244. return apply_filters("bpph_get_image",$image_url,$user_id);
  245. }
  246.  
  247. $_profbh=new BPProfileHChanger();
  248.  
  249. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement