Repository navigation
Expand file tree
/
Copy pathPlugin.php
More file actions
137 lines (120 loc) · 4.23 KB
/
Copy pathPlugin.php
File metadata and controls
137 lines (120 loc) · 4.23 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
<?php
namespace BennoThommo\UrlNormaliser;
use System\Classes\PluginBase;
use System\Classes\SettingsManager;
use BennoThommo\UrlNormaliser\Classes\Normalise;
use Backend;
use Event;
use DB;
use Illuminate\Database\QueryException;
class Plugin extends PluginBase
{
public $require = [
'BennoThommo.Meta'
];
public function pluginDetails()
{
return [
'name' => 'bennothommo.urlnormaliser::lang.plugin.name',
'description' => 'bennothommo.urlnormaliser::lang.plugin.description',
'author' => 'Ben Thomson',
'iconSvg' => Backend::url('bennothommo/urlnormaliser/assets/icon.svg')
];
}
public function registerPermissions()
{
return [
'bennothommo.urlnormaliser.change_prefs' => [
'label' => 'bennothommo.urlnormaliser::lang.permission.label',
'tab' => 'system::lang.permissions.name',
'order' => 600,
'roles' => ['developer']
]
];
}
public function registerSettings()
{
return [
'normalise' => [
'label' => 'bennothommo.urlnormaliser::lang.nav.normalise.label',
'description' => 'bennothommo.urlnormaliser::lang.nav.normalise.description',
'category' => SettingsManager::CATEGORY_SYSTEM,
'icon' => 'icon-link',
'class' => 'BennoThommo\UrlNormaliser\Models\Settings',
'keywords' => 'urls redirect url normalise canonical',
'order' => 450,
'permissions' => [
'bennothommo.urlnormaliser.change_prefs'
]
]
];
}
public function registerMarkupTags()
{
return [
'filters' => [
'normalise' => function ($text, $force = false) {
return Normalise::url($text, $force);
},
],
];
}
public function boot()
{
// Prevent plugin from loading if the database does not exist
if (!$this->databaseExists()) {
return;
}
// Add normalise middleware
$this->app['Illuminate\Contracts\Http\Kernel']
->prependMiddleware('BennoThommo\UrlNormaliser\Routing\NormaliseMiddleware');
// Normalise URLs in Static Menus, if enabled
if (Normalise::normaliseNavigation()) {
Event::listen('pages.menu.referencesGenerated', function (&$items) {
$iterator = function ($menuItems) use (&$iterator) {
$result = [];
foreach ($menuItems as $item) {
if ($item->items) {
$item->items = $iterator($item->items);
}
if (isset($item->normalised)) {
continue;
}
// Normalise URL if an internal link, and not a simple hash-bang URL
if (!empty($item->url) && substr($item->url, 0, 1) !== '#') {
$originalUrl = $item->url;
$normalisedUrl = Normalise::url($item->url);
if ($originalUrl !== $normalisedUrl) {
$item->url = $normalisedUrl;
$item->normalised = true;
} else {
$item->normalised = false;
}
} else {
$item->normalised = true;
}
$result[] = $item;
}
return $result;
};
$items = $iterator($items);
});
}
}
/**
* Detects if the database exists.
*
* @return bool
*/
public function databaseExists()
{
$databaseExists = false;
try {
// Test database connection (throw exception if no DB is configured yet)
$databaseExists = DB::table('system_settings')->exists();
} catch (QueryException $e) {
return false;
}
return $databaseExists;
}
}