Skip to content

Instantly share code, notes, and snippets.

@chrise86
Forked from const-g/getShares.php
Created July 25, 2014 11:26
Show Gist options
  • Save chrise86/d6d30c36edf98c8a3ff7 to your computer and use it in GitHub Desktop.
Save chrise86/d6d30c36edf98c8a3ff7 to your computer and use it in GitHub Desktop.
<?php
// =================================================================
// getShares
// @author: Constantin Guay
// @url: https://gist.github.com/Cog-g/8284663
// v. 1.5.6
//
// This script will get number of shares for usual social networks.
//
// @param:
// . p = List of posts
// . days = Age of the posts, in days (default: 7)
// . sort = {most} how to order the list (default: empty)
// . view = {json|table|csv} how to return data (default: table)
// =================================================================
define('WP_USE_THEMES', false);
if( file_exists('wp-blog-header.php') ) {
require_once('wp-blog-header.php');
}
// header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
// header("Cache-Control: post-check=0, pre-check=0", false);
// header("Pragma: no-cache"); // HTTP/1.0
// header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
if( !empty( $_GET['days'] ) )
$_GET['from'] = strtotime('-' . $_GET['days'] . ' days');
if( empty( $_GET['from'] ) )
$_GET['from'] = strtotime('-15 days');
$params = array(
'post_type' => 'post',
'posts_per_page' => -1,
'ignore_sticky_posts' => false
);
$date_start = $_GET['from'];
$nb_days = round( ( $date_start - time() ) / 3600 / 24 );
// $date_start = $_GET['from'];
$date_end = date('Y-m-d H:i:s', strtotime('+2 hours'));
// tags
$tags = "";
if( !empty( $_GET['tags'] ) )
{
$params['tag_slug__in'] = explode(',', $_GET['tags']);
}
// cat
$cats = "";
if( !empty( $_GET['cats'] ) )
{
$params['category_name'] = explode(',', $_GET['cats']);
}
// posts
$posts = "";
if( !empty( $_GET['p'] ) )
{
$params['tag'] = $params['category_name'] = $_GET['sort'] = "";
$params['post__in'] = explode(',', $_GET['p']);
$params['orderby'] = 'post__in'; // https://core.trac.wordpress.org/ticket/13729
}
// var_dump( $params ); exit();
function filter_where( $where = '' )
{
global $date_start, $date_end, $posts;
if( empty($posts) )
{
$date_start = date('Y-m-d H:i:s', $date_start);
$where .= " AND post_date >= '" . $date_start . "'" . " AND post_date <= '" . $date_end . "'";
}
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$view = ( empty( $_GET['view'] ) ) ? 'table' : $_GET['view'];
if( $view == 'table' )
{
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<title>Shared posts</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap-theme.min.css">
<!-- fonts -->
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>Last <?php echo( $nb_days ); ?> days posts shared count</h1>
<?php
}
query_posts( $params );
$results = array();
if ( have_posts() )
{
while ( have_posts() )
{
the_post();
$permalink = get_permalink();
$date = get_the_date('d/m/Y');
$obj = array();
$obj['id'] = get_the_ID();
$obj['url'] = $permalink;
$obj['title'] = get_the_title();
$obj['date'] = $date;
$obj['description'] = get_the_excerpt();
$obj['comments'] = get_comments_number();
// --- thumbnail ---------------------
$imgURL = wp_get_attachment_image_src( get_post_thumbnail_id( $obj['id'] ), 'thumbnail');
$obj['img'] = $imgURL[0];
// --- medium image ---------------------
$imgURL = wp_get_attachment_image_src( get_post_thumbnail_id( $obj['id'] ), array(180, 148));
$obj['img_m'] = $imgURL[0];
// --- big image ---------------------
$imgURL = wp_get_attachment_image_src( get_post_thumbnail_id( $obj['id'] ), array(520, 293));
$obj['img_l'] = $imgURL[0];
$obj['category'] = get_the_category();
$obj['category'] = $obj['category'][0]->cat_name;
// Facebook
$likes = file_get_contents('http://graph.facebook.com/?ids=' . urlencode( $obj['url'] ));
$likes = json_decode( $likes, true );
$l = $likes[$obj['url']];
if( !isset( $l['shares'] ) ) $l['shares'] = 0;
$like = max(0, $l['shares']);
$obj['likes'] = $like;
// Twitter
$tweets = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . urlencode( $obj['url'] ));
$tweets = json_decode($tweets);
$obj['tweets'] = max( 0, $tweets->count );
// Google +
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://clients6.google.com/rpc?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $obj['url'] . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
$curl_results = curl_exec ($ch);
curl_close ($ch);
$parsed_results = json_decode($curl_results, true);
if( !isset( $parsed_results[0]['result'] ) )
{
$obj['gplus'] = 0;
}
else
{
$obj['gplus'] = max( 0, $parsed_results[0]['result']['metadata']['globalCounts']['count'] );
}
// Linkedin
$linkedin = file_get_contents('http://www.linkedin.com/countserv/count/share?format=json&url=' . urlencode( $obj['url'] ));
$linkedin = json_decode($linkedin);
$obj['linkedin'] = max( 0, $linkedin->count );
// Total
$obj['total'] = $obj['likes'] + $obj['tweets'] + $obj['gplus'] + $obj['linkedin'];
$results[] = $obj;
}
}
else
{
exit('No result.');
}
if( !empty( $_GET['sort'] ) && $_GET['sort'] == 'most' )
{
foreach ($results as $key => $row) {
$total[$key] = $row['total'];
}
array_multisort($total, SORT_DESC, $results);
}
if( $view == 'table' )
{
?>
<table class="table table-condensed table-hover table-responsive">
<tr>
<th>Link</th>
<th><i class="fa fa-facebook fa-lg"></i></th>
<th><i class="fa fa-twitter fa-lg"></i></th>
<th><i class="fa fa-google-plus fa-lg"></i></th>
<th><i class="fa fa-linkedin fa-lg"></i></th>
<th><b>= TOTAL</b></th>
<th><i class="fa fa-comments fa-lg"></i></th>
</tr>
<?php
foreach( $results as $r )
{
?>
<tr id="<?php echo( $r['id'] ); ?>">
<td valign="center"><?php echo( $r['date'] ); ?> <a href="<?php echo( $r['url'] ); ?>" target="_blank"><?php echo( $r['title'] ); ?></a></td>
<td align="center"><?php echo( $r['likes'] ); ?></td>
<td align="center"><?php echo( $r['tweets'] ); ?></td>
<td align="center"><?php echo( $r['gplus'] ); ?></td>
<td align="center"><?php echo( $r['linkedin'] ); ?></td>
<td align="center"><b><?php echo( $r['total'] ); ?></b></td>
<td align="center" style="color:#cacaca"><?php echo( $r['comments'] ); ?></td>
</tr>
<?php
}
?>
</table>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src="../../dist/js/bootstrap.min.js"></script>
</body>
</html>
<?php
}
elseif( $view == 'json' )
{
echo( json_encode( $results ) );
}
elseif( $view == 'csv' )
{
echo( "url;likes;tweets;gplus;linkedin;total\n" );
foreach( $results as $r )
{
echo( $r['url'] . ';' );
echo( $r['likes'] . ';' );
echo( $r['tweets'] . ';' );
echo( $r['gplus'] . ';' );
echo( $r['linkedin'] . ';' );
echo( $r['total'] . ';' );
echo( "\n" );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment