-
Notifications
You must be signed in to change notification settings - Fork 58
/
social-twitter.php
executable file
·387 lines (348 loc) · 11 KB
/
social-twitter.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/**
* Twitter implementation for Social.
*
* @package Social
* @subpackage plugins
*/
if (class_exists('Social') and !class_exists('Social_Twitter')) {
final class Social_Twitter {
/**
* Registers Twitter to Social.
*
* @static
* @wp-filter social_register_service
*
* @param array $services
*
* @return array
*/
public static function register_service(array $services) {
$services[] = 'twitter';
return $services;
}
/**
* Adds to the avatar comment types array.
*
* @static
* @param array $types
* @return array
*/
public static function get_avatar_comment_types(array $types) {
return array_merge($types, Social_Service_Twitter::comment_types());
}
/**
* Pre-processor to the comments.
*
* @wp-filter social_comments_array
* @static
* @param array $comments
* @param int $post_id
* @return array
*/
public static function comments_array(array $comments, $post_id) {
// pre-load the hashes for broadcasted tweets
$broadcasted_ids = get_post_meta($post_id, '_social_broadcasted_ids', true);
if (empty($broadcasted_ids)) {
$broadcasted_ids = array();
}
global $wpdb;
// we need comments to be keyed by ID, check for Tweet comments
$tweet_comments = $_comments = $comment_ids = array();
foreach ($comments as $key => $comment) {
if (is_object($comment)) {
$_comments['id_'.$comment->comment_ID] = $comment;
if (in_array($comment->comment_type, Social_Service_Twitter::comment_types())) {
$comment_ids[] = $comment->comment_ID;
$tweet_comments['id_'.$comment->comment_ID] = $comment;
}
}
else { // social items
$_comments[$key] = $comment;
}
}
// if no tweet comments, get out now
if (!count($tweet_comments)) {
return $comments;
}
// use our keyed array
$comments = $_comments;
unset($_comments);
$social_map = array(); // key = social id, value = comment_ID
$hash_map = array(); // key = hash, value = comment_ID
$broadcasted_social_ids = array();
$broadcast_retweets = array(); // array of comments
if (isset($broadcasted_ids['twitter'])) {
foreach ($broadcasted_ids['twitter'] as $account_id => $broadcasted) {
foreach ($broadcasted as $id => $data) {
$broadcasted_social_ids[] = $id;
// if we don't have a message saved for a tweet, try to get it so that we can use it next time
if (empty($data['message'])) {
$url = Social::wp39_nonce_url(home_url('index.php?social_controller=aggregation&social_action=retrieve_twitter_content&broadcasted_id='.$id.'&post_id='.$post_id), 'retrieve_twitter_content');
wp_remote_get(str_replace('&', '&', $url), array(
'timeout' => 0.01,
'blocking' => false,
));
}
else {
// create a hash from the broadcast so we can match retweets to it
$hash = self::build_hash($data['message']);
// This is stored as broadcasted and not the ID so we can easily store broadcasted retweets
// instead of attaching retweets to non-existent comments.
$hash_map[$hash] = 'broadcasted';
}
}
}
}
// Load the comment meta
$results = $wpdb->get_results("
SELECT meta_key, meta_value, comment_id
FROM $wpdb->commentmeta
WHERE comment_id IN (".implode(',', $comment_ids).")
AND (
meta_key = 'social_in_reply_to_status_id'
OR meta_key = 'social_status_id'
OR meta_key = 'social_raw_data'
OR meta_key = 'social_profile_image_url'
OR meta_key = 'social_comment_type'
)
");
// Set up social data for twitter comments
foreach ($tweet_comments as $key => &$comment) {
$comment->social_items = array();
// Attach meta
foreach ($results as $result) {
if ($comment->comment_ID == $result->comment_id) {
switch ($result->meta_key) {
case 'social_raw_data':
$comment->social_raw_data = json_decode(base64_decode($result->meta_value));
break;
case 'social_status_id':
$social_map[$result->meta_value] = $result->comment_id;
default:
$comment->{$result->meta_key} = $result->meta_value;
}
}
}
// Attach hash
if (isset($comment->social_raw_data) and isset($comment->social_raw_data->text)) {
$text = trim($comment->social_raw_data->text);
}
else {
$text = trim($comment->comment_content);
}
$comment->social_hash = self::build_hash($text);
if (!isset($hash_map[$comment->social_hash])) {
$hash_map[$comment->social_hash] = $comment->comment_ID;
}
}
// merge data so that $comments has the data we've set up
$comments = array_merge($comments, $tweet_comments);
// set-up replies and retweets
foreach ($tweet_comments as $key => &$comment) {
if (is_object($comment)) {
// set reply/comment parent
if (!empty($comment->social_in_reply_to_status_id) and isset($social_map[$comment->social_in_reply_to_status_id])) {
$comments[$key]->comment_parent = $social_map[$comment->social_in_reply_to_status_id];
}
// set retweets
$rt_matched = false;
if (isset($comment->social_raw_data) and isset($comment->social_raw_data->retweeted_status)) {
// explicit match via API data
$rt_id = $comment->social_raw_data->retweeted_status->id_str;
if (in_array($rt_id, $broadcasted_social_ids)) {
$broadcast_retweets[] = $comment;
unset($comments[$key]);
$rt_matched = true;
}
else if (isset($social_map[$rt_id]) and isset($comments['id_'.$social_map[$rt_id]])) {
$comments['id_'.$social_map[$rt_id]]->social_items[$key] = $comment;
unset($comments[$key]);
$rt_matched = true;
}
}
if (!$rt_matched) {
// best guess via hashes
$hash_match = $hash_map[$comment->social_hash];
if ($hash_match != $comment->comment_ID) { // hash match to own tweet is expected, at minimum - set above
if ($hash_match == 'broadcasted') {
$broadcast_retweets[] = $comment;
}
else if (isset($comments['id_'.$hash_match])) {
$comments['id_'.$hash_match]->social_items[$key] = $comment;
}
else {
// Loop through the broadcasted retweets and see if this is a retweet of one of those.
foreach ($broadcast_retweets as $retweet) {
if ($retweet->comment_ID == $hash_match) {
$broadcast_retweets[] = $comment;
break;
}
}
}
unset($comments[$key]);
}
}
}
}
if (!isset($comments['social_items'])) {
$comments['social_items'] = array();
}
if (count($broadcast_retweets)) {
$comments['social_items']['twitter'] = $broadcast_retweets;
}
return $comments;
}
/**
* Sets the raw data for the broadcasted post.
*
* @wp-filter social_broadcast_response
* @static
* @param array $data
* @param Social_Service_Account $account
* @param string $service_key
* @param int $post_id
* @param Social_Response $response
* @return array
*/
public static function social_save_broadcasted_ids_data(array $data, Social_Service_Account $account, $service_key, $post_id, Social_Response $response = null) {
if ($service_key == 'twitter') {
if (!empty($response)) {
$data['message'] = base64_encode(json_encode($response->body()->response));
}
$data['account'] = (object) array(
'user' => $account->as_object()->user
);
}
return $data;
}
/**
* Strips extra retweet data before comparing.
*
* @static
* @param string $text
* @return string
*/
private static function build_hash($text) {
$text = explode(' ', $text);
$content = '';
foreach ($text as $_content) {
if (!empty($_content) and strpos($_content, 'http://') === false) {
if ($_content == 'RT' or preg_match('/@([\w_]+):/i', $_content)) {
continue;
}
$content .= $_content.' ';
}
}
return md5(trim($content));
}
/**
* Checks for a retweet via twitter API data and user perception.
*
* @static
* @param stdClass $comment
* @return bool
*/
public static function is_retweet($comment = null, $tweet = null) {
$is_retweet = false;
if (!is_null($comment)) {
if (isset($comment->social_raw_data) and !empty($comment->social_raw_data->retweeted_status)) {
$is_retweet = true;
}
if (social_substr($comment->comment_content, 0, 4) == 'RT @') {
$is_retweet = true;
}
}
else if (!is_null($tweet)) {
if (!empty($tweet->retweeted_status)) {
$is_retweet = true;
}
if (social_substr($tweet->text, 0, 4) == 'RT @') {
$is_retweet = true;
}
}
return $is_retweet;
}
/**
* Adds a retweet to the original broadcasted post social items stack.
*
* @static
* @param int $comment_id
* @param array $comments
* @param array $social_items
*/
private static function add_to_social_items($comment_id, &$comments, &$social_items) {
$object = null;
$_comments = array();
foreach ($comments as $id => $comment) {
if (is_int($id)) {
if ($comment->comment_ID == $comment_id) {
$object = $comment;
}
else {
$_comments[] = $comment;
}
}
else {
if (isset($_comments[$id])) {
$_comments[$id] = array_merge($_comments[$id], $comment);
}
else {
$_comments[$id] = $comment;
}
}
}
$comments = $_comments;
if ($object !== null) {
if (!isset($social_items['twitter'])) {
$social_items['twitter'] = array();
}
$social_items['twitter'][$comment_id] = $object;
}
}
/**
* Adds messaging to the title.
*
* @static
* @param string $title
* @param string $key
* @return string
*/
public static function social_item_output_title($title, $key) {
if ($key == 'twitter') {
$title .= __(' retweeted this', 'social');
}
return $title;
}
/**
* Add a "reply to" field to broadcast form.
*
* @static
* @param obj $post
* @param obj $service
* @param obj $account
* @return void
*/
public static function social_broadcast_form_item_edit($post, $service, $account) {
if ($service->key() != 'twitter') {
return;
}
$field_name = str_replace('_content', '_in_reply_to', $account['field_name_content']);
?>
<a href="#" class="tweet-reply-link"><?php _e('Send as a reply', 'social'); ?></a>
<div class="tweet-reply-fields">
<label for="<?php echo esc_attr($field_name); ?>"><?php _e('URL of Tweet (to reply to)', 'social'); ?></label>
<input type="text" class="tweet-reply-field" name="<?php echo esc_attr($field_name); ?>" value="" id="<?php echo esc_attr($field_name); ?>" />
</div>
<?php
}
} // End Social_Twitter
define('SOCIAL_TWITTER_FILE', __FILE__);
// Filters
add_filter('social_register_service', array('Social_Twitter', 'register_service'));
add_filter('get_avatar_comment_types', array('Social_Twitter', 'get_avatar_comment_types'));
add_filter('social_comments_array', array('Social_Twitter', 'comments_array'), 10, 2);
add_filter('social_save_broadcasted_ids_data', array('Social_Twitter', 'social_save_broadcasted_ids_data'), 10, 5);
add_filter('social_item_output_title', array('Social_Twitter', 'social_item_output_title'), 10, 2);
add_action('social_broadcast_form_item_edit', array('Social_Twitter', 'social_broadcast_form_item_edit'), 10, 3);
}