PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.6.2
Jetpack – WP Security, Backup, Speed, & Growth v7.6.2
16.3-a.1 16.2 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 All 503 releases
jetpack / modules / widgets / gravatar-profile.php

gravatar-profile.php in Jetpack – WP Security, Backup, Speed, & Growth 7.6.2, at modules/widgets/gravatar-profile.php

436 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Register the widget for use in Appearance -> Widgets
5 */
6 add_action( 'widgets_init', 'jetpack_gravatar_profile_widget_init' );
7
8 function jetpack_gravatar_profile_widget_init() {
9 register_widget( 'Jetpack_Gravatar_Profile_Widget' );
10 }
11
12 /**
13 * Display a widgetized version of your Gravatar Profile
14 * http://blog.gravatar.com/2010/03/26/gravatar-profiles/
15 */
16 class Jetpack_Gravatar_Profile_Widget extends WP_Widget {
17
18 function __construct() {
19 parent::__construct(
20 'grofile',
21 /** This filter is documented in modules/widgets/facebook-likebox.php */
22 apply_filters( 'jetpack_widget_name', __( 'Gravatar Profile', 'jetpack' ) ),
23 array(
24 'classname' => 'widget-grofile grofile',
25 'description' => __( 'Display a mini version of your Gravatar Profile', 'jetpack' ),
26 'customize_selective_refresh' => true,
27 )
28 );
29
30 if ( is_admin() ) {
31 add_action( 'admin_footer-widgets.php', array( $this, 'admin_script' ) );
32 }
33
34 if ( is_customize_preview() ) {
35 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
36 }
37 }
38
39 function widget( $args, $instance ) {
40 /**
41 * Fires when an item is displayed on the front end.
42 *
43 * Can be used to track stats about the number of displays for a specific item
44 *
45 * @module widgets, shortcodes
46 *
47 * @since 1.6.0
48 *
49 * @param string widget_view Item type (e.g. widget, or embed).
50 * @param string grofile Item description (e.g. grofile, goodreads).
51 */
52 do_action( 'jetpack_stats_extra', 'widget_view', 'grofile' );
53
54 $instance = wp_parse_args(
55 $instance, array(
56 'title' => '',
57 'email' => '',
58 )
59 );
60
61 /** This filter is documented in core/src/wp-includes/default-widgets.php */
62 $title = apply_filters( 'widget_title', $instance['title'] );
63
64 if ( ! $instance['email'] ) {
65 if ( current_user_can( 'edit_theme_options' ) ) {
66 echo $args['before_widget'];
67 if ( ! empty( $title ) ) {
68 echo $args['before_title'] . $title . $args['after_title'];
69 }
70 echo '<p>' . sprintf( __( 'You need to select what to show in this <a href="%s">Gravatar Profile widget</a>.', 'jetpack' ), admin_url( 'widgets.php' ) ) . '</p>';
71 echo $args['after_widget'];
72 }
73 return;
74 }
75
76 echo $args['before_widget'];
77 if ( ! empty( $title ) ) {
78 echo $args['before_title'] . $title . $args['after_title'];
79 }
80
81 $profile = $this->get_profile( $instance['email'] );
82
83 if ( ! empty( $profile ) ) {
84 $profile = wp_parse_args(
85 $profile, array(
86 'thumbnailUrl' => '',
87 'profileUrl' => '',
88 'displayName' => '',
89 'aboutMe' => '',
90 'urls' => array(),
91 'accounts' => array(),
92 )
93 );
94 $gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // the default grav returned by grofiles is super small
95
96 // Enqueue front end assets.
97 $this->enqueue_scripts();
98
99 ?>
100 <img src="<?php echo esc_url( $gravatar_url ); ?>" class="grofile-thumbnail no-grav" alt="<?php echo esc_attr( $profile['displayName'] ); ?>" />
101 <div class="grofile-meta">
102 <h4><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>"><?php echo esc_html( $profile['displayName'] ); ?></a></h4>
103 <p><?php echo wp_kses_post( $profile['aboutMe'] ); ?></p>
104 </div>
105
106 <?php
107
108 if ( $instance['show_personal_links'] ) {
109 $this->display_personal_links( (array) $profile['urls'] );
110 }
111
112 if ( $instance['show_account_links'] ) {
113 $this->display_accounts( (array) $profile['accounts'] );
114 }
115
116 ?>
117
118 <p><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>" class="grofile-full-link">
119 <?php
120 echo esc_html(
121 /**
122 * Filter the Gravatar Profile widget's profile link title.
123 *
124 * @module widgets
125 *
126 * @since 2.8.0
127 *
128 * @param string $str Profile link title.
129 */
130 apply_filters(
131 'jetpack_gravatar_full_profile_title',
132 __( 'View Full Profile &rarr;', 'jetpack' )
133 )
134 );
135 ?>
136 </a></p>
137
138 <?php
139 } else {
140 if ( current_user_can( 'edit_theme_options' ) ) {
141 echo '<p>' . esc_html__( 'Error loading profile', 'jetpack' ) . '</p>';
142 }
143 }
144
145 echo $args['after_widget'];
146 }
147
148 function display_personal_links( $personal_links = array() ) {
149 if ( empty( $personal_links ) ) {
150 return;
151 }
152 ?>
153
154 <h4>
155 <?php
156 echo esc_html(
157 apply_filters(
158 /**
159 * Filter the Gravatar Profile widget's "Personal Links" section title.
160 *
161 * @module widgets
162 *
163 * @since 2.8.0
164 *
165 * @param string $str "Personal Links" section title.
166 */
167 'jetpack_gravatar_personal_links_title',
168 __( 'Personal Links', 'jetpack' )
169 )
170 );
171 ?>
172 </h4>
173 <ul class="grofile-urls grofile-links">
174
175 <?php foreach ( $personal_links as $personal_link ) : ?>
176 <li>
177 <a href="<?php echo esc_url( $personal_link['value'] ); ?>">
178 <?php
179 $link_title = ( ! empty( $personal_link['title'] ) ) ? $personal_link['title'] : $personal_link['value'];
180 echo esc_html( $link_title );
181 ?>
182 </a>
183 </li>
184 <?php endforeach; ?>
185 </ul>
186
187 <?php
188 }
189
190 function display_accounts( $accounts = array() ) {
191 if ( empty( $accounts ) ) {
192 return;
193 }
194 ?>
195
196 <h4>
197 <?php
198 echo esc_html(
199 /**
200 * Filter the Gravatar Profile widget's "Verified Services" section title.
201 *
202 * @module widgets
203 *
204 * @since 2.8.0
205 *
206 * @param string $str "Verified Services" section title.
207 */
208 apply_filters(
209 'jetpack_gravatar_verified_services_title',
210 __( 'Verified Services', 'jetpack' )
211 )
212 );
213 ?>
214 </h4>
215 <ul class="grofile-urls grofile-accounts">
216
217 <?php
218 foreach ( $accounts as $account ) :
219 if ( $account['verified'] != 'true' ) {
220 continue;
221 }
222
223 $sanitized_service_name = $this->get_sanitized_service_name( $account['shortname'] );
224 ?>
225
226 <li>
227 <a href="<?php echo esc_url( $account['url'] ); ?>" title="<?php echo sprintf( _x( '%1$s on %2$s', '1: User Name, 2: Service Name (Facebook, Twitter, ...)', 'jetpack' ), esc_html( $account['display'] ), esc_html( $sanitized_service_name ) ); ?>">
228 <span class="grofile-accounts-logo grofile-accounts-<?php echo esc_attr( $account['shortname'] ); ?> accounts_<?php echo esc_attr( $account['shortname'] ); ?>"></span>
229 </a>
230 </li>
231
232 <?php endforeach; ?>
233 </ul>
234
235 <?php
236 }
237
238 /**
239 * Enqueue CSS and JavaScript.
240 *
241 * @since 4.0.0
242 */
243 function enqueue_scripts() {
244 wp_enqueue_style(
245 'gravatar-profile-widget',
246 plugins_url( 'gravatar-profile.css', __FILE__ ),
247 array(),
248 '20120711'
249 );
250
251 wp_enqueue_style(
252 'gravatar-card-services',
253 'https://secure.gravatar.com/css/services.css',
254 array(),
255 defined( 'GROFILES__CACHE_BUSTER' ) ? GROFILES__CACHE_BUSTER : gmdate( 'YW' )
256 );
257 }
258
259 function form( $instance ) {
260 $title = isset( $instance['title'] ) ? $instance['title'] : '';
261 $email = isset( $instance['email'] ) ? $instance['email'] : '';
262 $email_user = isset( $instance['email_user'] ) ? $instance['email_user'] : get_current_user_id();
263 $show_personal_links = isset( $instance['show_personal_links'] ) ? (bool) $instance['show_personal_links'] : '';
264 $show_account_links = isset( $instance['show_account_links'] ) ? (bool) $instance['show_account_links'] : '';
265 $profile_url = 'https://gravatar.com/profile/edit';
266
267 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
268 $profile_url = admin_url( 'profile.php' );
269
270 if ( isset( $_REQUEST['calypso'] ) ) {
271 $profile_url = 'https://wordpress.com/me';
272 }
273 }
274 ?>
275 <p>
276 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
277 <?php esc_html_e( 'Title', 'jetpack' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
278 </label>
279 </p>
280
281 <p>
282 <label for="<?php echo $this->get_field_id( 'email_user' ); ?>">
283 <?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'jetpack' ); ?>
284 <br />
285
286 <?php
287 wp_dropdown_users(
288 array(
289 'show_option_none' => __( 'Custom', 'jetpack' ),
290 'selected' => $email_user,
291 'name' => $this->get_field_name( 'email_user' ),
292 'id' => $this->get_field_id( 'email_user' ),
293 'class' => 'gravatar-profile-user-select',
294 )
295 );
296 ?>
297 </label>
298 </p>
299
300 <p class="gprofile-email-container <?php echo empty( $email_user ) || $email_user == -1 ? '' : 'hidden'; ?>">
301 <label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?>
302 <input class="widefat" id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" />
303 </label>
304 </p>
305
306 <p>
307 <label for="<?php echo $this->get_field_id( 'show_personal_links' ); ?>">
308 <input type="checkbox" name="<?php echo $this->get_field_name( 'show_personal_links' ); ?>" id="<?php echo $this->get_field_id( 'show_personal_links' ); ?>" <?php checked( $show_personal_links ); ?> />
309 <?php esc_html_e( 'Show Personal Links', 'jetpack' ); ?>
310 <br />
311 <small><?php esc_html_e( 'Links to your websites, blogs, or any other sites that help describe who you are.', 'jetpack' ); ?></small>
312 </label>
313 </p>
314
315 <p>
316 <label for="<?php echo $this->get_field_id( 'show_account_links' ); ?>">
317 <input type="checkbox" name="<?php echo $this->get_field_name( 'show_account_links' ); ?>" id="<?php echo $this->get_field_id( 'show_account_links' ); ?>" <?php checked( $show_account_links ); ?> />
318 <?php esc_html_e( 'Show Account Links', 'jetpack' ); ?>
319 <br />
320 <small><?php esc_html_e( 'Links to services that you use across the web.', 'jetpack' ); ?></small>
321 </label>
322 </p>
323
324 <p><a href="<?php echo esc_url( $profile_url ); ?>" target="_blank" title="<?php esc_attr_e( 'Opens in new window', 'jetpack' ); ?>"><?php esc_html_e( 'Edit Your Profile', 'jetpack' ); ?></a> | <a href="https://gravatar.com" target="_blank" title="<?php esc_attr_e( 'Opens in new window', 'jetpack' ); ?>"><?php esc_html_e( "What's a Gravatar?", 'jetpack' ); ?></a></p>
325
326 <?php
327 }
328
329 function admin_script() {
330 ?>
331 <script>
332 jQuery( function( $ ) {
333 $( '.wrap' ).on( 'change', '.gravatar-profile-user-select', function() {
334 var $input = $(this).closest('.widget-inside').find('.gprofile-email-container');
335 if ( '-1' === this.value.toLowerCase() ) {
336 $input.show();
337 } else {
338 $input.hide();
339 }
340 });
341 } );
342 </script>
343 <?php
344 }
345
346 function update( $new_instance, $old_instance ) {
347
348 $instance = array();
349
350 $instance['title'] = isset( $new_instance['title'] ) ? wp_kses( $new_instance['title'], array() ) : '';
351 $instance['email'] = isset( $new_instance['email'] ) ? wp_kses( $new_instance['email'], array() ) : '';
352 $instance['email_user'] = isset( $new_instance['email_user'] ) ? intval( $new_instance['email_user'] ) : -1;
353 $instance['show_personal_links'] = isset( $new_instance['show_personal_links'] ) ? (bool) $new_instance['show_personal_links'] : false;
354 $instance['show_account_links'] = isset( $new_instance['show_account_links'] ) ? (bool) $new_instance['show_account_links'] : false;
355
356 if ( $instance['email_user'] > 0 ) {
357 $user = get_userdata( $instance['email_user'] );
358 $instance['email'] = $user->user_email;
359 }
360
361 $hashed_email = md5( strtolower( trim( $instance['email'] ) ) );
362 $cache_key = 'grofile-' . $hashed_email;
363 delete_transient( $cache_key );
364
365 return $instance;
366 }
367
368 private function get_profile( $email ) {
369 $hashed_email = md5( strtolower( trim( $email ) ) );
370 $cache_key = 'grofile-' . $hashed_email;
371
372 if ( ! $profile = get_transient( $cache_key ) ) {
373 $profile_url = sprintf(
374 'https://secure.gravatar.com/%s.json',
375 $hashed_email
376 );
377
378 $expire = 300;
379 $response = wp_remote_get(
380 esc_url_raw( $profile_url ),
381 array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' )
382 );
383 $response_code = wp_remote_retrieve_response_code( $response );
384 if ( 200 == $response_code ) {
385 $profile = wp_remote_retrieve_body( $response );
386 $profile = json_decode( $profile, true );
387
388 if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) {
389 $expire = 900; // cache for 15 minutes
390 $profile = $profile['entry'][0];
391 } else {
392 // Something strange happened. Cache for 5 minutes.
393 $profile = array();
394 }
395 } else {
396 $expire = 900; // cache for 15 minutes
397 $profile = array();
398 }
399
400 set_transient( $cache_key, $profile, $expire );
401 }
402 return $profile;
403 }
404
405 private function get_sanitized_service_name( $shortname ) {
406 // Some services have stylized or mixed cap names *cough* WP *cough*
407 switch ( $shortname ) {
408 case 'friendfeed':
409 return 'FriendFeed';
410 case 'linkedin':
411 return 'LinkedIn';
412 case 'yahoo':
413 return 'Yahoo!';
414 case 'youtube':
415 return 'YouTube';
416 // phpcs:ignore WordPress.WP.CapitalPDangit
417 case 'wordpress':
418 return 'WordPress';
419 case 'tripit':
420 return 'TripIt';
421 case 'myspace':
422 return 'MySpace';
423 case 'foursquare':
424 return 'foursquare';
425 case 'google':
426 return 'Google+';
427 default:
428 // Others don't
429 $shortname = ucwords( $shortname );
430 }
431 return $shortname;
432 }
433 }
434
435 // END
436