説明なし

gravatar-profile.php 13KB

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