-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsocial_feed_aggregator.module
142 lines (120 loc) · 5.13 KB
/
social_feed_aggregator.module
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
<?php
use Brynjdigital\SocialFeed\SocialFeed;
use Drupal\node\Entity\Node;
/*
* Cron job to pull social feeds, as configured by module settings
*/
function social_feed_aggregator_cron() {
// We access our configuration.
$config = \Drupal::configFactory()->getEditable('social_feed_aggregator.settings');
$interval = $config->get('cron.interval');
// check if we need to run the cron yet
$next_execution = \Drupal::state()->get('social_feed_aggregator.next_execution');
$next_execution = !empty($next_execution) ? $next_execution : 0;
if (REQUEST_TIME >= $next_execution) {
// create instance of SocialFeed
$feed = new SocialFeed();
// get facebook posts, if enabled
$facebook_count = 0;
if($config->get('facebook.enabled')==true) {
$feed->facebook->setCredentials([
'app_id' => $config->get('facebook.app_id'),
'app_secret' => $config->get('facebook.app_secret'),
]);
// Get all recent posts facebook user
$data = $feed->facebook->getFeed($config->get('facebook.username'));
foreach ($data as $item) {
$facebook_count += social_feed_aggregator_create_node($item);
}
}
// get twitter posts, if enabled
$twitter_count = 0;
if($config->get('twitter.enabled')==true) {
$feed->twitter->setCredentials([
'consumer_key' => $config->get('twitter.consumer_key'),
'consumer_secret' => $config->get('twitter.consumer_secret'),
'access_token' => $config->get('twitter.access_token'),
'access_token_secret' => $config->get('twitter.access_token_secret'),
]);
// Get all recent posts twitter user
$data = $feed->twitter->getFeed($config->get('twitter.username'));
foreach ($data as $item) {
$twitter_count += social_feed_aggregator_create_node($item);
}
}
// get instagram posts, if enabled
$instagram_count = 0;
if($config->get('instagram.enabled')==true) {
$feed->instagram->setCredentials([
'client_id' => $config->get('instagram.client_id'),
'access_token' => $config->get('instagram.access_token'),
]);
// Get all recent posts instagram user
$data = $feed->instagram->getFeed($config->get('instagram.username'));
foreach ($data as $item) {
$instagram_count += social_feed_aggregator_create_node($item);
}
}
// log job
\Drupal::logger('social_feed_aggregator')->info('Social Feed Aggregator ran. Facebook @facebook. Twitter @twitter. Instagram @instagram.', [
'@facebook' => $config->get('facebook.enabled') ? ' enabled, '.$facebook_count.' new posts' : ' disabled',
'@twitter' => $config->get('twitter.enabled') ? ' enabled, '.$twitter_count.' new posts' : ' disabled',
'@instagram' => $config->get('instagram.enabled') ? ' enabled, '.$instagram_count.' new posts' : ' disabled',
]);
// has the cron job been manually called? show the user a message
if (\Drupal::state()->get('social_feed_aggregator_show_status_message')) {
drupal_set_message(t('The Social Feed Aggregator cron executed at %time', ['%time' => date_iso8601(REQUEST_TIME)]));
\Drupal::state()->set('social_feed_aggregator_show_status_message', FALSE);
}
// set next time for cron to run
\Drupal::state()->set('social_feed_aggregator.next_execution', REQUEST_TIME + $interval);
}
}
/*
* Ensure all post text links are hyperlinks
*/
function social_feed_aggregator_linkify($text)
{
$rexProtocol = '(https?://)?';
$rexDomain = '((?:[-a-zA-Z0-9]{1,63}\.)+[-a-zA-Z0-9]{2,63}|(?:[0-9]{1,3}\.){3}[0-9]{1,3})';
$rexPort = '(:[0-9]{1,5})?';
$rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?';
$rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?';
$text = preg_replace_callback("&\\b$rexProtocol$rexDomain$rexPort$rexPath$rexQuery$rexFragment(?=[?.!,;:\"]?(\s|$))&",
function($match) {
// Prepend http:// if no protocol specified
$completeUrl = $match[1] ? $match[0] : "http://{$match[0]}";
return '<a href="' . $completeUrl . '">'
. $match[2] . $match[3] . $match[4] . '</a>';
}, htmlspecialchars($text));
return $text;
}
/*
* Create the Social Post node, if it does not exist
*/
function social_feed_aggregator_create_node($item)
{
$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('type', 'sfa_social_post')
->condition('field_sfa_id', $item->id);
$entity_ids = $query->execute();
if(empty($entity_ids)) {
$node = Node::create([
'type' => 'sfa_social_post',
'title' => ucwords($item->service).' Post ID: '.$item->id,
'field_sfa_platform' => ucwords($item->service),
'field_sfa_id' => $item->id,
'field_sfa_post' => ['value' => social_feed_aggregator_linkify(html_entity_decode($item->text)), 'format' => 'basic_html'],
'field_sfa_link' => $item->link,
'field_sfa_image' => !empty($item->media->image) ? $item->media->image : '',
'field_sfa_posted' => date("Y-m-d\TH:i:s", $item->created)
]);
$node->save();
return 1;
}
else {
return 0;
}
}