gravatar-profile.php 13 KB

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