-
Notifications
You must be signed in to change notification settings - Fork 1
/
facebook-feed.php
141 lines (115 loc) · 5.36 KB
/
facebook-feed.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
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Page\Page;
use Grav\Common\GPM\Response;
use Grav\Common\Theme;
use \DateTime;
class FacebookFeedPlugin extends Plugin {
// Set Twig Template
private $template_post_html = 'partials/facebook-feed.html.twig';
// Initialize configuration.
public function onPluginsInitialized() {
$this->enable(['onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]]);
}
// Add Twig Extensions.
public function onTwigInitialized() {
$this->grav['twig']->twig->addFunction(new \Twig_SimpleFunction('facebook_feed',
[$this, 'getFacebookFeed']));
//Load CSS
if ($this->config->get('plugins.facebook-feed.fb_settings.enableCSS') == true || $this->config->get('plugins.facebook-feed.fb_settings.enableCSS') == null && $this->config->get('plugins.facebook-feed.fb_settings.enableCSS') != false ) {
$this->grav['assets']->add('plugins://facebook-feed/css/feed.css');
} else {
return false;
}
}
//Add current directory to twig lookup paths.
public function onTwigTemplatePaths() {
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function getFacebookFeed() {
// Get Configs from blueprint
$pageId = $this->config->get('plugins.facebook-feed.fb_settings.page_id');
$accessToken = $this->config->get('plugins.facebook-feed.fb_settings.token');
$limit = $this->config->get('plugins.facebook-feed.fb_settings.limit');
// Generate API with Page Id and Token
$url = 'https://graph.facebook.com/v8.0/' . $pageId . '/posts?fields=attachments,created_time,message&access_token=' . $accessToken . '&limit=' . $limit;
// Check 200 Ok
function get_http_response_code($url) {
$headers = get_headers($url);
return substr($headers[0], 9, 3);
}
$get_http_response_code = get_http_response_code($url);
if ( $get_http_response_code == 200 ) {
$response = Response::get($url);
$result = json_decode($response);
// Get Data from Facebook Feed
foreach ($result->data as $val) {
//Get Post Date
$postDate = date("d.m.Y", strtotime($val->created_time));
//Get Messages
if(property_exists($val, 'message')) {
$message = $val->message;
}
// Get Album Images
$album = [];
if(property_exists($val, 'attachments')) {
if(property_exists($val->attachments->data[0], 'subattachments')) {
$subData = $val->attachments->data[0]->subattachments->data;
foreach ($subData as $key) {
$albumImages = $key->media->image->src;
array_push($album, $albumImages);
}
}
if(property_exists($val->attachments->data[0], 'description')) {
$description = $val->attachments->data[0]->description;
$message = $description;
}
}
// Get post Image
$image = false;
if(property_exists($val, 'attachments') && property_exists($val->attachments->data[0], 'media')) {
$image = $val->attachments->data[0]->media->image->src;
}
//Get post Url
if(property_exists($val, 'attachments') && property_exists($val->attachments->data[0], 'url')) {
$url = $val->attachments->data[0]->target->url;
}
// Store feed Data inside Arrays
$values['values'][] = [
'postDate' => $postDate,
'message' => $message,
'image' => $image,
'album' => $album,
'url' => $url,
];
}
// Get Plugin Config
$pluginConfig = [
'headline' => $this->config->get('plugins.facebook-feed.fb.headline'),
'pageId' => $this->config->get('plugins.facebook-feed.fb_settings.page_id'),
'postLinkTitle' => $this->config->get('plugins.facebook-feed.fb.postLinkTitle'),
'postLinkAlt' => $this->config->get('plugins.facebook-feed.fb.postLinkAlt'),
'mainLinkTitle' => $this->config->get('plugins.facebook-feed.fb.mainLinkTitle'),
'mainLinkAlt' => $this->config->get('plugins.facebook-feed.fb.mainLinkAlt'),
'imgFallback' => $this->config->get('plugins.facebook-feed.fb.imgFallbackSelect'),
'imgFallbackAlt' => $this->config->get('plugins.facebook-feed.fb.imgFallbackAlt'),
'imgFallbackTitle' => $this->config->get('plugins.facebook-feed.fb.imgFallbackTitle'),
'theme' => $this->config->get('system.pages.theme')
];
$output = $this->grav['twig']->twig()->render($this->template_post_html, array(
'data' => $values,
'pluginConfig' => $pluginConfig
));
return $output;
} else {
echo '
<p style="padding:20px;background:#e2001a;color:#fff;text-align:center;font-size:1.5em">
Ooops, Looks like your Page Id and/or Access Token are missing or not correct
</p>
';
}
}
}