Skip to content

Instantly share code, notes, and snippets.

@gregrickaby
Last active February 6, 2017 08:56
Show Gist options
  • Save gregrickaby/6429669 to your computer and use it in GitHub Desktop.
Save gregrickaby/6429669 to your computer and use it in GitHub Desktop.
Display Likes, Tweets, Pageviews, and Comment Counts. Use transient cache to store data for 30 minutes.
<?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?&amp;url=<?php the_permalink(); ?>&amp;text=<?php the_title(); ?>&amp;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(); ?>&amp;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 }
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php wds_social_media_icons(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment