This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstagramfeed.php
312 lines (291 loc) · 9.07 KB
/
instagramfeed.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
<?php
/**
* A simple ZenphotoCMS plugin to display the latest public images from an Instagram account
*
* It does not use the API and does not require any login or tokens. It only works with public content.
*
* ## Installation
*
* Place the file `instagramfeed.php` into your `/plugins` folder, enable it and set the plugin options.
*
* Add `instragramFeed::printFreed(4);` to your theme where you want to display the images.
*
* Note the plugin does just print an unordered list with linked thumbs and does not provide any default CSS styling.
*
* ## Customize the display
*
* To customize the feed output create child class within your theme's function.php or a custom pugin like this:
*
* class myInstagramFeed extends instagramFeed {
*
* static function printFeed($number = 4, $size = 1, $class = 'instagramfeed') {
* $content = flickrFeed::getFeed();
* if ($content) {
* // add your customized output here
* }
* }
*
* }
*
* @author Malte Müller (acrylian)
* @licence GPL v3 or later
*/
$plugin_description = gettext('A simple plugin to display the latest public images from a Instagram account');
$plugin_author = 'Malte Müller (acrylian)';
$plugin_version = '1.1';
$plugin_url = 'https/github.com/acrylian/instagramfeed';
$plugin_category = gettext('Media');
$option_interface = 'instagramFeedOptions';
class instagramFeedOptions {
function __construct() {
setOptionDefault('instagramfeed_cachetime', 86400);
}
function getOptionsSupported() {
return array(
gettext('Instagram user') => array(
'key' => 'instagramfeed_user',
'type' => OPTION_TYPE_TEXTBOX,
'order' => 1,
'desc' => gettext('The user name of the Instagram account to fetch')),
gettext('Cache time') => array(
'key' => 'instagramfeed_cachetime',
'type' => OPTION_TYPE_TEXTBOX,
'order' => 1,
'desc' => gettext('The time in seconds the cache is kept until the data is fetched freshly')),
gettext('Clear cache') => array(
'key' => 'instagramfeed_cacheclear',
'type' => OPTION_TYPE_CHECKBOX,
'order' => 1,
'desc' => gettext('Check and save options to clear the cache on force.'))
);
}
function handleOptionSave($themename, $themealbum) {
if (isset($_POST['instagramfeed_cacheclear'])) {
instagramFeed::saveCache('');
instagramFeed::saveLastmod();
setOption('instagramfeed_cacheclear', false);
}
return false;
}
}
/**
* Class to fetch latest images from a Instagram account JSON feed
*/
class instagramFeed {
/**
* Returns an array with the feed information either freshly or from cache.
*
* @return array
*/
static function getFeed() {
$user = trim(getOption('instagramfeed_user'));
if ($user) {
$feedurl = 'https://www.instagram.com/' . $user . '/?__a=1';
$cache = instagramFeed::getCache();
$lastmod = instagramFeed::getLastMod();
$cachetime = intval(getOption('instagramfeed_cachetime'));
if (empty($cache) || (time() - $lastmod) > $cachetime) {
$options = array(
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_HEADER => false
);
$data = curlRequest($feedurl, $options);
$content = json_decode($data);
instagramFeed::saveCache($content);
instagramFeed::saveLastMod(time());
return $content;
} else {
return $cache;
}
}
return array();
}
/**
* Prints a list of images from a users instagramFeed
*
* @param int $number The number of images to display
* @param mixed $size The size to display (0-4 = 150x150, 240x240, 320x320, 480x480, 640x640) or "full" for the full image. Falls back to 0 if nothing is set
* @param string $class default "instragramfeed" to use the default styling
*/
static function printFeed($number = 4, $size = 1, $class = 'instagramfeed') {
$content = instagramFeed::getFeed();
$count = '';
if ($content) {
$posts = instagramFeed::getPosts($content);
?>
<ul class="<?php echo html_encode($class); ?>">
<?php
$count = '';
foreach ($posts as $post) {
$count++;
$posturl = instagramFeed::getPostURL($post);
$location = instagramFeed::getPostLocation($post);
$text = instagramFeed::getPostDescription($post);
$date = instagramFeed::getPostDate($post);
;
if ($size == 'full') {
$img = instagramFeed::getPostFullImage($post);
} else {
$thumbs = $post->node->thumbnail_resources;
$img = instagramFeed::getPostThumb($post, $size);
}
?>
<li>
<a href="<?php echo html_encode($posturl); ?>" title="<?php echo html_encode($location . $date); ?>" target="_blank" rel="noopener">
<img src="<?php echo html_encode($img['url']); ?>" alt="<?php echo html_encode($text); ?>" width="<?php echo $img['width']; ?>" height="<?php echo $img['height']; ?>">
</a>
</li>
<?php
if ($count == $number) {
break;
}
}
?>
</ul>
<?php
}
}
/**
* Gets the URL to the instragram account of the user
*
* @param type $username
* @return string
*/
static function getUserURL() {
$user = getOption('instagramfeed_user');
return 'https://www.instagram.com/' . $user;
}
/**
* Returns an array with object of the instramm posts.
* @param array $content
* @return array
*/
static function getPosts($content) {
if ($content) {
return $content->graphql->user->edge_owner_to_timeline_media->edges;
}
return array();
}
/**
* Returns the instragram post page using the "shortcode" url from the feeed
* @param object $post Post object
* @return string
*/
static function getPostURL($post) {
return 'https://www.instagram.com/p/' . $post->node->shortcode;
}
/**
* Returns the post location
* @param object $post
* @return string
*/
static function getPostLocation($post) {
if (!empty($post->node->location->name)) {
return $post->node->location->name;
}
}
/**
* Returns the post description/text
* @param object $post
* @return string
*/
static function getPostDescription($post) {
return $post->node->edge_media_to_caption->edges[0]->node->text;
}
/**
* Returns the date formatted following Zenphoto's settings
* @param object $post
* @return string
*/
static function getPostDate($post) {
return zpFormattedDate(DATE_FORMAT, $post->node->taken_at_timestamp);
}
/**
* Returns an array with "url", "width" and "height" of the full image
* @param object $post
* @return array
*/
static function getPostFullImage($post) {
return array(
'url' => $post->node->display_url,
'width' => $post->node->dimensions->width,
'height' => $post->node->dimensions->height
);
}
/**
* Returns an array with "url", "width" and "height" of the thumb
* @param object $post
* @param int $size The size to display (0-4 = 150x150, 240x240, 320x320, 480x480, 640x640)
* @return array
*/
static function getPostThumb($post, $size = 0) {
$thumbs = $post->node->thumbnail_resources;
if (!array_key_exists($size, $thumbs)) {
$size = 0;
}
return array(
'url' => $thumbs[$size]->src,
'width' => $thumbs[$size]->config_width,
'height' => $thumbs[$size]->config_height
);
}
/**
* Gets the content from cache if available
* @return array
*/
static function getCache() {
global $_zp_db;
$cache = $_zp_db->querySingleRow('SELECT data FROM ' . $_zp_db->prefix('plugin_storage') . ' WHERE `type` = "instagramfeed" AND `aux` = "instagramfeed_cache"');
if ($cache) {
return json_decode(unserialize($cache['data']));
}
return false;
}
/**
* Stores the content in cache
* @param array $content
*/
static function saveCache($content) {
global $_zp_db;
$hascache = instagramfeed::getCache();
$cache = serialize(json_encode($content));
if ($hascache) {
$sql = 'UPDATE ' . $_zp_db->prefix('plugin_storage') . ' SET `data`=' . $_zp_db->quote($cache) . ' WHERE `type`="instagramfeed" AND `aux` = "instagramfeed_cache"';
} else {
$sql = 'INSERT INTO ' . $_zp_db->prefix('plugin_storage') . ' (`type`,`aux`,`data`) VALUES ("instagramfeed", "instagramfeed_cache",' . $_zp_db->quote($cache) . ')';
}
$_zp_db->query($sql);
}
/**
* Returns the time of the last caching
* @return int
*/
static function getLastMod() {
global $_zp_db;
$lastmod = $_zp_db->querySingleRow('SELECT data FROM ' . $_zp_db->prefix('plugin_storage') . ' WHERE `type`="instagramfeed" AND `aux` = "instagramfeed_lastmod"');
if ($lastmod) {
return $lastmod['data'];
}
return false;
}
/**
* Sets the last modification time
*
* @param int $lastmod Time (time()) of the last caching
*/
static function saveLastmod() {
global $_zp_db;
$haslastmod = instagramfeed::getLastMod();
$lastmod = time();
if ($haslastmod) {
$sql = 'UPDATE ' . $_zp_db->prefix('plugin_storage') . ' SET `data` = ' . $lastmod . ' WHERE `type`="instagramfeed" AND `aux` = "instagramfeed_lastmod"';
} else {
$sql = 'INSERT INTO ' . $_zp_db->prefix('plugin_storage') . ' (`type`,`aux`,`data`) VALUES ("instagramfeed", "instagramfeed_lastmod",' . $lastmod . ')';
}
$_zp_db->query($sql);
}
}