/modules/widgets/gravatar-profile.php

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