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"> 
  84. <?php echo esc_html( 
  85. /** 
  86. * Filter the Gravatar Profile widget's profile link title. 
  87. * @module widgets 
  88. * @since 2.8.0 
  89. * @param string $str Profile link title. 
  90. */ 
  91. apply_filters( 
  92. 'jetpack_gravatar_full_profile_title',  
  93. __( 'View Full Profile →', 'jetpack' ) 
  94. ); ?> 
  95. </a></p> 
  96.  
  97. <?php 
  98.  
  99. /** 
  100. * Fires when an item is displayed on the frontend. 
  101. * Can be used to track stats about the number of displays for a specific item 
  102. * @module widgets, shortcodes 
  103. * @since 1.6.0 
  104. * @param string widget Item type (e.g. widget, or embed). 
  105. * @param string grofile Item description (e.g. grofile, goodreads). 
  106. */ 
  107. do_action( 'jetpack_stats_extra', 'widget', 'grofile' ); 
  108.  
  109. } else { 
  110. if ( current_user_can( 'edit_theme_options' ) ) { 
  111. echo '<p>' . esc_html__( 'Error loading profile', 'jetpack' ) . '</p>'; 
  112.  
  113. echo $args['after_widget']; 
  114.  
  115. function display_personal_links( $personal_links = array() ) { 
  116. if ( empty( $personal_links ) ) 
  117. return; 
  118. ?> 
  119.  
  120. <h4><?php echo esc_html( 
  121. apply_filters( 
  122. /** 
  123. * Filter the Gravatar Profile widget's "Personal Links" section title. 
  124. * @module widgets 
  125. * @since 2.8.0 
  126. * @param string $str "Personal Links" section title. 
  127. */ 
  128. 'jetpack_gravatar_personal_links_title',  
  129. __( 'Personal Links', 'jetpack' ) 
  130. ); ?></h4> 
  131. <ul class="grofile-urls grofile-links"> 
  132.  
  133. <?php foreach( $personal_links as $personal_link ) : ?> 
  134. <li> 
  135. <a href="<?php echo esc_url( $personal_link['value'] ); ?>"> 
  136. <?php 
  137. $link_title = ( ! empty( $personal_link['title'] ) ) ? $personal_link['title'] : $personal_link['value']; 
  138. echo esc_html( $link_title ); 
  139. ?> 
  140. </a> 
  141. </li> 
  142. <?php endforeach; ?> 
  143. </ul> 
  144.  
  145. <?php 
  146.  
  147. function display_accounts( $accounts = array() ) { 
  148. if ( empty( $accounts ) ) 
  149. return; 
  150. ?> 
  151.  
  152. <h4><?php echo esc_html( 
  153. /** 
  154. * Filter the Gravatar Profile widget's "Verified Services" section title. 
  155. * @module widgets 
  156. * @since 2.8.0 
  157. * @param string $str "Verified Services" section title. 
  158. */ 
  159. apply_filters( 
  160. 'jetpack_gravatar_verified_services_title',  
  161. __( 'Verified Services', 'jetpack' ) 
  162. ); ?></h4> 
  163. <ul class="grofile-urls grofile-accounts"> 
  164.  
  165. <?php foreach( $accounts as $account ) : 
  166. if( $account['verified'] != 'true' ) 
  167. continue; 
  168.  
  169. $sanitized_service_name = $this->get_sanitized_service_name( $account['shortname'] ); 
  170. ?> 
  171.  
  172. <li> 
  173. <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 ) ); ?>"> 
  174. <span class="grofile-accounts-logo grofile-accounts-<?php echo esc_attr( $account['shortname'] ); ?> accounts_<?php echo esc_attr( $account['shortname'] ); ?>"></span> 
  175. </a> 
  176. </li> 
  177.  
  178. <?php endforeach; ?> 
  179. </ul> 
  180.  
  181. <?php 
  182.  
  183. function form( $instance ) { 
  184.  
  185. $title = isset( $instance['title'] ) ? $instance['title'] : ''; 
  186. $email = isset( $instance['email'] ) ? $instance['email'] : ''; 
  187. $email_user = isset( $instance['email_user'] ) ? $instance['email_user'] : get_current_user_id(); 
  188. $show_personal_links = isset( $instance['show_personal_links'] ) ? (bool) $instance['show_personal_links'] : ''; 
  189. $show_account_links = isset( $instance['show_account_links'] ) ? (bool) $instance['show_account_links'] : ''; 
  190.  
  191. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { 
  192. $profile_url = admin_url( 'profile.php' ); 
  193. } else { 
  194. $profile_url = 'https://gravatar.com/profile/edit'; 
  195.  
  196. ?> 
  197. <p> 
  198. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  199. <?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 ); ?>" /> 
  200. </label> 
  201. </p> 
  202.  
  203. <p> 
  204. <label for="<?php echo $this->get_field_id( 'email_user' ); ?>"> 
  205. <?php esc_html_e( 'Select a user or pick "custom" and enter a custom email address.', 'jetpack' ); ?> 
  206. <br /> 
  207.  
  208. <?php wp_dropdown_users( array( 
  209. 'show_option_none' => __( 'Custom', 'jetpack' ),  
  210. 'selected' => $email_user,  
  211. 'name' => $this->get_field_name( 'email_user' ),  
  212. 'id' => $this->get_field_id( 'email_user' ),  
  213. 'class' => 'gravatar-profile-user-select',  
  214. ) );?> 
  215. </label> 
  216. </p> 
  217.  
  218. <p class="gprofile-email-container <?php echo empty( $email_user ) || $email_user == -1 ? '' : 'hidden'; ?>"> 
  219. <label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php esc_html_e( 'Custom Email Address', 'jetpack' ); ?> 
  220. <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 ); ?>" /> 
  221. </label> 
  222. </p> 
  223.  
  224. <p> 
  225. <label for="<?php echo $this->get_field_id( 'show_personal_links' ); ?>"> 
  226. <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 ); ?> /> 
  227. <?php esc_html_e( 'Show Personal Links', 'jetpack' ); ?> 
  228. <br /> 
  229. <small><?php esc_html_e( 'Links to your websites, blogs, or any other sites that help describe who you are.', 'jetpack' ); ?></small> 
  230. </label> 
  231. </p> 
  232.  
  233. <p> 
  234. <label for="<?php echo $this->get_field_id( 'show_account_links' ); ?>"> 
  235. <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 ); ?> /> 
  236. <?php esc_html_e( 'Show Account Links', 'jetpack' ); ?> 
  237. <br /> 
  238. <small><?php esc_html_e( 'Links to services that you use across the web.', 'jetpack' ); ?></small> 
  239. </label> 
  240. </p> 
  241.  
  242. <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> 
  243.  
  244. <?php 
  245.  
  246. function admin_script() { 
  247. ?> 
  248. <script> 
  249. jQuery( function( $ ) { 
  250. $( '.wrap' ).on( 'change', '.gravatar-profile-user-select', function() { 
  251. var $input = $(this).closest('.widget-inside').find('.gprofile-email-container'); 
  252. if ( '-1' === this.value.toLowerCase() ) { 
  253. $input.show(); 
  254. } else { 
  255. $input.hide(); 
  256. }); 
  257. } ); 
  258. </script> 
  259. <?php 
  260.  
  261. function update( $new_instance, $old_instance ) { 
  262.  
  263. $instance = array(); 
  264.  
  265. $instance['title'] = isset( $new_instance['title'] ) ? wp_kses( $new_instance['title'], array() ) : ''; 
  266. $instance['email'] = isset( $new_instance['email'] ) ? wp_kses( $new_instance['email'], array() ) : ''; 
  267. $instance['email_user'] = isset( $new_instance['email_user'] ) ? intval( $new_instance['email_user'] ) : -1; 
  268. $instance['show_personal_links'] = isset( $new_instance['show_personal_links'] ) ? (bool) $new_instance['show_personal_links'] : false; 
  269. $instance['show_account_links'] = isset( $new_instance['show_account_links'] ) ? (bool) $new_instance['show_account_links'] : false; 
  270.  
  271. if ( $instance['email_user'] > 0 ) { 
  272. $user = get_userdata( $instance['email_user'] ); 
  273. $instance['email'] = $user->user_email; 
  274.  
  275. $hashed_email = md5( strtolower( trim( $instance['email'] ) ) ); 
  276. $cache_key = 'grofile-' . $hashed_email; 
  277. delete_transient( $cache_key ); 
  278.  
  279. return $instance; 
  280.  
  281. private function get_profile( $email ) { 
  282. $hashed_email = md5( strtolower( trim( $email ) ) ); 
  283. $cache_key = 'grofile-' . $hashed_email; 
  284.  
  285. if( ! $profile = get_transient( $cache_key ) ) { 
  286. $profile_url = esc_url_raw( sprintf( '%s.gravatar.com/%s.json', ( is_ssl() ? 'https://secure' : 'http://www' ), $hashed_email ), array( 'http', 'https' ) ); 
  287.  
  288. $expire = 300; 
  289. $response = wp_remote_get( $profile_url, array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' ) ); 
  290. $response_code = wp_remote_retrieve_response_code( $response ); 
  291. if ( 200 == $response_code ) { 
  292. $profile = wp_remote_retrieve_body( $response ); 
  293. $profile = json_decode( $profile, true ); 
  294.  
  295. if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) { 
  296. $expire = 900; // cache for 15 minutes 
  297. $profile = $profile['entry'][0]; 
  298. } else { 
  299. // Something strange happened. Cache for 5 minutes. 
  300. $profile = array(); 
  301.  
  302. } else { 
  303. $expire = 900; // cache for 15 minutes 
  304. $profile = array(); 
  305.  
  306. set_transient( $cache_key, $profile, $expire ); 
  307. return $profile; 
  308.  
  309. private function get_sanitized_service_name( $shortname ) { 
  310. // Some services have stylized or mixed cap names *cough* WP *cough* 
  311. switch( $shortname ) { 
  312. case 'friendfeed': 
  313. return 'FriendFeed'; 
  314. case 'linkedin': 
  315. return 'LinkedIn'; 
  316. case 'yahoo': 
  317. return 'Yahoo!'; 
  318. case 'youtube': 
  319. return 'YouTube'; 
  320. case 'wordpress': 
  321. return 'WordPress'; 
  322. case 'tripit': 
  323. return 'TripIt'; 
  324. case 'myspace': 
  325. return 'MySpace'; 
  326. case 'foursquare': 
  327. return 'foursquare'; 
  328. case 'google': 
  329. return 'Google+'; 
  330. default: 
  331. // Others don't 
  332. $shortname = ucwords( $shortname ); 
  333. return $shortname;