Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gravatar Hovercards: jQuery to vanilla JS #15049

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions modules/gravatar-hovercards.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Additional Search Queries: gravatar, hovercards
*/

define( 'GROFILES__CACHE_BUSTER', gmdate( 'YM' ) . 'aa' ); // Break CDN cache, increment when gravatar.com/js/gprofiles.js changes
define( 'GROFILES__CACHE_BUSTER', gmdate( 'YM' ) . '-2' ); // Break CDN cache, increment when gravatar.com/js/gprofiles.js changes.

function grofiles_hovercards_init() {
add_filter( 'get_avatar', 'grofiles_get_avatar', 10, 2 );
Expand Down Expand Up @@ -182,7 +182,7 @@ function grofiles_attach_cards() {
return;
}

wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array( 'jquery' ), GROFILES__CACHE_BUSTER, true );
wp_enqueue_script( 'grofiles-cards', 'https://secure.gravatar.com/js/gprofiles.js', array(), GROFILES__CACHE_BUSTER, true );
wp_enqueue_script( 'wpgroho', plugins_url( 'wpgroho.js', __FILE__ ), array( 'grofiles-cards' ), false, true );
if ( is_user_logged_in() ) {
$cu = wp_get_current_user();
Expand Down
99 changes: 66 additions & 33 deletions modules/wpgroho.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,76 @@
/* global WPGroHo:true, Gravatar */
WPGroHo = jQuery.extend(
{
my_hash: '',
data: {},
renderers: {},
syncProfileData: function( hash, id ) {
if ( ! WPGroHo.data[ hash ] ) {
WPGroHo.data[ hash ] = {};
jQuery( 'div.grofile-hash-map-' + hash + ' span' ).each( function() {
WPGroHo.data[ hash ][ this.className ] = jQuery( this ).text();
} );
( function() {
var extend = function( out ) {
out = out || {};

for ( var i = 1; i < arguments.length; i++ ) {
if ( ! arguments[ i ] ) continue;

for ( var key in arguments[ i ] ) {
if ( arguments[ i ].hasOwnProperty( key ) ) out[ key ] = arguments[ i ][ key ];
}
}

WPGroHo.appendProfileData( WPGroHo.data[ hash ], hash, id );
},
appendProfileData: function( data, hash, id ) {
for ( var key in data ) {
if ( jQuery.isFunction( WPGroHo.renderers[ key ] ) ) {
return WPGroHo.renderers[ key ]( data[ key ], hash, id, key );
return out;
};

WPGroHo = extend(
{
my_hash: '',
data: {},
renderers: {},
syncProfileData: function( hash, id ) {
var hashElements;

if ( ! WPGroHo.data[ hash ] ) {
WPGroHo.data[ hash ] = {};
hashElements = document.querySelectorAll( 'div.grofile-hash-map-' + hash + ' span' );
for ( var i = 0; i < hashElements.length; i++ ) {
WPGroHo.data[ hash ][ hashElements[ i ].className ] = hashElements[ i ].innerText;
}
}

jQuery( '#' + id )
.find( 'h4' )
.after( jQuery( '<p class="grav-extra ' + key + '" />' ).html( data[ key ] ) );
}
WPGroHo.appendProfileData( WPGroHo.data[ hash ], hash, id );
},
appendProfileData: function( data, hash, id ) {
for ( var key in data ) {
if ( 'function' === typeof WPGroHo.renderers[ key ] ) {
return WPGroHo.renderers[ key ]( data[ key ], hash, id, key );
}

var card = document.getElementById( id );
if ( card ) {
var heading = card.querySelector( 'h4' );
if ( heading ) {
var extra = document.createElement( 'p' );
extra.className = 'grav-extra ' + key;
extra.innerHTML = data[ key ];

heading.insertAdjacentElement( 'afterend', extra );
}
}
}
},
},
},
WPGroHo
);
WPGroHo || {}
);

jQuery( document ).ready( function() {
if ( 'undefined' === typeof Gravatar ) {
return;
}
var jetpackHovercardsInit = function() {
if ( 'undefined' === typeof Gravatar ) {
return;
}

Gravatar.profile_cb = function( h, d ) {
WPGroHo.syncProfileData( h, d );
};

Gravatar.profile_cb = function( h, d ) {
WPGroHo.syncProfileData( h, d );
Gravatar.my_hash = WPGroHo.my_hash;
Gravatar.init( 'body', '#wpadminbar' );
};

Gravatar.my_hash = WPGroHo.my_hash;
Gravatar.init( 'body', '#wpadminbar' );
} );
if ( document.readyState === 'interactive' || document.readyState === 'complete' ) {
jetpackHovercardsInit();
} else {
document.addEventListener( 'DOMContentLoaded', jetpackHovercardsInit );
}
} )();