-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebhookPlugin.php
More file actions
154 lines (129 loc) · 4.42 KB
/
Copy pathWebhookPlugin.php
File metadata and controls
154 lines (129 loc) · 4.42 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace APP\plugins\generic\webhook;
use Illuminate\Support\Facades\Event;
use PKP\core\JSONMessage;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\observers\events\PublicationPublished;
use PKP\plugins\GenericPlugin;
use PKP\submission\PKPSubmission;
class WebhookPlugin extends GenericPlugin
{
public function getDisplayName()
{
return __('plugins.generic.webhook.displayName');
}
public function getDescription()
{
$description = __('plugins.generic.webhook.description');
if (!$this->hasValidWebhookConfig()) {
$description .= ' ' . __('plugins.generic.webhook.description.requiresConfig');
}
return $description;
}
/**
* @copydoc Plugin::register()
*/
public function register($category, $path, $mainContextId = null)
{
if (!parent::register($category, $path, $mainContextId)) {
return false;
}
if ($this->getEnabled($mainContextId)) {
Event::listen(
PublicationPublished::class,
function (PublicationPublished $event) {
$publication = $event->publication;
$oldPublication = $event->oldPublication;
$submission = $event->submission;
$wasPublished = $oldPublication->getData('status') === PKPSubmission::STATUS_PUBLISHED;
$isPublished = $publication->getData('status') === PKPSubmission::STATUS_PUBLISHED;
if (!$wasPublished && $isPublished) {
$this->dispatchWebhook($submission->getData('contextId'), $submission->getId(), $publication->getId());
}
}
);
}
return true;
}
/**
* Queue a webhook notification for an asynchronous send, if a webhook URL is configured.
*/
protected function dispatchWebhook(int $contextId, int $submissionId, int $publicationId): void
{
if (!$this->hasValidWebhookConfig($contextId)) {
return;
}
$webhookUrl = $this->getSetting($contextId, 'webhookUrl');
$webhookSecret = $this->getSetting($contextId, 'webhookSecret');
SendWebhookJob::dispatch($submissionId, $publicationId, $webhookUrl, $webhookSecret ?: null);
}
/**
* Whether a syntactically valid webhook URL is configured for the given context
* (or the current context/site if none is given).
*/
protected function hasValidWebhookConfig(?int $contextId = null): bool
{
if ($contextId === null) {
$contextId = $this->getCurrentContextId();
}
return WebhookUrlValidator::isValid($this->getSetting($contextId, 'webhookUrl'));
}
/**
* @copydoc Plugin::getCanEnable()
*/
public function getCanEnable()
{
return $this->hasValidWebhookConfig();
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $actionArgs)
{
$actions = parent::getActions($request, $actionArgs);
$router = $request->getRouter();
$linkAction = new LinkAction(
'settings',
new AjaxModal(
$router->url(
$request,
null,
null,
'manage',
null,
[
'verb' => 'settings',
'plugin' => $this->getName(),
'category' => 'generic',
]
),
$this->getDisplayName()
),
__('manager.plugins.settings'),
null
);
array_unshift($actions, $linkAction);
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request)
{
switch ($request->getUserVar('verb')) {
case 'settings':
$form = new WebhookSettingsForm($this);
if ($request->getUserVar('save')) {
$form->readInputData();
if ($form->validate()) {
$form->execute();
return new JSONMessage(true);
}
}
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
return parent::manage($args, $request);
}
}