/** Get tweet count from Twitter API (v1.1) */ function susy_post_tweet_count( $post_id ) { // Check for transient if ( ! ( $count = get_transient( 'susy_post_tweet_count' . $post_id ) ) ) { // Do API call $response = wp_remote_retrieve_body( wp_remote_get( 'https://cdn.api.twitter.com/1/urls/count.json?url=' . urlencode( get_permalink( $post_id ) ) ) ); // If error in API call, stop and don't store transient if ( is_wp_error( $response ) ) return 'error'; // Decode JSON $json = json_decode( $response ); // Set total count $count = absint( $json->count ); // Set transient to expire every 30 minutes set_transient( 'susy_post_tweet_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); } return absint( $count ); } /** Twitter End */ /** Get like count from Facebook FQL */ function susy_post_like_count( $post_id ) { // Check for transient if ( ! ( $count = get_transient( 'susy_post_like_count' . $post_id ) ) ) { // Setup query arguments based on post permalink $fql = "SELECT url, "; //$fql .= "share_count, "; // total shares //$fql .= "like_count, "; // total likes //$fql .= "comment_count, "; // total comments $fql .= "total_count "; // summed total of shares, likes, and comments (fastest query) $fql .= "FROM link_stat WHERE url = '" . get_permalink( $post_id ) . "'"; // Do API call $response = wp_remote_retrieve_body( wp_remote_get( 'https://api.facebook.com/method/fql.query?format=json&query=' . urlencode( $fql ) ) ); // If error in API call, stop and don't store transient if ( is_wp_error( $response ) ) return 'error'; // Decode JSON $json = json_decode( $response ); // Set total count $count = absint( $json[0]->total_count ); // Set transient to expire every 30 minutes set_transient( 'susy_post_like_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); } return absint( $count ); } /** Facebook End */ /** Get share count from Google Plus */ function susy_post_plusone_count($post_id) { // Check for transient if ( ! ( $count = get_transient( 'susy_post_plus_count' . $post_id ) ) ) { $args = array( 'method' => 'POST', 'headers' => array( // setup content type to JSON 'Content-Type' => 'application/json' ), // setup POST options to Google API 'body' => json_encode(array( 'method' => 'pos.plusones.get', 'id' => 'p', 'method' => 'pos.plusones.get', 'jsonrpc' => '2.0', 'key' => 'p', 'apiVersion' => 'v1', 'params' => array( 'nolog'=>true, 'id'=> get_permalink( $post_id ), 'source'=>'widget', 'userId'=>'@viewer', 'groupId'=>'@self' ) )), // disable checking SSL sertificates 'sslverify'=>false ); // retrieves JSON with HTTP POST method for current URL $json_string = wp_remote_post("https://clients6.google.com/rpc", $args); if (is_wp_error($json_string)){ // return zero if response is error return "0"; } else { $json = json_decode($json_string['body'], true); // return count of Google +1 for requsted URL $count = intval( $json['result']['metadata']['globalCounts']['count'] ); } // Set transient to expire every 30 minutes set_transient( 'susy_post_plus_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); } return absint( $count ); } /** Google Plus End */ /** Get Count from LinkedIn */ function susy_post_linkedin_count( $post_id ) { // Check for transient if ( ! ( $count = get_transient( 'susy_post_linkedin_count' . $post_id ) ) ) { // Do API call $response = wp_remote_retrieve_body( wp_remote_get( 'https://www.linkedin.com/countserv/count/share?url=' . urlencode( get_permalink( $post_id ) ) . '&format=json' ) ); // If error in API call, stop and don't store transient if ( is_wp_error( $response ) ) return 'error'; // Decode JSON $json = json_decode( $response ); // Set total count $count = absint( $json->count ); // Set transient to expire every 30 minutes set_transient( 'susy_post_linkedin_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); } return absint( $count ); } /** LinkedIn End */ /** Markup for Social Media Icons */ function susy_social_media_icons() { // Get the post ID $post_id = get_the_ID(); ?>