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