Instantly share code, notes, and snippets.
Last active
March 7, 2016 08:32
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save FrankM1/3deed62b1c7547fc5b60 to your computer and use it in GitHub Desktop.
Built for NewsCore http://themes.radiumthemes.com/wp/newscore/
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 | |
/* | |
* This file is a part of the RadiumFramework core. | |
* | |
* @category RadiumFramework | |
* @package NewsCore | |
* @author Franklin M Gitonga | |
* @link http://radiumthemes.com/ | |
*/ | |
/* | |
* SOCIAL SHARE COUNTERS | |
*/ | |
/** | |
* [radium_get_twitter_followers description] | |
* @param [type] $twitter_user [description] | |
* @param [type] $cache_time [description] | |
* @return [type] [description] | |
*/ | |
function radium_get_twitter_followers() { | |
$options = get_option( 'radium_tweets_settings' ); | |
// CHECK SETTINGS & DIE IF NOT SET | |
if( empty($options['consumerkey']) || empty($options['consumersecret']) ){ | |
return; | |
} | |
// some variables | |
$consumerKey = $options['consumerkey']; | |
$consumerSecret = $options['consumersecret']; | |
$token = get_option('cfTwitterToken'); | |
// get follower count from cache | |
$numberOfFollowers = get_transient('rm_twitter_followers'); | |
// cache version does not exist or expired | |
if (false === $numberOfFollowers) { | |
// getting new auth bearer only if we don't have one | |
if(!$token) { | |
// preparing credentials | |
$credentials = $consumerKey . ':' . $consumerSecret; | |
$toSend = base64_encode($credentials); | |
// http post arguments | |
$args = array( | |
'method' => 'POST', | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array( | |
'Authorization' => 'Basic ' . $toSend, | |
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8' | |
), | |
'body' => array( 'grant_type' => 'client_credentials' ) | |
); | |
add_filter('https_ssl_verify', '__return_false'); | |
$response = wp_remote_post('https://api.twitter.com/oauth2/token', $args); | |
$keys = json_decode(wp_remote_retrieve_body($response)); | |
if($keys) { | |
// saving token to wp_options table | |
update_option('cfTwitterToken', $keys->access_token); | |
$token = $keys->access_token; | |
} | |
} | |
// we have bearer token wether we obtained it from API or from options | |
$args = array( | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array( | |
'Authorization' => "Bearer $token" | |
) | |
); | |
add_filter('https_ssl_verify', '__return_false'); | |
$api_url = "https://api.twitter.com/1.1/users/show.json?screen_name={$options['username']}"; | |
$response = wp_remote_get($api_url, $args); | |
if (!is_wp_error($response)) { | |
$followers = json_decode(wp_remote_retrieve_body($response)); | |
$followers = $followers->followers_count; | |
} else { | |
// get old value and break | |
$followers = get_option('radium_twitter_followers'); | |
// uncomment below to debug | |
//die($response->get_error_message()); | |
} | |
// cache for an hour | |
set_transient('radium_twitter_followers', $followers, 1*60*60); | |
update_option('radium_twitter_followers', $followers); | |
} | |
return $followers; | |
} | |
/** | |
* [radium_get_facebook_likes description] | |
* @param [type] $facebook_user [description] | |
* @param [type] $cache_time [description] | |
* @return [type] [description] | |
*/ | |
function radium_get_facebook_likes( $facebook_user, $cache_time ) { | |
$transient_name = 'radium_overall_facebook_likes'; | |
$fbData = ''; | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if (false === ( $shares = get_transient($transient_name) )) { | |
$json = wp_remote_get("http://graph.facebook.com/" . $facebook_user, array('timeout' => 30)); | |
if (is_wp_error($json)) { | |
return "0"; | |
} else { | |
$fbData = json_decode($json['body'], true); | |
$shares = isset( $fbData['likes'] ) ? $fbData['likes'] : false; | |
if($shares) | |
set_transient($transient_name, intval($shares), $cache_time * 60); | |
return $shares; | |
} | |
} else { | |
return $shares; | |
} | |
} | |
/** | |
* [radium_gplus_count get googe plus circled count] | |
* @param [type] $gplus_username [description] | |
* @param [type] $gplus_api [description] | |
* @param [type] $cache_time [description] | |
* @return [type] [description] | |
*/ | |
function radium_gplus_count( $id, $cache_time ) { | |
$shares = 0; | |
$transient_name = 'radium_overall_google_pluses'; | |
$google_plus_count = ''; | |
if ($cache_time == 0) { | |
delete_transient($transient_name); | |
} | |
if ($cache_time == '') { | |
$cache_time = 600; | |
} | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
// Google Plus. | |
$link = "https://plus.google.com/".$id; | |
$gplus = array( | |
'method' => 'POST', | |
'sslverify' => false, | |
'timeout' => 30, | |
'headers' => array( 'Content-Type' => 'application/json' ), | |
'body' => '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $link . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' | |
); | |
$remote_data = wp_remote_get( 'https://clients6.google.com/rpc', $gplus ); | |
if (is_wp_error($remote_data)) { | |
return "0"; | |
} else { | |
$json_data = json_decode( $remote_data['body'], true ); | |
$shares = isset($json_data[0]['result']['metadata']['globalCounts']) ? $json_data[0]['result']['metadata']['globalCounts'] : false; | |
if ( $shares && is_array( $shares ) ) { | |
foreach($shares as $gcount){ | |
$shares = $gcount; | |
} | |
} else { | |
return "0"; | |
} | |
if ( !$shares) { | |
$link = "https://plus.google.com/".$id."/posts"; | |
$page = file_get_contents($link); | |
if (preg_match('/>([0-9,]+) people</i', $page, $matches)) | |
$shares = str_replace(',', '', $matches[1]); | |
} | |
if($google_plus_count) | |
set_transient($transient_name, intval($shares), $cache_time * 60); | |
return $shares; | |
} | |
} else { | |
return $shares; | |
} | |
} | |
/* single Article Social Counters */ | |
function radium_single_get_tweets( $url = null, $cache_time = 600 ) { | |
$shares = 0; | |
$post_id = get_the_ID(); | |
$transient_name = 'radium_single_get_tweets_'. $post_id; | |
$url = $url ? $url : get_permalink(); | |
$url = apply_filters(__FUNCTION__.'_url', $url); | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
$args = array( | |
'timeout' => 30, | |
'sslverify' => true, | |
); | |
$response = wp_remote_get('http://cdn.api.twitter.com/1/urls/count.json?url=' . $url, $args); | |
if( is_wp_error( $response ) ) | |
return "0"; | |
$xml = $response['body']; | |
if( is_wp_error( $xml ) ) | |
return "0"; | |
$json = json_decode( $xml, true ); | |
$shares = isset( $json['count'] ) ? $json['count'] : false; | |
if($shares) | |
set_transient($transient_name, $shares, $cache_time * 60); | |
return $shares; | |
} else { | |
return $shares; | |
} | |
} | |
/** | |
* [radium_single_get_shares description] | |
* @param [type] $url [description] | |
* @return [type] [description] | |
*/ | |
function radium_single_get_linkedin_shares( $url = null, $cache_time = 600 ) { | |
$shares = 0; | |
$post_id = get_the_ID(); | |
$transient_name = 'radium_single_get_linkedin_'. $post_id; | |
$url = $url ? $url : get_permalink(); | |
$url = apply_filters(__FUNCTION__.'_url', $url); | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
$args = array( | |
'timeout' => 30, | |
'sslverify' => true, | |
); | |
$response = wp_remote_get("http://www.linkedin.com/countserv/count/share?url=$url&format=json", $args); | |
if( is_wp_error( $response ) ) | |
return "0"; | |
$xml = $response['body']; | |
if( is_wp_error( $xml ) ) | |
return; | |
$json = json_decode( $xml, true ); | |
$shares = isset( $json['count'] ) ? $json['count'] : false; | |
if($shares) | |
set_transient($transient_name, $shares, $cache_time * 60); | |
return $shares; | |
} else { | |
return $shares; | |
} | |
} | |
/** | |
* [radium_single_get_likes description] | |
* @param [type] $url [description] | |
* @return [type] [description] | |
*/ | |
function radium_single_get_facebook_likes( $url = null, $cache_time = 600 ) { | |
$shares = 0; | |
$post_id = get_the_ID(); | |
$transient_name = 'radium_single_get_linkedin_'. $post_id; | |
$url = $url ? $url : get_permalink(); | |
$url = apply_filters(__FUNCTION__.'_url', $url); | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
$args = array( | |
'timeout' => 30, | |
'sslverify' => true, | |
); | |
$response = wp_remote_get('http://graph.facebook.com/?ids=' . $url, $args); | |
if( is_wp_error( $response ) ) | |
return "0"; | |
$xml = $response['body']; | |
if( is_wp_error( $xml ) ) | |
return "0"; | |
$json = json_decode( $xml, true ); | |
$shares = isset( $json[$url]['shares'] ) ? intval( $json[$url]['shares'] ) : false; | |
if ( $shares ) { | |
set_transient($transient_name, $shares, $cache_time * 60); | |
return $shares; | |
} else { | |
return 0; | |
} | |
} else { | |
return $shares; | |
} | |
} | |
/** | |
* [radium_single_get_plusones description] | |
* @param [type] $url [description] | |
* @return [type] [description] | |
*/ | |
function radium_single_get_plusones( $url = null, $cache_time = 600 ) { | |
$shares = 0; | |
$post_id = get_the_ID(); | |
$transient_name = 'radium_single_get_googleplus_'. $post_id; | |
$url = $url ? $url : get_permalink(); | |
$url = apply_filters(__FUNCTION__.'_url', $url); | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
// Google Plus. | |
$args = array( | |
'method' => 'POST', | |
'sslverify' => false, | |
'timeout' => 30, | |
'headers' => array( 'Content-Type' => 'application/json' ), | |
'body' => '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $url . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' | |
); | |
$response = wp_remote_get( 'https://clients6.google.com/rpc', $args ); | |
if( is_wp_error( $response ) ) | |
return "0"; | |
$xml = $response['body']; | |
if( is_wp_error( $xml ) ) | |
return "0"; | |
$json = json_decode( $xml, true ); | |
$shares = isset( $json[0]['result']['metadata']['globalCounts']['count'] ) ? intval( $json[0]['result']['metadata']['globalCounts']['count'] ) : false; | |
if ( $shares ) | |
set_transient($transient_name, $shares, $cache_time * 60); | |
return intval( $shares ); | |
} else { | |
return $shares; | |
} | |
} | |
/** | |
* [radium_single_get_pins description] | |
* @param [type] $url [description] | |
* @return [type] [description] | |
*/ | |
function radium_single_get_pins( $url = null, $cache_time = 600 ) { | |
$shares = 0; | |
$post_id = get_the_ID(); | |
$transient_name = 'radium_single_get_pins_'. $post_id; | |
$url = $url ? $url : get_permalink(); | |
$url = apply_filters(__FUNCTION__.'_url', $url); | |
if ($cache_time == 0) | |
delete_transient($transient_name); | |
if ($cache_time == '') | |
$cache_time = 600; | |
if ( false === ( $shares = get_transient($transient_name) ) ) { | |
// Pininterest Pins. | |
$args = array( | |
'method' => 'POST', | |
'sslverify' => false, | |
'timeout' => 30, | |
'headers' => array( 'Content-Type' => 'application/json' ), | |
); | |
$response = wp_remote_get( 'http://api.pinterest.com/v1/urls/count.json?callback=receiveCount&url='.$url, $args ); | |
if( is_wp_error( $response ) ) | |
return "0"; | |
$xml = $response['body']; | |
if( is_wp_error( $xml ) ) | |
return "0"; | |
$xml = preg_replace('/^receiveCount\((.*)\)$/', "\\1", $xml); | |
$json = json_decode( $xml, true ); | |
$shares = isset( $json['count'] ) ? intval( $json['count'] ) : false; | |
if ( $shares ) | |
set_transient($transient_name, $shares, $cache_time * 60); | |
return intval( $shares ); | |
} else { | |
return $shares; | |
} | |
} | |
function radium_single_get_stumble |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment