-
Notifications
You must be signed in to change notification settings - Fork 1
/
grouptwitter.php
215 lines (189 loc) · 6.44 KB
/
grouptwitter.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
Plugin Name: Group Twitter
Plugin URI: http://www.strangerstudios.com/wordpress-plugins/grouptwitter/
Description: Cache Twitter updates from multiple accounts.
Version: .2.1
Author: Jason Coleman
Author URI: http://www.strangerstudios.com/
*/
global $wpdb, $wp_grouptwitter, $wp_grouptwitter_accounts, $gt_secretkey;
$gt_secretkey = "6E2C2EFA"; //this value is used to secure the udpatecache script
$gt_db_version = ".1";
$wp_grouptwitter = $wpdb->prefix ."grouptwitter";
$wp_grouptwitter_accounts = $wpdb->prefix ."grouptwitter_accounts";
require_once(dirname(__FILE__) . "/classes/class.twitter.php");
require_once(dirname(__FILE__) . "/classes/class.grouptwitter_widget.php");
function gt_install()
{
global $wpdb;
global $gt_db_version;
global $wp_grouptwitter;
global $wp_grouptwitter_accounts;
$table_name = $wp_grouptwitter;
$table2_name = $wp_grouptwitter_accounts;
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
//our table
$sql = "CREATE TABLE " . $table_name . " (
`id` BIGINT( 10 ) UNSIGNED NOT NULL ,
`account_id` VARCHAR( 255 ) NOT NULL ,
`created_at` DATETIME NOT NULL ,
`source` VARCHAR( 255 ) NOT NULL ,
`in_reply_to_screen_name` VARCHAR( 255 ) NOT NULL ,
`text` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY `id` ( `id` ),
KEY `account_id` (`account_id`)
) ENGINE = MYISAM DEFAULT CHARSET = utf8";
$sql2 = "CREATE TABLE " . $table2_name . " (
`id` BIGINT(10) NOT NULL ,
`name` VARCHAR( 255 ) NOT NULL ,
`profile_image_url` VARCHAR( 255 ) NOT NULL ,
`last_update` datetime NOT NULL,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM";
//need this to run the dbDelta to create table
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
dbDelta($sql2);
//incase we upgrade DB in the future
add_option("gt_db_version", $gt_db_version);
}
}
function gt_menu()
{
add_options_page('Group Twitter', 'Group Twitter', 8, 'grouptwitter', 'gt_options_page');
}
function gt_options_page()
{
require_once(dirname(__FILE__) . "/adminpages/options.php");
}
function gt_addAdminHeaderCode()
{
echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/grouptwitter/css/admin.css" />' . "\n";
}
function gt_addFrontendHeaderCode()
{
echo '<link type="text/css" rel="stylesheet" href="' . $url . '/wp-content/plugins/grouptwitter/css/frontend.css" media="screen" />' . "\n";
}
function gt_linkify( $text ) {
$text = preg_replace( '/(?!<\S)(\w+:\/\/[^<>\s]+\w)(?!\S)/i', '<a href="$1" target="_blank">$1</a>', $text );
$text = preg_replace( '/(?!<\S)#(\w+\w)(?!\S)/i', '<a href="http://twitter.com/search?q=#$1" target="_blank">#$1</a>', $text );
$text = preg_replace( '/(?!<\S)@(\w+\w)(?!\S)/i', '@<a href="http://twitter.com/$1" target="_blank">$1</a>', $text );
return $text;
}
function gt_getTweets($n = 5, $a = "all", $s = "", $p = 1, $details = false)
{
global $wpdb, $wp_grouptwitter, $wp_grouptwitter_accounts;
$end = $p * $n;
$start = $end - $n;
$sqlQuery = "SELECT SQL_CALC_FOUND_ROWS t.*, UNIX_TIMESTAMP(t.created_at) as created_at, a.name, a.profile_image_url FROM $wp_grouptwitter t LEFT JOIN $wp_grouptwitter_accounts a ON t.account_id = a.id WHERE 1=1 ";
if($a && $a != "all")
$sqlQuery .= "AND a.name = '$a' ";
if($s)
{
$terms = split(" ", $s);
foreach($terms as $term)
{
$term = trim($term);
$sqlQuery .= "AND t.text LIKE '%$term%' ";
}
}
$sqlQuery .= " ORDER BY t.id DESC LIMIT $start, $n ";
$tweets = $wpdb->get_results($sqlQuery);
$totalrows = $wpdb->get_var("SELECT FOUND_ROWS() as found_rows");
$end = min($end, $totalrows);
//linkify
for($i = 0; $i < count($tweets); $i++)
{
$tweets[$i]->text = gt_linkify($tweets[$i]->text);
}
if($details)
{
$temp->tweets = $tweets;
$temp->start = $start;
$temp->last = $end;
$temp->totalrows = $totalrows;
return $temp;
}
else
return $tweets;
}
function gt_displayDateTime($dt)
{
if(date("Y") == date("Y", $dt))
return str_replace(" ", " ", date("g:iA M jS", $dt));
else
return str_replace(" ", " ", date("g:iA M j, Y", $dt));
}
function gt_showTweets($n = 5, $a = "all", $s = "", $p = 1, $shownav = false, $class = "twitterfeed", $avatar = false)
{
if($shownav)
{
$results = gt_getTweets($n, $a, $s, $p, true);
$tweets = $results->tweets;
if($results->last)
{
?>
<p class="gt_summary">Showing tweets <span><?=$results->start+1?></span> to <span><?=$results->last?></span> of <span><?=$results->totalrows?></span>.</p>
<?php
}
else
{
?>
<p class="gt_summary">No tweets found.</p>
<?php
}
}
else
$tweets = gt_getTweets($n, $a, $s, $p);
?>
<ul class="<?=$class?>">
<?php
foreach($tweets as $tweet)
{
?>
<li>
<?php if(!empty($avatar)) { ?>
<a class="twitterfeed-avatar" target="_blank" href="http://twitter.com/<?=$tweet->name?>" title="<?=$tweet->name?>"><img height="32" border="0" width="32" alt="<?=$tweet->name?>" src="<?=$tweet->profile_image_url?>"/></a>
<?php } ?>
<div class="twitterfeed-content">
<?php echo make_clickable($tweet->text); ?>
<small>• <a target="_blank" href="http://twitter.com/<?=$tweet->name?>/status/<?=$tweet->id?>"><?=gt_displayDatetime($tweet->created_at)?></a></small>
</div>
</li>
<?php
}
?>
</ul>
<?php
if($shownav)
{
?>
<div class="line"></div>
<div class="navigation">
<?php
if($p > 1)
{
?>
<div class="alignleft"><a href="/twitter/?ts=<?=$s?>&ta=<?=$a?>&tp=<?=$p-1?>">« Newer Tweets</a> </div>
<?php
}
if($results->totalrows > $results->last)
{
?>
<div class="alignright"><a href="/twitter/?ts=<?=$s?>&ta=<?=$a?>&tp=<?=$p+1?>">Older Tweets »</a></div>
<?php
}
?>
<div class="clear"></div>
</div> <!-- end navigation -->
<?php
}
}
register_activation_hook(__FILE__,'gt_install');
add_action('admin_menu', 'gt_menu');
add_action('admin_head', 'gt_addAdminHeaderCode');
add_action('wp_head', 'gt_addFrontendHeaderCode');
wp_enqueue_script('gt_js', '/wp-content/plugins/grouptwitter/js/gt.js', array('prototype'));
?>