PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.1
Jetpack – WP Security, Backup, Speed, & Growth v11.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / gravatar-hovercards.php
gravatar-hovercards.php
403 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( false !== strpos( $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://en.gravatar.com/' . $email_hash . '.json' ) );
205
206 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
207 $response_body = json_decode( $response['body'] );
208 wp_cache_set( $cache_key, $response_body, $cache_group, 60 * MINUTE_IN_SECONDS );
209 }
210 }
211
212 $profile = $response_body->entry[0];
213 $display_name = $profile->displayName; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
214 $location = isset( $profile->currentLocation ) ? $profile->currentLocation : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
215 $description = isset( $profile->aboutMe ) ? $profile->aboutMe : ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
216
217 $avatar = '
218 <figure data-amp-lightbox="true">
219 ' . $avatar . '
220 <figcaption>
221 ' . esc_html( $display_name ) . ( ! empty( $location ) ? '' . esc_html( $location ) : '' ) . ( ! empty( $description ) ? '' . esc_html( $description ) : '' ) . '
222 </figcaption>
223 </figure>
224 ';
225 }
226
227 return $avatar;
228 }
229
230 if ( '' !== $author->comment_type && 'comment' !== $author->comment_type ) {
231 return $avatar;
232 }
233 if ( $author->user_id ) {
234 grofiles_gravatars_to_append( $author->user_id );
235 } else {
236 grofiles_gravatars_to_append( $author->comment_author_email );
237 }
238 } elseif ( isset( $author->user_login ) ) {
239 grofiles_gravatars_to_append( $author->ID );
240 } elseif ( isset( $author->post_author ) ) {
241 grofiles_gravatars_to_append( $author->post_author );
242 }
243
244 return $avatar;
245 }
246
247 /**
248 * Loads Gravatar Hovercard script.
249 *
250 * @todo is_singular() only?
251 */
252 function grofiles_attach_cards() {
253
254 // Is the display of Avatars disabled?
255 if ( ! get_option( 'show_avatars' ) ) {
256 return;
257 }
258
259 // Is the display of Gravatar Hovercards disabled?
260 if ( 'disabled' === Jetpack_Options::get_option_and_ensure_autoload( 'gravatar_disable_hovercards', '0' ) ) {
261 return;
262 }
263
264 if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
265 wp_enqueue_style( 'gravatar-hovercard-style', plugins_url( '/gravatar/gravatar-hovercards-amp.css', __FILE__ ), array(), JETPACK__VERSION );
266 } else {
267 wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array(), GROFILES__CACHE_BUSTER, true );
268 wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), JETPACK__VERSION, true );
269 if ( is_user_logged_in() ) {
270 $cu = wp_get_current_user();
271 $my_hash = md5( $cu->user_email );
272 } elseif ( ! empty( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) {
273 $my_hash = md5( filter_var( wp_unslash( $_COOKIE[ 'comment_author_email_' . COOKIEHASH ] ) ) );
274 } else {
275 $my_hash = '';
276 }
277 wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) );
278 }
279 }
280 /**
281 * Add hovercards on Discussion settings panel.
282 */
283 function grofiles_attach_cards_forced() {
284 add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' );
285 grofiles_attach_cards();
286 }
287 /**
288 * Set hovercards as enabled on Discussion settings panel.
289 */
290 function grofiles_force_gravatar_enable_hovercards() {
291 return 'enabled';
292 }
293 /**
294 * Add script to admin footer on Discussion settings panel.
295 */
296 function grofiles_admin_cards_forced() {
297 add_action( 'admin_footer', 'grofiles_attach_cards_forced' );
298 }
299 /**
300 * Add script to admin footer.
301 */
302 function grofiles_admin_cards() {
303 add_action( 'admin_footer', 'grofiles_attach_cards' );
304 }
305 /**
306 * Dequeue the FE assets when there are no gravatars on the page to be displayed.
307 */
308 function grofiles_extra_data() {
309 $authors = grofiles_gravatars_to_append();
310
311 if ( ! $authors ) {
312 wp_dequeue_script( 'grofiles-cards' );
313 wp_dequeue_script( 'wpgroho' );
314 } else {
315 ?>
316 <div style="display:none">
317 <?php
318 foreach ( $authors as $author ) {
319 grofiles_hovercards_data_html( $author );
320 }
321 ?>
322 </div>
323 <?php
324 }
325 }
326
327 /**
328 * Echoes the data from grofiles_hovercards_data() as HTML elements.
329 *
330 * @since 5.5.0 Add support for a passed WP_User object
331 *
332 * @param int|string|WP_User $author User ID, email address, or a WP_User object.
333 */
334 function grofiles_hovercards_data_html( $author ) {
335 $data = grofiles_hovercards_data( $author );
336 $hash = '';
337 if ( is_numeric( $author ) ) {
338 $user = get_userdata( $author );
339 if ( $user ) {
340 $hash = md5( $user->user_email );
341 }
342 } elseif ( is_email( $author ) ) {
343 $hash = md5( $author );
344 } elseif ( is_a( $author, 'WP_User' ) ) {
345 $hash = md5( $author->user_email );
346 }
347
348 if ( ! $hash ) {
349 return;
350 }
351 ?>
352 <div class="grofile-hash-map-<?php echo esc_attr( $hash ); ?>">
353 <?php foreach ( $data as $key => $value ) : ?>
354 <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span>
355 <?php endforeach; ?>
356 </div>
357 <?php
358 }
359
360 /* API */
361
362 /**
363 * Returns the PHP callbacks for data sources.
364 *
365 * 'grofiles_hovercards_data_callbacks' filter
366 *
367 * @return array( data_key => data_callback, ... )
368 */
369 function grofiles_hovercards_data_callbacks() {
370 /**
371 * Filter the Gravatar Hovercard PHP callbacks.
372 *
373 * @module gravatar-hovercards
374 *
375 * @since 1.1.0
376 *
377 * @param array $args Array of data callbacks.
378 */
379 return apply_filters( 'grofiles_hovercards_data_callbacks', array() );
380 }
381
382 /**
383 * Keyed JSON object containing all profile data provided by registered callbacks
384 *
385 * @param int|strung $author User ID or email address.
386 *
387 * @return array( data_key => data, ... )
388 */
389 function grofiles_hovercards_data( $author ) {
390 $r = array();
391 foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) {
392 if ( ! is_callable( $callback ) ) {
393 continue;
394 }
395 $data = call_user_func( $callback, $author, $key );
396 if ( $data !== null ) {
397 $r[ $key ] = $data;
398 }
399 }
400
401 return $r;
402 }
403