Skip to content

Instantly share code, notes, and snippets.

@stnvh
Created April 17, 2015 10:23
Show Gist options
  • Save stnvh/8d801d782013003a8804 to your computer and use it in GitHub Desktop.
Save stnvh/8d801d782013003a8804 to your computer and use it in GitHub Desktop.
Simple social buttons wrapper for SilverStripe
<% require javascript('https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.0') %>
<div class="{$ClassName}">
<div id="fb-root"></div>
<div class="fb-share-button" data-href="{$URL}" data-layout="<% if Type %>{$Type}<% else %>button<% end_if %>"></div>
</div>
<% require javascript('https://apis.google.com/js/platform.js') %>
<div class="{$ClassName}">
<div class="g-plus" data-href="{$URL}" data-action="share" data-annotation="<% if Type %>{$Type}<% else %>none<% end_if %>"></div>
</div>
<% require javascript('http://platform.linkedin.com/in.js') %>
<div class="{$ClassName}">
<script type="IN/Share" data-url="{$URL}" <% if Type %>data-counter="{$Type}"<% end_if %>></script>
</div>
<?php
/**
* Social buttons
* To populate template vars in button template, use the following syntax:
* Social(Facebook[URL:{$Link}][Type:icon])
* Each argument is wrapped in [] e.g
* Social(Facebook[Name:Value][NameToo:ValueToo], Twitter, Googleplus)
*
* Default types: * = default
* Facebook:
* - Type: box_count | button_count | button* | icon_link | icon | link
* - URL
* Twitter:
* - Type: right | null*
* - Size: large | null*
* - Content
* - URL
* Linkedin:
* - Type: top | right | none*
* - URL
* Googleplus:
* - Type: bubble | vertical-bubble | inline | null*
* - Size: small | medium* | large | <int>
*/
class SocialButtons extends ViewableData implements TemplateGlobalProvider {
public static $class_format = 'social {$Name}';
public static function get_template_global_variables() {
return array (
'AllSocial',
'Social'
);
}
/**
* Static wrapper for self::Social to include all - used in templates
* @return string
*/
public static function AllSocial() {
return self::Social('Facebook', 'Twitter', 'Googleplus', 'Linkedin');
}
/**
* Static wrapper for $this->populateTemplates - used in templates
* @param mixed $types,... The strings to evaluate
* @return string
*/
public static function Social($types) {
return call_user_func_array(array(self::create(), 'populateTemplates'), func_get_args());
}
/**
* Concats HTML from an array of SocialElements
* @param mixed $types,... The strings to evaluate
* @return string
*/
public function populateTemplates($types) {
$socials = array_filter(array_map(array($this, 'parseSocial'), func_get_args()));
$output = HTMLText::create();
foreach($socials as $item) {
$output->value .= $item->forTemplate();
}
return $output;
}
/**
* Parses each template argument and evaluates any arguments
* @param string $item The string to evaluate
* @return SocialElement
*/
private function parseSocial($item) {
if(preg_match('/^(twitter|facebook|googleplus|linkedin)(?:\[(.*)\])?$/i', ucfirst($item), $match)) {
$data = array(
'URL' => Director::get_current_page()->AbsoluteLink(),
'ClassName' => preg_replace('/\{\$Name\}/', strtolower($match[1]), self::$class_format)
);
if(isset($match[2]) && $args = $match[2]) {
foreach(explode('][', $args) as $arg) {
$arg = explode(':', $arg);
$data[$arg[0]] = SSViewer::execute_string($arg[1], null);
}
}
return SocialElement::create($match[1], $data);
}
}
}
class SocialElement extends ViewableData {
/**
* @param string $name The item & template name
* @param mixed $customFields Fields to customise the template with.Passed directly to
* SSViewer->customise so same type casting applies.
* @return SocialElement
*/
public function __construct($name, $customFields = array(), $require = false) {
$this->template = $name;
$this->fields = $customFields;
}
/**
* Returns the customised template for the social element
* @return string
*/
public function forTemplate() {
$template = new SSViewer('social/' . ucfirst($this->template));
return $template->process($this->customise($this->fields));
}
}
<div class="{$ClassName}">
<a href="https://twitter.com/share" class="twitter-share-button" data-url="{$URL}" <% if Size == 'large' %>data-size="large"<% end_if %> <% if Content %>data-text="{$Content}"<% end_if %> <% if not Type %>data-count="none"<% end_if %> data-dnt="true">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment