PageRenderTime 20ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

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