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