Social Share Buttons with Count for WordPress
<?php
/**
* Social Share Buttons with Count for WordPress
*/
/**
* Performs HTTP request and returns response body with conditionally decoding json.
*
* @param string $request_url HTTP request URL
* @param string $request_args HTTP request args
* @param boolean $decode_json decode the json response or not
* @return mixed response body on success and false on failure.
*/
function wpd_get_http_response( $request_url = '', $request_args = '', $decode_json = true ) {
// return if no url
if ( ! $request_url )
return false;
$request_args = wp_parse_args( $request_args, array( 'method' => 'GET' ) );
w
$response = wp_remote_request( $request_url, $request_args );
if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
$response = wp_remote_retrieve_body( $response );
if ( $decode_json )
return json_decode( $response, true );
else
return $response;
}
return false;
}
/**
* Get facebook shares count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get shares count
* @return int Returns count on success and false on failure.
*/
function wpd_fb_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = sprintf( 'https://graph.facebook.com/?id=%s', esc_url( $url ) );
$response = wpd_get_http_response( $request_url );
$count = $response && isset( $response['shares'] ) ? absint( $response['shares'] ) : false;
return $count;
}
/**
* Get twitter tweets count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get tweets count
* @return int Returns count on success and false on failure.
*/
function wpd_twitter_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = sprintf( 'https://cdn.api.twitter.com/1/urls/count.json?url=%s', esc_url( $url ) );
$response = wpd_get_http_response( $request_url );
$count = $response && isset( $response['count'] ) ? absint( $response['count'] ) : false;
return $count;
}
/**
* Get google +1 count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get +1 count
* @return int Returns count on success and false on failure.
*/
function wpd_gplus_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = 'https://clients6.google.com/rpc';
$body_args = array(
array(
'method' => 'pos.plusones.get',
'id' => 'p',
'params' => array(
'nolog' => true,
'id' => esc_url( $url ),
'source' => 'widget',
'userId' => '@viewer',
'groupId' => '@self'
),
'jsonrpc' => '2.0',
'key' => 'p',
'apiVersion' => 'v1'
)
);
$request_args = array(
'method' => 'POST',
'body' => json_encode( $body_args ),
'headers' => array(
'content-type' => ' application/json',
),
);
$response = wpd_get_http_response( $request_url, $request_args );
$count = $response && isset( $response[0]['result']['metadata']['globalCounts']['count'] ) ? absint( $response[0]['result']['metadata']['globalCounts']['count'] ) : false;
return $count;
}
/**
* Get pinterest pin count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get pin count
* @return int Returns count on success and false on failure.
*/
function wpd_pinterest_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = sprintf( 'https://api.pinterest.com/v1/urls/count.json?url=%s', esc_url( $url ) );
$response = wpd_get_http_response( $request_url, '', false );
if ( ! $response )
return false;
$response = json_decode( preg_replace( '/receiveCount\((.*)\)/', '$1', $response ), true );
if ( isset( $response['count'] ) )
$count = absint( $response['count'] );
return $count;
}
/**
* Get linkedin shares count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get shares count
* @return int Returns count on success and false on failure.
*/
function wpd_linkedin_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = sprintf( 'https://www.linkedin.com/countserv/count/share?url=%s&format=json', esc_url( $url ) );
$response = wpd_get_http_response( $request_url );
$count = $response && isset( $response['count'] ) ? absint( $response['count'] ) : false;
return $count;
}
/**
* Get stumbleupon shares count from url
*
* @uses wpd_get_http_response
*
* @param string $url URL to get shares count
* @return int Returns count on success and false on failure.
*/
function wpd_stumble_count( $url = '' ) {
// return if empty url
if ( ! $url )
return false;
$count = 0; // Initial Value
$request_url = sprintf( 'https://www.stumbleupon.com/services/1.01/badge.getinfo?url=%s', esc_url( $url ) );
$response = wpd_get_http_response( $request_url );
$count = $response && isset( $response['result']['views'] ) ? absint( $response['result']['views'] ) : false;
return $count;
}
/**
* Social Share with Count
* @param string $icons
* @param boolean $counter
* @param boolean $vertical
* @return srting
*/
function wpd_social_share($icons = '', $counter = false, $vertical = true ) {
if(empty($icons)) {
$active = array('googlePlus', 'facebook', 'twitter', 'pinterest', 'linkedin', 'stumbleupon');
} else {
$active = $icons;
}
global $post;
$link = get_permalink($post->ID);
$tw_handle = '';
$img = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
if($counter == true) {
$count = get_transient('wpd_shares_'.$post->ID);
if( false === $count || '' === $count ) {
$count = get_transient( 'wpd_shares_test' );
if( false === $count || '' === $count ) {
$count['googlePlus'] = wpd_gplus_count( $link );
$count['facebook'] = wpd_fb_count( $link );
$count['twitter'] = wpd_twitter_count( $link );
$count['pinterest'] = wpd_pinterest_count( $link );
$count['linkedin'] = wpd_linkedin_count( $link );
$count['stumbleupon'] = wpd_stumble_count( $link );
set_transient( 'wpd_shares_test', $count, 5*60 );
}
set_transient('wpd_shares_'.$post->ID, $count, 5*60);
}
}
$googlePlus = isset( $count['googlePlus'] ) ? $count['googlePlus'] : __('Google+', 'TEXT-DOMAIN');
$facebook = isset( $count['facebook'] ) ? $count['facebook'] : __('Facebook', 'TEXT-DOMAIN');
$twitter = isset( $count['twitter'] ) ? $count['twitter'] : __('Twitter', 'TEXT-DOMAIN');
$pinterest = isset( $count['pinterest'] ) ? $count['pinterest'] : __('Pinterest', 'TEXT-DOMAIN');
$linkedin = isset( $count['linkedin'] ) ? $count['linkedin'] : __('LinkedIn', 'TEXT-DOMAIN');
$stumbleupon = isset( $count['stumbleupon'] ) ? $count['stumbleupon'] : __('Stumbleupon', 'TEXT-DOMAIN');
if($vertical == true) { $vertical = 'vertical'; }
?>
<div class="wpd-social-share <?php echo $vertical ?>">
<ul>
<?php
if(is_array($active)) {
foreach($active as $id ){
switch($id){
case 'facebook':
?>
<li>
<a class="social-share-facebook" data-title="Facebook" target="_blank" href="http://www.facebook.com/sharer/sharer.php?u=<?php echo the_permalink(); ?>">
<span class="count"><?php echo $facebook ?></span>
</a>
</li>
<?php
break;
case 'googlePlus':
?>
<li>
<a class="social-share-googlePlus" data-title="Google+" target="_blank" href="https://plus.google.com/share?url=<?php echo the_permalink(); ?>">
<span class="count"><?php echo $googlePlus ?></span>
</a>
</li>
<?php
break;
case 'twitter':
?>
<li>
<a class="social-share-twitter" data-title="Twitter" target="_blank" href="https://twitter.com/intent/tweet?text=<?php echo the_title(); ?>&url=<?php echo the_permalink(); ?>&via=<?php echo $tw_handle ?>">
<span class="count"><?php echo $twitter ?></span>
</a>
</li>
<?php
break;
case 'pinterest':
?>
<li>
<a class="social-share-pinterest" data-title="Pinterest" target="_blank" href="http://pinterest.com/pin/create/button/?url=<?php echo the_permalink(); ?>&media=<?php echo $img[0] ?>">
<span class="count"><?php echo $pinterest ?></span>
</a>
</li>
<?php
break;
case 'linkedin':
?>
<li>
<a class="social-share-linkedin" data-title="Linkedin" target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&url=<?php echo the_permalink(); ?>&title=<?php echo the_title(); ?>">
<span class="count"><?php echo $linkedin ?></span>
</a>
</li>
<?php
break;
case 'stumbleupon':
?>
<li>
<a class="social-share-stumbleupon" data-title="StumbleUpon" target="_blank" href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&title=<?php the_title(); ?>">
<span class="count"><?php echo $stumbleupon ?></span>
</a>
</li>
<?php
}
}
} ?>
</ul>
</div>
<?php
}