|
<?php |
|
|
|
/** |
|
* Get tweet count from Twitter API (v1.1) |
|
*/ |
|
function wds_post_tweet_count( $post_id ) { |
|
|
|
// Check for transient |
|
if ( ! ( $count = get_transient( 'wds_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, true ); |
|
|
|
// Set total count |
|
$count = absint( $json->count ); |
|
|
|
// Set transient to expire every 30 minutes |
|
set_transient( 'wds_post_tweet_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); |
|
|
|
} |
|
|
|
return absint( $count ); |
|
|
|
} |
|
|
|
/** |
|
* Get like count from Facebook FQL |
|
*/ |
|
function wds_post_like_count( $post_id ) { |
|
|
|
// Check for transient |
|
if ( ! ( $count = get_transient( 'wds_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, true ); |
|
|
|
// Set total count |
|
$count = absint( $json[0]->total_count ); |
|
|
|
// Set transient to expire every 30 minutes |
|
set_transient( 'wds_post_like_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); |
|
|
|
} |
|
|
|
return absint( $count ); |
|
|
|
} |
|
|
|
/** |
|
* Get post pageview count |
|
*/ |
|
function wds_post_pageview_count( $post_id ) { |
|
|
|
// Check for transient |
|
if ( ! ( $count = get_transient( 'wds_post_pageview_count' . $post_id ) ) ) { |
|
|
|
// Verify we're running Jetpack |
|
if ( function_exists( 'stats_get_csv' ) ) { |
|
|
|
// Do API call |
|
$response = stats_get_csv( 'postviews', 'post_id='. $post_id .'&period=month&limit=1' ); |
|
|
|
// Set total count |
|
$count = absint( $response[0]['views'] ); |
|
|
|
// If not, stop and don't set transient |
|
} else { |
|
return 'Jetpack stats not active'; |
|
} |
|
|
|
// Set transient to expire every 30 minutes |
|
set_transient( 'wds_post_pageview_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); |
|
|
|
} |
|
|
|
return absint( $count ); |
|
|
|
} |
|
|
|
/** |
|
* Get post comment count |
|
*/ |
|
function wds_post_comment_count( $post_id ) { |
|
|
|
// Check for transient |
|
if ( ! ( $count = get_transient( 'wds_post_comment_count' . $post_id ) ) ) { |
|
|
|
// Verify comments are open |
|
if ( comments_open() ) { |
|
|
|
// Get comment count |
|
$count = absint( get_comments_number( $post_id ) ); |
|
|
|
// If not, stop and don't set transient |
|
} else { |
|
return 'Comments closed'; |
|
} |
|
|
|
// Set transient to expire every 30 minutes |
|
set_transient( 'wds_post_comment_count' . $post_id, absint( $count ), 30 * MINUTE_IN_SECONDS ); |
|
|
|
} |
|
|
|
return absint( $count ); |
|
|
|
} |
|
|
|
/** |
|
* Markup for Social Media Icons |
|
*/ |
|
function wds_social_media_icons() { |
|
|
|
// Get the post ID |
|
$post_id = get_the_ID(); ?> |
|
|
|
<div class="social-icons-wrap"> |
|
<ul class="social-icons"> |
|
<li class="social-icon twitter"><a href="https://twitter.com/share?&url=<?php the_permalink(); ?>&text=<?php the_title(); ?>&via=XXXXXXXXXXXXX"><?php echo wds_post_tweet_count( $post_id ); ?></a></li> |
|
<li class="social-icon facebook"><a href="https://www.facebook.com/sharer/sharer.php?u=<?php the_permalink(); ?>&appId=XXXXXXXXXXXXX"><?php echo wds_post_like_count( $post_id ); ?></a></li> |
|
<li class="social-icon pageviews"><p><?php echo wds_post_pageview_count( $post_id ); ?></p></li> |
|
<li class="social-icon comments"><a href="<?php comments_link(); ?>"><?php echo wds_post_comment_count( $post_id ); ?></a></li> |
|
</ul> |
|
</div><!-- .social-icons-wrap --> |
|
|
|
<?php } |