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\drivers; |
10
|
|
|
|
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use ymaker\social\share\base\AbstractDriver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Driver for Tumblr. |
16
|
|
|
* |
17
|
|
|
* @see https://www.tumblr.com |
18
|
|
|
* |
19
|
|
|
* @author Vladimir Kuprienko <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @since 1.4.0 |
22
|
|
|
*/ |
23
|
|
|
class Tumblr extends AbstractDriver |
24
|
|
|
{ |
25
|
|
|
const POST_TYPE_LINK = 'link'; |
26
|
|
|
const POST_TYPE_TEXT = 'text'; |
27
|
|
|
const POST_TYPE_QUOTE = 'quote'; |
28
|
|
|
const POST_TYPE_PHOTO = 'photo'; |
29
|
|
|
const POST_TYPE_VIDEO = 'video'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $postType = self::POST_TYPE_LINK; |
35
|
|
|
/** |
36
|
|
|
* Share URL for type of post `link`. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
public $shareUrl; |
41
|
|
|
/** |
42
|
|
|
* Share photos list for type of post `photo`. |
43
|
|
|
* |
44
|
|
|
* @var array|string |
45
|
|
|
*/ |
46
|
|
|
public $sharePhotos = []; |
47
|
|
|
/** |
48
|
|
|
* Share URL or embed code for type of post `video`. |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
public $shareVideoUrl; |
53
|
|
|
/** |
54
|
|
|
* @var array|string |
55
|
|
|
*/ |
56
|
|
|
public $tags = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function init() |
62
|
|
|
{ |
63
|
|
|
$types = [ |
64
|
|
|
self::POST_TYPE_LINK, |
65
|
|
|
self::POST_TYPE_TEXT, |
66
|
|
|
self::POST_TYPE_QUOTE, |
67
|
|
|
self::POST_TYPE_PHOTO, |
68
|
|
|
self::POST_TYPE_VIDEO, |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
if (!\in_array($this->postType, $types)) { |
72
|
|
|
throw new InvalidConfigException('Invalid post type: "' . $this->postType . '"'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (self::POST_TYPE_PHOTO === $this->postType && empty($this->sharePhotos)) { |
76
|
|
|
throw new InvalidConfigException('Photos list cannot be blank!'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (self::POST_TYPE_VIDEO === $this->postType && null === $this->shareVideoUrl) { |
80
|
|
|
throw new InvalidConfigException('You should to set share video URL or embed code!'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
parent::init(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
protected function processShareData() |
90
|
|
|
{ |
91
|
|
|
$this->url = static::encodeData($this->url); |
92
|
|
|
$this->title = static::encodeData($this->title); |
93
|
|
|
$this->description = static::encodeData($this->description); |
94
|
|
|
|
95
|
|
|
switch ($this->postType) { |
96
|
|
|
case self::POST_TYPE_LINK: |
97
|
|
|
$this->appendToData( |
98
|
|
|
'content', |
99
|
|
|
null === $this->shareUrl ? $this->url : $this->shareUrl, |
100
|
|
|
null !== $this->shareUrl |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
break; |
104
|
|
|
case self::POST_TYPE_PHOTO: |
105
|
|
|
$this->appendToData('content', \implode(',', $this->sharePhotos)); |
|
|
|
|
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
case self::POST_TYPE_VIDEO: |
109
|
|
|
$this->appendToData('content', $this->shareVideoUrl); |
110
|
|
|
|
111
|
|
|
break; |
112
|
|
|
default: |
113
|
|
|
$this->appendToData('content', $this->description, false); |
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (!empty($this->tags)) { |
119
|
|
|
$this->appendToData('tags', \implode(',', $this->tags)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
protected function buildLink() |
127
|
|
|
{ |
128
|
|
|
$link = 'https://www.tumblr.com/widgets/share/tool?canonicalUrl={url}'; |
129
|
|
|
|
130
|
|
|
$this->addUrlParam($link, 'posttype', $this->postType); |
131
|
|
|
|
132
|
|
|
if (self::POST_TYPE_TEXT === $this->postType) { |
133
|
|
|
$this->addUrlParam($link, 'title', '{title}'); |
134
|
|
|
} else { |
135
|
|
|
$this->addUrlParam($link, 'caption', '{title}'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->addUrlParam($link, 'content', '{content}'); |
139
|
|
|
|
140
|
|
|
if (!empty($this->tags)) { |
141
|
|
|
$this->addUrlParam($link, 'tags', '{tags}'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $link; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|