-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsletter.php
72 lines (61 loc) · 1.78 KB
/
newsletter.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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Plugin\Newsletter\Container;
use Grav\Plugin\Newsletter\Controller\NewsletterController;
use RocketTheme\Toolbox\Event\Event;
/**
* Class NewsletterPlugin
* @package Grav\Plugin
*/
class NewsletterPlugin extends Plugin
{
const UNSUBSCRIBE_PATH = '/newsletter/unsubscribe';
/** @var Container */
private $container;
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
return;
}
$this->setContainer();
$this->enable([
'onFormProcessed' => ['onFormProcessed', 0],
'onPageInitialized' => ['onPageInitialized']
]);
}
public function onFormProcessed(Event $event)
{
$form = $event['form'];
$action = $event['action'];
$params = $event['params'];
switch ($action) {
case 'subscribe':
$handlers = $this->container->getFormProcessor()->getHandlers($form, $params);
$this->container->getFormProcessor()->processSubscribe($handlers);
}
}
public function onPageInitialized(Event $event)
{
if ($this->grav['uri']->path() === self::UNSUBSCRIBE_PATH) {
$controller = new NewsletterController($this->container);
$controller->execute('unsubscribe');
}
}
private function setContainer(): void
{
$this->container = new Container();
$this->container['grav'] = function () {
return $this->grav;
};
$this->container['config'] = function () {
return $this->config;
};
}
}