Last active
August 29, 2015 14:05
-
-
Save k4zuki02h4t4/abad609195c29bbc9bd2 to your computer and use it in GitHub Desktop.
[ WordPress ] 記事のシェア数を取得し表示する関数たち。
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 | |
/** | |
* Get Facebook count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_facebook_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'facebook_count_' . $post_id ); | |
$answer = array(); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_get( 'https://api.facebook.com/method/links.getStats?urls=' . rawurlencode( $url ) . '&format=json' ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode($response['body']); | |
$answer = get_object_vars( $response[0] ); | |
$result = (int)$answer['total_count']; | |
set_transient( 'facebook_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Twitter count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_twitter_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'twitter_count_' . $post_id ); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_get( 'http://urls.api.twitter.com/1/urls/count.json?url=' . rawurlencode( $url ) . '&callback=?' ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode($response['body']); | |
$result = (int)$response->count; | |
set_transient( 'twitter_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Google Plus count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_googleplus_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'googleplus_count_' . $post_id ); | |
$remote = 'https://clients6.google.com/rpc'; | |
$bosy = array( | |
'method' => 'pos.plusones.get', | |
'id' => 'p', | |
'params' => array( | |
'nolog' => true, | |
'id' => $url, | |
'source' => 'widget', | |
'userId' => '@viewer', | |
'groupId' => '@self' | |
), | |
'jsonrpc' => '2.0', | |
'key' => 'p', | |
'apiVersion' => 'v1' | |
); | |
$args = array( | |
'method' => 'POST', | |
'timeout' => 5, | |
'headers' => array( 'Content-type' => 'application/json' ), | |
'body' => json_encode( $bosy ) | |
); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_post( $remote, $args ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode($response['body']); | |
$result = (int)$response->result->metadata->globalCounts->count; | |
set_transient( 'googleplus_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Hatena bookmark count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_hatena_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'hatena_count_' . $post_id ); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_get( 'http://api.b.st-hatena.com/entry.count?url=' . rawurlencode( $url ) ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode($response['body']); | |
$result = (int)$response; | |
set_transient( 'hatena_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Pinterest count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_pinterest_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'pinterest_count_' . $post_id ); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_get( 'http://api.pinterest.com/v1/urls/count.json?url=' . rawurlencode( $url ) . '&callback=receiveCount' ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode( preg_replace( '/\AreceiveCount\((.*)\)$/', "\\1", $response['body'] ) ); | |
$result = (int)$response->count; | |
set_transient( 'pinterest_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Linkedin count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_linkedin_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = ( get_permalink( $post_id ) !== false ) ? get_permalink( $post_id ) : false; | |
$result = get_transient( 'linkedin_count_' . $post_id ); | |
if ( $url !== false && $result === false ) { | |
$response = wp_remote_get( 'http://www.linkedin.com/countserv/count/share?url=' . rawurlencode( $url ) . '&format=json' ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode( $response['body'] ); | |
$result = (int)$response->count; | |
set_transient( 'linkedin_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} | |
/** | |
* Get Feedly count. | |
* | |
* @param intval $post_id | |
* @return intval | |
*/ | |
function get_feedly_count( $post_id = 0 ) { | |
global $post; | |
$post_id = ( (int)$post_id > 0 ) ? (int)$post_id : (int)$post->ID; | |
$url = get_feed_link( 'rss2' ); | |
$result = get_transient( 'feedly_count_' . $post_id ); | |
if ( isset( $url ) && $result === false ) { | |
$response = wp_remote_get( 'http://cloud.feedly.com/v3/feeds/' . rawurlencode( 'feed/' . $url ) ); | |
if ( ! is_wp_error( $response ) && $response['response']['code'] === 200 ) { | |
$response = json_decode( $response['body'] ); | |
$result = ( ! empty( $response ) ) ? (int)$response->subscribers : 0; | |
set_transient( 'feedly_count_' . $post_id, $result, 60 * MINUTE_IN_SECONDS ); | |
} else { | |
$result = false; | |
} | |
} | |
return (int)$result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment