Completed
Push — master ( 970ef8...72844a )
by Vladimir
02:10
created

SocialShare::isSeoEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-social-share
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\social\share\widgets;
9
10
use Yii;
11
use yii\base\Widget;
12
use yii\di\Instance;
13
use yii\helpers\ArrayHelper;
14
use yii\helpers\Html;
15
use yii\helpers\Inflector;
16
use yii\helpers\Url;
17
use ymaker\social\share\assets\SocialIconsAsset;
18
use ymaker\social\share\configurators\Configurator;
19
use ymaker\social\share\configurators\ConfiguratorInterface;
20
21
/**
22
 * Widget for rendering the share links.
23
 *
24
 * @author Vladimir Kuprienko <[email protected]>
25
 * @since 1.0
26
 */
27
class SocialShare extends Widget
28
{
29
    /**
30
     * @var string ID of configurator component.
31
     */
32
    public $configuratorId;
33
    /**
34
     * @var string Absolute URL to the page.
35
     */
36
    public $url = '';
37
    /**
38
     * @var string Title for share.
39
     */
40
    public $title = '';
41
    /**
42
     * @var string Description for share.
43
     */
44
    public $description = '';
45
    /**
46
     * @var string Absolute URL to the image for share.
47
     */
48
    public $imageUrl = '';
49
    /**
50
     * @var array Special properties for specific driver.
51
     * @since 1.4.0
52
     */
53
    public $driverProperties = [];
54
    /**
55
     * @var string Name of the wrapper tag.
56
     */
57
    public $wrapperTag = 'ul';
58
    /**
59
     * @var array HTML options for wrapper tag.
60
     */
61
    public $wrapperOptions = ['class' => 'social-share'];
62
    /**
63
     * @var bool|string Name of the wrapper tag for link.
64
     * Set `false` value if you don't want using wrapper for link.
65
     */
66
    public $linkWrapperTag = 'li';
67
    /**
68
     * @var array HTML options for link wrapper tag.
69
     */
70
    public $linkWrapperOptions = [];
71
72
    /**
73
     * @var \ymaker\social\share\configurators\ConfiguratorInterface
74
     */
75
    protected $_configurator;
76
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function init()
82
    {
83
        $this->_configurator = Instance::ensure($this->configuratorId, ConfiguratorInterface::class);
84
85
        if (empty($this->url)) {
86
            $this->url = Url::to('', true);
87
        }
88
89
        parent::init();
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    private function enableDefaultIcons()
96
    {
97
        return $this->_configurator instanceof Configurator &&
98
            $this->_configurator->enableDefaultIcons;
99
    }
100
101
    /**
102
     * @return bool
103
     * @since 1.4.1
104
     */
105
    private function isSeoEnabled()
106
    {
107
        return $this->_configurator instanceof Configurator &&
108
            $this->_configurator->enableSeoOptions;
109
    }
110
111
    /**
112
     * Creates driver object.
113
     * 
114
     * @param array $driverConfig
115
     * @return object
116
     */
117
    private function createDriver($driverConfig)
118
    {
119
        /* @var \ymaker\social\share\base\Driver $driver */
120
        $driver = Yii::createObject(ArrayHelper::merge([
121
            'class'       => $driverConfig['class'],
122
            'url'         => $this->url,
123
            'title'       => $this->title,
124
            'description' => $this->description,
125
            'imageUrl'    => $this->imageUrl
126
        ], isset($driverConfig['config']) ? $driverConfig['config'] : []));
127
128
        if (key_exists($driverConfig['class'], $this->driverProperties)) {
129
            foreach ($this->driverProperties[$driverConfig['class']] as $property => $value) {
130
                if ($driver->hasProperty($property)) {
131
                    $driver->$property = $value;
132
                }
133
            }
134
        }
135
136
        return $driver;
137
    }
138
139
    /**
140
     * Build label for driver.
141
     * 
142
     * @param array $driverConfig
143
     * @param string $defaultLabel
144
     * @return string
145
     */
146
    protected function buildLabel($driverConfig, $defaultLabel)
147
    {
148
        return $this->enableDefaultIcons()
149
            ? Html::tag('i', '', ['class' => $this->_configurator->getIconSelector($driverConfig['class'])])
150
            : (isset($driverConfig['label']) ? $driverConfig['label'] : $defaultLabel);
151
    }
152
153
    /**
154
     * Combine global and custom HTML options.
155
     *
156
     * @param array $driverConfig
157
     * @return array
158
     */
159
    private function combineOptions($driverConfig)
160
    {
161
        $options = isset($driverConfig['options']) ? $driverConfig['options'] : [];
162
163
        $globalOptions = $this->_configurator->getOptions();
164
        if (empty($globalOptions)) {
165
            return $options;
166
        }
167
168
        if (isset($options['class'])) {
169
            Html::addCssClass($globalOptions, $options['class']);
170
            unset($options['class']);
171
        }
172
173
        return ArrayHelper::merge($globalOptions, $options);
174
    }
175
176
    /**
177
     * Returns array with share links in <a> HTML tag.
178
     *
179
     * @return array
180
     */
181
    protected function processSocialNetworks()
182
    {
183
        $socialNetworks = $this->_configurator->getSocialNetworks();
184
        $shareLinks = [];
185
186
        foreach ($socialNetworks as $key => $socialNetwork) {
187
            if (isset($socialNetwork['class'])) {
188
                /* @var \ymaker\social\share\base\Driver $driver */
189
                $driver = $this->createDriver($socialNetwork);
190
191
                $linkOptions = $this->combineOptions($socialNetworks);
192
                $linkOptions['href'] = $driver->getLink();
193
                $shareLinks[] = Html::tag('a', $this->buildLabel($socialNetwork, Inflector::camel2words($key)), $linkOptions);
194
            }
195
        }
196
197
        return $shareLinks;
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203
    public function run()
204
    {
205
        $links = $this->processSocialNetworks();
206
207
        if ($this->enableDefaultIcons()) {
208
            $this->getView()->registerAssetBundle(SocialIconsAsset::class);
209
        }
210
211
        $isSeoEnabled = $this->isSeoEnabled();
212
        if ($isSeoEnabled) {
213
            echo '<!--noindex-->';
214
        }
215
216
        echo Html::beginTag($this->wrapperTag, $this->wrapperOptions);
217
        foreach ($links as $link) {
218
            echo ($this->linkWrapperTag !== false) ? Html::tag($this->linkWrapperTag, $link, $this->linkWrapperOptions) : $link;
219
        }
220
        echo Html::endTag($this->wrapperTag);
221
222
        if ($isSeoEnabled) {
223
            echo '<!--/noindex-->';
224
        }
225
    }
226
}
227