-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.php
64 lines (56 loc) · 2.99 KB
/
Main.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
<?php
namespace IdnoPlugins\InstagramShim {
class Main extends \Idno\Common\Plugin
{
const INSTAGRAM_REGEX = '/https?:\/\/(?:www\.)?instagram.com\/p\/([a-zA-Z0-9_\-]+)/i';
function registerPages()
{
// Auth URL
\Idno\Core\Idno::site()->addPageHandler('account/instagramshim', '\IdnoPlugins\InstagramShim\Pages\Account');
// Template extensions
\Idno\Core\Idno::site()->template()->extendTemplate('account/menu/items', 'account/instagramshim/menu');
}
function registerEventHooks()
{
\Idno\Core\Idno::site()->addEventHook('indiepub/post/start', function (\Idno\Core\Event $evt) {
$data = $evt->data();
$page = $data['page'];
// unfortunately micropub and Known use "syndication" to mean two different things, so
// we'll stash the incoming syndication value somewhere safe.
$syndication = $page->getInput('syndication');
\Idno\Core\Idno::site()->logging()->debug("started indiepub with syndication input: $syndication");
if (!empty($syndication)) {
$page->setInput('igshim_syndication', $syndication);
}
});
\Idno\Core\Idno::site()->addEventHook('indiepub/post/success', function (\Idno\Core\Event $evt) {
$data = $evt->data();
$page = $data['page'];
$object = $data['object'];
$syndication = $page->getInput('igshim_syndication');
\Idno\Core\Idno::site()->logging()->debug("finished indiepub with syndication input: $syndication");
$changed = false;
if (!empty($syndication)) {
foreach ((array) $syndication as $url) {
if (preg_match(self::INSTAGRAM_REGEX, $url, $matches)) {
$user = \Idno\Core\Idno::site()->session()->currentUser();
$account = '';
if (!empty($user) && !empty($user->instagram_account)) {
$account = $user->instagram_account;
}
// remove low-quality posse links
unset($object->posse['www.instagram.com']);
unset($object->posse['instagram.com']);
unset($object->posse['instagram']);
$object->setPosseLink('instagram', $url, $account, $matches[1], $account);
$changed = true;
}
}
}
if ($changed) {
$object->save();
}
});
}
}
}