forked from jnadaud/grav-plugin-gitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitter.php
More file actions
74 lines (64 loc) · 2.19 KB
/
Copy pathgitter.php
File metadata and controls
74 lines (64 loc) · 2.19 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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
class GitterPlugin extends Plugin
{
protected $enable = false;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
];
}
/**
* Initialize form if the page has one. Also catches form processing if user posts the form.
*/
public function onPageInitialized()
{
if (!$this->isAdmin()) {
/** @var Page $page */
$page = $this->grav['page'];
if (!$page) {
return;
}
if ($this->enable) {
$this->grav['assets']
->addJs('https://sidecar.gitter.im/dist/sidecar.v1.js', array('loading' => 'defer'))
->addInlineJs('((window.gitter = {}).chat = {}).options = { room: \''.$this->config->get('plugins.gitter.channel').'\' };', array('loading' => 'defer'));
}
}
}
public function onPluginsInitialized()
{
if (!$this->isAdmin()) {
$uri = $this->grav['uri'];
$disable_on_routes = (array) $this->config->get('plugins.gitter.disable_on_routes');
$enable_on_routes = (array) $this->config->get('plugins.gitter.enable_on_routes');
$path = $uri->path();
if (!in_array($path, $disable_on_routes)) {
if (in_array($path, $enable_on_routes)) {
$this->enable = true;
} else {
foreach($enable_on_routes as $route) {
if ($this->startsWith($path, $route)) {
$this->enable = true;
break;
}
}
}
}
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
]);
}
}
/**
* Determine if $haystack starts with $needle. Credit: http://stackoverflow.com/a/10473026/205039
*/
private function startsWith($haystack, $needle) {
return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}
}