|
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\base; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\Object; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base driver for social network definition classes. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class Driver extends Object |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string Absolute URL to the page. |
|
23
|
|
|
*/ |
|
24
|
|
|
public $url; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string Title for share. |
|
27
|
|
|
*/ |
|
28
|
|
|
public $title; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string Description for share. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $description; |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string Absolute URL to the image for share. |
|
35
|
|
|
*/ |
|
36
|
|
|
public $imageUrl; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $_link; |
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $_metaTags = []; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_data = []; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function init() |
|
56
|
|
|
{ |
|
57
|
|
|
$this->processShareData(); |
|
58
|
|
|
$this->_data = [ |
|
59
|
|
|
'{url}' => $this->url, |
|
60
|
|
|
'{title}' => $this->title, |
|
61
|
|
|
'{description}' => $this->description, |
|
62
|
|
|
'{imageUrl}' => $this->imageUrl |
|
63
|
|
|
]; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Encode data for URL. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string|array $data |
|
70
|
|
|
* @return string|array |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function encodeData($data) |
|
73
|
|
|
{ |
|
74
|
|
|
if (is_array($data)) { |
|
75
|
|
|
foreach ($data as $key => $value) { |
|
76
|
|
|
$data[$key] = urlencode($value); |
|
77
|
|
|
} |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
return urlencode($data); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Decode the encoded data. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $data |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function decodeData($data) |
|
90
|
|
|
{ |
|
91
|
|
|
return urldecode($data); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Adds URL param to link. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name Param name. |
|
98
|
|
|
* @param string $value Param value. |
|
99
|
|
|
* @since 1.4.0 |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function addUrlParam($name, $value) |
|
102
|
|
|
{ |
|
103
|
|
|
$base = $name . '=' . $value; |
|
104
|
|
|
|
|
105
|
|
|
if (strpos($this->_link, '?') !== false) { |
|
106
|
|
|
$last = substr($this->_link, -1); |
|
107
|
|
|
if ($last === '?' || $last === '&') { |
|
108
|
|
|
$this->_link .= $base; |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->_link .= '&' . $base; |
|
111
|
|
|
} |
|
112
|
|
|
} else { |
|
113
|
|
|
$this->_link .= '?' . $base; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* This method should return a share link. |
|
119
|
|
|
* |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getLink() |
|
123
|
|
|
{ |
|
124
|
|
|
if (!empty($this->_metaTags)) { |
|
125
|
|
|
$data = []; |
|
126
|
|
|
foreach ($this->_data as $key => $value) { |
|
127
|
|
|
$data[$key] = static::decodeData($value); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
foreach ($this->_metaTags as $metaTag) { |
|
131
|
|
|
$metaTag['content'] = strtr($metaTag['content'], $data); |
|
132
|
|
|
Yii::$app->getView()->registerMetaTag($metaTag); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return strtr($this->_link, $this->_data); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Method should process the share data for current driver. |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract protected function processShareData(); |
|
143
|
|
|
} |
|
144
|
|
|
|