1 | <?php |
||
2 | namespace WebOfTalent\TwitterTools; |
||
3 | |||
4 | use SilverStripe\View\ArrayData; |
||
5 | use SilverStripe\View\Parsers\ShortcodeHandler; |
||
6 | use SilverStripe\View\SSViewer; |
||
7 | |||
8 | class TwitterShortCodeHandler implements ShortcodeHandler |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * Gets the list of shortcodes provided by this handler |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public static function get_shortcodes() |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
17 | { |
||
18 | return ['tweet']; |
||
19 | } |
||
20 | |||
21 | |||
22 | /** |
||
23 | * |
||
24 | * @param array $arguments |
||
25 | * @param string $content |
||
26 | * @param ShortcodeParser $parser |
||
27 | * @param string $shortcode |
||
28 | * @param array $extra |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) |
||
0 ignored issues
–
show
|
|||
33 | { |
||
34 | if (empty($arguments['id'])) { |
||
35 | return; |
||
36 | } |
||
37 | |||
38 | if (!empty($caption)) { |
||
39 | $arguments['Caption'] = $caption; |
||
40 | } |
||
41 | |||
42 | $customise = array(); |
||
43 | $customise['TweetID'] = $arguments['id']; |
||
44 | |||
45 | //overide the defaults with the arguments supplied |
||
46 | $customise = array_merge($customise, $arguments); |
||
47 | |||
48 | $template = new SSViewer('RenderTweet'); |
||
49 | |||
50 | //return the customised template |
||
51 | return $template->process(new ArrayData($customise)); |
||
52 | /* |
||
53 | if (!isset($arguments['id'])) { |
||
54 | return null; |
||
55 | } |
||
56 | if (substr($arguments['id'], 0, 4) == 'http') { |
||
57 | $id = explode('/status/', $arguments['id']); |
||
58 | $id = $id[1]; |
||
59 | } else { |
||
60 | $id = $arguments['id']; |
||
61 | } |
||
62 | |||
63 | $tweet = EmbeddedTweet::get()->filter('TwitterID', $id)->first(); |
||
64 | if (!$tweet) { |
||
65 | $data = json_decode(file_get_contents('https://api.twitter.com/1/statuses/oembed.json?align=center&id='.$id.'&omit_script=true'), 1); |
||
66 | $tweet = new EmbeddedTweet(); |
||
67 | $tweet->URL = $data['url']; |
||
68 | $tweet->TwitterID = $id; |
||
69 | $tweet->HTML = $data['html']; |
||
70 | |||
71 | $author = EmbeddedTweetAuthor::get()->filter('Name', $data['author_name'])->first(); |
||
72 | if (!$author) { |
||
73 | $author = new EmbeddedTweetAuthor(); |
||
74 | $author->Name = $data['author_name']; |
||
75 | $author->URL = $data['author_url']; |
||
76 | $author->write(); |
||
77 | } |
||
78 | |||
79 | $tweet->Author = $author; |
||
80 | $tweet->write(); |
||
81 | } else { |
||
82 | return 'No embedded tweet to return'; |
||
83 | } |
||
84 | |||
85 | |||
86 | return $tweet->HTML; |
||
87 | */ |
||
88 | } |
||
89 | } |
||
90 |