Skip to content
This repository has been archived by the owner on Dec 17, 2019. It is now read-only.

Commit

Permalink
Updated user inputted options to work correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Chapman committed May 27, 2014
1 parent 8b76bbb commit 88e164e
Showing 1 changed file with 67 additions and 58 deletions.
125 changes: 67 additions & 58 deletions craftysocialshare/twigextensions/CraftySocialShareTwigExtension.php
Expand Up @@ -3,9 +3,7 @@

// Define our script instead of rewriting them everywhere
define("FACEBOOK_SCRIPT", "<div id=\"fb-root\"></div><script>(function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = \"//connect.facebook.net/en_US/all.js#xfbml=1&appId=543069402473732\"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script>");

define("TWITTER_SCRIPT", "<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=\"https://platform.twitter.com/widgets.js\";fjs.parentNode.insertBefore(js,fjs);}}(document,\"script\",\"twitter-wjs\");</script>");

define("GOOGLE_SCRIPT", "<script type=\"text/javascript\">(function() {var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/platform.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script>");


Expand All @@ -26,12 +24,11 @@ public function getFunctions()
}


/*
* @param
* $scripts(string) - pipe delimited list of scripts to include
/**
* Displays scripts markup
*
* @return
* $scripts(string) = echos appropriate script tags
* @param string $scriptList Pipe delimited list of scripts to show (facebook, twitter, google)
* @return html Button scripts markup
*/
public function getSocialShareScripts($scriptList = null)
{
Expand All @@ -43,17 +40,11 @@ public function getSocialShareScripts($scriptList = null)

foreach ($scriptList as $script) {
if ( $script == 'facebook' ) {

$scripts .= FACEBOOK_SCRIPT;

} elseif ( $script == 'twitter' ) {

$scripts .= TWITTER_SCRIPT;

} elseif ( $script == 'google' ) {

$scripts .= GOOGLE_SCRIPT;

}
} // end foreach

Expand All @@ -67,50 +58,66 @@ public function getSocialShareScripts($scriptList = null)
}


/*
* @param
* $type(string, required) - Type of button to display
* $url(string, optional) - URL to use for button (default is current request url)
* $opts(array, optional) - override parameters for button data attributes
/**
* Displays social button of specified type
*
* @return
* (string) - echos appropriate button markup
* @param string $type Type of button to display (facebook, twitter, google)
* @param array $options User supplied options array
* @return html Button markup
*/
public function getSocialBtn($type = null, $opts = null) {

$url = $this->checkOpt($opts['url'], craft()->request->getHostInfo() . craft()->request->getUrl());
public function getSocialBtn($type = null, $options = array()) {
$button = '';

switch ($type) {
case 'facebook':

if ( $opts !== null ) {

$button = "<div class=\"fb-like\" data-href=". $url ." data-width=\"{$this->checkOpt($opts["width"], '100px')}\" data-layout=\"{$this->checkOpt($opts['layout'], 'button')}\" data-action=\"{$this->checkOpt($opts["action"], 'like')}\" data-show-faces=\"{$this->checkOpt($opts['faces'], 'false')}\" data-share=\"{$this->checkOpt($opts['share'], 'false')}\"></div>";

} else {
$button = "<div class=\"fb-like\" data-href=". $url ." data-width=\"100px\" data-layout=\"button_count\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"></div>";
}
$defaults = array(
'url' => craft()->request->getHostInfo() . craft()->request->getUrl(),
'width' => '150px',
'layout' => 'button_count',
'action' => 'like',
'show-faces' => 'false',
'share' => 'false'
);
$opts = $this->setOpts($defaults, $options);
$button = "<div class=\"fb-like\" data-href=". $opts["url"] ." data-width=\"{$opts["width"]}\" data-layout=\"{$opts["layout"]}\" data-action=\"{$opts["action"]}\" data-show-faces=\"{$opts['show-faces']}\" data-share=\"{$opts['share']}\"></div>";

break;

case 'twitter':

if ( $opts !== null ) {
$button = "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-size=\"{$this->checkOpt($opts["size"], '100px')}\" data-via=\"{$this->checkOpt($opts["via"], '')}\" data-url=\"{$this->checkOpt($opts["url"], $url)}\" data-lang=\"{$this->checkOpt($opts["lang"], 'en')}\" data-text=\"{$this->checkOpt($opts["text"], '')}\" data-related=\"{$this->checkOpt($opts["related"], '')}\" data-count=\"{$this->checkOpt($opts["count"], 'none')}\" data-counturl=\"{$this->checkOpt($opts["counturl"], $url)}\" data-hashtags=\"{$this->checkOpt($opts["hashtags"], "")}\" data-dnt=\"{$this->checkOpt($opts["opt-out"], "false")}\">Tweet</a>";
} else {
$button = "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-size=\"100px\" data-url=". $url ." data-lang=\"en\">Tweet</a>";
}
$defaults = array(
'url' => craft()->request->getHostInfo() . craft()->request->getUrl(),
'size' => '100px',
'via' => '',
'lang' => 'en',
'text' => '',
'related' => '',
'count' => 'none',
'counturl' => craft()->request->getHostInfo() . craft()->request->getUrl(),
'hashtags' => '',
'opt-out' => 'false'
);
$opts = $this->setOpts($defaults, $options);
$button = "<a href=\"https://twitter.com/share\" class=\"twitter-share-button\" data-size=\"{$opts["size"]}\" data-via=\"{$opts["via"]}\" data-url=\"{$opts["url"]}\" data-lang=\"{$opts["lang"]}\" data-text=\"{$opts["text"]}\" data-related=\"{$opts["related"]}\" data-count=\"{$opts["count"]}\" data-counturl=\"{$opts["counturl"]}\" data-hashtags=\"{$opts["hashtags"]}\" data-dnt=\"{$opts["opt-out"]}\">Tweet</a>";

break;

case 'google':
if ( $opts !== null ) {
$button = "<div class=\"g-plusone\" data-href=\"{$this->checkOpt($opts["href"], $url)}\" data-size=\"{$this->checkOpt($opts["size"], 'standard')}\" data-annotation=\"{$this->checkOpt($opts["annotation"], 'bubble')}\" data-width=\"{$this->checkOpt($opts["width"], '120')}\" data-align=\"{$this->checkOpt($opts["align"], 'left')}\" expandTo=\"{$this->checkOpt($opts["expandTo"], '')}\" data-recommendations=\"{$this->checkOpt($opts["recommendations"], 'true')}\" data-count=\"{$this->checkOpt($opts["count"], 'true')}\"></div>";
} else {
$button = "<div class=\"g-plusone\" data-annotation=\"inline\" data-width=\"120px\"></div>";
}
$defaults = array(
'href' => craft()->request->getHostInfo() . craft()->request->getUrl(),
'size' => 'standard',
'annotation' => 'bubble',
'width' => '120px',
'align' => 'left',
'expandTo' => '',
'recommendations' => 'true',
'count' => 'true'
);
$opts = $this->setOpts($defaults, $options);
$button = "<div class=\"g-plusone\" data-href=\"{$opts["href"]}\" data-size=\"{$opts["size"]}\" data-annotation=\"{$opts["annotation"]}\" data-width=\"{$opts["width"]}\" data-align=\"{$opts["align"]}\" expandTo=\"{$opts["expandTo"]}\" data-recommendations=\"{$opts["recommendations"]}\" data-count=\"{$opts["count"]}\"></div>";
break;

default:
$button = null;
break;
Expand All @@ -120,24 +127,22 @@ public function getSocialBtn($type = null, $opts = null) {
}


/*
* @param
* $type(string) - Type of button to display
* $url(string) - URL to use for button (default is current request url)
* $width(string) - Width in pixels for button to be
/**
* Displays html for user supplied list of buttons to show
*
* @return
* $button(string) - echos appropriate button markup
* @param array $btnList Pipe delimited list of buttons to display
* @param string $width Size to display buttons
* @return mixed Buttons to display html
*/
public function getSocialBtns($btnList = null, $width = '120')
{
$buttons = ''; // our return list of markup
$buttons; // our return list of markup
$btnList = explode('|', $btnList); // pipe delimited list of buttons to include
$url = craft()->request->getHostInfo() . craft()->request->getUrl(); // Get Current URL

if ( $btnList != null ) {
foreach ($btnList as $btn) {

if ( $btn == 'facebook' ) {

$buttons .= "<div class=\"fb-like\" data-href=". $url ." data-width=". $width ." data-layout=\"button_count\" data-action=\"like\" data-show-faces=\"false\" data-share=\"false\"></div>";
Expand Down Expand Up @@ -165,14 +170,18 @@ public function getSocialBtns($btnList = null, $width = '120')
}



public function checkOpt($var, $fallback) {
if ( $var == null ) {
return $fallback;
} else {
return $var;
/**
* Merges default options with user supplied options
*
* @param array $defaults default options array set within switch case
* @param array $opts user supplied options array
* @return array newly merged array of button options
*/
public function setOpts($defaults, $opts) {
foreach( $opts as $key => $value ) {
$defaults[$key] = $value;
}
return $defaults;
}


}
}

0 comments on commit 88e164e

Please sign in to comment.