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