unchanged
Title
Simple Share Widget for Facebook, twitter and google+
Simple Share Widget
------------------
This widget will show a Facebook "like" button and
"comments" box together with "tweet" button and "google
plusone" button on your page.
~~~
[php]
<?php
class SimpleShare extends CWidget
{
/**
* /**
* Site Name
*
* @var string
*
* defaults to Yii::app()->name
*/
public
*/
public $siteName = '';
/**
* /**
* Site Administrator's Facebook ID
*
* @var string
*/
public
*/
public $fbSiteAdmin = 'XXXXXXXXXXXXXX';
/**
* /**
* URL of the page
*
* @var string
*
* defaults to the current page URL
*/
public
*/
public $pageUrl = '';
/**
* /**
* Title of the page
*
* @var string
*/
public
*/
public $pageTitle = '';
/**
* /**
* Type of the Page : eg. website, article, ... etc.
*
* @var string
*
* defaults to 'article'
*/
public
*/
public $pageType = '';
/**
* /**
* Description of the page
*
* @var string
*/
public
*/
public $pageDescription = '';
/**
* /**
* Image(s) of the page
*
* @var mixed
*
* can be a single string or array of strings
*
* defaults $this->defaultPageImage
*/
public
*/
public $pageImages = '';
/**
* /**
* Default image of the page
*
* @var string
*/
public
*/
public $defaultPageImage = '/images/fb/site-logo.jpg';
/**
* /**
* Show Comments
*
* @var type boolean
*
* defaults to true
*/
public
*/
public $showComments = true;
/**
* Minimum IE version required
* @var string
*/
public $minimumIEVersion = '8'; /**
* Initialization
* @see CWidget::init()
*/
public function init()
{
parent::init();
/**
* Initialization
* @see CWidget::init()
*/
public function init()
{
parent::init(); // Site Name
if ($this->siteName == '')
{
$this->siteName = Yii::app()->name;
}
// Site Name
if ($this->siteName // base URL
$baseUrl = Yii::app()->request->hostInfo .
Yii::app()->request->baseUrl;
// URL of the page
if ($this->pageUrl == '') {
$this->siteName
{
$this->pageUrl = Yii::app()->name;
}$baseUrl . '/' . Yii::app()->request->pathInfo;
}
// base URL
$baseUrl = Yii::app()->request->hostInfo .
Yii::app()->request->baseUrl;
// URL // Type of the page
if ($this->pageUrl
if ($this->pageType == '') {
$this->pageUrl
{
$this->pageType = $baseUrl . '/' .
Yii::app()->request->pathInfo;
}'article';
}
// Type // Set opengraph meta tags
$cs = Yii::app()->getClientScript();
$cs->registerMetaTag($this->siteName, NULL, NULL,
array('property'=>'og:site_name'));
$cs->registerMetaTag($this->fbSiteAdmin, NULL, NULL,
array('property'=>'fb:admins'));
$cs->registerMetaTag($this->pageUrl, NULL, NULL, array('property'
=>'og:url'));
$cs->registerMetaTag($this->pageTitle, NULL, NULL,
array('property'=>'og:title'));
$cs->registerMetaTag($this->pageType, NULL, NULL,
array('property'=>'og:type'));
// Description of the page
if ($this->pageType
if ($this->pageDescription != "")
{
$cs->registerMetaTag($this->pageDescription, NULL, NULL,
array('property'=>'og:description'));
}
// Image(s) of the page
if (is_array($this->pageImages))
{
if (count($this->pageImages) == 0)
{
$this->pageImages = $this->defaultPageImage;
}
foreach($this->pageImages as $image)
{
$cs->registerMetaTag($baseUrl . $image, NULL, NULL, array('property'=>
'og:image'));
}
}
else
{
if ($this->pageImages == '') {
$this->pageType"")
{
$this->pageImages = 'article';
}$this->defaultPageImage;
}
$cs->registerMetaTag($baseUrl . $this->pageImages, NULL, NULL,
array('property'=> 'og:image'));
}
// Set opengraph meta tags
/** @var CClientScript $cs */
$cs = Yii::app()->getClientScript();
$cs->registerMetaTag($this->siteName, NULL, NULL,
array('property'=>'og:site_name'));
$cs->registerMetaTag($this->fbSiteAdmin, NULL, NULL,
array('property'=>'fb:admins'));
$cs->registerMetaTag($this->pageUrl, NULL, NULL, array('property'
=>'og:url'));
$cs->registerMetaTag($this->pageTitle, NULL, NULL,
array('property'=>'og:title'));
$cs->registerMetaTag($this->pageType, NULL, NULL,
array('property'=>'og:type'));
// Description of the page
if ($this->pageDescription != "") // javascript
for facebook
// 'ja_JP' should be replaced according to you locale
$fb_js =<<< EOS
(function(d, s, id) {
$cs->registerMetaTag($this->pageDescription, NULL, NULL,
array('property'=>'og:description'));
}
// Image(s) of the page
if (is_array($this->pageImages)) {
if (count($this->pageImages) == 0) {
$this->pageImages
var js, fjs = $this->defaultPageImage;
}
foreach($this->pageImages as $image) {
$cs->registerMetaTag($baseUrl . $image, NULL, NULL,
array('property'=> 'og:image'));
}
} else {
if ($this->pageImages == "") {
$this->pageImagesd.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = $this->defaultPageImage;
}
$cs->registerMetaTag($baseUrl . $this->pageImages, NULL, NULL,
array('property'=> 'og:image'));
}"https://connect.facebook.net/ja_JP/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
EOS;
$cs->registerScript('fb-jssdk', $fb_js, CClientScript::POS_BEGIN);
$cs->registerScriptFile('//platform.twitter.com/widgets.js',
CClientScript::POS_HEAD);
}
// javasctipt to enable the gadgets
$init_js = <<< INIT_JS
var msie = navigator.appVersion.toLowerCase();
msie = (msie.indexOf('msie')>-1) ? parseInt(msie.replace(/.*msie[
]/,'').match(/^[0-9]+/)) : 0;
if (msie == 0 || msie >= $this->minimumIEVersion) {
$('#sns-share').show();
// google plus one
window.___gcfg = {
lang: 'ja'
};
(function() {
var po = document.createElement('script');
po.type = 'text/javascript';
po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(po, s);
})();
// twitter
!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');
// facebook
(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/ja_JP/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
}
INIT_JS;
$cs->registerScript('init-sns-share', $init_js,
CClientScript::POS_READY);
}
/**
* /**
* Display the widget
*
* @see CWidget::run()
*/
public
*/
public function run()
{
echo '<div id="sns-share"
style="display:none">' . "\n";
echo
{
echo '<h3>Share the page with Facebook, twitter and google
plusone</h3>' . "\n";
echo
echo '<div class="sns-share-buttons">' . "\n";
// // google plusone
echo
echo '<script type="text/javascript"
src="https://apis.google.com/js/plusone.js">{lang:
\'ja\'}</script>' . "\n";
/* replace 'ja' with your language */
echo '<div class="google-plus"
style="float:right">' . "\n";
echo
echo '<g:plusone size="medium" href="' .
$this->pageUrl . '"></g:plusone>' . "\n";
echo
echo '</div>' . "\n";
// // twitter
$tw_text
$tw_text = $this->siteName . ' - ' . $this->pageTitle;
if
if ( $this->pageDescription != '')
{
$tw_text
{
$tw_text .= ' : ' . $this->pageDescription;
}
echo
}
echo '<div class="tweet-button"
style="float:right">' . "\n";
echo
echo '<a href="https://twitter.com/share" '
.
. 'class="twitter-share-button" '
.
. 'data-url="' . $this->pageUrl . '" '
.
. 'data-text="' . $tw_text . '" '
.
. 'data-count="horizontal">Tweet</a>' .
"\n";
echo
echo '</div>' . "\n";
// // facebook
echo
echo '<div class="fb-like" '
.
. 'data-href="' . $this->pageUrl . '" '
.
. 'data-send="true" '
.
. 'data-width="500" '
.
. 'data-show-faces="false"></div>' .
"\n";
if
if ($this->showComments)
{
echo
{
echo '</div>' . "\n";
echo
echo '<div class="facebook-comments">' .
"\n";
echo
echo '<div class="fb-comments" '
.
. 'data-href="' . $this->pageUrl . '" '
.
. 'data-num-posts="4" '
.
. 'data-width="600"></div>' . "\n";
}
echo
}
echo '</div>' . "\n";
echo '</div>' . "\n";
}
}
}
~~~
Usage
------------------
You can put the widget anywhere in your view like the following.
~~~
[php]
<?php
$this->widget('SimpleShare', array(
'pageTitle' 'pageTitle' => 'The title of the
page.',
'pageDescription'
'pageDescription' => 'The long descriptions of the page.',
'pageType'
'pageType' => 'article',
'pageImages'
'pageImages' => array('/images/001.jpg', '/images/002.jpg'),
));
?>
~~~
'pageImages' can be an array of strings (up to 5 images).
You may want to use the default values for 'pageType' and 'pageImages'.
~~~
[php]
<?php
$this->widget('SimpleShare', array(
'pageTitle' 'pageTitle' => 'The title of the
page.',
'pageDescription'
'pageDescription' => 'The long descriptions of the page.',
));
?>
~~~
Notice
------------------
This is a kind of **quick and dirty** code just to satisfy the specific needs of
mine. But I hope it will show the basic know-hows to implement those gadgets in
an yii-powered site.
You may want to customize the widget according to your needs, or want to make it
much more flexible and generic.
And also you would like to give some tries to these extensions.
- [social](http://www.yiiframework.com/extension/social/ "social")
- [wsocialbutton](http://www.yiiframework.com/extension/wsocialbutton/
"wsocialbutton")
- [faceplugs](http://www.yiiframework.com/extension/faceplugs
"faceplugs")