/inc/functions.php

  1. <?php 
  2. /** 
  3. * Function definitions 
  4. * 
  5. * @package BuddyPress Identicons 
  6. * @subpackage Functions 
  7. */ 
  8.  
  9. /** 
  10. * Load the plugin textdomain. 
  11. * 
  12. * @since 1.1.0 
  13. */ 
  14. function identicons_i18n() { 
  15. load_plugin_textdomain( 'buddypress-identicons' ); 
  16.  
  17. /** 
  18. * Register the admin settings. 
  19. * 
  20. * @since 1.1.0 
  21. */ 
  22. function identicons_register_admin_settings() { 
  23.  
  24. add_settings_section( 
  25. 'identicons',  
  26. __( 'Identicons', 'buddypress-identicons' ),  
  27. 'identicons_settings_section_callback',  
  28. 'buddypress' 
  29. ); 
  30. add_settings_field( 
  31. 'identicons-background',  
  32. __( 'Background', 'buddypress-identicons' ),  
  33. 'identicons_settings_field_callback_background',  
  34. 'buddypress',  
  35. 'identicons' 
  36. ); 
  37. register_setting( 
  38. 'buddypress',  
  39. 'identicons-background',  
  40. 'intval' 
  41. ); 
  42.  
  43. /** 
  44. * Fill the section with content. 
  45. * 
  46. * @since 1.1.0 
  47. */ 
  48. function identicons_settings_section_callback() {} 
  49.  
  50. /** 
  51. * Add an input to the field. 
  52. * 
  53. * @since 1.1.0 
  54. */ 
  55. function identicons_settings_field_callback_background() { 
  56.  
  57. $value = get_blog_option( get_current_blog_id(), 'identicons-background' ); 
  58. ?> 
  59. <input type="checkbox" name="identicons-background" id="identicons-background" value="1" <?php checked( $value ); ?> /> 
  60. <label for="identicons-background"><?php _e( 'Set a transparent background', 'buddypress-identicons' ); ?></label> 
  61. <?php 
  62.  
  63. /** 
  64. * Add to the default avatar list. 
  65. * 
  66. * @since 1.2.0 
  67. * 
  68. * @param string $list HTML markup of the default avatar list. 
  69. * @return string 
  70. */ 
  71. function identicons_filter_default_avatar_list( $list ) { 
  72.  
  73. $option = get_blog_option( get_current_blog_id(), 'avatar_default' ); 
  74.  
  75. $path = plugins_url( 'images/pixicon.png', __DIR__ ); 
  76.  
  77. $defaults['pixicon'] = __( 'Pixicon', 'buddypress-identicons' ); 
  78.  
  79. foreach ( $defaults as $k => $v ) { 
  80. $list .= '<label>'; 
  81. $list .= '<input type="radio" name="avatar_default" id="avatar_' . $k . '" value="' . esc_attr( $k ) . '" ' . checked( $option, $k, false ) . '/>'; 
  82. $list .= ' '; 
  83. $list .= '<img src="' . esc_url( $path ) . '" class="avatar avatar-32" alt="' . esc_attr( $v ) . '" height="32" width="32" />'; 
  84. $list .= ' '; 
  85. $list .= $v; 
  86. $list .= '</label>'; 
  87. $list .= '<br />'; 
  88. return $list; 
  89.  
  90. /** 
  91. * Check if a Gravatar image exists for a given email hash. 
  92. * 
  93. * @since 1.2.0 
  94. * 
  95. * @param string $email The user's email address. 
  96. * @return bool True if a Gravatar image exists, else false. 
  97. */ 
  98. function identicons_grav_exists( $email ) { 
  99.  
  100. $hash = md5( strtolower( trim( $email ) ) ); 
  101.  
  102. $response = wp_remote_head( 'http://www.gravatar.com/avatar/' . $hash . '?d=404' ); 
  103.  
  104. if ( $response['response']['code'] === 200 ) { 
  105. return true; 
  106. } else { 
  107. return false; 
  108.  
  109. /** 
  110. * Determine whether to skip Gravatar. 
  111. * 
  112. * @since 1.2.0 
  113. * 
  114. * @param bool $no_grav True to disable Gravatar, false to not. 
  115. * @param array $params The parameters for the request. 
  116. * @return bool 
  117. */ 
  118. function identicons_filter_fetch_avatar_no_grav( $no_grav, $params ) { 
  119.  
  120. // Bail if identicons aren't in use. 
  121. if ( ! identicons_usage_check() ) { 
  122. return $no_grav; 
  123.  
  124. // Bail if not a user. 
  125. if ( $params['object'] !== 'user' ) { 
  126. return $no_grav; 
  127.  
  128. if ( empty( $params['email'] ) ) { 
  129. $params['email'] = bp_core_get_user_email( $params['item_id'] ); 
  130.  
  131. if ( identicons_grav_exists( $params['email'] ) ) { 
  132. return false; 
  133. } else { 
  134. return true; 
  135.  
  136. /** 
  137. * Determine if get_avatar should be skipped. 
  138. * 
  139. * @since 1.2.0 
  140. * 
  141. * @param string $avatar The avatar HTML. 
  142. * @param mixed $id_or_email The ID or email. 
  143. * @param array $args The avatar's arguments. 
  144. * @return null|string 
  145. */ 
  146. function identicons_pre_get_avatar( $avatar, $id_or_email, $args ) { 
  147.  
  148. global $pagenow; 
  149.  
  150. if ( $pagenow === 'options-discussion.php' || ! identicons_usage_check() ) { 
  151. return $avatar; 
  152. } else { 
  153. $url = plugins_url( 'images/pixicon.png', dirname( __FILE__ ) ); 
  154. /** 
  155. * Filters the avatar URL. 
  156. * 
  157. * @since 2.0.1 
  158. * 
  159. * @param string The avatar URL. 
  160. */ 
  161. $url = apply_filters( 'identicons_get_avatar_url', $url ); 
  162.  
  163. return '<img alt="' . $args['alt'] . '" src="' . $url . '" class="' . $args['class'] . '" height="' . $args['height'] . '" width="' . $args['width'] . '">'; 
  164.  
  165. /** 
  166. * Filter the default avatar URL. 
  167. * 
  168. * @since 1.1.0 
  169. * 
  170. * @param string $avatar_default The URL of the default avatar. 
  171. * @param array $params See {@see bp_core_fetch_avatar()}. 
  172. * @return string 
  173. */ 
  174. function identicons_default_avatar_url( $avatar_default, $params ) { 
  175.  
  176. // Bail if identicons aren't in use. 
  177. if ( ! identicons_usage_check() ) { 
  178. return $avatar_default; 
  179.  
  180. $identicon = identicons_factory( $params['item_id'] ); 
  181.  
  182. $identicon->create(); 
  183.  
  184. return $identicon->read(); 
  185.  
  186. /** 
  187. * Delete a user's identicon if they change their email. 
  188. * 
  189. * @since 1.2.0 
  190. * 
  191. * @param int $user_id The ID of the person updating their profile. 
  192. * @param object $old_data The user's data prior to updating. 
  193. */ 
  194. function identicons_email_change( $user_id, $old_data ) { 
  195.  
  196. $user = new WP_User( $user_id ); 
  197.  
  198. if ( $user->user_email === $old_data->user_email ) { 
  199. // No change. 
  200. return; 
  201. delete( $user_id ); 
  202.  
  203. /** 
  204. * Delete an identicon. 
  205. * 
  206. * @since 1.1.0 
  207. * 
  208. * @param int $user_id The ID of the identicon owner. 
  209. */ 
  210. function identicons_delete( $user_id ) { 
  211.  
  212. $identicon = identicons_factory( $user_id ); 
  213.  
  214. $identicon->delete(); 
  215.  
  216. /** 
  217. * Check if identicons are in use. 
  218. * 
  219. * @since 1.1.0 
  220. * 
  221. * @return bool True if identicons are being used, else false. 
  222. */ 
  223. function identicons_usage_check() { 
  224.  
  225. $avatar_default = get_blog_option( get_current_blog_id(), 'avatar_default' ); 
  226.  
  227. switch( $avatar_default ) { 
  228.  
  229. case plugins_url( 'images/pixicon.png', __DIR__ ): 
  230. case 'pixicon': 
  231. return true; 
  232.  
  233. default: 
  234. return false; 
  235.  
  236. /** 
  237. * Instantiate a new object based on the value of avatar_default. 
  238. * 
  239. * @since 1.1.4 
  240. * 
  241. * @param int $user_id The ID of the identicon owner. 
  242. * @return object|bool 
  243. */ 
  244. function identicons_factory( $user_id ) { 
  245.  
  246. switch( get_blog_option( get_current_blog_id(), 'avatar_default' ) ) { 
  247.  
  248. case plugins_url( 'images/pixicon.png', __DIR__ ): 
  249. case 'pixicon': 
  250. return new Pixicon( $user_id ); 
  251. return false; 
.