-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
150 lines (130 loc) · 4.92 KB
/
Plugin.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
<?php
namespace matthiasott\webmention;
use Craft;
use craft\base\Element;
use craft\base\Event;
use craft\base\Model;
use craft\base\Plugin as BasePlugin;
use craft\console\Controller as ConsoleController;
use craft\console\controllers\ResaveController;
use craft\elements\Entry;
use craft\events\DefineBehaviorsEvent;
use craft\events\DefineConsoleActionsEvent;
use craft\events\ModelEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\log\MonologTarget;
use craft\services\Entries;
use craft\services\Fields;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use matthiasott\webmention\behaviors\ElementBehavior;
use matthiasott\webmention\elements\Webmention;
use matthiasott\webmention\fields\WebmentionSwitch;
use matthiasott\webmention\models\Settings;
use matthiasott\webmention\services\Sender;
use matthiasott\webmention\services\Webmentions;
use matthiasott\webmention\variables\WebmentionVariable;
use Monolog\Formatter\LineFormatter;
use Psr\Log\LogLevel;
use yii\base\Event as YiiEvent;
/**
* @property-read Sender $sender
* @property-read Webmentions $webmentions
* @property-read Settings $settings
*/
class Plugin extends BasePlugin
{
public static function config(): array
{
return [
'components' => [
'sender' => Sender::class,
'webmentions' => Webmentions::class,
],
];
}
public bool $hasCpSection = true;
public bool $hasCpSettings = true;
public bool $hasReadOnlyCpSettings = true;
public string $schemaVersion = '1.0.0.6';
public function init(): void
{
parent::init();
Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function(ModelEvent $event) {
$this->webmentions->onSaveEntry($event);
});
Event::on(Entries::class, Entries::EVENT_AFTER_SAVE_SECTION, function() {
$this->webmentions->syncEntryTypes();
});
Event::on(Entries::class, Entries::EVENT_AFTER_DELETE_SECTION, function() {
$this->webmentions->syncEntryTypes();
});
Event::on(Entries::class, Entries::EVENT_AFTER_SAVE_ENTRY_TYPE, function() {
$this->webmentions->syncEntryTypes();
});
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = WebmentionSwitch::class;
});
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_SITE_URL_RULES, function(RegisterUrlRulesEvent $event) {
$event->rules += [
$this->settings->endpointSlug => 'webmention/webmention/handle-request',
];
});
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
$event->rules += [
'settings/webmention' => ['template' => 'webmention/settings'],
];
});
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(YiiEvent $event) {
$event->sender->set('webmention', WebmentionVariable::class);
});
Event::on(Element::class, Model::EVENT_DEFINE_BEHAVIORS, function(DefineBehaviorsEvent $event) {
$event->behaviors['webmention'] = ElementBehavior::class;
});
Event::on(ResaveController::class, ConsoleController::EVENT_DEFINE_ACTIONS, static function(DefineConsoleActionsEvent $e) {
$e->actions['webmentions'] = [
'action' => function(): int {
/** @var ResaveController $controller */
$controller = Craft::$app->controller;
return $controller->resaveElements(Webmention::class);
},
'helpSummary' => 'Re-saves webmentions.',
];
});
Craft::getLogger()->dispatcher->targets[] = new MonologTarget([
'name' => 'webmention',
'categories' => ['webmention'],
'level' => LogLevel::INFO,
'logContext' => false,
'allowLineBreaks' => false,
'formatter' => new LineFormatter(
format: "%datetime% %message%\n",
dateFormat: 'Y-m-d H:i:s',
),
]);
}
public function getCpNavItem(): ?array
{
return [
...parent::getCpNavItem(),
'label' => Craft::t('webmention', 'Webmentions'),
];
}
protected function createSettingsModel(): ?Model
{
return new Settings();
}
public function getSettingsResponse(): mixed
{
$this->webmentions->syncEntryTypes();
return parent::getSettingsResponse();
}
protected function settingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('webmention/_settings.twig', [
'settings' => $this->settings,
'readOnly' => !Craft::$app->config->general->allowAdminChanges,
]);
}
}