diff --git a/README.md b/README.md index 9da1676..df4b241 100644 --- a/README.md +++ b/README.md @@ -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' ] @@ -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' ]) ``` \ No newline at end of file diff --git a/SocialShare.php b/SocialShare.php index 2441828..4f90fb6 100644 --- a/SocialShare.php +++ b/SocialShare.php @@ -5,50 +5,40 @@ /** * @author Vladimir Kuprienko + * + * 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 = []; /** diff --git a/base/SocialNetwork.php b/base/SocialNetwork.php index d8ec157..d2c55a9 100644 --- a/base/SocialNetwork.php +++ b/base/SocialNetwork.php @@ -20,13 +20,14 @@ abstract class SocialNetwork extends Object */ public $label; /** - * Attributes for tag + * HTML attributes for tag * @var array $attributes */ public $attributes = []; /** - * Method for add meta tags to from array + * Method for adding meta tags to from array + * * @param array $metaTags */ protected function addMetaTags($metaTags = []) { @@ -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 diff --git a/classes/Facebook.php b/classes/Facebook.php index e4441e7..c06ac07 100644 --- a/classes/Facebook.php +++ b/classes/Facebook.php @@ -1,11 +1,16 @@ + * + * @property string $_link + * @property string $label + * @property array $attributes */ class Facebook extends SocialNetwork { @@ -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"; diff --git a/classes/GooglePlus.php b/classes/GooglePlus.php index 633b7c6..fe04073 100644 --- a/classes/GooglePlus.php +++ b/classes/GooglePlus.php @@ -1,11 +1,16 @@ + * + * @property string $_link + * @property string $label + * @property array $attributes */ class GooglePlus extends SocialNetwork { @@ -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"; diff --git a/classes/Twitter.php b/classes/Twitter.php index 5268950..b40238c 100644 --- a/classes/Twitter.php +++ b/classes/Twitter.php @@ -6,6 +6,10 @@ /** * @author Vladimir Kuprienko + * + * @property string $_link + * @property string $label + * @property array $attributes */ class Twitter extends SocialNetwork { @@ -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"; diff --git a/classes/Vkontakte.php b/classes/Vkontakte.php index a4892e1..138b90a 100644 --- a/classes/Vkontakte.php +++ b/classes/Vkontakte.php @@ -1,11 +1,16 @@ + * + * @property string $_link + * @property string $label + * @property array $attributes */ class Vkontakte extends SocialNetwork { @@ -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"; diff --git a/widgets/SocialShareWidget.php b/widgets/SocialShareWidget.php index 7d45fb4..2d383f1 100644 --- a/widgets/SocialShareWidget.php +++ b/widgets/SocialShareWidget.php @@ -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 + * + * @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, @@ -45,7 +51,11 @@ public function init() { } } + /** + * @inheritdoc + */ public function run() { + parent::run(); return $this->render('social_buttons', [ 'links' => $this->_links ]);