carousel
1 year ago
cloudflare-analytics
2 years ago
comment-likes
5 years ago
comments
1 year ago
custom-post-types
1 year ago
external-media
1 year ago
geo-location
1 year ago
google-fonts
1 year ago
gravatar
6 years ago
infinite-scroll
1 year ago
likes
1 year ago
markdown
1 year ago
memberships
1 year ago
photon-cdn
1 year ago
plugin-search
1 year ago
post-by-email
1 year ago
related-posts
1 year ago
scan
1 year ago
seo-tools
1 year ago
sharedaddy
1 year ago
shortcodes
1 year ago
simple-payments
1 year ago
site-icon
2 years ago
sitemaps
1 year ago
stats
2 years ago
subscriptions
1 year ago
theme-tools
1 year ago
tiled-gallery
1 year ago
verification-tools
4 years ago
videopress
1 year ago
widget-visibility
1 year ago
widgets
1 year ago
woocommerce-analytics
1 year ago
wordads
1 year ago
wpcom-tos
5 years ago
account-protection.php
1 year ago
blaze.php
2 years ago
blocks.php
1 year ago
carousel.php
2 years ago
comment-likes.php
2 years ago
comments.php
2 years ago
contact-form.php
1 year ago
copy-post.php
1 year ago
custom-content-types.php
1 year ago
geo-location.php
1 year ago
google-fonts.php
2 years ago
gravatar-hovercards.php
2 years ago
infinite-scroll.php
3 years ago
json-api.php
5 years ago
latex.php
4 years ago
likes.php
1 year ago
markdown.php
4 years ago
masterbar.php
1 year ago
module-extras.php
1 year ago
module-headings.php
1 year ago
module-info.php
1 year ago
monitor.php
2 years ago
notes.php
1 year ago
photon-cdn.php
1 year ago
photon.php
3 years ago
plugin-search.php
1 year ago
post-by-email.php
5 years ago
post-list.php
4 years ago
protect.php
2 years ago
publicize.php
1 year ago
related-posts.php
2 years ago
search.php
4 years ago
seo-tools.php
2 years ago
sharedaddy.php
2 years ago
shortcodes.php
1 year ago
shortlinks.php
1 year ago
sitemaps.php
1 year ago
sso.php
1 year ago
stats.php
1 year ago
subscriptions.php
1 year ago
theme-tools.php
1 year ago
tiled-gallery.php
4 years ago
vaultpress.php
2 years ago
verification-tools.php
5 years ago
videopress.php
4 years ago
waf.php
1 year ago
widget-visibility.php
4 years ago
widgets.php
1 year ago
woocommerce-analytics.php
2 years ago
wordads.php
2 years ago
wpgroho.js
1 year ago
gravatar-hovercards.php
402 lines
| 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 | * @package automattic/jetpack |
| 15 | */ |
| 16 | |
| 17 | define( 'GROFILES__CACHE_BUSTER', gmdate( 'YW' ) ); |
| 18 | |
| 19 | /** |
| 20 | * Actions that are run on init. |
| 21 | */ |
| 22 | function grofiles_hovercards_init() { |
| 23 | add_filter( 'get_avatar', 'grofiles_get_avatar', 10, 2 ); |
| 24 | add_action( 'wp_enqueue_scripts', 'grofiles_attach_cards' ); |
| 25 | add_action( 'wp_footer', 'grofiles_extra_data' ); |
| 26 | add_action( 'admin_init', 'grofiles_add_settings' ); |
| 27 | |
| 28 | add_action( 'load-index.php', 'grofiles_admin_cards' ); |
| 29 | add_action( 'load-users.php', 'grofiles_admin_cards' ); |
| 30 | add_action( 'load-edit-comments.php', 'grofiles_admin_cards' ); |
| 31 | add_action( 'load-options-discussion.php', 'grofiles_admin_cards_forced' ); |
| 32 | |
| 33 | add_filter( 'jetpack_module_configuration_url_gravatar-hovercards', 'gravatar_hovercards_configuration_url' ); |
| 34 | |
| 35 | add_filter( 'get_comment_author_url', 'grofiles_amp_comment_author_url', 10, 2 ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Set configuration page URL. |
| 40 | */ |
| 41 | function gravatar_hovercards_configuration_url() { |
| 42 | return admin_url( 'options-discussion.php#show_avatars' ); |
| 43 | } |
| 44 | |
| 45 | add_action( 'jetpack_modules_loaded', 'grofiles_hovercards_init' ); |
| 46 | |
| 47 | /* Hovercard Settings */ |
| 48 | |
| 49 | /** |
| 50 | * Adds Gravatar Hovercard setting |
| 51 | * |
| 52 | * @todo - always print HTML, hide via CSS/JS if !show_avatars |
| 53 | */ |
| 54 | function grofiles_add_settings() { |
| 55 | if ( ! get_option( 'show_avatars' ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | add_settings_field( 'gravatar_disable_hovercards', __( 'Gravatar Hovercards', 'jetpack' ), 'grofiles_setting_callback', 'discussion', 'avatars' ); |
| 60 | register_setting( 'discussion', 'gravatar_disable_hovercards', 'grofiles_hovercard_option_sanitize' ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * HTML for Gravatar Hovercard setting |
| 65 | */ |
| 66 | function grofiles_setting_callback() { |
| 67 | global $current_user; |
| 68 | |
| 69 | $option = get_option( 'gravatar_disable_hovercards' ); |
| 70 | printf( |
| 71 | "<label id='gravatar-hovercard-options'><input %s name='gravatar_disable_hovercards' id='gravatar_disable_hovercards' type='checkbox' value='enabled' class='code'/>%s</label>", |
| 72 | checked( $option, 'enabled', false ), |
| 73 | esc_html__( 'View people\'s profiles when you mouse over their Gravatars', 'jetpack' ) |
| 74 | ); |
| 75 | |
| 76 | ?> |
| 77 | <style type="text/css"> |
| 78 | #grav-profile-example img { |
| 79 | float: left; |
| 80 | } |
| 81 | #grav-profile-example span { |
| 82 | padding: 0 1em; |
| 83 | } |
| 84 | </style> |
| 85 | <script type="text/javascript"> |
| 86 | // <![CDATA[ |
| 87 | jQuery( function($) { |
| 88 | var tr = $( '#gravatar_disable_hovercards' ).change( function() { |
| 89 | if ( $( this ).is( ':checked' ) ) { |
| 90 | $( '#grav-profile-example' ).slideDown( 'fast' ); |
| 91 | } else { |
| 92 | $( '#grav-profile-example' ).slideUp( 'fast' ); |
| 93 | } |
| 94 | } ).parents( 'tr' ); |
| 95 | var ftr = tr.parents( 'table' ).find( 'tr:first' ); |
| 96 | if ( ftr.length && !ftr.find( '#gravatar_disable_hovercards' ).length ) { |
| 97 | ftr.after( tr ); |
| 98 | } |
| 99 | } ); |
| 100 | // ]]> |
| 101 | </script> |
| 102 | <p id="grav-profile-example" class="hide-if-no-js" |
| 103 | <?php |
| 104 | if ( 'disabled' === $option ) { |
| 105 | echo ' style="display:none"';} |
| 106 | ?> |
| 107 | > |
| 108 | <?php echo get_avatar( $current_user->ID, 64 ); ?> <span><?php esc_html_e( 'Put your mouse over your Gravatar to check out your profile.', 'jetpack' ); ?> <br class="clear" /></span></p> |
| 109 | <?php |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Sanitation filter for Gravatar Hovercard setting |
| 114 | * |
| 115 | * @param string $val Disabled or enabled. |
| 116 | */ |
| 117 | function grofiles_hovercard_option_sanitize( $val ) { |
| 118 | if ( 'disabled' === $val ) { |
| 119 | return $val; |
| 120 | } |
| 121 | |
| 122 | return $val ? 'enabled' : 'disabled'; |
| 123 | } |
| 124 | |
| 125 | /* Hovercard Display */ |
| 126 | |
| 127 | /** |
| 128 | * Stores the gravatars' users that need extra profile data attached. |
| 129 | * |
| 130 | * Getter/Setter |
| 131 | * |
| 132 | * @param int|string|null $author Setter: User ID or email address. Getter: null. |
| 133 | * |
| 134 | * @return mixed Setter: void. Getter: array of user IDs and email addresses. |
| 135 | */ |
| 136 | function grofiles_gravatars_to_append( $author = null ) { |
| 137 | static $authors = array(); |
| 138 | |
| 139 | // Get. |
| 140 | if ( $author === null ) { |
| 141 | return array_keys( $authors ); |
| 142 | } |
| 143 | |
| 144 | // Set. |
| 145 | |
| 146 | if ( is_numeric( $author ) ) { |
| 147 | $author = (int) $author; |
| 148 | } |
| 149 | |
| 150 | $authors[ $author ] = true; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * In AMP, override the comment URL to allow for interactivity without |
| 155 | * navigating to a new page |
| 156 | * |
| 157 | * @param string $url The comment author's URL. |
| 158 | * @param int $id The comment ID. |
| 159 | * |
| 160 | * @return string The adjusted URL |
| 161 | */ |
| 162 | function grofiles_amp_comment_author_url( $url, $id ) { |
| 163 | if ( 'comment' === get_comment_type( $id ) && class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 164 | // @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. |
| 165 | return '#!'; |
| 166 | } |
| 167 | |
| 168 | return $url; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Stores the user ID or email address for each gravatar generated. |
| 173 | * |
| 174 | * Attached to the 'get_avatar' filter. |
| 175 | * |
| 176 | * @param string $avatar The <img/> element of the avatar. |
| 177 | * @param mixed $author User ID, email address, user login, comment object, user object, post object. |
| 178 | * |
| 179 | * @return string The <img/> element of the avatar. |
| 180 | */ |
| 181 | function grofiles_get_avatar( $avatar, $author ) { |
| 182 | $is_amp = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request(); |
| 183 | |
| 184 | if ( is_numeric( $author ) ) { |
| 185 | grofiles_gravatars_to_append( $author ); |
| 186 | } elseif ( is_string( $author ) ) { |
| 187 | if ( str_contains( $author, '@' ) ) { |
| 188 | grofiles_gravatars_to_append( $author ); |
| 189 | } else { |
| 190 | $user = get_user_by( 'slug', $author ); |
| 191 | if ( $user ) { |
| 192 | grofiles_gravatars_to_append( $user->ID ); |
| 193 | } |
| 194 | } |
| 195 | } elseif ( isset( $author->comment_type ) ) { |
| 196 | if ( $is_amp ) { |
| 197 | if ( 1 === preg_match( '/avatar\/([a-zA-Z0-9]+)\?/', $avatar, $email_hash ) ) { |
| 198 | $email_hash = $email_hash[1]; |
| 199 | $cache_group = 'gravatar_profiles_'; |
| 200 | $cache_key = 'gravatar_profile_' . $email_hash; |
| 201 | |
| 202 | $response_body = wp_cache_get( $cache_key, $cache_group ); |
| 203 | if ( false === $response_body ) { |
| 204 | $response = wp_remote_get( esc_url_raw( 'https://gravatar.com/' . $email_hash . '.json' ) ); |
| 205 | if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
| 206 | $response_body = json_decode( $response['body'] ); |
| 207 | wp_cache_set( $cache_key, $response_body, $cache_group, 60 * MINUTE_IN_SECONDS ); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | $profile = isset( $response_body->entry[0] ) ? $response_body->entry[0] : null; |
| 212 | $display_name = isset( $profile->displayName ) ? $profile->displayName : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 213 | $location = isset( $profile->currentLocation ) ? $profile->currentLocation : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 214 | $description = isset( $profile->aboutMe ) ? $profile->aboutMe : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 215 | |
| 216 | $avatar = ' |
| 217 | <figure data-amp-lightbox="true"> |
| 218 | ' . $avatar . ' |
| 219 | <figcaption> |
| 220 | ' . esc_html( $display_name ) . ( ! empty( $location ) ? ' – ' . esc_html( $location ) : '' ) . ( ! empty( $description ) ? ' – ' . esc_html( $description ) : '' ) . ' |
| 221 | </figcaption> |
| 222 | </figure> |
| 223 | '; |
| 224 | } |
| 225 | |
| 226 | return $avatar; |
| 227 | } |
| 228 | |
| 229 | if ( '' !== $author->comment_type && 'comment' !== $author->comment_type ) { |
| 230 | return $avatar; |
| 231 | } |
| 232 | if ( $author->user_id ) { |
| 233 | grofiles_gravatars_to_append( $author->user_id ); |
| 234 | } else { |
| 235 | grofiles_gravatars_to_append( $author->comment_author_email ); |
| 236 | } |
| 237 | } elseif ( isset( $author->user_login ) ) { |
| 238 | grofiles_gravatars_to_append( $author->ID ); |
| 239 | } elseif ( isset( $author->post_author ) ) { |
| 240 | grofiles_gravatars_to_append( $author->post_author ); |
| 241 | } |
| 242 | |
| 243 | return $avatar; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Loads Gravatar Hovercard script. |
| 248 | * |
| 249 | * @todo is_singular() only? |
| 250 | */ |
| 251 | function grofiles_attach_cards() { |
| 252 | |
| 253 | // Is the display of Avatars disabled? |
| 254 | if ( ! get_option( 'show_avatars' ) ) { |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | // Is the display of Gravatar Hovercards disabled? |
| 259 | if ( 'disabled' === Jetpack_Options::get_option_and_ensure_autoload( 'gravatar_disable_hovercards', '0' ) ) { |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 264 | wp_enqueue_style( 'gravatar-hovercard-style', plugins_url( '/gravatar/gravatar-hovercards-amp.css', __FILE__ ), array(), JETPACK__VERSION ); |
| 265 | } else { |
| 266 | wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array(), GROFILES__CACHE_BUSTER, true ); |
| 267 | wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), JETPACK__VERSION, true ); |
| 268 | if ( is_user_logged_in() ) { |
| 269 | $cu = wp_get_current_user(); |
| 270 | $my_hash = md5( $cu->user_email ); |
| 271 | } elseif ( ! empty( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) { |
| 272 | $my_hash = md5( filter_var( wp_unslash( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) ); |
| 273 | } else { |
| 274 | $my_hash = ''; |
| 275 | } |
| 276 | wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) ); |
| 277 | } |
| 278 | } |
| 279 | /** |
| 280 | * Add hovercards on Discussion settings panel. |
| 281 | */ |
| 282 | function grofiles_attach_cards_forced() { |
| 283 | add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' ); |
| 284 | grofiles_attach_cards(); |
| 285 | } |
| 286 | /** |
| 287 | * Set hovercards as enabled on Discussion settings panel. |
| 288 | */ |
| 289 | function grofiles_force_gravatar_enable_hovercards() { |
| 290 | return 'enabled'; |
| 291 | } |
| 292 | /** |
| 293 | * Add script to admin footer on Discussion settings panel. |
| 294 | */ |
| 295 | function grofiles_admin_cards_forced() { |
| 296 | add_action( 'admin_footer', 'grofiles_attach_cards_forced' ); |
| 297 | } |
| 298 | /** |
| 299 | * Add script to admin footer. |
| 300 | */ |
| 301 | function grofiles_admin_cards() { |
| 302 | add_action( 'admin_footer', 'grofiles_attach_cards' ); |
| 303 | } |
| 304 | /** |
| 305 | * Dequeue the FE assets when there are no gravatars on the page to be displayed. |
| 306 | */ |
| 307 | function grofiles_extra_data() { |
| 308 | $authors = grofiles_gravatars_to_append(); |
| 309 | |
| 310 | if ( ! $authors ) { |
| 311 | wp_dequeue_script( 'grofiles-cards' ); |
| 312 | wp_dequeue_script( 'wpgroho' ); |
| 313 | } else { |
| 314 | ?> |
| 315 | <div style="display:none"> |
| 316 | <?php |
| 317 | foreach ( $authors as $author ) { |
| 318 | grofiles_hovercards_data_html( $author ); |
| 319 | } |
| 320 | ?> |
| 321 | </div> |
| 322 | <?php |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Echoes the data from grofiles_hovercards_data() as HTML elements. |
| 328 | * |
| 329 | * @since 5.5.0 Add support for a passed WP_User object |
| 330 | * |
| 331 | * @param int|string|WP_User $author User ID, email address, or a WP_User object. |
| 332 | */ |
| 333 | function grofiles_hovercards_data_html( $author ) { |
| 334 | $data = grofiles_hovercards_data( $author ); |
| 335 | $hash = ''; |
| 336 | if ( is_numeric( $author ) ) { |
| 337 | $user = get_userdata( $author ); |
| 338 | if ( $user ) { |
| 339 | $hash = md5( $user->user_email ); |
| 340 | } |
| 341 | } elseif ( is_email( $author ) ) { |
| 342 | $hash = md5( $author ); |
| 343 | } elseif ( is_a( $author, 'WP_User' ) ) { |
| 344 | $hash = md5( $author->user_email ); |
| 345 | } |
| 346 | |
| 347 | if ( ! $hash ) { |
| 348 | return; |
| 349 | } |
| 350 | ?> |
| 351 | <div class="grofile-hash-map-<?php echo esc_attr( $hash ); ?>"> |
| 352 | <?php foreach ( $data as $key => $value ) : ?> |
| 353 | <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span> |
| 354 | <?php endforeach; ?> |
| 355 | </div> |
| 356 | <?php |
| 357 | } |
| 358 | |
| 359 | /* API */ |
| 360 | |
| 361 | /** |
| 362 | * Returns the PHP callbacks for data sources. |
| 363 | * |
| 364 | * 'grofiles_hovercards_data_callbacks' filter |
| 365 | * |
| 366 | * @return array<string,callable> ( data_key => data_callback, ... ) |
| 367 | */ |
| 368 | function grofiles_hovercards_data_callbacks() { |
| 369 | /** |
| 370 | * Filter the Gravatar Hovercard PHP callbacks. |
| 371 | * |
| 372 | * @module gravatar-hovercards |
| 373 | * |
| 374 | * @since 1.1.0 |
| 375 | * |
| 376 | * @param array $args Array of data callbacks. |
| 377 | */ |
| 378 | return apply_filters( 'grofiles_hovercards_data_callbacks', array() ); |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Keyed JSON object containing all profile data provided by registered callbacks |
| 383 | * |
| 384 | * @param int|string $author User ID or email address. |
| 385 | * |
| 386 | * @return array<string,mixed> ( data_key => data, ... ) |
| 387 | */ |
| 388 | function grofiles_hovercards_data( $author ) { |
| 389 | $r = array(); |
| 390 | foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) { |
| 391 | if ( ! is_callable( $callback ) ) { |
| 392 | continue; |
| 393 | } |
| 394 | $data = call_user_func( $callback, $author, $key ); |
| 395 | if ( $data !== null ) { |
| 396 | $r[ $key ] = $data; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return $r; |
| 401 | } |
| 402 |