-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Plugin.php
357 lines (322 loc) · 12.1 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php namespace Winter\Pages;
use Backend;
use Backend\Models\UserRole;
use Cms\Classes\Controller as CmsController;
use Cms\Classes\Theme;
use Config;
use Event;
use System\Classes\PluginBase;
use Winter\Pages\Classes\Controller;
use Winter\Pages\Classes\Page as StaticPage;
use Winter\Pages\Classes\Router;
use Winter\Pages\Classes\Snippet;
use Winter\Pages\Classes\SnippetManager;
use Winter\Pages\Controllers\Index;
class Plugin extends PluginBase
{
public function pluginDetails()
{
return [
'name' => 'winter.pages::lang.plugin.name',
'description' => 'winter.pages::lang.plugin.description',
'author' => 'Winter CMS',
'icon' => 'icon-files-o',
'homepage' => 'https://github.com/wintercms/wn-pages-plugin',
'replaces' => ['RainLab.Pages' => '<= 1.4.10'],
];
}
/**
* Register the CMS components provided by this plugin
*/
public function registerComponents(): array
{
return [
\Winter\Pages\Components\ChildPages::class => 'childPages',
\Winter\Pages\Components\StaticPage::class => 'staticPage',
\Winter\Pages\Components\StaticMenu::class => 'staticMenu',
\Winter\Pages\Components\StaticBreadcrumbs::class => 'staticBreadcrumbs'
];
}
/**
* Register the permissions provided by this plugin
*/
public function registerPermissions(): array
{
return [
'winter.pages.manage_pages' => [
'tab' => 'winter.pages::lang.page.tab',
'order' => 200,
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'label' => 'winter.pages::lang.page.manage_pages',
],
'winter.pages.manage_menus' => [
'tab' => 'winter.pages::lang.page.tab',
'order' => 200,
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'label' => 'winter.pages::lang.page.manage_menus',
],
'winter.pages.manage_content' => [
'tab' => 'winter.pages::lang.page.tab',
'order' => 200,
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'label' => 'winter.pages::lang.page.manage_content',
],
'winter.pages.access_snippets' => [
'tab' => 'winter.pages::lang.page.tab',
'order' => 200,
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'label' => 'winter.pages::lang.page.access_snippets',
],
'winter.pages.access_preview' => [
'tab' => 'winter.pages::lang.page.tab',
'order' => 200,
'roles' => [UserRole::CODE_DEVELOPER, UserRole::CODE_PUBLISHER],
'label' => 'winter.pages::lang.page.access_preview',
],
];
}
/**
* Register the backend navigation items provided by this plugin
*/
public function registerNavigation(): array
{
return [
'pages' => [
'label' => 'winter.pages::lang.plugin.name',
'url' => Backend::url('winter/pages'),
'icon' => 'icon-files-o',
'iconSvg' => 'plugins/winter/pages/assets/images/pages-icon.svg',
'permissions' => ['winter.pages.*'],
'order' => 200,
'sideMenu' => [
'pages' => [
'label' => 'winter.pages::lang.page.menu_label',
'icon' => 'icon-files-o',
'url' => 'javascript:;',
'attributes' => ['data-menu-item'=>'pages'],
'permissions' => ['winter.pages.manage_pages'],
],
'menus' => [
'label' => 'winter.pages::lang.menu.menu_label',
'icon' => 'icon-sitemap',
'url' => 'javascript:;',
'attributes' => ['data-menu-item'=>'menus'],
'permissions' => ['winter.pages.manage_menus'],
],
'content' => [
'label' => 'winter.pages::lang.content.menu_label',
'icon' => 'icon-file-text-o',
'url' => 'javascript:;',
'attributes' => ['data-menu-item'=>'content'],
'permissions' => ['winter.pages.manage_content'],
],
'snippets' => [
'label' => 'winter.pages::lang.snippet.menu_label',
'icon' => 'icon-newspaper-o',
'url' => 'javascript:;',
'attributes' => ['data-menu-item'=>'snippets'],
'permissions' => ['winter.pages.access_snippets'],
],
],
],
];
}
/**
* Register the backend FormWidgets provided by this plugin
*/
public function registerFormWidgets(): array
{
return [
FormWidgets\PagePicker::class => 'staticpagepicker',
FormWidgets\MenuPicker::class => 'staticmenupicker',
];
}
/**
* Register Twig extensions provided by this plugin
*/
public function registerMarkupTags(): array
{
return [
'filters' => [
'staticPage' => ['Winter\Pages\Classes\Page', 'url']
]
];
}
/**
* Clear the caches used by this plugin
*/
public static function clearCache(): void
{
$theme = Theme::getEditTheme();
$router = new Router($theme);
$router->clearCache();
StaticPage::clearMenuCache($theme);
SnippetManager::clearCache($theme);
}
/**
* Boot the plugin
*/
public function boot(): void
{
$this->extendThemeSync();
$this->extendBackendForms();
$this->extendCmsTemplates();
$this->extendCmsRouter();
$this->extendCmsRenderer();
$this->registerMenuItemTypes();
$this->registerRichEditorLinkTypes();
}
/**
* Extend the theme:sync command to include Pages & Menus in the sync
*/
protected function extendThemeSync(): void
{
Event::listen('system.console.theme.sync.getAvailableModelClasses', function () {
return [
Classes\Menu::class,
Classes\Page::class,
];
});
}
/**
* Extend the backend forms
*/
protected function extendBackendForms(): void
{
Event::listen('backend.form.extendFieldsBefore', function ($formWidget) {
if ($formWidget->model instanceof \Cms\Classes\Partial) {
Snippet::extendPartialForm($formWidget);
}
});
// Add the Preview tab as the last tab with a event priority that still
// allows other plugins to change it if desired.
Event::listen('backend.form.extendFieldsBefore', function ($formWidget) {
if (
$formWidget->isNested
|| !($formWidget->getController() instanceof Index)
|| !($formWidget->model instanceof StaticPage)
) {
return;
}
$existingFields = array_merge(
array_keys($formWidget->fields ?? []),
array_keys($formWidget->tabs['fields'] ?? []),
array_keys($formWidget->secondaryTabs['fields'] ?? [])
);
$formWidget->secondaryTabs['fields'] = array_merge($formWidget->secondaryTabs['fields'], [
'_preview' => [
'tab' => 'winter.pages::lang.editor.preview',
'type' => 'partial',
'span' => 'full',
'path' => '$/winter/pages/classes/page/field.preview.php',
'permissions' => ['winter.pages.access_preview'],
'dependsOn' => $existingFields,
],
]);
}, PHP_INT_MIN + 1);
}
/**
* Extend CMS templates
*/
protected function extendCmsTemplates(): void
{
Event::listen('cms.template.save', function($controller, $template, $type) {
Plugin::clearCache();
});
Event::listen('cms.template.processSettingsBeforeSave', function($controller, $dataHolder) {
$dataHolder->settings = Snippet::processTemplateSettingsArray($dataHolder->settings);
});
Event::listen('cms.template.processSettingsAfterLoad', function($controller, $template) {
Snippet::processTemplateSettings($template);
});
Event::listen('cms.template.processTwigContent', function($template, $dataHolder) {
if ($template instanceof \Cms\Classes\Layout) {
$dataHolder->content = Controller::instance()->parseSyntaxFields($dataHolder->content);
}
});
}
/**
* Extend the CMS router
*/
protected function extendCmsRouter(): void
{
Event::listen('cms.router.beforeRoute', function($url) {
return Controller::instance()->initCmsPage($url);
});
}
/**
* Extend the CMS renderer
*/
protected function extendCmsRenderer(): void
{
Event::listen('cms.page.beforeRenderPage', function($controller, $page) {
/*
* Before twig renders
*/
$twig = $controller->getTwig();
$loader = $controller->getLoader();
Controller::instance()->injectPageTwig($page, $loader, $twig);
/*
* Get rendered content
*/
$contents = Controller::instance()->getPageContents($page);
if (!empty($contents)) {
return $contents;
}
});
Event::listen('cms.page.initComponents', function($controller, $page) {
Controller::instance()->initPageComponents($controller, $page);
});
Event::listen('cms.block.render', function($blockName, $blockContents) {
$page = CmsController::getController()->getPage();
if (!isset($page->apiBag['staticPage'])) {
return;
}
$contents = Controller::instance()->getPlaceholderContents($page, $blockName, $blockContents);
if (strlen($contents)) {
return $contents;
}
});
}
/**
* Register the frontend Menu Item types provided by this plugin
*/
protected function registerMenuItemTypes(): void
{
Event::listen('pages.menuitem.listTypes', function() {
return [
'static-page' => 'winter.pages::lang.menuitem.static_page',
'all-static-pages' => 'winter.pages::lang.menuitem.all_static_pages'
];
});
Event::listen('pages.menuitem.getTypeInfo', function($type) {
if ($type == 'url') {
return [];
}
if ($type == 'static-page'|| $type == 'all-static-pages') {
return StaticPage::getMenuTypeInfo($type);
}
});
Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) {
if ($type == 'static-page' || $type == 'all-static-pages') {
return StaticPage::resolveMenuItem($item, $url, $theme);
}
});
}
/**
* Register the link types provided by this plugin to the richeditor
*/
protected function registerRichEditorLinkTypes(): void
{
Event::listen('backend.richeditor.listTypes', function () {
return [
'static-page' => 'winter.pages::lang.menuitem.static_page',
];
});
Event::listen('backend.richeditor.getTypeInfo', function ($type) {
if ($type === 'static-page') {
return StaticPage::getRichEditorTypeInfo($type);
}
});
}
}