gravatar.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Gravatar shortcode for avatar and profile.
  4. *
  5. * Usage:
  6. *
  7. * [gravatar email="user@example.org" size="48"]
  8. * [gravatar_profile who="user@example.org"]
  9. */
  10. add_shortcode( 'gravatar', 'jetpack_gravatar_shortcode' );
  11. add_shortcode( 'gravatar_profile', 'jetpack_gravatar_profile_shortcode' );
  12. /**
  13. * Get gravatar using the email provided at the specified size.
  14. *
  15. * @since 4.5.0
  16. *
  17. * @param array $atts Shortcode attributes.
  18. *
  19. * @return bool|string
  20. */
  21. function jetpack_gravatar_shortcode( $atts ) {
  22. $atts = shortcode_atts( array(
  23. 'email' => '',
  24. 'size' => 96,
  25. ), $atts );
  26. if ( empty( $atts['email'] ) || ! is_email( $atts['email'] ) ) {
  27. return false;
  28. }
  29. $atts['size'] = intval( $atts['size'] );
  30. if ( 0 > $atts['size'] ) {
  31. $atts['size'] = 96;
  32. }
  33. return get_avatar( $atts['email'], $atts['size'] );
  34. }
  35. /**
  36. * Display Gravatar profile
  37. *
  38. * @since 4.5.0
  39. *
  40. * @param array $atts Shortcode attributes.
  41. *
  42. * @uses shortcode_atts()
  43. * @uses get_user_by()
  44. * @uses is_email()
  45. * @uses sanitize_email()
  46. * @uses sanitize_user()
  47. * @uses set_url_scheme()
  48. * @uses wpcom_get_avatar_url()
  49. * @uses get_user_attribute()
  50. * @uses esc_url()
  51. * @uses esc_html()
  52. * @uses _e()
  53. *
  54. * @return string
  55. */
  56. function jetpack_gravatar_profile_shortcode( $atts ) {
  57. // Give each use of the shortcode a unique ID
  58. static $instance = 0;
  59. // Process passed attributes
  60. $atts = shortcode_atts( array(
  61. 'who' => null,
  62. ), $atts, 'jetpack_gravatar_profile' );
  63. // Can specify username, user ID, or email address
  64. if ( is_numeric( $atts['who'] ) ) {
  65. $user = get_user_by( 'id', (int) $atts['who'] );
  66. } elseif ( is_email( $atts['who'] ) ) {
  67. $user = get_user_by( 'email', sanitize_email( $atts['who'] ) );
  68. } elseif ( is_string( $atts['who'] ) ) {
  69. $user = get_user_by( 'login', sanitize_user( $atts['who'] ) );
  70. } else {
  71. $user = false;
  72. }
  73. // Bail if we don't have a user
  74. if ( false === $user ) {
  75. return false;
  76. }
  77. // Render the shortcode
  78. $gravatar_url = set_url_scheme( 'http://gravatar.com/' . $user->user_login );
  79. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  80. $avatar_url = wpcom_get_avatar_url( $user->ID, 96 );
  81. $avatar_url = $avatar_url[0];
  82. $user_location = get_user_attribute( $user->ID, 'location' );
  83. } else {
  84. $avatar_url = get_avatar_url( $user->user_email, array( 'size' => 96 ) );
  85. $user_location = get_user_meta( $user->ID, 'location', true );
  86. }
  87. ob_start();
  88. ?>
  89. <script type="text/javascript">
  90. ( function() {
  91. if ( null === document.getElementById( 'gravatar-profile-embed-styles' ) ) {
  92. var headID = document.getElementsByTagName( 'head' )[0];
  93. var styleNode = document.createElement( 'style' );
  94. styleNode.type = 'text/css';
  95. styleNode.id = 'gravatar-profile-embed-styles';
  96. var gCSS = '.grofile-wrap { border: solid 1px #eee; padding: 10px; } .grofile { padding: 0 0 5px 0; } .grofile-left { float: left; display: block; width: 96px; margin-right: 15px; } .grofile .gravatar { margin-bottom: 5px; } .grofile-clear { clear: left; font-size: 1px; height: 1px; } .grofile ul li a { text-indent: -99999px; } .grofile .grofile-left a:hover { text-decoration: none !important; border: none !important; } .grofile-name { margin-top: 0; }';
  97. if ( document.all ) {
  98. styleNode.innerText = gCSS;
  99. } else {
  100. styleNode.textContent = gCSS;
  101. }
  102. headID.appendChild( styleNode );
  103. }
  104. } )();
  105. </script>
  106. <div class="grofile vcard" id="grofile-embed-<?php echo esc_attr( $instance ); ?>">
  107. <div class="grofile-inner">
  108. <div class="grofile-left">
  109. <div class="grofile-img">
  110. <a href="<?php echo esc_url( $gravatar_url ); ?>">
  111. <img src="<?php echo esc_url( $avatar_url ); ?>" width="96" height="96" class="no-grav gravatar photo" />
  112. </a>
  113. </div>
  114. </div>
  115. <div class="grofile-right">
  116. <p class="grofile-name fn">
  117. <strong><?php echo esc_html( $user->display_name ); ?></strong>
  118. <?php if ( ! empty( $user_location ) ) : ?><br><span class="grofile-location adr"><?php echo esc_html( $user_location ); ?></span><?php endif; ?>
  119. </p>
  120. <p class="grofile-bio"><strong><?php esc_html_e( 'Bio:', 'jetpack' ); ?></strong> <?php echo wp_kses_post( $user->description ); ?></p>
  121. <p class="grofile-view">
  122. <a href="<?php echo esc_url( $gravatar_url ); ?>"><?php esc_html_e( 'View complete profile', 'jetpack' ); ?></a>
  123. </p>
  124. </div>
  125. <span class="grofile-clear">&nbsp;</span>
  126. </div>
  127. </div><?php
  128. // Increment and return the rendered profile
  129. $instance++;
  130. return ob_get_clean();
  131. }