Ei kuvausta

gravatar.php 4.7KB

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