Instantly share code, notes, and snippets.
Last active
August 29, 2015 14:16
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save isarmstrong/1e75003895af580812cb to your computer and use it in GitHub Desktop.
A class file that outputs social sharing buttons for WordPress, instantiated by array
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 | |
/** | |
* Class social_share | |
* @author : Ian Armstrong (@imperativeideas) | |
* A class file that outputs social sharing buttons for WordPress, instantiated by array | |
*/ | |
/* Sample Usage: | |
$socialshares = new social_share(array( | |
'facebook' => '', | |
'linkedin' => '', | |
'google' => '', | |
'twitter' => array( | |
'text' => get_the_title(), | |
'hashtags' => naked_cats(), // function outputs cats without links | |
), | |
'pinterest' => array( | |
'media' => wp_get_attachment_url( get_post_thumbnail_id($post->ID) ), | |
'description' => strip_tags(strip_shortcodes(get_the_excerpt())), | |
), | |
)); | |
echo $socialshares; | |
*/ | |
class social_share { | |
protected $url; | |
protected $networks = array(); | |
public $shareset = array(); | |
/** | |
* Constructor | |
* @param array $networks :: networks for sharing are supplied by each page on instantiation | |
*/ | |
function __construct($networks = array()) { | |
global $post; | |
$this->url = get_the_permalink($post->ID); | |
// Ensure that this is actually an array | |
if (!array($networks)) { | |
trigger_error('Unable to construct social output in ' . get_class($this) . 'using the given input'); | |
} | |
// If there is at least one value, populate | |
if (count($networks) > 0) { | |
foreach ($networks as $name => $meta) { | |
$this->networks[] = $name; | |
$this->shareset[$name] = $this->make_share_link($name, $this->url, $meta); | |
} | |
} | |
} | |
/** | |
* Outputs formatted links that target social sites with the shared content request | |
* @param $network | |
* @param $url | |
* @param bool $meta | |
* @return null|string | |
*/ | |
private function make_share_link($network, $url, &$meta=false) { | |
$share_link = NULL; | |
switch($network) { | |
// Facebook is nice and easy | |
case 'facebook' : | |
$share_link = "https://www.facebook.com/sharer.php?u=" . $url; | |
break; | |
// LinkedIn also keeps things on the smooth tip | |
case 'linkedin' : | |
$share_link = "https://www.linkedin.com/shareArticle?mini=true&url=" . $url; | |
break; | |
// Google comes in with a "me too" | |
case 'google' : | |
$share_link = "https://plus.google.com/share?url=". $url; | |
break; | |
// Twitter is more complex with its hash tag array, based on post categories (if available) | |
case 'twitter' : | |
$tweet = ""; | |
$hashtags = ""; | |
if(array_key_exists('hashtags', $meta)) { | |
$count = count($meta['hashtags']); | |
$i = 0; | |
foreach( $meta['hashtags'] as $key => $value ) { | |
$hashtags .= htmlspecialchars($value); | |
$i++; | |
if ( $i != $count ) { | |
$hashtags .= ","; | |
} | |
} | |
} | |
if(!empty($meta['text'])) { | |
$tweet = htmlspecialchars($meta['text']); | |
} | |
$share_link = "http://twitter.com/share?url=" . $url; | |
if(!empty($tweet)) : $share_link .= "&text=" . $tweet; endif; | |
if(!empty($hashtags)) : $share_link .= "&hashtags=" . $hashtags; endif; | |
break; | |
// Pinterest - ignore the API docs, it's easier than all that noise | |
case 'pinterest' : | |
if(empty($meta['media'])) { | |
$share_link = NULL; | |
} else { | |
$media = $meta['media']; | |
} | |
if(!empty($meta['description'])) { | |
$description = htmlspecialchars($meta['description']); | |
} | |
$share_link = "http://pinterest.com/pin/create/link/?url=" . urlencode($url); | |
if(!empty($media)) : $share_link .= "&media=" . urlencode($media); endif; | |
if(!empty($description)) : $share_link .= "&description=" . urlencode($description); endif; | |
break; | |
default : | |
$share_link = NULL; | |
} | |
return $share_link; | |
} | |
/** | |
* Allow the class name to be echoed directly without calling the display method | |
* @return string | |
*/ | |
function __toString() { | |
return $this->display(); | |
} | |
/** | |
* Display the class result as HTML | |
* @return string | |
*/ | |
public function display() { | |
$output = "<div class='output-social'><ul>"; | |
foreach( $this->shareset as $name => $link ) { | |
if( !empty($link)) : | |
$output .= "<li class='{$name}'>"; | |
$output .= "<i class='icon-{$name}'></i>"; | |
$output .= "<a href='{$link}' target='_blank'></a>"; | |
endif; | |
} | |
$output .= "</ul></div>"; | |
return $output; | |
} | |
} | |
/** | |
* Get a simple array of assigned categories without links | |
* @return array | |
* Used by: class.social-share.inc | |
*/ | |
function naked_cats() { | |
global $post; | |
$thecats = get_the_category($post->ID); | |
$term_list = array(); | |
foreach ($thecats as $term) { | |
foreach ($term as $key => $named_cat) { | |
if('cat_name' == $key ) { | |
$term_list[] = $named_cat; | |
} | |
} | |
} | |
return $term_list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can find some additional share strings here, if you want to expand this for Reddit, Digg, etc
https://simplesharebuttons.com/html-share-buttons/