gravatar-hovercards.php
| 1 | <?php |
| 2 | /** |
| 3 | * Module Name: Gravatar Hovercards |
| 4 | * Module Description: Enable pop-up business cards over commenters’ Gravatars. |
| 5 | * Sort Order: 11 |
| 6 | * Recommendation Order: 13 |
| 7 | * First Introduced: 1.1 |
| 8 | * Requires Connection: No |
| 9 | * Auto Activate: No |
| 10 | * Module Tags: Social, Appearance |
| 11 | * Feature: Appearance |
| 12 | * Additional Search Queries: gravatar, hovercards |
| 13 | */ |
| 14 | |
| 15 | define( 'GROFILES__CACHE_BUSTER', gmdate( 'YM' ) . 'aa' ); // Break CDN cache, increment when gravatar.com/js/gprofiles.js changes |
| 16 | |
| 17 | function grofiles_hovercards_init() { |
| 18 | add_filter( 'get_avatar', 'grofiles_get_avatar', 10, 2 ); |
| 19 | add_action( 'wp_enqueue_scripts', 'grofiles_attach_cards' ); |
| 20 | add_action( 'wp_footer', 'grofiles_extra_data' ); |
| 21 | add_action( 'admin_init', 'grofiles_add_settings' ); |
| 22 | |
| 23 | add_action( 'load-index.php', 'grofiles_admin_cards' ); |
| 24 | add_action( 'load-users.php', 'grofiles_admin_cards' ); |
| 25 | add_action( 'load-edit-comments.php', 'grofiles_admin_cards' ); |
| 26 | add_action( 'load-options-discussion.php', 'grofiles_admin_cards_forced' ); |
| 27 | |
| 28 | add_filter( 'jetpack_module_configuration_url_gravatar-hovercards', 'gravatar_hovercards_configuration_url' ); |
| 29 | } |
| 30 | |
| 31 | function gravatar_hovercards_configuration_url() { |
| 32 | return admin_url( 'options-discussion.php#show_avatars' ); |
| 33 | } |
| 34 | |
| 35 | add_action( 'jetpack_modules_loaded', 'grofiles_hovercards_init' ); |
| 36 | |
| 37 | /* Hovercard Settings */ |
| 38 | |
| 39 | /** |
| 40 | * Adds Gravatar Hovercard setting |
| 41 | * |
| 42 | * @todo - always print HTML, hide via CSS/JS if !show_avatars |
| 43 | */ |
| 44 | function grofiles_add_settings() { |
| 45 | if ( !get_option( 'show_avatars' ) ) |
| 46 | return; |
| 47 | |
| 48 | add_settings_field( 'gravatar_disable_hovercards', __( 'Gravatar Hovercards', 'jetpack' ), 'grofiles_setting_callback', 'discussion', 'avatars' ); |
| 49 | register_setting( 'discussion', 'gravatar_disable_hovercards', 'grofiles_hovercard_option_sanitize' ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * HTML for Gravatar Hovercard setting |
| 54 | */ |
| 55 | function grofiles_setting_callback() { |
| 56 | global $current_user; |
| 57 | |
| 58 | $checked = 'disabled' == get_option( 'gravatar_disable_hovercards' ) ? '' : 'checked="checked" '; |
| 59 | |
| 60 | echo "<label id='gravatar-hovercard-options'><input {$checked}name='gravatar_disable_hovercards' id='gravatar_disable_hovercards' type='checkbox' value='enabled' class='code' /> " . __( "View people's profiles when you mouse over their Gravatars", 'jetpack' ) . "</label>"; |
| 61 | ?> |
| 62 | <style type="text/css"> |
| 63 | #grav-profile-example img { |
| 64 | float: left; |
| 65 | } |
| 66 | #grav-profile-example span { |
| 67 | padding: 0 1em; |
| 68 | } |
| 69 | </style> |
| 70 | <script type="text/javascript"> |
| 71 | // <![CDATA[ |
| 72 | jQuery( function($) { |
| 73 | var tr = $( '#gravatar_disable_hovercards' ).change( function() { |
| 74 | if ( $( this ).is( ':checked' ) ) { |
| 75 | $( '#grav-profile-example' ).slideDown( 'fast' ); |
| 76 | } else { |
| 77 | $( '#grav-profile-example' ).slideUp( 'fast' ); |
| 78 | } |
| 79 | } ).parents( 'tr' ); |
| 80 | var ftr = tr.parents( 'table' ).find( 'tr:first' ); |
| 81 | if ( ftr.length && !ftr.find( '#gravatar_disable_hovercards' ).length ) { |
| 82 | ftr.after( tr ); |
| 83 | } |
| 84 | } ); |
| 85 | // ]]> |
| 86 | </script> |
| 87 | <p id="grav-profile-example" class="hide-if-no-js"<?php if ( !$checked ) echo ' style="display:none"'; ?>><?php echo get_avatar( $current_user->ID, 64 ); ?> <span><?php _e( 'Put your mouse over your Gravatar to check out your profile.', 'jetpack' ); ?> <br class="clear" /></span></p> |
| 88 | <?php |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Sanitation filter for Gravatar Hovercard setting |
| 93 | */ |
| 94 | function grofiles_hovercard_option_sanitize( $val ) { |
| 95 | if ( 'disabled' == $val ) { |
| 96 | return $val; |
| 97 | } |
| 98 | |
| 99 | return $val ? 'enabled' : 'disabled'; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /* Hovercard Display */ |
| 104 | |
| 105 | /** |
| 106 | * Stores the gravatars' users that need extra profile data attached. |
| 107 | * |
| 108 | * Getter/Setter |
| 109 | * |
| 110 | * @param int|string|null $author Setter: User ID or email address. Getter: null. |
| 111 | * |
| 112 | * @return mixed Setter: void. Getter: array of user IDs and email addresses. |
| 113 | */ |
| 114 | function grofiles_gravatars_to_append( $author = null ) { |
| 115 | static $authors = array(); |
| 116 | |
| 117 | // Get |
| 118 | if ( is_null( $author ) ) { |
| 119 | return array_keys( $authors ); |
| 120 | } |
| 121 | |
| 122 | // Set |
| 123 | |
| 124 | if ( is_numeric( $author ) ) { |
| 125 | $author = (int) $author; |
| 126 | } |
| 127 | |
| 128 | $authors[$author] = true; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Stores the user ID or email address for each gravatar generated. |
| 133 | * |
| 134 | * Attached to the 'get_avatar' filter. |
| 135 | * |
| 136 | * @param string $avatar The <img/> element of the avatar. |
| 137 | * @param mixed $author User ID, email address, user login, comment object, user object, post object |
| 138 | * |
| 139 | * @return The <img/> element of the avatar. |
| 140 | */ |
| 141 | function grofiles_get_avatar( $avatar, $author ) { |
| 142 | if ( is_numeric( $author ) ) { |
| 143 | grofiles_gravatars_to_append( $author ); |
| 144 | } else if ( is_string( $author ) ) { |
| 145 | if ( false !== strpos( $author, '@' ) ) { |
| 146 | grofiles_gravatars_to_append( $author ); |
| 147 | } else { |
| 148 | if ( $user = get_user_by( 'slug', $author ) ) |
| 149 | grofiles_gravatars_to_append( $user->ID ); |
| 150 | } |
| 151 | } else if ( isset( $author->comment_type ) ) { |
| 152 | if ( '' != $author->comment_type && 'comment' != $author->comment_type ) |
| 153 | return $avatar; |
| 154 | if ( $author->user_id ) |
| 155 | grofiles_gravatars_to_append( $author->user_id ); |
| 156 | else |
| 157 | grofiles_gravatars_to_append( $author->comment_author_email ); |
| 158 | } else if ( isset( $author->user_login ) ) { |
| 159 | grofiles_gravatars_to_append( $author->ID ); |
| 160 | } else if ( isset( $author->post_author ) ) { |
| 161 | grofiles_gravatars_to_append( $author->post_author ); |
| 162 | } |
| 163 | |
| 164 | return $avatar; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Loads Gravatar Hovercard script. |
| 169 | * |
| 170 | * @todo is_singular() only? |
| 171 | */ |
| 172 | function grofiles_attach_cards() { |
| 173 | global $blog_id; |
| 174 | |
| 175 | // Is the display of Avatars disabled? |
| 176 | if ( ! get_option( 'show_avatars' ) ) { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // Is the display of Gravatar Hovercards disabled? |
| 181 | if ( 'disabled' == Jetpack_Options::get_option_and_ensure_autoload( 'gravatar_disable_hovercards', '0' ) ) { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array( 'jquery' ), GROFILES__CACHE_BUSTER, true ); |
| 186 | wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), false, true ); |
| 187 | if ( is_user_logged_in() ) { |
| 188 | $cu = wp_get_current_user(); |
| 189 | $my_hash = md5( $cu->user_email ); |
| 190 | } else if ( !empty( $_COOKIE['comment_author_email_' . COOKIEHASH] ) ) { |
| 191 | $my_hash = md5( $_COOKIE['comment_author_email_' . COOKIEHASH] ); |
| 192 | } else { |
| 193 | $my_hash = ''; |
| 194 | } |
| 195 | wp_localize_script( 'wpgroho', 'WPGroHo', compact( 'my_hash' ) ); |
| 196 | } |
| 197 | |
| 198 | function grofiles_attach_cards_forced() { |
| 199 | add_filter( 'pre_option_gravatar_disable_hovercards', 'grofiles_force_gravatar_enable_hovercards' ); |
| 200 | grofiles_attach_cards(); |
| 201 | } |
| 202 | |
| 203 | function grofiles_force_gravatar_enable_hovercards() { |
| 204 | return 'enabled'; |
| 205 | } |
| 206 | |
| 207 | function grofiles_admin_cards_forced() { |
| 208 | add_action( 'admin_footer', 'grofiles_attach_cards_forced' ); |
| 209 | } |
| 210 | |
| 211 | function grofiles_admin_cards() { |
| 212 | add_action( 'admin_footer', 'grofiles_attach_cards' ); |
| 213 | } |
| 214 | |
| 215 | function grofiles_extra_data() { |
| 216 | ?> |
| 217 | <div style="display:none"> |
| 218 | <?php |
| 219 | foreach ( grofiles_gravatars_to_append() as $author ) |
| 220 | grofiles_hovercards_data_html( $author ); |
| 221 | ?> |
| 222 | </div> |
| 223 | <?php |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Echoes the data from grofiles_hovercards_data() as HTML elements. |
| 228 | * |
| 229 | * @since 5.5.0 Add support for a passed WP_User object |
| 230 | * |
| 231 | * @param int|string|WP_User $author User ID, email address, or a WP_User object |
| 232 | */ |
| 233 | function grofiles_hovercards_data_html( $author ) { |
| 234 | $data = grofiles_hovercards_data( $author ); |
| 235 | $hash = ''; |
| 236 | if ( is_numeric( $author ) ) { |
| 237 | $user = get_userdata( $author ); |
| 238 | if ( $user ) { |
| 239 | $hash = md5( $user->user_email ); |
| 240 | } |
| 241 | } elseif ( is_email( $author ) ) { |
| 242 | $hash = md5( $author ); |
| 243 | } elseif ( is_a( $author, 'WP_User' ) ) { |
| 244 | $hash = md5( $author->user_email ); |
| 245 | } |
| 246 | |
| 247 | if ( ! $hash ) { |
| 248 | return; |
| 249 | } |
| 250 | ?> |
| 251 | <div class="grofile-hash-map-<?php echo $hash; ?>"> |
| 252 | <?php foreach ( $data as $key => $value ) : ?> |
| 253 | <span class="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></span> |
| 254 | <?php endforeach; ?> |
| 255 | </div> |
| 256 | <?php |
| 257 | } |
| 258 | |
| 259 | |
| 260 | /* API */ |
| 261 | |
| 262 | /** |
| 263 | * Returns the PHP callbacks for data sources. |
| 264 | * |
| 265 | * 'grofiles_hovercards_data_callbacks' filter |
| 266 | * |
| 267 | * @return array( data_key => data_callback, ... ) |
| 268 | */ |
| 269 | function grofiles_hovercards_data_callbacks() { |
| 270 | /** |
| 271 | * Filter the Gravatar Hovercard PHP callbacks. |
| 272 | * |
| 273 | * @module gravatar-hovercards |
| 274 | * |
| 275 | * @since 1.1.0 |
| 276 | * |
| 277 | * @param array $args Array of data callbacks. |
| 278 | */ |
| 279 | return apply_filters( 'grofiles_hovercards_data_callbacks', array() ); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Keyed JSON object containing all profile data provided by registered callbacks |
| 284 | * |
| 285 | * @param int|strung $author User ID or email address |
| 286 | * |
| 287 | * @return array( data_key => data, ... ) |
| 288 | */ |
| 289 | function grofiles_hovercards_data( $author ) { |
| 290 | $r = array(); |
| 291 | foreach ( grofiles_hovercards_data_callbacks() as $key => $callback ) { |
| 292 | if ( !is_callable( $callback ) ) |
| 293 | continue; |
| 294 | $data = call_user_func( $callback, $author, $key ); |
| 295 | if ( !is_null( $data ) ) |
| 296 | $r[$key] = $data; |
| 297 | } |
| 298 | |
| 299 | return $r; |
| 300 | } |
| 301 |