-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitter-news-feed-class.php
executable file
·264 lines (226 loc) · 9.38 KB
/
twitter-news-feed-class.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Twitter News Feed
*
* PHP5 Class to query the Twitter search API for
* tweets with given hashtags from given Twitter users
*
* @author Keir Whitaker
**/
class TwitterNewsFeed
{
/**
* Twitter News Feed options
*
* @var array
**/
var $options;
/**
* Twitter search query string
*
* @var string
**/
var $search_query;
/**
* SimplePie feed
*
* @var SimplePie object
**/
var $feed;
/**
* WordPress Database object
*
* @var wpdb object
**/
var $wpdb;
function __construct($wpdb)
{
// Check for the existance of SimplePie
if (!class_exists("SimplePie")) {
require_once("simplepie/simplepie.inc");
};
// Set up the internal class variables
$this->wpdb = $wpdb;
$this->get_options();
$this->get_search_query();
}
/**
* Return an array of consumed Tweets from the RSS feed
*
* @access public
* @return array
**/
public function get_news_feed($add_news_to_db = TRUE) {
// Use SimplePie to get the RSS feed
$feed = new SimplePie();
$feed->set_feed_url(array($this->search_query));
$feed->set_item_limit(50);
$feed->handle_content_type();
$feed->enable_cache(false);
$feed->init();
// Get the feed and create the SimplePie feed object
$this->feed = $feed->get_items();
$post = array();
// Array to hold all the tweet info for returning as an array
$retval = array();
// Set up two counters (1 for use in the return array and 1 for counting the number of inserted posts if applicable)
$n = 0;
$i = 0;
// Array to hold the stored hashtags
$hashes = explode(',', $this->options["hashtags"]);
foreach ($feed->get_items() as $item) {
// Get the Twitter status id from the status href
$twitter_status_id = explode("/", $item->get_id());
// Check to see if the username is in the user profile meta data
$post["user_id"] = (int)$this->options["user"];
$user_id = $this->map_twitter_to_user($twitter_status_id[3]);
if(!$user_id==NULL) {
$post["user_id"] = (int)$user_id;
};
// Add individual Tweet data to array
$post["date"] = date("Y-m-d H:i:",strtotime($item->get_date()));
$post["link"] = $item->get_id();
$post["id"] = $twitter_status_id[count($twitter_status_id)-1];
$post["description"] = $item->get_description();
$post["description_filtered"] = $this->strip_hashes($item->get_description(), $hashes);
$post["twitter_username"] = $twitter_status_id[3];
$post["twitter_username_link"] = $this->create_twitter_link($twitter_status_id[3]);
$post["post_type"] = "twitter";
// Add the new post to the db?
if($add_news_to_db) {
if($this->add_item_as_post($post)) {
$i++;
};
};
// Add the Tweet to the return array
$retval[$n] = $post;
$n++;
};
// Return correct values depending on the $add_news_to_db boolean
if($add_news_to_db) {
return $i;
} else {
return $retval;
};
}
/**
* Strip hash tags (#'s) from the Tweet string
*
* @access private
* @return string
**/
private function strip_hashes($tweet, $hashes = array()) {
if(is_array($hashes)) {
foreach ($hashes as $hash) {
$tweet = str_replace("<a href=\"http://search.twitter.com/search?q=%23".$hash."\"><b>#".$hash."</b></a>", "", $tweet);
};
};
return $tweet;
}
/**
* Create a href to the Twitter users Twitter homepage
*
* @access private
* @return string
**/
private function create_twitter_link($twitter_username) {
return "<a href=\"http://twitter.com/".$twitter_username."\">@".$twitter_username."</a>";
}
/**
* Adds a Tweet to a given WP post category
* Additionally adds post meta data to the post
*
* @access private
* @return boolean
**/
private function add_item_as_post($item = array()) {
// Check to see if the post already exists
$sql = "SELECT id FROM ".$this->wpdb->posts." WHERE post_title='".$item['twitter_username']."-".$item['id']."'";
if($this->wpdb->get_var($sql) == NULL) {
// It's a new entry so add it to the posts table
$new_post = array();
$new_post["post_date"] = $item["date"];
$new_post["post_title"] = $item["twitter_username"]."-".$item["id"];
$new_post["post_name"] = $item["id"];
$new_post["post_content"] = $item["description_filtered"];
$new_post["post_status"] = "publish";
$new_post["post_author"] = (int)($item["user_id"]);
$new_post["post_category"] = array($this->options["category"]);
$new_post["post_type"] = $this->options["post_type"];
// Insert the post into the database and store the id
$post_id = wp_insert_post($new_post);
// Add custom fields to the post
add_post_meta($post_id, $meta_key = "status_id", $meta_value=$item["id"], $unique=TRUE);
add_post_meta($post_id, $meta_key = "status", $meta_value=$item["description"], $unique=TRUE);
add_post_meta($post_id, $meta_key = "status_href", $meta_value=$item["link"], $unique=TRUE);
add_post_meta($post_id, $meta_key = "twitter_username", $meta_value=$item["twitter_username"], $unique=TRUE);
add_post_meta($post_id, $meta_key = "twitter_username_link", $meta_value=$item["twitter_username_link"], $unique=TRUE);
return TRUE;
} else {
return FALSE;
};
}
/**
* Checks to see if a given Twitter user id exists in
* the Jabber field of any of the user profiles
*
* @access private
* @return int || null
**/
private function map_twitter_to_user($twitter_username) {
$sql = "SELECT user_id FROM ".$this->wpdb->usermeta." WHERE meta_key='jabber' AND meta_value='".$twitter_username."'";
$user_id = $this->wpdb->get_var($sql);
return $user_id;
}
/**
* Get the Twitter News Feed (prefix tnf_) options
* from wp_options table using get_option($key) WP function
*
* @access private
* @return void
**/
private function get_options() {
$options = array();
$options["hashtags"] = get_option("tnf_hashtags");
$options["usernames"] = get_option("tnf_usernames");
$options["exceptions"] = get_option("tnf_exceptions");
$options["category"] = get_option("tnf_category");
$options["user"] = get_option("tnf_user");
$options["add_news_to_db"] = get_option("tnf_add_news_to_db");
$post_type = get_option("tnf_post_type");
$options["post_type"] = !empty($post_type) ? $post_type : 'post';
$this->options = $options;
}
/**
* Builds a Twitter API search string
*
* @access private
* @return string
**/
public function get_search_query() {
$search_url = "http://search.twitter.com/search.rss?rpp=50&q=";
$hashtag_q = explode(",", $this->options['hashtags']);
$n = 0;
foreach($hashtag_q as $hashtag) {
$retval .= urlencode("#".$hashtag);
if(!($n == count($hashtag_q)-1)) {
$retval .= "+OR+";
};
$n ++;
};
$usernames_q = explode(",", $this->options["usernames"]);
$n = 0;
foreach($usernames_q as $username) {
if(!($n == 0)) {
$retval .= "+OR+from%3A";
} else {
$retval .= "+from%3A";
};
$n ++;
$retval .= urlencode($username);
};
$this->search_query = $search_url.$retval;
}
}
// END class
?>