-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
executable file
·190 lines (168 loc) · 4.88 KB
/
Copy pathplugin.php
File metadata and controls
executable file
·190 lines (168 loc) · 4.88 KB
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
<?php
class InstagramFeedPlugin extends KokenPlugin
{
/**
* Template for rendering a full Instagram feed.
*
* @var string
*/
protected $feedTemplate;
/**
* Template for rendering an Instagram image.
*
* @var string
*/
protected $imageTemplate;
/**
* A pattern to find and replace 'instagram' tags.
*
* @var string
*/
protected $instagramTag;
/**
* Create a new Instagram Feed Plugin instance.
*
* @return void
*/
public function __construct()
{
$this->feedTemplate = file_get_contents(__DIR__.'/assets/views/feed.html');
$this->imageTemplate = file_get_contents(__DIR__.'/assets/views/image.html');
$this->instagramTag = '/<(\s*?)instagram(.*?)>(.*?)<(\s*?)\/instagram(\s*?)>/';
$this->register_hook('before_closing_head', 'insertInstagramStyles');
$this->register_filter('site.output', 'insertInstagramFeed');
}
/**
* Append default styles to the <head> tag.
*
* @return void
*/
public function insertInstagramStyles()
{
echo '<style>'.file_get_contents(__DIR__.'/assets/css/main.css').'</style>';
}
/**
* Replace any 'instagram' tags found in a page before it is rendered.
*
* @see $this->replaceInstagramTag()
*
* @param string $content
*
* @return string
*/
public function insertInstagramFeed($content)
{
return preg_replace_callback($this->instagramTag, array($this, 'replaceInstagramTag'), $content);
}
/**
* Replaces any 'instagram' tag found in html markup with a feed of most recent images.
*
* Example: '<instagram username="name" count="4"></instagram>'
*
* @param array $matches
*
* @return string
*/
protected function replaceInstagramTag($matches)
{
$attributes = (new SimpleXMLElement('<element '.$matches[2].' />'))->attributes();
if ($feed = $this->getFeedForUsername($attributes['username'])) {
return $this->createFeedTag($feed, $attributes['images']);
}
return '';
}
/**
* Retrieves and converts a user's Instagram feed to an array.
*
* @param string|null $username
*
* @return array|null
*/
protected function getFeedForUsername($username = null)
{
if ($username = $username ?: $this->data->username) {
return json_decode(file_get_contents("https://www.instagram.com/{$username}/media/"), true);
}
}
/**
* Creates an html tag for using data from an Instagram feed.
*
* @param array $feed
* @param integer $count
*
* @return string
*/
protected function createFeedTag($feed, $count = null)
{
return $this->replaceTemplateVariables($this->feedTemplate, [
'images' => $this->createImageTags($feed['items'], $count),
'username' => $feed['items'][0]['user']['username'],
]);
}
/**
* Creates multiple html tags using data from an Instagram feed.
*
* @see $this->createImageTag()
*
* @param array $images
* @param integer $count
*
* @return string
*/
protected function createImageTags($images, $count = null)
{
$images = array_slice($images, 0, $count ?: $this->data->count, true);
$imageViews = array_map(function ($image) {
return $this->createImageTag($image);
}, $images);
return join('', $imageViews);
}
/**
* Creates a html tag using data from an Instagram feed image.
*
* @param array $image
*
* @return string
*/
protected function createImageTag($image)
{
return $this->replaceTemplateVariables($this->imageTemplate, [
'src' => $image['images']['thumbnail']['url'],
'link' => $image['link'],
'title' => $image['caption']['text'],
]);
}
/**
* Replaces multiple variables in a template with values.
*
* @see $this->replaceTemplateVariable()
*
* @param string $template
* @param array $variables
*
* @return string
*/
protected function replaceTemplateVariables($template, $variables)
{
foreach ($variables as $name => $value) {
$template = $this->replaceTemplateVariable($template, $name, $value);
}
return $template;
}
/**
* Replaces variables in a template with values using handlebar syntax.
*
* Example: 'Hello, {{ name }}!', 'name', 'world'
*
* Result: 'Hello, world!'
*
* @param string $template
* @param array $variables
*
* @return string
*/
protected function replaceTemplateVariable($template, $name, $value)
{
return preg_replace('/\{\{(\s*?)'.$name.'(\s*?)\}\}/', $value, $template);
}
}