Created
January 16, 2017 14:28
-
-
Save pranali333/0fdfc8d4278c7fe6a9b29d21b854ef0b to your computer and use it in GitHub Desktop.
Fetching google plus count directly from google API for rtSocial plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fetch google plus count from google API. | |
*/ | |
function rtmedia_wp_footer_callback(){ | |
/* Check if user is logged-in. */ | |
if( ! is_user_logged_in() ){ | |
/* Get option settings of the rtSocial plugin. */ | |
$options = get_option( 'rtsocial_plugin_options' ); | |
/* Check if the google API key exists. */ | |
if ( isset( $options[ 'google_api_key' ] ) && $options[ 'google_api_key' ] != '' ) { | |
?> | |
<script type="text/javascript" charset="utf-8"> | |
jQuery( document ).ready( function( $ ){ | |
jQuery.ajaxPrefilter( function( options, originalOptions, jqXHR ) { | |
/* Modify options, control originalOptions, store jqXHR, etc */ | |
try { | |
if ( originalOptions.data == null || typeof ( originalOptions.data ) == 'undefined' || typeof ( originalOptions.data.action ) == 'undefined' ) { | |
return true; | |
} | |
} catch ( e ) { | |
return true; | |
} | |
/* Check if ajax action is rtsocial_gplus */ | |
if ( originalOptions.data.action == 'rtsocial_gplus' ) { | |
options.beforeSend = function() { | |
if ( originalOptions.data.action == 'rtsocial_gplus' ) { | |
/* URL of the media */ | |
var rtsocial_gplusurl = originalOptions.data.url; | |
/* ID of the media */ | |
var rtsocial_gplusid = originalOptions.data.id; | |
/* Social Share: Google Plus JSON */ | |
var data = { | |
"method":"pos.plusones.get", | |
"id":"p", | |
"params":{ | |
"nolog":true, | |
"id": rtsocial_gplusurl, | |
"source":"widget", | |
"userId":"@viewer", | |
"groupId":"@self" | |
}, | |
"jsonrpc":"2.0", | |
"key": "<?php echo $options[ 'google_api_key' ]; ?>", | |
"apiVersion":"v1" | |
}; | |
/* Sending a request to google for count */ | |
$.ajax({ | |
type: "POST", | |
url: "https://clients6.google.com/rpc", | |
processData: true, | |
contentType: 'application/json', | |
data: JSON.stringify(data), | |
success: function(r){ | |
/* Check if the html exists */ | |
if( jQuery( '.rts_id:input[value="'+rtsocial_gplusid+'"]' ).length > 0 ){ | |
/* Add the count in the google count view */ | |
jQuery( '.rts_id:input[value="'+rtsocial_gplusid+'"]' ).closest( '.rtsocial-container' ).find( '.rtsocial-horizontal-count .rtsocial-gplus-count' ).text( ( r.result.metadata.globalCounts.count ) ? ( r.result.metadata.globalCounts.count ) : '0' ); | |
} | |
} | |
}); | |
/* Stop the previous ajax */ | |
return false; | |
} | |
}; | |
} | |
} ); | |
}); | |
</script> | |
<?php | |
} | |
} | |
} | |
add_action( 'wp_footer', 'rtmedia_wp_footer_callback', 1000 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment