-
Notifications
You must be signed in to change notification settings - Fork 7
/
WebFeedPlugin.php
167 lines (146 loc) · 5.24 KB
/
WebFeedPlugin.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* @file WebFeedPlugin.php
*
* Copyright (c) 2014-2023 Simon Fraser University
* Copyright (c) 2003-2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class WebFeedPlugin
*
* @brief Web Feeds plugin class
*/
namespace APP\plugins\generic\webFeed;
use APP\core\Application;
use APP\notification\NotificationManager;
use APP\template\TemplateManager;
use PKP\core\JSONMessage;
use PKP\core\PKPPageRouter;
use PKP\linkAction\LinkAction;
use PKP\linkAction\request\AjaxModal;
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\plugins\PluginRegistry;
class WebFeedPlugin extends GenericPlugin
{
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null): bool
{
if (!parent::register($category, $path, $mainContextId)) {
return false;
}
if ($this->getEnabled($mainContextId)) {
$this->setupTemplateLinks();
// Register the block plugin to display feed links at the application sidebar
PluginRegistry::register('blocks', new WebFeedBlockPlugin($this), $this->getPluginPath());
// Register the gateway plugin, which will handle the feed request
PluginRegistry::register('gateways', new WebFeedGatewayPlugin($this), $this->getPluginPath());
}
return true;
}
/**
* Add feed links to page <head> on select/all pages.
*/
public function setupTemplateLinks(): void
{
Hook::add('TemplateManager::display', function (string $hookName, array $args): int {
// Only page requests will be handled
$request = Application::get()->getRequest();
if (!($request->getRouter() instanceof PKPPageRouter)) {
return Hook::CONTINUE;
}
/** @var TemplateManager */
$templateManager = $args[0];
$context = $request->getContext();
if (is_null($context)) {
return Hook::CONTINUE;
}
$displayPage = $this->getSetting($context->getId(), 'displayPage');
// Define when the <link> elements should appear
$contexts = match ($displayPage) {
'homepage' => ['frontend-index', 'frontend-issue'],
// Issue settings are only available for OJS
'issue' => 'frontend-issue',
default => 'frontend'
};
$className = explode('\\', WebFeedGatewayPlugin::class);
$className = end($className);
foreach (WebFeedGatewayPlugin::FEED_MIME_TYPE as $feedType => $mimeType) {
$url = $request->url(null, 'gateway', 'plugin', [$className, $feedType]);
$templateManager->addHeader("webFeedPlugin{$feedType}", "<link rel=\"alternate\" type=\"{$mimeType}\" href=\"{$url}\">", ['contexts' => $contexts]);
}
return Hook::CONTINUE;
});
}
/**
* Retrieves whether the installation supports issues
*/
public static function hasIssues(): bool
{
return str_contains(Application::getName(), 'ojs');
}
/**
* @copydoc Plugin::getContextSpecificPluginSettingsFile()
*/
public function getContextSpecificPluginSettingsFile(): string
{
return $this->getPluginPath() . '/settings.xml';
}
/**
* @copydoc Plugin::getActions()
*/
public function getActions($request, $verb): array
{
$actions = parent::getActions($request, $verb);
if (!$this->getEnabled()) {
return $actions;
}
$router = $request->getRouter();
$url = $router->url($request, null, null, 'manage', null, ['verb' => 'settings', 'plugin' => $this->getName(), 'category' => 'generic']);
array_unshift($actions, new LinkAction('settings', new AjaxModal($url, $this->getDisplayName()), __('manager.plugins.settings')));
return $actions;
}
/**
* @copydoc Plugin::manage()
*/
public function manage($args, $request): JSONMessage
{
if ($request->getUserVar('verb') !== 'settings') {
return parent::manage($args, $request);
}
$form = new SettingsForm($this, $request->getContext()->getId());
if (!$request->getUserVar('save')) {
$form->initData();
return new JSONMessage(true, $form->fetch($request));
}
$form->readInputData();
if (!$form->validate()) {
return new JSONMessage(true, $form->fetch($request));
}
$form->execute();
$notificationManager = new NotificationManager();
$notificationManager->createTrivialNotification($request->getUser()->getId());
return new JSONMessage(true);
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDisplayName(): string
{
return __('plugins.generic.webfeed.displayName');
}
/**
* @copydoc Plugin::getDisplayName()
*/
public function getDescription(): string
{
return __('plugins.generic.webfeed.description');
}
}
if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\generic\webFeed\WebFeedPlugin', '\WebFeedPlugin');
}