Skip to content

Commit

Permalink
Added 'LinkedIn' social network and small code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
greeflas committed Oct 18, 2016
1 parent 0426177 commit 17a8b24
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 32 deletions.
31 changes: 25 additions & 6 deletions base/SocialNetwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@

/**
* @author Vladimir Kuprienko <[email protected]>
*
* @property string $label
* @property array $attributes
*/
abstract class SocialNetwork extends Object
{
/**
* Link to the social network
* @var string $_link
*/
protected $_link;
/**
* Social network link label
* @var string $label
Expand All @@ -26,18 +24,39 @@ abstract class SocialNetwork extends Object
* @var array $attributes
*/
public $attributes = [];
/**
* Link to the social network
* @var string $_link
*/
protected $_link;

/**
* Method for adding meta tags to <head> from array
*
* @param array $metaTags
*/
protected function addMetaTags($metaTags = []) {
protected function addMetaTags($metaTags = [])
{
foreach($metaTags as $tagOptions) {
Yii::$app->view->registerMetaTag($tagOptions);
}
}

/**
* Method for adding custom HTML attributes to the social share link
*
* @param array $htmlAttrs
*/
protected function addCustomAttributes($htmlAttrs)
{
$this->attributes['target'] = '_blank';
if(!empty($htmlAttrs)) {
foreach ($htmlAttrs as $name => $value) {
$this->attributes[$name] = $value;
}
}
}

/**
* Method for getting link to the social network
*
Expand Down
7 changes: 1 addition & 6 deletions classes/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ public function getLink($url, $title, $description, $image, $htmlAttrs) {
];
$this->addMetaTags($metaTags);

$this->attributes['target'] = '_blank';
if(!empty($htmlAttrs)) {
foreach ($htmlAttrs as $name => $value) {
$this->attributes[$name] = $value;
}
}
$this->addCustomAttributes($htmlAttrs);

return Html::a($this->label, $this->_link, $this->attributes);
}
Expand Down
7 changes: 1 addition & 6 deletions classes/GooglePlus.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ public function getLink($url, $title, $description, $image, $htmlAttrs)
{
$this->_link = "https://plusone.google.com/_/+1/confirm?hl=en&url=$url";

$this->attributes['target'] = '_blank';
if(!empty($htmlAttrs)) {
foreach ($htmlAttrs as $name => $value) {
$this->attributes[$name] = $value;
}
}
$this->addCustomAttributes($htmlAttrs);

return Html::a($this->label, $this->_link, $this->attributes);
}
Expand Down
39 changes: 39 additions & 0 deletions classes/LinkedIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace bl\socialShare\classes;

use yii\helpers\Html;

use bl\socialShare\base\SocialNetwork;

/**
* @author Vladimir Kuprienko <[email protected]>
*
* @property string $_link
* @property string $label
* @property array $attributes
*
* @property string $siteName
*/
class LinkedIn extends SocialNetwork
{
/**
* @var string Title of your website
*/
public $siteName;

/**
* @inheritdoc
*/
public function getLink($url, $title, $description, $image, $htmlAttrs)
{
$this->_link = "https://www.linkedin.com/shareArticle?mini=true"
."&url=$url"
."&title=$title"
."&summary=$description"
."&source=$this->siteName";

$this->addCustomAttributes($htmlAttrs);

return Html::a($this->label, $this->_link, $this->attributes);
}
}
17 changes: 10 additions & 7 deletions classes/Twitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@
* @property string $_link
* @property string $label
* @property array $attributes
*
* @property string $account
*/
class Twitter extends SocialNetwork
{
/**
* @var string Twitter login without `@`
*/
public $account;

/**
* @inheritdoc
*/
public function getLink($url, $title, $description, $image, $htmlAttrs)
{
$this->_link = "http://twitter.com/share?url=$url&text=$description&via=$this->account";
$this->_link = "http://twitter.com/share?"
."url=$url"
."&text=$description"
."&via=$this->account";;

$this->attributes['target'] = '_blank';
if(!empty($htmlAttrs)) {
foreach ($htmlAttrs as $name => $value) {
$this->attributes[$name] = $value;
}
}
$this->addCustomAttributes($htmlAttrs);

return Html::a($this->label, $this->_link, $this->attributes);
}
Expand Down
13 changes: 6 additions & 7 deletions classes/Vkontakte.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ class Vkontakte extends SocialNetwork
*/
public function getLink($url, $title, $description, $image, $htmlAttrs)
{
$this->_link = "http://vk.com/share.php?url=$url&title=$title&description=$description&image=$image";
$this->_link = "http://vk.com/share.php?"
."url=$url"
."&title=$title"
."&description=$description"
."&image=$image";

$this->attributes['target'] = '_blank';
if(!empty($htmlAttrs)) {
foreach ($htmlAttrs as $name => $value) {
$this->attributes[$name] = $value;
}
}
$this->addCustomAttributes($htmlAttrs);

return Html::a($this->label, $this->_link, $this->attributes);
}
Expand Down

0 comments on commit 17a8b24

Please sign in to comment.