PageRenderTime 313ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/gravatar-hovercards.php

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