Jetpack_Gravatar_Profile_Widget

Display a widgetized version of your Gravatar Profile http://blog.gravatar.com/2010/03/26/gravatar-profiles/.

Defined (1)

The class is defined in the following location(s).

/modules/widgets/gravatar-profile.php  
  1. class Jetpack_Gravatar_Profile_Widget extends WP_Widget { 
  2.  
  3. function __construct() { 
  4. parent::__construct( 
  5. 'grofile',  
  6. /** This filter is documented in modules/widgets/facebook-likebox.php */ 
  7. apply_filters( 'jetpack_widget_name', __( 'Gravatar Profile', 'jetpack' ) ),  
  8. array( 
  9. 'classname' => 'widget-grofile grofile',  
  10. 'description' => __( 'Display a mini version of your Gravatar Profile', 'jetpack' ) 
  11. ); 
  12.  
  13. if ( is_admin() ) { 
  14. add_action( 'admin_footer-widgets.php', array( $this, 'admin_script' ) ); 
  15.  
  16. function widget( $args, $instance ) { 
  17.  
  18. $instance = wp_parse_args( $instance, array( 
  19. 'title' => '',  
  20. 'email' => '' 
  21. ) ); 
  22.  
  23. /** This filter is documented in core/src/wp-includes/default-widgets.php */ 
  24. $title = apply_filters( 'widget_title', $instance['title'] ); 
  25.  
  26. if ( !$instance['email'] ) { 
  27. if ( current_user_can( 'edit_theme_options' ) ) { 
  28. echo $args['before_widget']; 
  29. if ( ! empty( $title ) ) 
  30. echo $args['before_title'] . $title . $args['after_title']; 
  31. 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>'; 
  32. echo $args['after_widget']; 
  33. return; 
  34.  
  35. echo $args['before_widget']; 
  36. if ( ! empty( $title ) ) 
  37. echo $args['before_title'] . $title . $args['after_title']; 
  38.  
  39. $profile = $this->get_profile( $instance['email'] ); 
  40.  
  41. if( ! empty( $profile ) ) { 
  42. $profile = wp_parse_args( $profile, array( 
  43. 'thumbnailUrl' => '',  
  44. 'profileUrl' => '',  
  45. 'displayName' => '',  
  46. 'aboutMe' => '',  
  47. 'urls' => array(),  
  48. 'accounts' => array(),  
  49. ) ); 
  50. $gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // the default grav returned by grofiles is super small 
  51.  
  52. wp_enqueue_style( 
  53. 'gravatar-profile-widget',  
  54. plugins_url( 'gravatar-profile.css', __FILE__ ),  
  55. array(),  
  56. '20120711' 
  57. ); 
  58.  
  59. wp_enqueue_style( 
  60. 'gravatar-card-services',  
  61. is_ssl() ? 'https://secure.gravatar.com/css/services.css' : 'http://s.gravatar.com/css/services.css',  
  62. array(),  
  63. defined( 'GROFILES__CACHE_BUSTER' ) ? GROFILES__CACHE_BUSTER : gmdate( 'YW' ) 
  64. ); 
  65.  
  66. ?> 
  67. <img src="<?php echo esc_url( $gravatar_url ); ?>" class="grofile-thumbnail no-grav" alt="<?php echo esc_attr( $profile['displayName'] ); ?>" /> 
  68. <div class="grofile-meta"> 
  69. <h4><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>"><?php echo esc_html( $profile['displayName'] ); ?></a></h4> 
  70. <p><?php echo wp_kses_post( $profile['aboutMe'] ); ?></p> 
  71. </div> 
  72.  
  73. <?php 
  74.  
  75. if( $instance['show_personal_links'] ) 
  76. $this->display_personal_links( (array) $profile['urls'] ); 
  77.  
  78. if( $instance['show_account_links'] ) 
  79. $this->display_accounts( (array) $profile['accounts'] ); 
  80.  
  81. ?> 
  82.  
  83. <p><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>" class="grofile-full-link"><?php echo esc_html( apply_filters( 'jetpack_gravatar_full_profile_title', __( 'View Full Profile →', 'jetpack' ) ) ); ?></a></p> 
  84.  
  85. <?php 
  86.  
  87. do_action( 'jetpack_stats_extra', 'widget', 'grofile' ); 
  88.  
  89. } else { 
  90. if ( current_user_can( 'edit_theme_options' ) ) { 
  91. echo '<p>' . esc_html__( 'Error loading profile', 'jetpack' ) . '</p>'; 
  92.  
  93. echo $args['after_widget']; 
  94.  
  95. function display_personal_links( $personal_links = array() ) { 
  96. if ( empty( $personal_links ) ) 
  97. return; 
  98. ?> 
  99.  
  100. <h4><?php echo esc_html( apply_filters( 'jetpack_gravatar_personal_links_title', __( 'Personal Links', 'jetpack' ) ) ); ?></h4> 
  101. <ul class="grofile-urls grofile-links"> 
  102.  
  103. <?php foreach( $personal_links as $personal_link ) : ?> 
  104. <li> 
  105. <a href="<?php echo esc_url( $personal_link['value'] ); ?>"> 
  106. <?php 
  107. $link_title = ( ! empty( $personal_link['title'] ) ) ? $personal_link['title'] : $personal_link['value']; 
  108. echo esc_html( $link_title ); 
  109. ?> 
  110. </a> 
  111. </li> 
  112. <?php endforeach; ?> 
  113. </ul> 
  114.  
  115. <?php 
  116.  
  117. function display_accounts( $accounts = array() ) { 
  118. if ( empty( $accounts ) ) 
  119. return; 
  120. ?> 
  121.  
  122. <h4><?php echo esc_html( apply_filters( 'jetpack_gravatar_verified_services_title', __( 'Verified Services', 'jetpack' ) ) ); ?></h4> 
  123. <ul class="grofile-urls grofile-accounts"> 
  124.  
  125. <?php foreach( $accounts as $account ) : 
  126. if( $account['verified'] != 'true' ) 
  127. continue; 
  128.  
  129. $sanitized_service_name = $this->get_sanitized_service_name( $account['shortname'] ); 
  130. ?> 
  131.  
  132. <li> 
  133. <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 ) ); ?>"> 
  134. <span class="grofile-accounts-logo grofile-accounts-<?php echo esc_attr( $account['shortname'] ); ?> accounts_<?php echo esc_attr( $account['shortname'] ); ?>"></span> 
  135. </a> 
  136. </li> 
  137.  
  138. <?php endforeach; ?> 
  139. </ul> 
  140.  
  141. <?php 
  142.  
  143. function form( $instance ) { 
  144.  
  145. $title = isset( $instance['title'] ) ? $instance['title'] : ''; 
  146. $email = isset( $instance['email'] ) ? $instance['email'] : ''; 
  147. $email_user = isset( $instance['email_user'] ) ? $instance['email_user'] : get_current_user_id(); 
  148. $show_personal_links = isset( $instance['show_personal_links'] ) ? (bool) $instance['show_personal_links'] : ''; 
  149. $show_account_links = isset( $instance['show_account_links'] ) ? (bool) $instance['show_account_links'] : ''; 
  150.  
  151. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  152. $profile_url = admin_url( 'profile.php' ); 
  153. } else { 
  154. $profile_url = 'https://gravatar.com/profile/edit'; 
  155.  
  156. ?> 
  157. <p> 
  158. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  159. <?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 ); ?>" /> 
  160. </label> 
  161. </p> 
  162.  
  163. <p> 
  164. <label for="<?php echo $this->get_field_id( 'email_user' ); ?>"> 
  165. <?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'jetpack' ); ?> 
  166. <br /> 
  167.  
  168. <?php wp_dropdown_users( array( 
  169. 'show_option_none' => __( 'Custom', 'jetpack' ),  
  170. 'selected' => $email_user,  
  171. 'name' => $this->get_field_name( 'email_user' ),  
  172. 'id' => $this->get_field_id( 'email_user' ),  
  173. 'class' => 'gravatar-profile-user-select',  
  174. ) );?> 
  175. </label> 
  176. </p> 
  177.  
  178. <p class="gprofile-email-container <?php echo empty( $email_user ) || $email_user == -1 ? '' : 'hidden'; ?>"> 
  179. <label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?> 
  180. <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 ); ?>" /> 
  181. </label> 
  182. </p> 
  183.  
  184. <p> 
  185. <label for="<?php echo $this->get_field_id( 'show_personal_links' ); ?>"> 
  186. <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 ); ?> /> 
  187. <?php esc_html_e( 'Show Personal Links', 'jetpack' ); ?> 
  188. <br /> 
  189. <small><?php esc_html_e( 'Links to your websites, blogs, or any other sites that help describe who you are.', 'jetpack' ); ?></small> 
  190. </label> 
  191. </p> 
  192.  
  193. <p> 
  194. <label for="<?php echo $this->get_field_id( 'show_account_links' ); ?>"> 
  195. <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 ); ?> /> 
  196. <?php esc_html_e( 'Show Account Links', 'jetpack' ); ?> 
  197. <br /> 
  198. <small><?php esc_html_e( 'Links to services that you use across the web.', 'jetpack' ); ?></small> 
  199. </label> 
  200. </p> 
  201.  
  202. <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="http://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> 
  203.  
  204. <?php 
  205.  
  206. function admin_script() { 
  207. ?> 
  208. <script> 
  209. jQuery( function( $ ) { 
  210. $( '.wrap' ).on( 'change', '.gravatar-profile-user-select', function() { 
  211. var $input = $(this).closest('.widget-inside').find('.gprofile-email-container'); 
  212. if ( '-1' === this.value.toLowerCase() ) { 
  213. $input.show(); 
  214. } else { 
  215. $input.hide(); 
  216. }); 
  217. } ); 
  218. </script> 
  219. <?php 
  220.  
  221. function update( $new_instance, $old_instance ) { 
  222.  
  223. $instance = array(); 
  224.  
  225. $instance['title'] = isset( $new_instance['title'] ) ? wp_kses( $new_instance['title'], array() ) : ''; 
  226. $instance['email'] = isset( $new_instance['email'] ) ? wp_kses( $new_instance['email'], array() ) : ''; 
  227. $instance['email_user'] = isset( $new_instance['email_user'] ) ? intval( $new_instance['email_user'] ) : -1; 
  228. $instance['show_personal_links'] = isset( $new_instance['show_personal_links'] ) ? (bool) $new_instance['show_personal_links'] : false; 
  229. $instance['show_account_links'] = isset( $new_instance['show_account_links'] ) ? (bool) $new_instance['show_account_links'] : false; 
  230.  
  231. if ( $instance['email_user'] > 0 ) { 
  232. $user = get_userdata( $instance['email_user'] ); 
  233. $instance['email'] = $user->user_email; 
  234.  
  235. $hashed_email = md5( strtolower( trim( $instance['email'] ) ) ); 
  236. $cache_key = 'grofile-' . $hashed_email; 
  237. delete_transient( $cache_key ); 
  238.  
  239. return $instance; 
  240.  
  241. private function get_profile( $email ) { 
  242. $hashed_email = md5( strtolower( trim( $email ) ) ); 
  243. $cache_key = 'grofile-' . $hashed_email; 
  244.  
  245. if( ! $profile = get_transient( $cache_key ) ) { 
  246. $profile_url = esc_url_raw( sprintf( '%s.gravatar.com/%s.json', ( is_ssl() ? 'https://secure' : 'http://www' ), $hashed_email ), array( 'http', 'https' ) ); 
  247.  
  248. $expire = 300; 
  249. $response = wp_remote_get( $profile_url, array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' ) ); 
  250. $response_code = wp_remote_retrieve_response_code( $response ); 
  251. if ( 200 == $response_code ) { 
  252. $profile = wp_remote_retrieve_body( $response ); 
  253. $profile = json_decode( $profile, true ); 
  254.  
  255. if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) { 
  256. $expire = 900; // cache for 15 minutes 
  257. $profile = $profile['entry'][0]; 
  258. } else { 
  259. // Something strange happened. Cache for 5 minutes. 
  260. $profile = array(); 
  261.  
  262. } else { 
  263. $expire = 900; // cache for 15 minutes 
  264. $profile = array(); 
  265.  
  266. set_transient( $cache_key, $profile, $expire ); 
  267. return $profile; 
  268.  
  269. private function get_sanitized_service_name( $shortname ) { 
  270. // Some services have stylized or mixed cap names *cough* WP *cough* 
  271. switch( $shortname ) { 
  272. case 'friendfeed': 
  273. return 'FriendFeed'; 
  274. case 'linkedin': 
  275. return 'LinkedIn'; 
  276. case 'yahoo': 
  277. return 'Yahoo!'; 
  278. case 'youtube': 
  279. return 'YouTube'; 
  280. case 'wordpress': 
  281. return 'WordPress'; 
  282. case 'tripit': 
  283. return 'TripIt'; 
  284. case 'myspace': 
  285. return 'MySpace'; 
  286. case 'foursquare': 
  287. return 'foursquare'; 
  288. case 'google': 
  289. return 'Google+'; 
  290. default: 
  291. // Others don't 
  292. $shortname = ucwords( $shortname ); 
  293. return $shortname;