/pp-local-avatars.php

  1. <?php 
  2.  
  3. // Exit if accessed directly. 
  4. if ( ! defined( 'ABSPATH' ) ) exit; 
  5.  
  6.  
  7. // settings in wp-admin 
  8. function pp_lc_add_settings() { 
  9.  
  10. $show = get_option('show_avatars'); 
  11.  
  12. if( $show ) { 
  13.  
  14. add_filter( 'avatar_defaults', 'pp_lc_add_avatar_default_option', 11, 1 ); 
  15.  
  16. add_filter( 'default_avatar_select', 'pp_lc_add_avatar_default_option_img', 11, 1 ); 
  17.  
  18. $default_avatar = get_option('avatar_default'); 
  19.  
  20. if( $default_avatar == 'identicon_local' ) 
  21. pp_lc_add_settings_section(); 
  22.  
  23.  
  24. add_action('admin_init', 'pp_lc_add_settings'); 
  25.  
  26. // add an option in Settings > Discussion > Avatars 
  27. function pp_lc_add_avatar_default_option( $avatar_defaults ) { 
  28.  
  29. $avatar_defaults['identicon_local'] = __('BuddyPress Identicon (Generated and Stored Locally)'); 
  30.  
  31. return $avatar_defaults; 
  32.  
  33. // add an icon to the option in Settings > Discussion > Avatars 
  34. function pp_lc_add_avatar_default_option_img( $avatar_list ) { 
  35.  
  36. $str_array = array( 'http://0.gravatar.com/avatar/ffd294ab5833ba14aaf175f9acc71cc4?s=64&d=identicon_local&r=g&forcedefault=1 2x', 'http://0.gravatar.com/avatar/ffd294ab5833ba14aaf175f9acc71cc4?s=32&d=identicon_local&r=g&forcedefault=1', 'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=64&d=identicon_local&r=g&forcedefault=1 2x', 'http://1.gravatar.com/avatar/1ea18284b39b7e184779ea1ddc5f4ee2?s=32&d=identicon_local&r=G&forcedefault=1' ); 
  37.  
  38. $icon = plugins_url( 'icon.png', __FILE__ ); 
  39.  
  40. $avatar_list = str_ireplace($str_array, $icon, $avatar_list); 
  41.  
  42. return $avatar_list; 
  43.  
  44.  
  45. // Add Bulk Generation section to Settings > Discussion > Avatars 
  46. function pp_lc_add_settings_section() { 
  47. add_settings_section( 
  48. 'generate_avatars',  
  49. 'Bulk Generate',  
  50. 'pp_lc_generate_avatars_callback',  
  51. 'discussion' 
  52. ); 
  53.  
  54. function pp_lc_generate_avatars_callback( $arg ) { 
  55. ?> 
  56. <table class="form-table"> 
  57. <tr> 
  58. <th scope="row"><?php _e('BuddyPress Avatars'); ?></th> 
  59. <td><fieldset> 
  60. <label for="gen_avatars"> 
  61. Generate Identicons and store them locally for all BP Members and Groups without an Avatar. <br/><br/> 
  62. If you have a large number of members without Avatars, <em>this may take too long</em>. <br/><br/> 
  63.  
  64. <a href="<?php print wp_nonce_url(admin_url('options-discussion.php?task=bulk-generate'), 'bulk_gen', 'pp_nonce');?>">Generate Identicons</a> 
  65.  
  66. </label> 
  67. </fieldset></td> 
  68. </tr> 
  69. </table> 
  70. <?php 
  71.  
  72.  
  73.  
  74. /** 
  75. * create class instance 
  76. * maybe bulk generate avatars 
  77. * uses the bp_core_set_avatar_globals hook via bp_setup_globals 
  78. */ 
  79.  
  80. function pp_lc_load_class() { 
  81. global $wpdb, $bp; 
  82.  
  83. $default = get_option('avatar_default'); 
  84.  
  85. if( $default == 'identicon_local' ) 
  86. $pp_local_avatar_instance = new PP_Local_Avatars(); 
  87.  
  88.  
  89. if( is_admin() ) { 
  90.  
  91. if( isset( $_GET['task'] ) && $_GET['task'] == 'bulk-generate' ) { 
  92.  
  93. if ( ! wp_verify_nonce($_GET['pp_nonce'], 'bulk_gen') ) 
  94. die( 'Security check' ); 
  95.  
  96. else { 
  97.  
  98. $users = get_users( array( 'fields' => 'ID' ) ); 
  99.  
  100. foreach ( $users as $user ) 
  101. $pp_local_avatar_instance->create( $user ); 
  102.  
  103.  
  104. $group_ids = $wpdb->get_col( "SELECT id FROM {$wpdb->prefix}bp_groups" ); 
  105.  
  106. foreach ( $group_ids as $group_id ) 
  107. $pp_local_avatar_instance->group_create( $group_id ); 
  108.  
  109. wp_redirect( admin_url( '/options-discussion.php?avs_gen=1' ) ); 
  110. exit; 
  111.  
  112. add_action( 'bp_core_set_avatar_globals', 'pp_lc_load_class', 100 ); 
  113.  
  114.  
  115. function pp_lc_avatars_admin_notice() { 
  116.  
  117. if ( ! empty( $_GET['avs_gen'] ) ) { 
  118. echo '<div class="updated"><p>Avatars have been generated.</p></div>'; 
  119. add_action('admin_notices', 'pp_lc_avatars_admin_notice'); 
  120.  
  121. class PP_Local_Avatars { 
  122.  
  123. private $upload_dir; 
  124. private $group_upload_dir; 
  125.  
  126. function __construct() { 
  127.  
  128. $this->upload_dir = bp_core_avatar_upload_path() . '/avatars'; 
  129. $this->group_upload_dir = bp_core_avatar_upload_path() . '/group-avatars'; 
  130.  
  131. add_action( 'wp_login', array( $this, 'login' ), 10, 2 ); 
  132.  
  133. add_action( 'user_register', array( $this, 'register' ) ); 
  134.  
  135. add_filter( 'bp_core_fetch_avatar_no_grav', array( $this, 'no_grav' ) ); 
  136.  
  137.  
  138. function login( $user_login, $user ) { 
  139. $this->create( $user->ID ); 
  140.  
  141. function register( $user_id ) { 
  142. $this->create( $user_id ); 
  143.  
  144. // Creates an identicon if no local avatar exists 
  145. public function create( $user_id ) { 
  146. global $wpdb; 
  147.  
  148. // Bail if an avatar already exists for this user. 
  149. if ( $this->has_avatar( $user_id ) ) 
  150. return; 
  151.  
  152. wp_mkdir_p( $this->upload_dir . '/' . $user_id ); 
  153.  
  154. $user_email = $wpdb->get_var( "SELECT user_email FROM $wpdb->users WHERE ID = $user_id" ); 
  155.  
  156. // thumbnail 
  157. $dim = BP_AVATAR_THUMB_WIDTH; 
  158. $url = $this->gravatar_url( $user_email, $dim, 'identicon', 'g' ); 
  159.  
  160. $path = $this->upload_dir . '/' . $user_id . '/' . $user_id . '-bpthumb.jpg'; 
  161. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  162.  
  163. // full size 
  164. $dim = BP_AVATAR_FULL_WIDTH; 
  165. $url = $this->gravatar_url( $user_email, $dim, 'identicon', 'g' ); 
  166.  
  167. $path = $this->upload_dir . '/' . $user_id . '/' . $user_id . '-bpfull.jpg'; 
  168. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  169.  
  170.  
  171. // Creates a Group identicon if no Group avatar exists 
  172. public function group_create( $group_id ) { 
  173.  
  174. // Bail if an avatar already exists for this group. 
  175. if ( $this->group_has_avatar( $group_id ) ) 
  176. return; 
  177.  
  178. wp_mkdir_p( $this->group_upload_dir . '/' . $group_id ); 
  179.  
  180. $fake_email = uniqid('', true) . '@gmail.com'; 
  181.  
  182. // thumbnail 
  183. $dim = BP_AVATAR_THUMB_WIDTH; 
  184. $url = $this->gravatar_url( $fake_email, $dim, 'identicon', 'g' ); 
  185.  
  186. $path = $this->group_upload_dir . '/' . $group_id . '/' . $group_id . '-bpthumb.jpg'; 
  187. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  188.  
  189. // full size 
  190. $dim = BP_AVATAR_FULL_WIDTH; 
  191. $url = $this->gravatar_url( $fake_email, $dim, 'identicon', 'g' ); 
  192.  
  193. $path = $this->group_upload_dir . '/' . $group_id . '/' . $group_id . '-bpfull.jpg'; 
  194. copy($url, $path); //NOTE: requires allow_url_fopen set to true 
  195.  
  196. }  
  197.  
  198.  
  199. /** 
  200. * Generate a Gravatar URL for a specified email address 
  201. * @param string $email The email address 
  202. * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ] 
  203. * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ] 
  204. * @param string $r Maximum rating (inclusive) [ g | pg | r | x ] 
  205. * @return String containing a URL 
  206. * @source http://gravatar.com/site/implement/ 
  207. */ 
  208.  
  209. private function gravatar_url( $email, $s = 50, $d = 'identicon', $r = 'g' ) { 
  210.  
  211. $url = 'http://www.gravatar.com/avatar/'; 
  212. $url .= md5( strtolower( trim( $email ) ) ); 
  213. $url .= ".jpg?s=$s&d=$d&r=$r"; 
  214.  
  215. return $url; 
  216.  
  217.  
  218. // Checks if a given user has local avatar dir 
  219. private function has_avatar( $user_id ) { 
  220.  
  221. $dir_path = $this->upload_dir . '/' . $user_id; 
  222.  
  223. if ( ! file_exists( $dir_path ) ) 
  224. return false; 
  225. else 
  226. return true; 
  227.  
  228. // Checks if a given Group has avatar dir 
  229. private function group_has_avatar( $group_id ) { 
  230.  
  231. $dir_path = $this->group_upload_dir . '/' . $group_id; 
  232.  
  233. if ( ! file_exists( $dir_path ) ) 
  234. return false; 
  235. else 
  236. return true; 
  237. }  
  238.  
  239. // Disables Gravatar. 
  240. function no_grav() { 
  241. return true; 
  242.  
.