1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sunnysideup\ShareThisSimple\Api; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\ArrayList; |
6
|
|
|
use SilverStripe\View\ArrayData; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use Sunnysideup\ShareThisSimple\Api\ShareThisSimpleProvider; |
9
|
|
|
use SilverStripe\View\ViewableData; |
10
|
|
|
|
11
|
|
|
class ShareThisSimpleProvider extends ViewableData |
12
|
|
|
{ |
13
|
|
|
private static $description_method = ''; |
14
|
|
|
|
15
|
|
|
private static $default_mentions = []; |
16
|
|
|
|
17
|
|
|
private static $default_vias = []; |
18
|
|
|
|
19
|
|
|
private static $default_hash_tags = []; |
20
|
|
|
|
21
|
|
|
private static $image_methods = []; |
22
|
|
|
|
23
|
|
|
private static $casting = [ |
24
|
|
|
"FacebookShareLink" => "Varchar", |
25
|
|
|
"TwitterShareLink" => "Varchar", |
26
|
|
|
"TumblrShareLink" => "Varchar", |
27
|
|
|
"PinterestShareLink" => "Varchar", |
28
|
|
|
"EmailShareLink" => "Varchar", |
29
|
|
|
"RedditShareLink" => "Varchar", |
30
|
|
|
"PinterestLinkForSpecificImage" => "Varchar", |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var DataObject |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
protected $object; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param DataObject $object |
41
|
|
|
*/ |
42
|
|
|
public function __construct($object) |
43
|
|
|
{ |
44
|
|
|
$this->object = $object; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected $linkMethod = 'AbsoluteLink'; |
48
|
|
|
|
49
|
|
|
public function setLinkMethod($s) |
50
|
|
|
{ |
51
|
|
|
$this->linkMethod = $s; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected $titleMethod = 'Title'; |
55
|
|
|
|
56
|
|
|
public function setTitleMethod($s) |
57
|
|
|
{ |
58
|
|
|
$this->titleMethod = $s; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected $imageMethods = []; |
62
|
|
|
|
63
|
|
|
public function setImageMethods($a) |
64
|
|
|
{ |
65
|
|
|
$this->imageMethods = $a; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected $descriptionMethod = 'SocialMediaDescription'; //change to 'SocialMediaDescription' |
69
|
|
|
|
70
|
|
|
public function setDescriptionMethod($s) |
71
|
|
|
{ |
72
|
|
|
$this->descriptionMethod = $s; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected $hashTags = []; |
76
|
|
|
|
77
|
|
|
public function setHashTags($a) |
78
|
|
|
{ |
79
|
|
|
$this->hashTags = $a; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected $mentions = ''; |
83
|
|
|
|
84
|
|
|
public function setMentions($a) |
85
|
|
|
{ |
86
|
|
|
$this->mentions = $a; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected $vias = []; |
90
|
|
|
|
91
|
|
|
public function setVias($a) |
92
|
|
|
{ |
93
|
|
|
$this->vias = $a; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* return of ShareThisLinks. |
98
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
99
|
|
|
* @return ArrayList |
100
|
|
|
*/ |
101
|
|
|
public function ShareThisLinks($customDescription = '') |
102
|
|
|
{ |
103
|
|
|
$arrayList = ArrayList::create(); |
104
|
|
|
$options = array_keys($this->config()->get('casting')); //$this->config()->get('casting') ??? |
105
|
|
|
foreach ($options as $option) { |
106
|
|
|
$className = str_replace('ShareLink', '', $option); |
107
|
|
|
$className = strtolower($className); |
108
|
|
|
$method = "get".$option; |
109
|
|
|
$arrayList->push( |
110
|
|
|
ArrayData::create( |
111
|
|
|
[ |
112
|
|
|
'Class' => $className, |
113
|
|
|
'Link' => $this->$method($customDescription), |
114
|
|
|
] |
115
|
|
|
) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $arrayList; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* ALIAS |
125
|
|
|
* Generate a URL to share this content on Facebook. |
126
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
127
|
|
|
* @return string|false |
128
|
|
|
*/ |
129
|
|
|
public function FacebookShareLink($customDescription = '') |
130
|
|
|
{ |
131
|
|
|
return $this->getFacebookShareLink(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Generate a URL to share this content on Facebook. |
136
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
137
|
|
|
* https://www.facebook.com/dialog/feed? |
138
|
|
|
* &link=URL_HERE |
139
|
|
|
* &picture=IMAGE_LINK_HERE |
140
|
|
|
* &name=TITLE_HERE |
141
|
|
|
* &caption=%20 |
142
|
|
|
* &description=DESCRIPTION_HERE |
143
|
|
|
* &redirect_uri=http%3A%2F%2Fwww.facebook.com%2F |
144
|
|
|
* @return string|false |
145
|
|
|
*/ |
146
|
|
|
public function getFacebookShareLink($customDescription = '') |
147
|
|
|
{ |
148
|
|
|
extract($this->getShareThisArray($customDescription)); |
149
|
|
|
|
150
|
|
|
return ($pageURL) ? |
151
|
|
|
"https://www.facebook.com/sharer/sharer.php?u=$pageURL&t=$title" |
152
|
|
|
: |
153
|
|
|
false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* ALIAS |
158
|
|
|
* Generate a URL to share this content on Twitter |
159
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
160
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
161
|
|
|
* @return string|false |
162
|
|
|
*/ |
163
|
|
|
public function TwitterShareLink($customDescription = '') |
164
|
|
|
{ |
165
|
|
|
return $this->getTwitterShareLink($customDescription); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Generate a URL to share this content on Twitter |
170
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
171
|
|
|
* example: https://twitter.com/intent/tweet? |
172
|
|
|
* &source=http%3A%2F%2Fsunnysideup.co.nz |
173
|
|
|
* &text=test:%20http%3A%2F%2Fsunnysideup.co.nz |
174
|
|
|
* &via=foobar |
175
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
176
|
|
|
* @return string|false |
177
|
|
|
*/ |
178
|
|
|
public function getTwitterShareLink($customDescription = '') |
179
|
|
|
{ |
180
|
|
|
extract($this->getShareThisArray($customDescription)); |
181
|
|
|
|
182
|
|
|
return ($pageURL) ? |
183
|
|
|
"https://twitter.com/intent/tweet?source=$pageURL&text=$titleFull".urlencode(': ').$pageURL |
184
|
|
|
: |
185
|
|
|
false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* ALIAS |
190
|
|
|
* Generate a URL to share this content on Twitter |
191
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
192
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
193
|
|
|
* @return string|false |
194
|
|
|
*/ |
195
|
|
|
public function LinkedInShareLink($customDescription = '') |
196
|
|
|
{ |
197
|
|
|
return $this->getLinkedInShareLink($customDescription); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Generate a URL to share this content on Twitter |
202
|
|
|
* Specs: ??? |
203
|
|
|
* example: https://www.linkedin.com/shareArticle? |
204
|
|
|
* mini=true&url=http://www.cnn.com&title=&summary=chek this out&source= |
205
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
206
|
|
|
* @return string|false |
207
|
|
|
*/ |
208
|
|
|
public function getLinkedInShareLink($customDescription = '') |
209
|
|
|
{ |
210
|
|
|
extract($this->getShareThisArray($customDescription)); |
211
|
|
|
|
212
|
|
|
return ($pageURL) ? |
213
|
|
|
"https://www.linkedin.com/shareArticle?mini=true&url=$pageURL&summary=$titleFull" |
214
|
|
|
: |
215
|
|
|
false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* ALIAS |
221
|
|
|
* Generate a URL to share this content on Twitter |
222
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
223
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
224
|
|
|
* @return string|false |
225
|
|
|
*/ |
226
|
|
|
public function TumblrShareLink($customDescription = '') |
227
|
|
|
{ |
228
|
|
|
return $this->getTumblrShareLink($customDescription); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Generate a URL to share this content on Twitter |
233
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
234
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
235
|
|
|
* @return string|false |
236
|
|
|
*/ |
237
|
|
|
public function getTumblrShareLink($customDescription = '') |
238
|
|
|
{ |
239
|
|
|
extract($this->getShareThisArray($customDescription)); |
240
|
|
|
|
241
|
|
|
return ($pageURL) ? |
242
|
|
|
"http://www.tumblr.com/share/link?url=$pageURL&name=$title&description=$description" |
243
|
|
|
: |
244
|
|
|
false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* ALIAS |
249
|
|
|
* Generate a URL to share this content on Twitter |
250
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
251
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
252
|
|
|
* @return string|false |
253
|
|
|
*/ |
254
|
|
|
public function PinterestShareLink($customDescription = '') |
255
|
|
|
{ |
256
|
|
|
return $this->getPinterestShareLink($customDescription); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Generate a URL to share this content on Twitter |
261
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
262
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
263
|
|
|
* @return string|false |
264
|
|
|
*/ |
265
|
|
|
public function getPinterestShareLink($customDescription = '') |
266
|
|
|
{ |
267
|
|
|
extract($this->getShareThisArray($customDescription)); |
268
|
|
|
|
269
|
|
|
return ($pageURL) ? |
270
|
|
|
"http://pinterest.com/pin/create/button/?url=$pageURL&description=$description&media=$media" |
271
|
|
|
: |
272
|
|
|
false; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* ALIAS |
277
|
|
|
* Generate a 'mailto' URL to share this content via Email. |
278
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
279
|
|
|
* @return string|false |
280
|
|
|
*/ |
281
|
|
|
public function EmailShareLink($customDescription = '') |
282
|
|
|
{ |
283
|
|
|
return $this->getEmailShareLink($customDescription); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Generate a 'mailto' URL to share this content via Email. |
288
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
289
|
|
|
* @return string|false |
290
|
|
|
*/ |
291
|
|
|
public function getEmailShareLink($customDescription = '') |
292
|
|
|
{ |
293
|
|
|
extract($this->getShareThisArray($customDescription)); |
294
|
|
|
|
295
|
|
|
return ($pageURL) ? "mailto:?subject=$title&body=$pageURL" : false; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* ALIAS |
301
|
|
|
* Generate a URL to share this content on Twitter |
302
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
303
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
304
|
|
|
* @return string|false |
305
|
|
|
*/ |
306
|
|
|
public function RedditShareLink($customDescription = '') |
307
|
|
|
{ |
308
|
|
|
return $this->getRedditShareLink($customDescription); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Generate a URL to share this content on Twitter |
313
|
|
|
* Specs: https://dev.twitter.com/web/tweet-button/web-intent. |
314
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
315
|
|
|
* @return string|false |
316
|
|
|
*/ |
317
|
|
|
public function getRedditShareLink($customDescription = '') |
318
|
|
|
{ |
319
|
|
|
extract($this->getShareThisArray($customDescription)); |
320
|
|
|
|
321
|
|
|
return ($pageURL) ? "http://reddit.com/submit?url=$pageURL&title=$title" : false; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
protected static $cacheGetShareThisArray = []; |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @param string $customDescription e.g. foo bar cool stuff |
328
|
|
|
* |
329
|
|
|
* @return array |
330
|
|
|
*/ |
331
|
|
|
public function getShareThisArray($customDescription = '') |
332
|
|
|
{ |
333
|
|
|
if (! isset(self::$cacheGetShareThisArray[$this->object->ID])) { |
334
|
|
|
//1. link |
335
|
|
|
$link = $this->shareThisLinkField(); |
336
|
|
|
|
337
|
|
|
$title = $this->shareThisTitleField(); |
338
|
|
|
|
339
|
|
|
$media = $this->shareThisMediaField(); |
340
|
|
|
|
341
|
|
|
$description = $this->shareThisDescriptionField($customDescription); |
342
|
|
|
|
343
|
|
|
$hashTags = $this->getValuesFromArrayToString('hashTags', 'hash_tags', '#'); |
344
|
|
|
$mentions = $this->getValuesFromArrayToString('mentions', 'mentions', '@'); |
345
|
|
|
$vias = $this->getValuesFromArrayToString('vias', 'vias', '@'); |
346
|
|
|
$titleFull = trim($mentions.' '.$title.' '.$hashTags.' '.$vias); |
347
|
|
|
$descriptionFull = trim($mentions.' '.$description.' '.$hashTags.' '.$vias); |
348
|
|
|
|
349
|
|
|
//return ... |
350
|
|
|
self::$cacheGetShareThisArray[$this->object->ID] = [ |
351
|
|
|
"pageURL" => rawurlencode($link), |
352
|
|
|
"title" => rawurlencode($title), |
353
|
|
|
"titleFull" => rawurlencode($titleFull), |
354
|
|
|
"media" => rawurlencode($media), |
355
|
|
|
"description" => rawurlencode($description), |
356
|
|
|
"descriptionFull" => rawurlencode($descriptionFull), |
357
|
|
|
"hashTags" => rawurlencode($mentions), |
358
|
|
|
"mentions" => rawurlencode($mentions), |
359
|
|
|
"vias" => rawurlencode($vias), |
360
|
|
|
]; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return self::$cacheGetShareThisArray[$this->object->ID]; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
protected function getValuesFromArrayToString($variable, $staticVariable, $prepender = '@') |
367
|
|
|
{ |
368
|
|
|
if (!empty($this->$variable)) { |
369
|
|
|
$a = $this->$variable; |
370
|
|
|
} else { |
371
|
|
|
$a = $this->Config()->get($staticVariable); |
372
|
|
|
} |
373
|
|
|
$str = ''; |
374
|
|
|
if (is_array($a) && count($a)) { |
375
|
|
|
$str = $prepender.implode(' '.$prepender, $a); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
return trim($str); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* |
383
|
|
|
* |
384
|
|
|
* @param string $imageMethod e.g. MyImage |
385
|
|
|
* @param bool $useImageTitle if set to false, it will use the page title as the image title |
386
|
|
|
* |
387
|
|
|
* @return string |
388
|
|
|
*/ |
389
|
|
|
public function PinterestLinkForSpecificImage($imageMethod, $useImageTitle = false) |
390
|
|
|
{ |
391
|
|
|
return $this->getPinterestLinkForSpecificImage( |
392
|
|
|
$imageMethod, |
393
|
|
|
$useImageTitle |
394
|
|
|
); |
395
|
|
|
} |
396
|
|
|
public function getPinterestLinkForSpecificImage($imageMethod, $useImageTitle = false) |
397
|
|
|
{ |
398
|
|
|
if ($this->object && $this->object->hasMethod($imageMethod)) { |
399
|
|
|
$image = $this->object->$imageMethod(); |
400
|
|
|
if ($image && $image->exists()) { |
401
|
|
|
if ($useImageTitle) { |
402
|
|
|
$imageTitle = $image->Title; |
403
|
|
|
} else { |
404
|
|
|
$imageTitle = $this->object->Title; |
405
|
|
|
} |
406
|
|
|
return 'http://pinterest.com/pin/create/button/' |
407
|
|
|
.'?url='.urlencode($this->object->AbsoluteLink()).'&' |
408
|
|
|
.'description='.urlencode($imageTitle).'&' |
409
|
|
|
.'media='.urlencode($image->AbsoluteLink()); |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
private function shareThisLinkField() |
415
|
|
|
{ |
416
|
|
|
$link = ''; |
417
|
|
|
$linkMethod = $this->linkMethod; |
418
|
|
|
if ($this->object->hasMethod($linkMethod)) { |
419
|
|
|
$link = $this->object->$linkMethod(); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
return $link; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
private function shareThisTitleField() : string |
426
|
|
|
{ |
427
|
|
|
$title = ''; |
428
|
|
|
$titleMethod = $this->titleMethod; |
429
|
|
|
if ($this->object->hasMethod($titleMethod)) { |
430
|
|
|
$title = $this->object->$titleMethod(); |
431
|
|
|
} elseif (isset($this->object->$titleMethod)) { |
432
|
|
|
$title = $this->object->$titleMethod; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
return (string) $title; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
private function shareThisMediaField() : string |
439
|
|
|
{ |
440
|
|
|
$media = ''; |
441
|
|
|
$imageMethods = $this->imageMethods; |
442
|
|
|
if (is_array($imageMethods) && count($imageMethods)) { |
443
|
|
|
//do nothing |
444
|
|
|
} else { |
445
|
|
|
$imageMethods = Config::inst()->get( |
446
|
|
|
"ShareThisSimpleProvider", |
447
|
|
|
"image_methods" |
448
|
|
|
); |
449
|
|
|
} |
450
|
|
|
if (is_array($imageMethods) && count($imageMethods)) { |
451
|
|
|
foreach ($imageMethods as $imageMethod) { |
452
|
|
|
if ($this->object->hasMethod($imageMethod)) { |
453
|
|
|
$imageField = $imageMethod."ID"; |
454
|
|
|
if ($this->$imageField) { |
455
|
|
|
$image = $this->object->$imageMethod(); |
456
|
|
|
if ($image && $image->exists()) { |
457
|
|
|
$media = $image->AbsoluteLink(); |
458
|
|
|
break; |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
return $media; |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
|
469
|
|
|
private function shareThisDescriptionField(string $customDescription) : string |
470
|
|
|
{ |
471
|
|
|
$description = ''; |
472
|
|
|
|
473
|
|
|
if ($customDescription) { |
474
|
|
|
$description = $customDescription; |
475
|
|
|
} else { |
476
|
|
|
$description = ""; |
477
|
|
|
if ($descriptionMethod = $this->descriptionMethod) { |
478
|
|
|
//do nothing |
479
|
|
|
} else { |
480
|
|
|
$descriptionMethod = Config::inst()->get( |
481
|
|
|
"ShareThisSimpleProvider", |
482
|
|
|
"description_method" |
483
|
|
|
); |
484
|
|
|
} |
485
|
|
|
if ($descriptionMethod) { |
486
|
|
|
if ($this->object->hasMethod($descriptionMethod)) { |
487
|
|
|
$description = $this->object->$descriptionMethod(); |
488
|
|
|
} elseif (isset($this->object->$descriptionMethod)) { |
489
|
|
|
$description = $this->object->$descriptionMethod; |
490
|
|
|
} |
491
|
|
|
} |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return (string)$description; |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths