/pp-local-avatars.php

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