yiimaker /
yii2-social-share
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /** |
||||||
| 4 | * @link https://github.com/yiimaker/yii2-social-share |
||||||
| 5 | * @copyright Copyright (c) 2017-2021 Volodymyr Kupriienko |
||||||
| 6 | * @license BSD 3-Clause License |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace ymaker\social\share\widgets; |
||||||
| 10 | |||||||
| 11 | use Yii; |
||||||
| 12 | use yii\base\Widget; |
||||||
| 13 | use yii\di\Instance; |
||||||
| 14 | use yii\helpers\ArrayHelper; |
||||||
| 15 | use yii\helpers\Html; |
||||||
| 16 | use yii\helpers\Inflector; |
||||||
| 17 | use yii\helpers\Url; |
||||||
| 18 | use ymaker\social\share\assets\SocialIconsAsset; |
||||||
| 19 | use ymaker\social\share\configurators\ConfiguratorInterface; |
||||||
| 20 | use ymaker\social\share\configurators\IconsConfigInterface; |
||||||
| 21 | use ymaker\social\share\configurators\SeoConfigInterface; |
||||||
| 22 | |||||||
| 23 | /** |
||||||
| 24 | * Widget for rendering the share links. |
||||||
| 25 | * |
||||||
| 26 | * @author Vladimir Kuprienko <[email protected]> |
||||||
| 27 | * |
||||||
| 28 | * @since 1.0 |
||||||
| 29 | */ |
||||||
| 30 | class SocialShare extends Widget |
||||||
| 31 | { |
||||||
| 32 | /** |
||||||
| 33 | * @var array|ConfiguratorInterface|IconsConfigInterface|SeoConfigInterface|string |
||||||
| 34 | */ |
||||||
| 35 | public $configurator; |
||||||
| 36 | /** |
||||||
| 37 | * Absolute URL to the page. |
||||||
| 38 | * |
||||||
| 39 | * @var string |
||||||
| 40 | */ |
||||||
| 41 | public $url = ''; |
||||||
| 42 | /** |
||||||
| 43 | * Title for share. |
||||||
| 44 | * |
||||||
| 45 | * @var string |
||||||
| 46 | */ |
||||||
| 47 | public $title = ''; |
||||||
| 48 | /** |
||||||
| 49 | * Description for share. |
||||||
| 50 | * |
||||||
| 51 | * @var string |
||||||
| 52 | */ |
||||||
| 53 | public $description = ''; |
||||||
| 54 | /** |
||||||
| 55 | * Absolute URL to the image for share. |
||||||
| 56 | * |
||||||
| 57 | * @var string |
||||||
| 58 | */ |
||||||
| 59 | public $imageUrl = ''; |
||||||
| 60 | /** |
||||||
| 61 | * Special properties for specific driver. |
||||||
| 62 | * |
||||||
| 63 | * @var array |
||||||
| 64 | * |
||||||
| 65 | * @since 1.4.0 |
||||||
| 66 | */ |
||||||
| 67 | public $driverProperties = []; |
||||||
| 68 | /** |
||||||
| 69 | * HTML options for links container tag. |
||||||
| 70 | * If you won't to use it - set `tag` option to `false`. |
||||||
| 71 | * |
||||||
| 72 | * @var array |
||||||
| 73 | */ |
||||||
| 74 | public $containerOptions = ['tag' => 'ul', 'class' => 'social-share']; |
||||||
| 75 | /** |
||||||
| 76 | * HTML options for link container tag. |
||||||
| 77 | * If you won't to use it - set `tag` option to `false`. |
||||||
| 78 | * |
||||||
| 79 | * @var array |
||||||
| 80 | */ |
||||||
| 81 | public $linkContainerOptions = ['tag' => 'li']; |
||||||
| 82 | |||||||
| 83 | /** |
||||||
| 84 | * Initialize the widget: gets configurator instance, |
||||||
| 85 | * sets [[url]] property if empty. Triggers [[EVENT_INIT]] event after initialization. |
||||||
| 86 | * |
||||||
| 87 | * @throws \yii\base\InvalidConfigException |
||||||
| 88 | */ |
||||||
| 89 | public function init() |
||||||
| 90 | { |
||||||
| 91 | $this->configurator = Instance::ensure($this->configurator, ConfiguratorInterface::class); |
||||||
| 92 | |||||||
| 93 | if (empty($this->url)) { |
||||||
| 94 | $this->url = Url::to('', true); |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | parent::init(); |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * {@inheritdoc} |
||||||
| 102 | */ |
||||||
| 103 | public function run() |
||||||
| 104 | { |
||||||
| 105 | if ($this->isDefaultIconsAssetEnabled()) { |
||||||
| 106 | $this->getView()->registerAssetBundle(SocialIconsAsset::class); |
||||||
| 107 | } |
||||||
| 108 | |||||||
| 109 | if ($this->isSeoEnabled()) { |
||||||
| 110 | echo '<!--noindex-->'; |
||||||
| 111 | } |
||||||
| 112 | |||||||
| 113 | $containerTag = ArrayHelper::remove($this->containerOptions, 'tag', false); |
||||||
| 114 | |||||||
| 115 | if ($containerTag) { |
||||||
| 116 | echo Html::beginTag($containerTag, $this->containerOptions); |
||||||
| 117 | } |
||||||
| 118 | |||||||
| 119 | $wrapTag = ArrayHelper::remove($this->linkContainerOptions, 'tag', false); |
||||||
| 120 | |||||||
| 121 | foreach ($this->getLinkList() as $link) { |
||||||
| 122 | echo $wrapTag ? Html::tag($wrapTag, $link, $this->linkContainerOptions) : $link; |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | if ($containerTag) { |
||||||
| 126 | echo Html::endTag($containerTag); |
||||||
| 127 | } |
||||||
| 128 | |||||||
| 129 | if ($this->isSeoEnabled()) { |
||||||
| 130 | echo '<!--/noindex-->'; |
||||||
| 131 | } |
||||||
| 132 | } |
||||||
| 133 | |||||||
| 134 | /** |
||||||
| 135 | * @return bool |
||||||
| 136 | */ |
||||||
| 137 | final protected function isDefaultIconsAssetEnabled() |
||||||
| 138 | { |
||||||
| 139 | return $this->configurator instanceof IconsConfigInterface && $this->configurator->isDefaultAssetEnabled(); |
||||||
| 140 | } |
||||||
| 141 | |||||||
| 142 | /** |
||||||
| 143 | * @return bool |
||||||
| 144 | */ |
||||||
| 145 | final protected function isIconsEnabled() |
||||||
| 146 | { |
||||||
| 147 | return $this->configurator instanceof IconsConfigInterface && $this->configurator->isIconsEnabled(); |
||||||
| 148 | } |
||||||
| 149 | |||||||
| 150 | /** |
||||||
| 151 | * @return bool |
||||||
| 152 | * |
||||||
| 153 | * @since 1.4.1 |
||||||
| 154 | */ |
||||||
| 155 | final protected function isSeoEnabled() |
||||||
| 156 | { |
||||||
| 157 | return $this->configurator instanceof SeoConfigInterface && $this->configurator->isSeoEnabled(); |
||||||
| 158 | } |
||||||
| 159 | |||||||
| 160 | /** |
||||||
| 161 | * @return bool |
||||||
| 162 | * |
||||||
| 163 | * @since 2.1 |
||||||
| 164 | */ |
||||||
| 165 | final protected function registerMetaTags() |
||||||
| 166 | { |
||||||
| 167 | return $this->configurator instanceof ConfiguratorInterface && $this->configurator->canRegisterMetaTags(); |
||||||
| 168 | } |
||||||
| 169 | |||||||
| 170 | /** |
||||||
| 171 | * Build label for driver. |
||||||
| 172 | * |
||||||
| 173 | * @param array $driverConfig |
||||||
| 174 | * @param string $defaultLabel |
||||||
| 175 | * |
||||||
| 176 | * @return string |
||||||
| 177 | */ |
||||||
| 178 | protected function getLinkLabel($driverConfig, $defaultLabel) |
||||||
| 179 | { |
||||||
| 180 | return $this->isIconsEnabled() |
||||||
| 181 | ? Html::tag('i', '', ['class' => $this->configurator->getIconSelector($driverConfig['class'])]) |
||||||
| 182 | : (isset($driverConfig['label']) ? $driverConfig['label'] : $defaultLabel); |
||||||
| 183 | } |
||||||
| 184 | |||||||
| 185 | /** |
||||||
| 186 | * Creates driver instance. |
||||||
| 187 | * |
||||||
| 188 | * @param array $config configuration for driver |
||||||
| 189 | * |
||||||
| 190 | * @return \ymaker\social\share\base\AbstractDriver |
||||||
| 191 | * |
||||||
| 192 | * @throws \yii\base\InvalidConfigException |
||||||
| 193 | */ |
||||||
| 194 | private function createDriver($config) |
||||||
| 195 | { |
||||||
| 196 | $fullConfig = ArrayHelper::merge( |
||||||
| 197 | [ |
||||||
| 198 | 'class' => $config['class'], |
||||||
| 199 | 'url' => $this->url, |
||||||
| 200 | 'title' => $this->title, |
||||||
| 201 | 'description' => $this->description, |
||||||
| 202 | 'imageUrl' => $this->imageUrl, |
||||||
| 203 | 'registerMetaTags' => $this->registerMetaTags(), |
||||||
| 204 | ], |
||||||
| 205 | isset($config['config']) ? $config['config'] : [], |
||||||
| 206 | isset($this->driverProperties[$config['class']]) ? $this->driverProperties[$config['class']] : [] |
||||||
| 207 | ); |
||||||
| 208 | |||||||
| 209 | return Yii::createObject($fullConfig); |
||||||
| 210 | } |
||||||
| 211 | |||||||
| 212 | /** |
||||||
| 213 | * Combine global and custom HTML options. |
||||||
| 214 | * |
||||||
| 215 | * @param array $driverConfig |
||||||
| 216 | * |
||||||
| 217 | * @return array |
||||||
| 218 | */ |
||||||
| 219 | private function combineOptions($driverConfig) |
||||||
| 220 | { |
||||||
| 221 | $options = isset($driverConfig['options']) ? $driverConfig['options'] : []; |
||||||
| 222 | |||||||
| 223 | $globalOptions = $this->configurator->getOptions(); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
The method
getOptions() does not exist on ymaker\social\share\conf...tors\SeoConfigInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to ymaker\social\share\conf...tors\SeoConfigInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 224 | |||||||
| 225 | if (empty($globalOptions)) { |
||||||
| 226 | return $options; |
||||||
| 227 | } |
||||||
| 228 | |||||||
| 229 | if (isset($options['class'])) { |
||||||
| 230 | Html::addCssClass($globalOptions, $options['class']); |
||||||
| 231 | unset($options['class']); |
||||||
| 232 | } |
||||||
| 233 | |||||||
| 234 | return ArrayHelper::merge($globalOptions, $options); |
||||||
| 235 | } |
||||||
| 236 | |||||||
| 237 | /** |
||||||
| 238 | * Returns array with share links in <a> HTML tag. |
||||||
| 239 | * |
||||||
| 240 | * @return array |
||||||
| 241 | * |
||||||
| 242 | * @throws \yii\base\InvalidConfigException |
||||||
| 243 | */ |
||||||
| 244 | private function getLinkList() |
||||||
| 245 | { |
||||||
| 246 | $linkList = []; |
||||||
| 247 | |||||||
| 248 | foreach ($this->configurator->getSocialNetworks() as $key => $socialNetwork) { |
||||||
|
0 ignored issues
–
show
The method
getSocialNetworks() does not exist on ymaker\social\share\conf...tors\SeoConfigInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to ymaker\social\share\conf...tors\SeoConfigInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The method
getSocialNetworks() does not exist on ymaker\social\share\conf...rs\IconsConfigInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to ymaker\social\share\conf...rs\IconsConfigInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 249 | if (isset($socialNetwork['class'])) { |
||||||
| 250 | $linkOptions = $this->combineOptions($socialNetwork); |
||||||
| 251 | $linkOptions['href'] = $this->createDriver($socialNetwork)->getLink(); |
||||||
| 252 | $linkList[] = Html::tag('a', $this->getLinkLabel($socialNetwork, Inflector::camel2words($key)), $linkOptions); |
||||||
| 253 | } |
||||||
| 254 | } |
||||||
| 255 | |||||||
| 256 | return $linkList; |
||||||
| 257 | } |
||||||
| 258 | } |
||||||
| 259 |