PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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