-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.vanillatwitterembed.plugin.php
140 lines (121 loc) · 3.82 KB
/
class.vanillatwitterembed.plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Vanilla Twitter Embed: Embed tweets directly into discussion posts by pasting the tweet URL.
* @author Jamie Chung <me@jamiechung.me>
* @homepage http://www.jamiechung.me
* @twitter @jamiechung
* @github https://github.com/JamieChung/VanillaTwitterEmbed
*/
$PluginInfo['VanillaTwitterEmbed'] = array (
'Name' => 'Vanilla Twitter Embed',
'Description' => 'Embed tweets directly into discussion posts by pasting the tweet URL.',
'Version' => '0.4',
'RequiredApplications' => array('Vanilla' => '2.0.18'),
'RequiredPlugins' => FALSE,
'HasLocale' => FALSE,
'Author' => 'Jamie Chung',
'AuthorEmail' => 'me@jamiechung.me',
'AuthorUrl' => 'http://www.jamiechung.me'
);
class VanillaTwitterEmbedPlugin implements Gdn_IPlugin
{
// Regex for replace callback. Identifies a url to a individual tweet.
private $twitterStatusRegex = '/<a(.*?)href="(http|https)\:\/\/twitter.com\/(.*?)\/status\/([0-9]+)"(.*?)>(http|https)\:\/\/twitter.com\/(.*?)\/status\/([0-9]+)<\/a>/';
/**
* Setup the database structure required to cache the used tweets.
*/
public function Setup()
{
Gdn::Database()->Query("CREATE TABLE IF NOT EXISTS `GDN_TwitterEmbed` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TweetID` varchar(255) NOT NULL,
`Response` text NOT NULL,
PRIMARY KEY (`ID`),
KEY `TweetID` (`TweetID`)
)");
}
/**
* Destructs the database when we disable. Simple act of clearing the cache.
*/
public function OnDisable ()
{
Gdn::Database()->Query("DROP TABLE IF EXISTS `GDN_TwitterEmbed`");
}
/**
* If a match is found within the body of a post, the
* proper code is injected in place of it.
* @param $matches array Matched results from the twitter status regex.
*/
protected function CreateEmbed ( $matches )
{
// Tweet ID is the fourth matched result.
$id = $matches[4];
$tweet = false;
// We cannot force $id to be an int datatype
// because the value is too large for PHP.
if ( ctype_digit($id) )
{
// Check if the selected tweet is in the database.
$tweet = Gdn::Database()->SQL()
->Select('*')
->From('TwitterEmbed')
->Where('TweetID = ', $id)
->Limit(1)
->Get()
->FirstRow();
}
// If we don't have the tweet, let's get it from the twitter server.
if ( !$tweet )
{
// Create the twitter embed code based on the Garden Locale
$locale = explode('-', C('Garden.Locale'));
$lang = substr($locale[0], 0, 2);
$api = 'http://api.twitter.com/1/statuses/oembed.json?id='.$id.'&omit_script=true&lang='.$lang;
$response = file_get_contents($api);
// If we can't get the proper response from the server
// simply return the matched result (url link in post)
if ( !$response )
{
return $matches[0];
}
// Insert the matched tweet and the cached results into the database.
Gdn::Database()->SQL()
->Insert('TwitterEmbed', array(
'TweetID' => $id,
'Response' => $response,
));
$response = json_decode($response);
}
else
{
$response = json_decode($tweet->Response);
}
return $response->html;
}
/**
* Setup the callback to replace the content of the post with the oembed code.
*/
protected function TwitterEmbed ( $content )
{
$this->content = $content;
$content = preg_replace_callback($this->twitterStatusRegex,
array($this, 'CreateEmbed'), $content);
return $content;
}
/**
* Setup the callback for every page that handles comments to a post.
*/
public function Base_AfterCommentFormat_Handler ( &$Sender )
{
$Object = $Sender->EventArguments['Object'];
$Object->FormatBody = $this->TwitterEmbed($Object->FormatBody);
$Sender->EventArguments['Object'] = $Object;
}
/**
* Injects the twitter widget into all pages.
*/
public function Base_Render_Before ( $Sender )
{
$Sender->AddJsFile('https://platform.twitter.com/widgets.js');
}
}