| @@ -9,8 +9,12 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @package automattic/jetpack |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 13 | 17 | add_shortcode( 'gravatar', 'jetpack_gravatar_shortcode' ); |
| 14 | 18 | add_shortcode( 'gravatar_profile', 'jetpack_gravatar_profile_shortcode' ); |
| 15 | 19 | |
| 16 | 20 | /** |
| @@ -78,9 +82,14 @@ | ||
| 78 | 82 | ); |
| 79 | 83 | |
| 80 | 84 | // Can specify username, user ID, or email address. |
| 81 | 85 | if ( is_numeric( $atts['who'] ) ) { |
| 82 | - $user = get_user_by( 'id', (int) $atts['who'] ); | |
| 86 | + $user_id = (int) $atts['who']; | |
| 87 | + if ( ! is_user_member_of_blog( $user_id, get_current_blog_id() ) ) { | |
| 88 | + // Bail if the user id is not a member of this site. | |
| 89 | + return false; | |
| 90 | + } | |
| 91 | + $user = get_user_by( 'id', $user_id ); | |
| 83 | 92 | } elseif ( is_email( $atts['who'] ) ) { |
| 84 | 93 | $user = get_user_by( 'email', sanitize_email( $atts['who'] ) ); |
| 85 | 94 | } elseif ( is_string( $atts['who'] ) ) { |
| 86 | 95 | $user = get_user_by( 'login', sanitize_user( $atts['who'] ) ); |
| @@ -92,18 +101,60 @@ | ||
| 92 | 101 | if ( false === $user ) { |
| 93 | 102 | return false; |
| 94 | 103 | } |
| 95 | 104 | |
| 105 | + $hashed_email = hash( 'sha256', strtolower( trim( $user->user_email ) ) ); | |
| 106 | + $cache_key = 'jetpack_gravatar_profile_' . $hashed_email; | |
| 107 | + $profile = get_transient( $cache_key ); | |
| 108 | + | |
| 109 | + if ( empty( $profile ) ) { | |
| 110 | + $profile_url = sprintf( | |
| 111 | + 'https://secure.gravatar.com/%s.json', | |
| 112 | + $hashed_email | |
| 113 | + ); | |
| 114 | + | |
| 115 | + $response = wp_remote_get( | |
| 116 | + esc_url_raw( $profile_url ), | |
| 117 | + array( 'User-Agent' => 'Jetpack Plugin Gravatar Profile Shortcode' ) | |
| 118 | + ); | |
| 119 | + $response_code = wp_remote_retrieve_response_code( $response ); | |
| 120 | + $expire = 300; // Cache any errors for 5 minutes. | |
| 121 | + $profile = array(); | |
| 122 | + | |
| 123 | + if ( $response_code === 200 ) { | |
| 124 | + $profile = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 125 | + | |
| 126 | + if ( | |
| 127 | + is_array( $profile ) | |
| 128 | + && ! empty( $profile['entry'] ) | |
| 129 | + && is_array( $profile['entry'] ) | |
| 130 | + ) { | |
| 131 | + // Sucessfully fetched the profile. Cache for 15 minutes. | |
| 132 | + $expire = 900; | |
| 133 | + $profile = $profile['entry'][0]; | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + set_transient( $cache_key, $profile, $expire ); | |
| 138 | + } | |
| 139 | + | |
| 140 | + // Fetching the profile returned an error. Bail. | |
| 141 | + if ( empty( $profile ) ) { | |
| 142 | + return false; | |
| 143 | + } | |
| 144 | + | |
| 96 | 145 | // Render the shortcode. |
| 97 | - $gravatar_url = 'https://gravatar.com/' . $user->user_login; | |
| 146 | + $gravatar_url = 'https://gravatar.com/' . $hashed_email; | |
| 147 | + $user_location = ! empty( $profile['currentLocation'] ) ? $profile['currentLocation'] : ''; | |
| 148 | + $display_name = ! empty( $profile['displayName'] ) ? $profile['displayName'] : ''; | |
| 149 | + $user_description = ! empty( $profile['aboutMe'] ) ? $profile['aboutMe'] : ''; | |
| 98 | 150 | |
| 99 | 151 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 100 | - $avatar_url = wpcom_get_avatar_url( $user->ID, 96 ); | |
| 101 | - $avatar_url = $avatar_url[0]; | |
| 102 | - $user_location = get_user_attribute( $user->ID, 'location' ); | |
| 152 | + $gravatar_url = 'https://gravatar.com/' . $user->user_login; | |
| 153 | + $avatar_url = wpcom_get_avatar_url( $user->ID, 96 ); | |
| 154 | + $avatar_url = $avatar_url[0]; | |
| 103 | 155 | } else { |
| 104 | - $avatar_url = get_avatar_url( $user->user_email, array( 'size' => 96 ) ); | |
| 105 | - $user_location = get_user_meta( $user->ID, 'location', true ); | |
| 156 | + $avatar_url = get_avatar_url( $user->user_email, array( 'size' => 96 ) ); | |
| 106 | 157 | } |
| 107 | 158 | |
| 108 | 159 | ob_start(); |
| 109 | 160 | |
| @@ -145,15 +196,17 @@ | ||
| 145 | 196 | </div> |
| 146 | 197 | </div> |
| 147 | 198 | <div class="grofile-right"> |
| 148 | 199 | <p class="grofile-name fn"> |
| 149 | - <strong><?php echo esc_html( $user->display_name ); ?></strong> | |
| 200 | + <strong><?php echo esc_html( $display_name ); ?></strong> | |
| 150 | 201 | <?php |
| 151 | 202 | if ( ! empty( $user_location ) ) : |
| 152 | 203 | ?> |
| 153 | 204 | <br><span class="grofile-location adr"><?php echo esc_html( $user_location ); ?></span><?php endif; ?> |
| 154 | 205 | </p> |
| 155 | - <p class="grofile-bio"><strong><?php esc_html_e( 'Bio:', 'jetpack' ); ?></strong> <?php echo wp_kses_post( $user->description ); ?></p> | |
| 206 | + <p class="grofile-bio"> | |
| 207 | + <strong><?php esc_html_e( 'Bio:', 'jetpack' ); ?></strong> <?php echo wp_kses_post( $user_description ); ?> | |
| 208 | + </p> | |
| 156 | 209 | <p class="grofile-view"> |
| 157 | 210 | <a href="<?php echo esc_url( $gravatar_url ); ?>"><?php esc_html_e( 'View complete profile', 'jetpack' ); ?></a> |
| 158 | 211 | </p> |
| 159 | 212 | </div> |