PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/memuller.web/wp_site
PHP | 405 lines | 285 code | 62 blank | 58 comment | 31 complexity | 10c636d79932e1b33e63f780a606ba9a 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. /** 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. $instance = wp_parse_args( $instance, array(
  34. 'title' => '',
  35. 'email' => ''
  36. ) );
  37. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  38. $title = apply_filters( 'widget_title', $instance['title'] );
  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. }
  47. return;
  48. }
  49. echo $args['before_widget'];
  50. if ( ! empty( $title ) )
  51. echo $args['before_title'] . $title . $args['after_title'];
  52. $profile = $this->get_profile( $instance['email'] );
  53. if( ! empty( $profile ) ) {
  54. $profile = wp_parse_args( $profile, array(
  55. 'thumbnailUrl' => '',
  56. 'profileUrl' => '',
  57. 'displayName' => '',
  58. 'aboutMe' => '',
  59. 'urls' => array(),
  60. 'accounts' => array(),
  61. ) );
  62. $gravatar_url = add_query_arg( 's', 320, $profile['thumbnailUrl'] ); // the default grav returned by grofiles is super small
  63. // Enqueue front end assets.
  64. $this->enqueue_scripts();
  65. ?>
  66. <img src="<?php echo esc_url( $gravatar_url ); ?>" class="grofile-thumbnail no-grav" alt="<?php echo esc_attr( $profile['displayName'] ); ?>" />
  67. <div class="grofile-meta">
  68. <h4><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>"><?php echo esc_html( $profile['displayName'] ); ?></a></h4>
  69. <p><?php echo wp_kses_post( $profile['aboutMe'] ); ?></p>
  70. </div>
  71. <?php
  72. if( $instance['show_personal_links'] )
  73. $this->display_personal_links( (array) $profile['urls'] );
  74. if( $instance['show_account_links'] )
  75. $this->display_accounts( (array) $profile['accounts'] );
  76. ?>
  77. <p><a href="<?php echo esc_url( $profile['profileUrl'] ); ?>" class="grofile-full-link">
  78. <?php echo esc_html(
  79. /**
  80. * Filter the Gravatar Profile widget's profile link title.
  81. *
  82. * @module widgets
  83. *
  84. * @since 2.8.0
  85. *
  86. * @param string $str Profile link title.
  87. */
  88. apply_filters(
  89. 'jetpack_gravatar_full_profile_title',
  90. __( 'View Full Profile &rarr;', 'jetpack' )
  91. )
  92. ); ?>
  93. </a></p>
  94. <?php
  95. /**
  96. * Fires when an item is displayed on the front end.
  97. *
  98. * Can be used to track stats about the number of displays for a specific item
  99. *
  100. * @module widgets, shortcodes
  101. *
  102. * @since 1.6.0
  103. *
  104. * @param string widget Item type (e.g. widget, or embed).
  105. * @param string grofile Item description (e.g. grofile, goodreads).
  106. */
  107. do_action( 'jetpack_stats_extra', 'widget', 'grofile' );
  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. is_ssl() ? 'https://secure.gravatar.com/css/services.css' : 'http://s.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="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>
  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 = esc_url_raw( sprintf( '%s.gravatar.com/%s.json', ( is_ssl() ? 'https://secure' : 'http://www' ), $hashed_email ), array( 'http', 'https' ) );
  294. $expire = 300;
  295. $response = wp_remote_get( $profile_url, array( 'User-Agent' => 'WordPress.com Gravatar Profile Widget' ) );
  296. $response_code = wp_remote_retrieve_response_code( $response );
  297. if ( 200 == $response_code ) {
  298. $profile = wp_remote_retrieve_body( $response );
  299. $profile = json_decode( $profile, true );
  300. if ( is_array( $profile ) && ! empty( $profile['entry'] ) && is_array( $profile['entry'] ) ) {
  301. $expire = 900; // cache for 15 minutes
  302. $profile = $profile['entry'][0];
  303. } else {
  304. // Something strange happened. Cache for 5 minutes.
  305. $profile = array();
  306. }
  307. } else {
  308. $expire = 900; // cache for 15 minutes
  309. $profile = array();
  310. }
  311. set_transient( $cache_key, $profile, $expire );
  312. }
  313. return $profile;
  314. }
  315. private function get_sanitized_service_name( $shortname ) {
  316. // Some services have stylized or mixed cap names *cough* WP *cough*
  317. switch( $shortname ) {
  318. case 'friendfeed':
  319. return 'FriendFeed';
  320. case 'linkedin':
  321. return 'LinkedIn';
  322. case 'yahoo':
  323. return 'Yahoo!';
  324. case 'youtube':
  325. return 'YouTube';
  326. case 'wordpress':
  327. return 'WordPress';
  328. case 'tripit':
  329. return 'TripIt';
  330. case 'myspace':
  331. return 'MySpace';
  332. case 'foursquare':
  333. return 'foursquare';
  334. case 'google':
  335. return 'Google+';
  336. default:
  337. // Others don't
  338. $shortname = ucwords( $shortname );
  339. }
  340. return $shortname;
  341. }
  342. }
  343. // END