Нема описа

gravatar-hovercards.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * Module Name: Gravatar Hovercards
  4. * Module Description: Enable pop-up business cards over commenters’ Gravatars.
  5. * Sort Order: 11
  6. * Recommendation Order: 13
  7. * First Introduced: 1.1
  8. * Requires Connection: No
  9. * Auto Activate: No
  10. * Module Tags: Social, Appearance
  11. * Feature: Appearance
  12. * Additional Search Queries: gravatar, hovercards
  13. */
  14. define( 'GROFILES__CACHE_BUSTER', gmdate( 'YW' ) );
  15. function grofiles_hovercards_init() {
  16. add_filter( 'get_avatar', 'grofiles_get_avatar', 10, 2 );
  17. add_action( 'wp_enqueue_scripts', 'grofiles_attach_cards' );
  18. add_action( 'wp_footer', 'grofiles_extra_data' );
  19. add_action( 'admin_init', 'grofiles_add_settings' );
  20. add_action( 'load-index.php', 'grofiles_admin_cards' );
  21. add_action( 'load-users.php', 'grofiles_admin_cards' );
  22. add_action( 'load-edit-comments.php', 'grofiles_admin_cards' );
  23. add_action( 'load-options-discussion.php', 'grofiles_admin_cards_forced' );
  24. add_filter( 'jetpack_module_configuration_url_gravatar-hovercards', 'gravatar_hovercards_configuration_url' );
  25. add_filter( 'get_comment_author_url', 'grofiles_amp_comment_author_url', 10, 2 );
  26. }
  27. function gravatar_hovercards_configuration_url() {
  28. return admin_url( 'options-discussion.php#show_avatars' );
  29. }
  30. add_action( 'jetpack_modules_loaded', 'grofiles_hovercards_init' );
  31. /* Hovercard Settings */
  32. /**
  33. * Adds Gravatar Hovercard setting
  34. *
  35. * @todo - always print HTML, hide via CSS/JS if !show_avatars
  36. */
  37. function grofiles_add_settings() {
  38. if ( !get_option( 'show_avatars' ) )
  39. return;
  40. add_settings_field( 'gravatar_disable_hovercards', __( 'Gravatar Hovercards', 'jetpack' ), 'grofiles_setting_callback', 'discussion', 'avatars' );
  41. register_setting( 'discussion', 'gravatar_disable_hovercards', 'grofiles_hovercard_option_sanitize' );
  42. }
  43. /**
  44. * HTML for Gravatar Hovercard setting
  45. */
  46. function grofiles_setting_callback() {
  47. global $current_user;
  48. $checked = 'disabled' == get_option( 'gravatar_disable_hovercards' ) ? '' : 'checked="checked" ';
  49. 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>";
  50. ?>
  51. <style type="text/css">
  52. #grav-profile-example img {
  53. float: left;
  54. }
  55. #grav-profile-example span {
  56. padding: 0 1em;
  57. }
  58. </style>
  59. <script type="text/javascript">
  60. // <![CDATA[
  61. jQuery( function($) {
  62. var tr = $( '#gravatar_disable_hovercards' ).change( function() {
  63. if ( $( this ).is( ':checked' ) ) {
  64. $( '#grav-profile-example' ).slideDown( 'fast' );
  65. } else {
  66. $( '#grav-profile-example' ).slideUp( 'fast' );
  67. }
  68. } ).parents( 'tr' );
  69. var ftr = tr.parents( 'table' ).find( 'tr:first' );
  70. if ( ftr.length && !ftr.find( '#gravatar_disable_hovercards' ).length ) {
  71. ftr.after( tr );
  72. }
  73. } );
  74. // ]]>
  75. </script>
  76. <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>
  77. <?php
  78. }
  79. /**
  80. * Sanitation filter for Gravatar Hovercard setting
  81. */
  82. function grofiles_hovercard_option_sanitize( $val ) {
  83. if ( 'disabled' == $val ) {
  84. return $val;
  85. }
  86. return $val ? 'enabled' : 'disabled';
  87. }
  88. /* Hovercard Display */
  89. /**
  90. * Stores the gravatars' users that need extra profile data attached.
  91. *
  92. * Getter/Setter
  93. *
  94. * @param int|string|null $author Setter: User ID or email address. Getter: null.
  95. *
  96. * @return mixed Setter: void. Getter: array of user IDs and email addresses.
  97. */
  98. function grofiles_gravatars_to_append( $author = null ) {
  99. static $authors = array();
  100. // Get
  101. if ( is_null( $author ) ) {
  102. return array_keys( $authors );
  103. }
  104. // Set
  105. if ( is_numeric( $author ) ) {
  106. $author = (int) $author;
  107. }
  108. $authors[$author] = true;
  109. }
  110. /**
  111. * In AMP, override the comment URL to allow for interactivity without
  112. * navigating to a new page
  113. *
  114. * @param string $url The comment author's URL.
  115. * @param int $id The comment ID.
  116. *
  117. * @return string The adjusted URL
  118. */
  119. function grofiles_amp_comment_author_url( $url, $id ) {
  120. if ( 'comment' === get_comment_type( $id ) && class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  121. // @todo Disabling the comment author link in this way is not ideal since clicking the link does not cause the lightbox to open in the same way as clicking the gravatar. Likely get_comment_author_url_link should be used instead so that the href attribute can be replaced with an `on` attribute that activates the gallery.
  122. return '#!';
  123. }
  124. return $url;
  125. }
  126. /**
  127. * Stores the user ID or email address for each gravatar generated.
  128. *
  129. * Attached to the 'get_avatar' filter.
  130. *
  131. * @param string $avatar The <img/> element of the avatar.
  132. * @param mixed $author User ID, email address, user login, comment object, user object, post object
  133. *
  134. * @return string The <img/> element of the avatar.
  135. */
  136. function grofiles_get_avatar( $avatar, $author ) {
  137. $is_amp = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
  138. if ( is_numeric( $author ) ) {
  139. grofiles_gravatars_to_append( $author );
  140. } else if ( is_string( $author ) ) {
  141. if ( false !== strpos( $author, '@' ) ) {
  142. grofiles_gravatars_to_append( $author );
  143. } else {
  144. if ( $user = get_user_by( 'slug', $author ) )
  145. grofiles_gravatars_to_append( $user->ID );
  146. }
  147. } else if ( isset( $author->comment_type ) ) {
  148. if ( $is_amp ) {
  149. if ( 1 === preg_match( '/avatar\/([a-zA-Z0-9]+)\?/', $avatar, $email_hash ) ) {
  150. $email_hash = $email_hash[1];
  151. $cache_group = 'gravatar_profiles_';
  152. $cache_key = 'gravatar_profile_' . $email_hash;
  153. $response_body = wp_cache_get( $cache_key, $cache_group );
  154. if ( false === $response_body ) {
  155. $response = wp_remote_get( esc_url_raw( 'https://en.gravatar.com/' . $email_hash . '.json' ) );
  156. if ( is_array( $response ) && ! is_wp_error( $response ) ) {
  157. $response_body = json_decode( $response['body'] );
  158. wp_cache_set( $cache_key, $response_body, $cache_group, 60 * MINUTE_IN_SECONDS );
  159. }
  160. }
  161. $profile = $response_body->entry[0];
  162. $display_name = $profile->displayName; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  163. $location = isset( $profile->currentLocation ) ? $profile->currentLocation : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  164. $description = isset( $profile->aboutMe ) ? $profile->aboutMe : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  165. $avatar = '
  166. <figure data-amp-lightbox="true">
  167. ' . $avatar . '
  168. <figcaption>
  169. ' . esc_html( $display_name ) . ( ! empty( $location ) ? ' – ' . esc_html( $location ) : '' ) . ( ! empty( $description ) ? ' – ' . esc_html( $description ) : '' ) . '
  170. </figcaption>
  171. </figure>
  172. ';
  173. }
  174. return $avatar;
  175. }
  176. if ( '' != $author->comment_type && 'comment' != $author->comment_type )
  177. return $avatar;
  178. if ( $author->user_id )
  179. grofiles_gravatars_to_append( $author->user_id );
  180. else
  181. grofiles_gravatars_to_append( $author->comment_author_email );
  182. } else if ( isset( $author->user_login ) ) {
  183. grofiles_gravatars_to_append( $author->ID );
  184. } else if ( isset( $author->post_author ) ) {
  185. grofiles_gravatars_to_append( $author->post_author );
  186. }
  187. return $avatar;
  188. }
  189. /**
  190. * Loads Gravatar Hovercard script.
  191. *
  192. * @todo is_singular() only?
  193. */
  194. function grofiles_attach_cards() {
  195. global $blog_id;
  196. // Is the display of Avatars disabled?
  197. if ( ! get_option( 'show_avatars' ) ) {
  198. return;
  199. }
  200. // Is the display of Gravatar Hovercards disabled?
  201. if ( 'disabled' == Jetpack_Options::get_option_and_ensure_autoload( 'gravatar_disable_hovercards', '0' ) ) {
  202. return;
  203. }
  204. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  205. wp_enqueue_style( 'gravatar-hovercard-style', plugins_url( '/gravatar/gravatar-hovercards-amp.css', __FILE__ ), array(), JETPACK__VERSION );
  206. } else {
  207. wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array(), GROFILES__CACHE_BUSTER, true );
  208. wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), JETPACK__VERSION, true );
  209. if ( is_user_logged_in() ) {
  210. $cu = wp_get_current_user();
  211. $my_hash = md5( $cu->user_email );
  212. } elseif ( ! empty( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
  213. $my_hash = md5( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] );
  214. } else {
  215. $my_hash = '';
  216. }
  217. wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) );
  218. }
  219. }
  220. function grofiles_attach_cards_forced() {
  221. add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' );
  222. grofiles_attach_cards();
  223. }
  224. function grofiles_force_gravatar_enable_hovercards() {
  225. return 'enabled';
  226. }
  227. function grofiles_admin_cards_forced() {
  228. add_action( 'admin_footer', 'grofiles_attach_cards_forced' );
  229. }
  230. function grofiles_admin_cards() {
  231. add_action( 'admin_footer', 'grofiles_attach_cards' );
  232. }
  233. function grofiles_extra_data() {
  234. $authors = grofiles_gravatars_to_append();
  235. if ( ! $authors ) {
  236. wp_dequeue_script( 'grofiles-cards' );
  237. wp_dequeue_script( 'wpgroho' );
  238. } else {
  239. ?>
  240. <div style="display:none">
  241. <?php
  242. foreach ( $authors as $author ) {
  243. grofiles_hovercards_data_html( $author );
  244. }
  245. ?>
  246. </div>
  247. <?php
  248. }
  249. }
  250. /**
  251. * Echoes the data from grofiles_hovercards_data() as HTML elements.
  252. *
  253. * @since 5.5.0 Add support for a passed WP_User object
  254. *
  255. * @param int|string|WP_User $author User ID, email address, or a WP_User object
  256. */
  257. function grofiles_hovercards_data_html( $author ) {
  258. $data = grofiles_hovercards_data( $author );
  259. $hash = '';
  260. if ( is_numeric( $author ) ) {
  261. $user = get_userdata( $author );
  262. if ( $user ) {
  263. $hash = md5( $user->user_email );
  264. }
  265. } elseif ( is_email( $author ) ) {
  266. $hash = md5( $author );
  267. } elseif ( is_a( $author, 'WP_User' ) ) {
  268. $hash = md5( $author->user_email );
  269. }
  270. if ( ! $hash ) {
  271. return;
  272. }
  273. ?>
  274. <div class="grofile-hash-map-<?php echo $hash; ?>">
  275. <?php foreach ( $data as $key => $value ) : ?>
  276. <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span>
  277. <?php endforeach; ?>
  278. </div>
  279. <?php
  280. }
  281. /* API */
  282. /**
  283. * Returns the PHP callbacks for data sources.
  284. *
  285. * 'grofiles_hovercards_data_callbacks' filter
  286. *
  287. * @return array( data_key => data_callback, ... )
  288. */
  289. function grofiles_hovercards_data_callbacks() {
  290. /**
  291. * Filter the Gravatar Hovercard PHP callbacks.
  292. *
  293. * @module gravatar-hovercards
  294. *
  295. * @since 1.1.0
  296. *
  297. * @param array $args Array of data callbacks.
  298. */
  299. return apply_filters( 'grofiles_hovercards_data_callbacks', array() );
  300. }
  301. /**
  302. * Keyed JSON object containing all profile data provided by registered callbacks
  303. *
  304. * @param int|strung $author User ID or email address
  305. *
  306. * @return array( data_key => data, ... )
  307. */
  308. function grofiles_hovercards_data( $author ) {
  309. $r = array();
  310. foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) {
  311. if ( !is_callable( $callback ) )
  312. continue;
  313. $data = call_user_func( $callback, $author, $key );
  314. if ( !is_null( $data ) )
  315. $r[$key] = $data;
  316. }
  317. return $r;
  318. }