forked from getgrav/grav-plugin-maintenance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaintenance.php
98 lines (82 loc) · 2.56 KB
/
maintenance.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
class MaintenancePlugin extends Plugin
{
public $maintenance;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->maintenance = $this->config->get('plugins.maintenance');
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onPageInitialized' => ['onPageInitialized', 0]
]);
}
/**
* Initialize a maintenance page
*/
public function onPageInitialized()
{
$user = $this->grav['user'];
$user->authorise($this->maintenance['login_access']);
if ($this->maintenance['active']) {
if (!$user->authenticated) {
/** @var $page */
$page = null;
/** @var Pages $pages */
$pages = $this->grav['pages'];
// Get the custom page route if specified
$custom_page_route = $this->config->get('plugins.maintenance.maintenance_page_route');
if ($custom_page_route) {
// Try to load user error page.
$page = $pages->dispatch($custom_page_route, true);
}
// If no page found yet, use the built-in one...
if (!$page) {
$page = new Page;
$page->init(new \SplFileInfo(__DIR__ . "/pages/maintenance.md"));
}
// unset the old page, and use the new one
unset($this->grav['page']);
$this->grav['page'] = $page;
}
}
}
/**
* Add current directory to twig lookup paths.
*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Set needed variables to display the maintenance page.
*/
public function onTwigSiteVariables()
{
/** @var User $user */
$user = $this->grav['user'];
if ($this->maintenance['active'] && !$user->authenticated) {
$this->grav['twig']->twig_vars['maintenance'] = $this->maintenance;
}
}
}