Skip to content

Commit

Permalink
Social share widget is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
greeflas committed Oct 10, 2016
1 parent c7b2ba7 commit 3e71129
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 80 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
Social share widget for Yii2
============================
INSTALATION
-----------
#### Composer require section
```javascript
Installation
------------
#### Run command
```
composer require black-lamp/yii2-socialshare
```
or add
```json
"black-lamp/yii2-socialshare": "*"
```
### Add SocalShare component to application config
#### Add 'SocialShare' component to application config
```php
'components' => [
...
// ...
'socialShare' => [
'class' => bl\socialShare\SocialShare::className(),
'networks' => [
'facebook' => [
'class' => bl\socialShare\classes\Facebook::className(),
// not required
'label' => 'Facebook',
// html attributes
'attributes' => [
'class' => 'social-share'
]
Expand Down Expand Up @@ -47,13 +53,14 @@ INSTALATION
]
]
```
USING
Using
-----
```php
bl\socialShare\widgets\SocialShareWidget::widget([
'url' => 'http://example.com/',
'title' => 'Web site title',
'description' => 'Web site description...',
'image' => 'url to image'
'componentId' => 'socialShare', // id of component from config
'url' => 'http://example.com/', // url to your web site
'title' => 'Page title',
'description' => 'Page description...',
'image' => 'url/to/image'
])
```
72 changes: 31 additions & 41 deletions SocialShare.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,40 @@

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* Installation
* Add this component to application config
* ```php
* 'components' => [
* // ...
* 'socialShare' => [
* 'class' => bl\socialShare\SocialShare::className(),
* 'networks' => [
* 'facebook' => [
* 'class' => bl\socialShare\classes\Facebook::className(),
* // not required
* 'label' => 'Facebook',
* // html attributes
* 'attributes' => [
* 'class' => 'social-btn' // html class
* ]
* ],
* 'twitter' => [
* 'class' => bl\socialShare\classes\Twitter::className(),
* 'label' => 'Twitter',
* 'account' => 'username',
* 'attributes' => [
* 'class' => 'social-btn'
* ]
* ],
* // other social networks ...
* ]
* ]
* ]
* ```
*/
class SocialShare extends Component
{
/**
* Configuration array
* Example
* ```php
* 'socialShare' => [
* 'class' => bl\socialShare\SocialShare::className(),
* 'networks' => [
* 'facebook' => [
* 'class' => bl\socialShare\classes\Facebook::className(),
* 'label' => 'Facebook',
* 'attributes' => [
* 'class' => 'social-share'
* ]
* ],
* 'twitter' => [
* 'class' => bl\socialShare\classes\Twitter::className(),
* 'label' => 'Twitter',
* 'account' => 'twitterAccount',
* 'attributes' => [
* 'class' => 'social-share'
* ]
* ],
* 'googlePlus' => [
* 'class' => bl\socialShare\classes\GooglePlus::className(),
* 'label' => 'Google+',
* 'attributes' => [
* 'class' => 'social-share',
* 'id' => 'google'
* ]
* ],
* 'vk' => [
* 'class' => bl\socialShare\classes\Vkontakte::className(),
* 'label' => 'vk',
* 'attributes' => [
* 'class' => 'social-share'
* ]
* ]
* ]
* ]
* ```
*/
public $networks = [];

/**
Expand Down
8 changes: 5 additions & 3 deletions base/SocialNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ abstract class SocialNetwork extends Object
*/
public $label;
/**
* Attributes for tag <a>
* HTML attributes for tag <a>
* @var array $attributes
*/
public $attributes = [];

/**
* Method for add meta tags to <head> from array
* Method for adding meta tags to <head> from array
*
* @param array $metaTags
*/
protected function addMetaTags($metaTags = []) {
Expand All @@ -37,7 +38,8 @@ protected function addMetaTags($metaTags = []) {

/**
* Method for getting link to the social network
* @param string $url Url to yor website
*
* @param string $url Url to your website
* @param string $title Title of the page
* @param string $description Page description
* @param string $image Link to image
Expand Down
9 changes: 7 additions & 2 deletions classes/Facebook.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
namespace bl\socialShare\classes;

use bl\socialShare\base\SocialNetwork;
use yii\helpers\Html;

use bl\socialShare\base\SocialNetwork;

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* @property string $_link
* @property string $label
* @property array $attributes
*/
class Facebook extends SocialNetwork
{
Expand All @@ -14,7 +19,7 @@ class Facebook extends SocialNetwork
* @param string $title
* @param string $description
* @param string $image
* @return string
* @return string HTML link tag
*/
public function getLink($url, $title, $description, $image) {
$this->_link = "http://www.facebook.com/sharer.php?u=$url";
Expand Down
9 changes: 7 additions & 2 deletions classes/GooglePlus.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
namespace bl\socialShare\classes;

use bl\socialShare\base\SocialNetwork;
use yii\helpers\Html;

use bl\socialShare\base\SocialNetwork;

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* @property string $_link
* @property string $label
* @property array $attributes
*/
class GooglePlus extends SocialNetwork
{
Expand All @@ -14,7 +19,7 @@ class GooglePlus extends SocialNetwork
* @param string $title
* @param string $description
* @param string $image
* @return string
* @return string HTML link tag
*/
public function getLink($url, $title, $description, $image) {
$this->_link = "https://plusone.google.com/_/+1/confirm?hl=en&url=$url";
Expand Down
6 changes: 5 additions & 1 deletion classes/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* @property string $_link
* @property string $label
* @property array $attributes
*/
class Twitter extends SocialNetwork
{
Expand All @@ -16,7 +20,7 @@ class Twitter extends SocialNetwork
* @param string $title
* @param string $description
* @param string $image
* @return string
* @return string HTML link tag
*/
public function getLink($url, $title, $description, $image) {
$this->_link = "http://twitter.com/share?url=$url&text=$description&via=$this->account";
Expand Down
9 changes: 7 additions & 2 deletions classes/Vkontakte.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
namespace bl\socialShare\classes;

use bl\socialShare\base\SocialNetwork;
use yii\helpers\Html;

use bl\socialShare\base\SocialNetwork;

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* @property string $_link
* @property string $label
* @property array $attributes
*/
class Vkontakte extends SocialNetwork
{
Expand All @@ -14,7 +19,7 @@ class Vkontakte extends SocialNetwork
* @param string $title
* @param string $description
* @param string $image
* @return string
* @return string HTML link tag
*/
public function getLink($url, $title, $description, $image) {
$this->_link = "http://vk.com/share.php?url=$url&title=$title&description=$description&image=$image";
Expand Down
46 changes: 28 additions & 18 deletions widgets/SocialShareWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,46 @@

use yii;
use yii\base\Widget;
use frontend\components\socialShare;
use frontend\components\socialShare\base\SocialNetwork;

use bl\socialShare\SocialShare;
use bl\socialShare\base\SocialNetwork;

/**
* @author Vladimir Kuprienko <vldmr.kuprienko@gmail.com>
*
* @link https://github.com/black-lamp/yii2-socialshare
* @license https://opensource.org/licenses/GPL-3.0 GNU Public License
*
* @property string $componentId Id of SocialShare component
* @property string $url Url to yor website
* @property string $title Title of the page
* @property string $description Page description
* @property string $image Link to image
*
* @see SocialShare
*/
class SocialShareWidget extends Widget
{
/**
* @var string $url Url to yor website
*/
public $componentId;
public $url;
/**
* @var string $title Title of the page
*/
public $title;
/**
* @var string $description Page description
*/
public $description;
/**
* @var string $image Link to image
*/
public $image;

protected $_links = [];

/**
* @inheritdoc
*/
public function init() {
$networks = Yii::$app->socialShare->getNetworks();
$component = $this->componentId;
$networks = Yii::$app->$component->getNetworks();

foreach($networks as $network) {
/** @var SocialNetwork $networkClassObj */
$networkClassObj = Yii::createObject($network);
$this->_links[] = $networkClassObj->getLink(
/** @var SocialNetwork $networkObj */
$networkObj = Yii::createObject($network);

$this->_links[] = $networkObj->getLink(
$this->url,
$this->title,
$this->description,
Expand All @@ -45,7 +51,11 @@ public function init() {
}
}

/**
* @inheritdoc
*/
public function run() {
parent::run();
return $this->render('social_buttons', [
'links' => $this->_links
]);
Expand Down

0 comments on commit 3e71129

Please sign in to comment.