Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 167
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
380
grofiles_attach_cards
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
156
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      = $response_body->entry[0] ?? null;
216                $display_name = $profile->displayName ?? ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
217                $location     = $profile->currentLocation ?? ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
218                $description  = $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 */
253function grofiles_attach_cards() {
254
255    // Is the display of Avatars disabled?
256    if ( ! get_option( 'show_avatars' ) ) {
257        return;
258    }
259
260    // Is the display of Gravatar Hovercards disabled?
261    if ( 'disabled' === Jetpack_Options::get_option_and_ensure_autoload( 'gravatar_disable_hovercards', '0' ) ) {
262        return;
263    }
264
265    // Hovercards are only relevant on pages that render avatars.
266    if ( ! is_singular() && ! is_home() && ! is_front_page() && ! is_archive() && ! is_search() ) {
267        return;
268    }
269
270    if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
271        wp_enqueue_style( 'gravatar-hovercard-style', plugins_url( '/gravatar/gravatar-hovercards-amp.css', __FILE__ ), array(), JETPACK__VERSION );
272    } else {
273        wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array(), GROFILES__CACHE_BUSTER, true );
274        wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), JETPACK__VERSION, true );
275        if ( is_user_logged_in() ) {
276            $cu      = wp_get_current_user();
277            $my_hash = md5( $cu->user_email );
278        } elseif ( ! empty( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
279            $my_hash = md5( filter_var( wp_unslash( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) );
280        } else {
281            $my_hash = '';
282        }
283        wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) );
284    }
285}
286/**
287 * Add hovercards on Discussion settings panel.
288 */
289function grofiles_attach_cards_forced() {
290    add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' );
291    grofiles_attach_cards();
292}
293/**
294 * Set hovercards as enabled on Discussion settings panel.
295 */
296function grofiles_force_gravatar_enable_hovercards() {
297    return 'enabled';
298}
299/**
300 * Add script to admin footer on Discussion settings panel.
301 */
302function grofiles_admin_cards_forced() {
303    add_action( 'admin_footer', 'grofiles_attach_cards_forced' );
304}
305/**
306 * Add script to admin footer.
307 */
308function grofiles_admin_cards() {
309    add_action( 'admin_footer', 'grofiles_attach_cards' );
310}
311/**
312 * Dequeue the FE assets when there are no gravatars on the page to be displayed.
313 */
314function grofiles_extra_data() {
315    $authors = grofiles_gravatars_to_append();
316
317    if ( ! $authors ) {
318        wp_dequeue_script( 'grofiles-cards' );
319        wp_dequeue_script( 'wpgroho' );
320    } else {
321        ?>
322    <div style="display:none">
323        <?php
324        foreach ( $authors as $author ) {
325            grofiles_hovercards_data_html( $author );
326        }
327        ?>
328    </div>
329        <?php
330    }
331}
332
333/**
334 * Echoes the data from grofiles_hovercards_data() as HTML elements.
335 *
336 * @since 5.5.0 Add support for a passed WP_User object
337 *
338 * @param int|string|WP_User $author User ID, email address, or a WP_User object.
339 */
340function grofiles_hovercards_data_html( $author ) {
341    $data = grofiles_hovercards_data( $author );
342    $hash = '';
343    if ( is_numeric( $author ) ) {
344        $user = get_userdata( $author );
345        if ( $user ) {
346            $hash = md5( $user->user_email );
347        }
348    } elseif ( is_email( $author ) ) {
349        $hash = md5( $author );
350    } elseif ( is_a( $author, 'WP_User' ) ) {
351        $hash = md5( $author->user_email );
352    }
353
354    if ( ! $hash ) {
355        return;
356    }
357    ?>
358    <div class="grofile-hash-map-<?php echo esc_attr( $hash ); ?>">
359    <?php    foreach ( $data as $key => $value ) : ?>
360        <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span>
361<?php    endforeach; ?>
362    </div>
363    <?php
364}
365
366/* API */
367
368/**
369 * Returns the PHP callbacks for data sources.
370 *
371 * 'grofiles_hovercards_data_callbacks' filter
372 *
373 * @return array<string,callable> ( data_key => data_callback, ... )
374 */
375function grofiles_hovercards_data_callbacks() {
376    /**
377     * Filter the Gravatar Hovercard PHP callbacks.
378     *
379     * @module gravatar-hovercards
380     *
381     * @since 1.1.0
382     *
383     * @param array $args Array of data callbacks.
384     */
385    return apply_filters( 'grofiles_hovercards_data_callbacks', array() );
386}
387
388/**
389 * Keyed JSON object containing all profile data provided by registered callbacks
390 *
391 * @param int|string $author User ID or email address.
392 *
393 * @return array<string,mixed> ( data_key => data, ... )
394 */
395function grofiles_hovercards_data( $author ) {
396    $r = array();
397    foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) {
398        if ( ! is_callable( $callback ) ) {
399            continue;
400        }
401        $data = call_user_func( $callback, $author, $key );
402        if ( $data !== null ) {
403            $r[ $key ] = $data;
404        }
405    }
406
407    return $r;
408}