PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/widgets/gravatar-profile.php

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