/modules/gravatar-hovercards.php

  1. <?php 
  2. /** 
  3. * Module Name: Gravatar Hovercards 
  4. * Module Description: Enable pop-up business cards over commenters* Gravatars. 
  5. * Jumpstart Description: Let commenters link their profiles to their Gravatar accounts, making it easy for your visitors to learn more about your community. 
  6. * Sort Order: 11 
  7. * Recommendation Order: 13 
  8. * First Introduced: 1.1 
  9. * Requires Connection: No 
  10. * Auto Activate: Yes 
  11. * Module Tags: Social, Appearance 
  12. * Feature: Jumpstart 
  13. * Additional Search Queries: gravatar, hovercards 
  14. */ 
  15.  
  16. define( 'GROFILES__CACHE_BUSTER', gmdate( 'YM' ) . 'aa' ); // Break CDN cache, increment when gravatar.com/js/gprofiles.js changes 
  17.  
  18. function grofiles_hovercards_init() { 
  19. add_filter( 'get_avatar', 'grofiles_get_avatar', 10, 2 ); 
  20. add_action( 'wp_enqueue_scripts', 'grofiles_attach_cards' ); 
  21. add_action( 'wp_footer', 'grofiles_extra_data' ); 
  22. add_action( 'admin_init', 'grofiles_add_settings' ); 
  23.  
  24. add_action( 'load-index.php', 'grofiles_admin_cards' ); 
  25. add_action( 'load-users.php', 'grofiles_admin_cards' ); 
  26. add_action( 'load-edit-comments.php', 'grofiles_admin_cards' ); 
  27. add_action( 'load-options-discussion.php', 'grofiles_admin_cards_forced' ); 
  28.  
  29. Jetpack::enable_module_configurable( __FILE__ ); 
  30. Jetpack::module_configuration_load( __FILE__, 'gravatar_hovercards_configuration_load' ); 
  31.  
  32. function gravatar_hovercards_configuration_load() { 
  33. wp_safe_redirect( admin_url( 'options-discussion.php#show_avatars' ) ); 
  34. exit; 
  35.  
  36. add_action( 'jetpack_modules_loaded', 'grofiles_hovercards_init' ); 
  37.  
  38. /** Hovercard Settings */ 
  39.  
  40. /** 
  41. * Adds Gravatar Hovercard setting 
  42. * 
  43. * @todo - always print HTML, hide via CSS/JS if !show_avatars 
  44. */ 
  45. function grofiles_add_settings() { 
  46. if ( !get_option( 'show_avatars' ) ) 
  47. return; 
  48.  
  49. add_settings_field( 'gravatar_disable_hovercards', __( 'Gravatar Hovercards', 'jetpack' ), 'grofiles_setting_callback', 'discussion', 'avatars' ); 
  50. register_setting( 'discussion', 'gravatar_disable_hovercards', 'grofiles_hovercard_option_sanitize' ); 
  51.  
  52. /** 
  53. * HTML for Gravatar Hovercard setting 
  54. */ 
  55. function grofiles_setting_callback() { 
  56. global $current_user; 
  57.  
  58. $checked = 'disabled' == get_option( 'gravatar_disable_hovercards' ) ? '' : 'checked="checked" '; 
  59.  
  60. echo "<label id='gravatar-hovercard-options'><input {$checked}name='gravatar_disable_hovercards' id='gravatar_disable_hovercards' type='checkbox' value='enabled' class='code' /> " . __( "View people's profiles when you mouse over their Gravatars", 'jetpack' ) . "</label>"; 
  61. ?> 
  62. <style type="text/css"> 
  63. #grav-profile-example img { 
  64. float: left; 
  65. #grav-profile-example span { 
  66. padding: 0 1em; 
  67. </style> 
  68. <script type="text/javascript"> 
  69. // <![CDATA[ 
  70. jQuery( function($) { 
  71. var tr = $( '#gravatar_disable_hovercards' ).change( function() { 
  72. if ( $( this ).is( ':checked' ) ) { 
  73. $( '#grav-profile-example' ).slideDown( 'fast' ); 
  74. } else { 
  75. $( '#grav-profile-example' ).slideUp( 'fast' ); 
  76. } ).parents( 'tr' ); 
  77. var ftr = tr.parents( 'table' ).find( 'tr:first' ); 
  78. if ( ftr.size() && !ftr.find( '#gravatar_disable_hovercards' ).size() ) { 
  79. ftr.after( tr ); 
  80. } ); 
  81. // ]]> 
  82. </script> 
  83. <p id="grav-profile-example" class="hide-if-no-js"<?php if ( !$checked ) echo ' style="display:none"'; ?>><?php echo get_avatar( $current_user->ID, 64 ); ?> <span><?php _e( 'Put your mouse over your Gravatar to check out your profile.', 'jetpack' ); ?> <br class="clear" /></span></p> 
  84. <?php 
  85.  
  86. /** 
  87. * Sanitation filter for Gravatar Hovercard setting 
  88. */ 
  89. function grofiles_hovercard_option_sanitize( $val ) { 
  90. if ( 'disabled' == $val ) { 
  91. return $val; 
  92.  
  93. return $val ? 'enabled' : 'disabled'; 
  94.  
  95.  
  96. /** Hovercard Display */ 
  97.  
  98. /** 
  99. * Stores the gravatars' users that need extra profile data attached. 
  100. * 
  101. * Getter/Setter 
  102. * 
  103. * @param int|string|null $author Setter: User ID or email address. Getter: null. 
  104. * 
  105. * @return mixed Setter: void. Getter: array of user IDs and email addresses. 
  106. */ 
  107. function grofiles_gravatars_to_append( $author = null ) { 
  108. static $authors = array(); 
  109.  
  110. // Get 
  111. if ( is_null( $author ) ) { 
  112. return array_keys( $authors ); 
  113.  
  114. // Set 
  115.  
  116. if ( is_numeric( $author ) ) { 
  117. $author = (int) $author; 
  118.  
  119. $authors[$author] = true; 
  120.  
  121. /** 
  122. * Stores the user ID or email address for each gravatar generated. 
  123. * 
  124. * Attached to the 'get_avatar' filter. 
  125. * 
  126. * @param string $avatar The <img/> element of the avatar. 
  127. * @param mixed $author User ID, email address, user login, comment object, user object, post object 
  128. * 
  129. * @return The <img/> element of the avatar. 
  130. */ 
  131. function grofiles_get_avatar( $avatar, $author ) { 
  132. if ( is_numeric( $author ) ) { 
  133. grofiles_gravatars_to_append( $author ); 
  134. } else if ( is_string( $author ) ) { 
  135. if ( false !== strpos( $author, '@' ) ) { 
  136. grofiles_gravatars_to_append( $author ); 
  137. } else { 
  138. if ( $user = get_user_by( 'slug', $author ) ) 
  139. grofiles_gravatars_to_append( $user->ID ); 
  140. } else if ( isset( $author->comment_type ) ) { 
  141. if ( '' != $author->comment_type && 'comment' != $author->comment_type ) 
  142. return $avatar; 
  143. if ( $author->user_id ) 
  144. grofiles_gravatars_to_append( $author->user_id ); 
  145. else 
  146. grofiles_gravatars_to_append( $author->comment_author_email ); 
  147. } else if ( isset( $author->user_login ) ) { 
  148. grofiles_gravatars_to_append( $author->ID ); 
  149. } else if ( isset( $author->post_author ) ) { 
  150. grofiles_gravatars_to_append( $author->post_author ); 
  151.  
  152. return $avatar; 
  153.  
  154. /** 
  155. * Loads Gravatar Hovercard script. 
  156. * 
  157. * @todo is_singular() only? 
  158. */ 
  159. function grofiles_attach_cards() { 
  160. global $blog_id; 
  161.  
  162. // Is the display of Avatars disabled? 
  163. if ( ! get_option( 'show_avatars' ) ) { 
  164. return; 
  165.  
  166. // Is the display of Gravatar Hovercards disabled? 
  167. if ( 'disabled' == get_option( 'gravatar_disable_hovercards' ) ) { 
  168. return; 
  169.  
  170. wp_enqueue_script( 'grofiles-cards', ( is_ssl() ? 'https://secure' : 'http://s' ) . '.gravatar.com/js/gprofiles.js', array( 'jquery' ), GROFILES__CACHE_BUSTER, true ); 
  171. wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), false, true ); 
  172. if ( is_user_logged_in() ) { 
  173. $cu = wp_get_current_user(); 
  174. $my_hash = md5( $cu->user_email ); 
  175. } else if ( !empty( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) { 
  176. $my_hash = md5( $_COOKIE['comment_author_email_' . COOKIEHASH] ); 
  177. } else { 
  178. $my_hash = ''; 
  179. wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) ); 
  180.  
  181. function grofiles_attach_cards_forced() { 
  182. add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' ); 
  183. grofiles_attach_cards(); 
  184.  
  185. function grofiles_force_gravatar_enable_hovercards() { 
  186. return 'enabled'; 
  187.  
  188. function grofiles_admin_cards_forced() { 
  189. add_action( 'admin_footer', 'grofiles_attach_cards_forced' ); 
  190.  
  191. function grofiles_admin_cards() { 
  192. add_action( 'admin_footer', 'grofiles_attach_cards' ); 
  193.  
  194. function grofiles_extra_data() { 
  195. ?> 
  196. <div style="display:none"> 
  197. <?php 
  198. foreach ( grofiles_gravatars_to_append() as $author ) 
  199. grofiles_hovercards_data_html( $author ); 
  200. ?> 
  201. </div> 
  202. <?php 
  203.  
  204. /** 
  205. * Echoes the data from grofiles_hovercards_data() as HTML elements. 
  206. * 
  207. * @param int|string $author User ID or email address 
  208. */ 
  209. function grofiles_hovercards_data_html( $author ) { 
  210. $data = grofiles_hovercards_data( $author ); 
  211. if ( is_numeric( $author ) ) { 
  212. $user = get_userdata( $author ); 
  213. $hash = md5( $user->user_email ); 
  214. } else { 
  215. $hash = md5( $author ); 
  216. ?> 
  217. <div class="grofile-hash-map-<?php echo $hash; ?>"> 
  218. <?php foreach ( $data as $key => $value ) : ?> 
  219. <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span> 
  220. <?php endforeach; ?> 
  221. </div> 
  222. <?php 
  223.  
  224.  
  225. /** API */ 
  226.  
  227. /** 
  228. * Returns the PHP callbacks for data sources. 
  229. * 
  230. * 'grofiles_hovercards_data_callbacks' filter 
  231. * 
  232. * @return array( data_key => data_callback, ... ) 
  233. */ 
  234. function grofiles_hovercards_data_callbacks() { 
  235. /** 
  236. * Filter the Gravatar Hovercard PHP callbacks. 
  237. * 
  238. * @module gravatar-hovercards 
  239. * 
  240. * @since 1.1.0 
  241. * 
  242. * @param array $args Array of data callbacks. 
  243. */ 
  244. return apply_filters( 'grofiles_hovercards_data_callbacks', array() ); 
  245.  
  246. /** 
  247. * Keyed JSON object containing all profile data provided by registered callbacks 
  248. * 
  249. * @param int|strung $author User ID or email address 
  250. * 
  251. * @return array( data_key => data, ... ) 
  252. */ 
  253. function grofiles_hovercards_data( $author ) { 
  254. $r = array(); 
  255. foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) { 
  256. if ( !is_callable( $callback ) ) 
  257. continue; 
  258. $data = call_user_func( $callback, $author, $key ); 
  259. if ( !is_null( $data ) ) 
  260. $r[$key] = $data; 
  261.  
  262. return $r; 
.