PP_Local_Avatars

The BP Local Avatars PP Local Avatars class.

Defined (1)

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

/pp-local-avatars.php  
  1. class PP_Local_Avatars { 
  2.  
  3. private $upload_dir; 
  4. private $group_upload_dir; 
  5.  
  6. function __construct() { 
  7.  
  8. $this->upload_dir = bp_core_avatar_upload_path() . '/avatars'; 
  9. $this->group_upload_dir = bp_core_avatar_upload_path() . '/group-avatars'; 
  10.  
  11. add_action( 'wp_login', array( $this, 'login' ), 10, 2 ); 
  12.  
  13. add_action( 'user_register', array( $this, 'register' ) ); 
  14.  
  15. add_filter( 'bp_core_fetch_avatar_no_grav', array( $this, 'no_grav' ) ); 
  16.  
  17.  
  18. function login( $user_login, $user ) { 
  19. $this->create( $user->ID ); 
  20.  
  21. function register( $user_id ) { 
  22. $this->create( $user_id ); 
  23.  
  24. // Creates an identicon if no local avatar exists 
  25. public function create( $user_id ) { 
  26. global $wpdb; 
  27.  
  28. // Bail if an avatar already exists for this user. 
  29. if ( $this->has_avatar( $user_id ) ) 
  30. return; 
  31.  
  32. wp_mkdir_p( $this->upload_dir . '/' . $user_id ); 
  33.  
  34. $user_email = $wpdb->get_var( "SELECT user_email FROM $wpdb->users WHERE ID = $user_id" ); 
  35.  
  36. // thumbnail 
  37. $dim = BP_AVATAR_THUMB_WIDTH; 
  38. $url = $this->gravatar_url( $user_email, $dim, 'identicon', 'g' ); 
  39.  
  40. $path = $this->upload_dir . '/' . $user_id . '/' . $user_id . '-bpthumb.jpg'; 
  41. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  42.  
  43. // full size 
  44. $dim = BP_AVATAR_FULL_WIDTH; 
  45. $url = $this->gravatar_url( $user_email, $dim, 'identicon', 'g' ); 
  46.  
  47. $path = $this->upload_dir . '/' . $user_id . '/' . $user_id . '-bpfull.jpg'; 
  48. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  49.  
  50.  
  51. // Creates a Group identicon if no Group avatar exists 
  52. public function group_create( $group_id ) { 
  53.  
  54. // Bail if an avatar already exists for this group. 
  55. if ( $this->group_has_avatar( $group_id ) ) 
  56. return; 
  57.  
  58. wp_mkdir_p( $this->group_upload_dir . '/' . $group_id ); 
  59.  
  60. $fake_email = uniqid('', true) . '@gmail.com'; 
  61.  
  62. // thumbnail 
  63. $dim = BP_AVATAR_THUMB_WIDTH; 
  64. $url = $this->gravatar_url( $fake_email, $dim, 'identicon', 'g' ); 
  65.  
  66. $path = $this->group_upload_dir . '/' . $group_id . '/' . $group_id . '-bpthumb.jpg'; 
  67. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  68.  
  69. // full size 
  70. $dim = BP_AVATAR_FULL_WIDTH; 
  71. $url = $this->gravatar_url( $fake_email, $dim, 'identicon', 'g' ); 
  72.  
  73. $path = $this->group_upload_dir . '/' . $group_id . '/' . $group_id . '-bpfull.jpg'; 
  74. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  75.  
  76. }  
  77.  
  78.  
  79. /** 
  80. * Generate a Gravatar URL for a specified email address 
  81. * @param string $email The email address 
  82. * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] 
  83. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] 
  84. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] 
  85. * @return String containing a URL 
  86. * @source http://gravatar.com/site/implement/ 
  87. */ 
  88.  
  89. private function gravatar_url( $email, $s = 50, $d = 'identicon', $r = 'g' ) { 
  90.  
  91. $url = 'http://www.gravatar.com/avatar/'; 
  92. $url .= md5( strtolower( trim( $email ) ) ); 
  93. //$url .= ".jpg?s=$s&d=$d&r=$r"; // old structure 
  94. $url .= ".?s=$s&d=$d&r=$r"; 
  95.  
  96. return $url; 
  97.  
  98.  
  99. // Checks if a given user has local avatar dir 
  100. private function has_avatar( $user_id ) { 
  101.  
  102. $dir_path = $this->upload_dir . '/' . $user_id; 
  103.  
  104. if ( ! file_exists( $dir_path ) ) 
  105. return false; 
  106. else 
  107. return true; 
  108.  
  109. // Checks if a given Group has avatar dir 
  110. private function group_has_avatar( $group_id ) { 
  111.  
  112. $dir_path = $this->group_upload_dir . '/' . $group_id; 
  113.  
  114. if ( ! file_exists( $dir_path ) ) 
  115. return false; 
  116. else 
  117. return true; 
  118. }  
  119.  
  120. // Disables Gravatar. 
  121. function no_grav() { 
  122. return true; 
  123.