-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathHyper.php
234 lines (195 loc) · 8.83 KB
/
Hyper.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace verbb\hyper;
use verbb\hyper\base\PluginTrait;
use verbb\hyper\base\LinkInterface;
use verbb\hyper\fields\HyperField;
use verbb\hyper\fieldlayoutelements\AriaLabelField;
use verbb\hyper\fieldlayoutelements\ClassesField;
use verbb\hyper\fieldlayoutelements\CustomAttributesField;
use verbb\hyper\fieldlayoutelements\EmbedPreview;
use verbb\hyper\fieldlayoutelements\LinkField;
use verbb\hyper\fieldlayoutelements\LinkTextField;
use verbb\hyper\fieldlayoutelements\LinkTitleField;
use verbb\hyper\fieldlayoutelements\UrlSuffixField;
use verbb\hyper\gql\interfaces\LinkInterface as GqlLinkInterface;
use verbb\hyper\integrations\feedme\fields\Hyper as FeedMeHyperField;
use verbb\hyper\links\Embed;
use verbb\hyper\models\Settings;
use verbb\hyper\variables\HyperVariable;
use Craft;
use craft\base\Plugin;
use craft\elements\db\ElementQuery;
use craft\events\DefineFieldLayoutElementsEvent;
use craft\events\DefineFieldLayoutFieldsEvent;
use craft\events\PopulateElementEvent;
use craft\events\RegisterCacheOptionsEvent;
use craft\events\RegisterComponentTypesEvent;
use craft\events\RegisterGqlTypesEvent;
use craft\events\RegisterUrlRulesEvent;
use craft\helpers\UrlHelper;
use craft\models\FieldLayout;
use craft\services\Elements;
use craft\services\Fields;
use craft\services\Gql;
use craft\services\ProjectConfig;
use craft\utilities\ClearCaches;
use craft\web\Application;
use craft\web\Controller;
use craft\web\twig\variables\CraftVariable;
use craft\web\UrlManager;
use craft\web\View;
use yii\base\ActionEvent;
use yii\base\Event;
use craft\feedme\events\RegisterFeedMeFieldsEvent;
use craft\feedme\services\Fields as FeedMeFields;
class Hyper extends Plugin
{
// Properties
// =========================================================================
public bool $hasCpSettings = true;
public string $schemaVersion = '1.0.0';
// Traits
// =========================================================================
use PluginTrait;
// Public Methods
// =========================================================================
public function init(): void
{
parent::init();
self::$plugin = $this;
$this->_registerVariables();
$this->_registerFieldTypes();
$this->_registerFieldLayoutElements();
$this->_registerProjectConfigEventHandlers();
$this->_registerEventHandlers();
$this->_registerGraphQl();
$this->_registerCacheTypes();
if (Craft::$app->getRequest()->getIsCpRequest()) {
$this->_registerCpRoutes();
}
$this->_registerCachePreload();
}
public function getSettingsResponse(): mixed
{
return Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('hyper/settings'));
}
// Protected Methods
// =========================================================================
protected function createSettingsModel(): Settings
{
return new Settings();
}
// Private Methods
// =========================================================================
private function _registerCpRoutes(): void
{
Event::on(UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, function(RegisterUrlRulesEvent $event) {
$event->rules['hyper'] = 'hyper/plugin/settings';
$event->rules['hyper/settings'] = 'hyper/plugin/settings';
});
}
private function _registerVariables(): void
{
Event::on(CraftVariable::class, CraftVariable::EVENT_INIT, function(Event $event) {
$event->sender->set('hyper', HyperVariable::class);
});
}
private function _registerFieldTypes(): void
{
Event::on(Fields::class, Fields::EVENT_REGISTER_FIELD_TYPES, function(RegisterComponentTypesEvent $event) {
$event->types[] = HyperField::class;
});
}
private function _registerFieldLayoutElements(): void
{
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_NATIVE_FIELDS, function(DefineFieldLayoutFieldsEvent $event) {
if (is_subclass_of($event->sender->type, LinkInterface::class)) {
$event->fields[] = AriaLabelField::class;
$event->fields[] = ClassesField::class;
$event->fields[] = CustomAttributesField::class;
$event->fields[] = LinkField::class;
$event->fields[] = LinkTextField::class;
$event->fields[] = LinkTitleField::class;
$event->fields[] = UrlSuffixField::class;
}
});
Event::on(FieldLayout::class, FieldLayout::EVENT_DEFINE_UI_ELEMENTS, function(DefineFieldLayoutElementsEvent $event) {
if ($event->sender->type === Embed::class) {
$event->elements[] = EmbedPreview::class;
}
});
}
private function _registerProjectConfigEventHandlers(): void
{
$projectConfig = Craft::$app->getProjectConfig();
$projectConfig
->onAdd(ProjectConfig::PATH_FIELDS . '.{uid}', [$this->getService(), 'handleChangedField'])
->onUpdate(ProjectConfig::PATH_FIELDS . '.{uid}', [$this->getService(), 'handleChangedField'])
->onRemove(ProjectConfig::PATH_FIELDS . '.{uid}', [$this->getService(), 'handleDeletedField']);
}
private function _registerEventHandlers(): void
{
Event::on(Elements::class, Elements::EVENT_AFTER_UPDATE_SLUG_AND_URI, [$this->getElementCache(), 'onSaveElement']);
Event::on(Elements::class, Elements::EVENT_AFTER_SAVE_ELEMENT, [$this->getElementCache(), 'onSaveElement']);
Event::on(Elements::class, Elements::EVENT_AFTER_DELETE_ELEMENT, [$this->getElementCache(), 'onDeleteElement']);
// Hijack requests to `actions/matrix/create-entry` to handle non-saved-element owners.
Event::on(Controller::class, Controller::EVENT_BEFORE_ACTION, function(ActionEvent $event) {
// For "As inline-editable blocks"
if ($event->action->id == 'create-entry' && $event->sender->id == 'matrix') {
$ownerElementType = $event->sender->request->getParam('ownerElementType');
// Only override things if this is coming from a Hyper field
if (is_subclass_of($ownerElementType, LinkInterface::class)) {
Craft::$app->runAction('hyper/fields/create-matrix-entry')->send();
}
}
});
if (class_exists(FeedMeFields::class)) {
Event::on(FeedMeFields::class, FeedMeFields::EVENT_REGISTER_FEED_ME_FIELDS, function(RegisterFeedMeFieldsEvent $event) {
$event->fields[] = FeedMeHyperField::class;
});
}
}
private function _registerCachePreload(): void
{
// Before rendering a site-based template, preload the cache for element links for significant
// performance gains. This allows us to query every Hyper link field used on the page in one
// go, rather than one at a time, for each field.
Event::on(Application::class, Application::EVENT_INIT, function() {
// When rendering a page, do a single Hyper cache query for all Hyper fields on the page.
Event::on(View::class, View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, function(Event $event) {
if (!Craft::$app->getRequest()->getIsSiteRequest()) {
return;
}
if (Craft::$app->getResponse()->getIsOk()) {
Hyper::$plugin->getElementCache()->preloadCache();
}
});
// For every element that is populated (on the front-end), record it in our render cache for Hyper.
// We'll use this to check against any element-link Hyper links that refer to _this_ element being recorded.
Event::on(ElementQuery::class, ElementQuery::EVENT_AFTER_POPULATE_ELEMENT, function(PopulateElementEvent $event) {
if (!Craft::$app->getRequest()->getIsSiteRequest()) {
return;
}
if (Craft::$app->getResponse()->getIsOk() && $event->element->id) {
Hyper::$plugin->getElementCache()->addToRenderCache($event->element->id, $event->element->siteId);
}
});
});
}
private function _registerGraphQl(): void
{
Event::on(Gql::class, Gql::EVENT_REGISTER_GQL_TYPES, function(RegisterGqlTypesEvent $event) {
$event->types[] = GqlLinkInterface::class;
});
}
private function _registerCacheTypes(): void
{
Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, function(RegisterCacheOptionsEvent $event) {
$event->options[] = [
'key' => 'hyper',
'label' => Craft::t('hyper', 'Hyper field element cache'),
'action' => [Hyper::$plugin->getElementCache(), 'clearCache'],
];
});
}
}