-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailBlockerPlugin.php
More file actions
128 lines (100 loc) · 3.6 KB
/
EmailBlockerPlugin.php
File metadata and controls
128 lines (100 loc) · 3.6 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
<?php
/**
* @file EmailBlockerPlugin.php
*
* @class EmailBlockerPlugin
* @brief Allows journal managers to block specific email types from being sent.
*/
namespace APP\plugins\generic\emailBlocker;
use APP\core\Application;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Support\Facades\Event;
use PKP\observers\events\MessageSendingFromContext;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
class EmailBlockerPlugin extends GenericPlugin
{
public function register($category, $path, $mainContextId = null): bool
{
$success = parent::register($category, $path, $mainContextId);
if ($success && $this->getEnabled($mainContextId)) {
// Inject mailable class into viewData so it's available in the event
Hook::add('Mailable::build', [$this, 'injectMailableClass']);
// Block emails based on plugin settings
Event::listen(MessageSendingFromContext::class, [$this, 'blockEmail']);
// Add section to workflow settings emails tab
Hook::add('Template::Settings::workflow', [$this, 'addSettingsSection']);
Hook::add('Schema::get::context', array($this, 'addToSchema'));
}
return $success;
}
/**
* Inject the mailable class name into viewData so it travels to MessageSendingFromContext
*/
public function injectMailableClass(string $hookName, array $args): bool
{
$mailable = $args['mailable'];
$mailable->addData(['mailableClass' => get_class($mailable)]);
return false;
}
/**
* Block emails based on journal settings
*/
public function blockEmail(MessageSendingFromContext $event): bool|null
{
$mailableClass = $event->data['mailableClass'] ?? null;
if (!$mailableClass) {
return null;
}
$blockedMailables = $event->context->getData('emailBlockerMailablesSelected') ?? [];
if (in_array($mailableClass, $blockedMailables)) {
error_log('[EmailBlocker] Blocked: ' . $mailableClass . ' for context ' . $event->context->getId());
return false;
}
return null;
}
/**
* Add settings section to workflow > emails tab
*/
public function addSettingsSection(string $hookName, array $args): bool
{
$templateMgr =& $args[1];
$output =& $args[2];
$request =& Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();
$contextApiUrl = $dispatcher->url(
$request,
Application::ROUTE_API,
$context->getPath(),
'contexts/' . $context->getId()
);
$emailBlockerSettingsForm = new EmailBlockerSettingsForm($contextApiUrl, $context->getSupportedLocaleNames(), $context);
$templateMgr->setConstants([
'FORM_EMAIL_BLOCKER_SETTINGS',
]);
$state = $templateMgr->getTemplateVars('state');
$state['components'][FORM_EMAIL_BLOCKER_SETTINGS] = $emailBlockerSettingsForm->getConfig();
$templateMgr->assign('state', $state);
$output .= $templateMgr->display($this->getTemplateResource('settingsForm.tpl'));
return false;
}
public function addToSchema($hookName, $params) {
$schema =& $params[0];
$schema->properties->{"emailBlockerMailablesSelected"} = (object) [
'type' => 'array',
'apiSummary' => true,
'validation' => ['nullable'],
'items' => (object) ['type' => 'string']
];
return false;
}
public function getDisplayName(): string
{
return __('plugins.generic.emailBlocker.displayName');
}
public function getDescription(): string
{
return __('plugins.generic.emailBlocker.description');
}
}