-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboot.php
More file actions
195 lines (166 loc) · 7.98 KB
/
boot.php
File metadata and controls
195 lines (166 loc) · 7.98 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
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
<?php
/**
* JSON-LD Manager AddOn - Boot
*
* Initialisierung und Extension Points für das JSON-LD Manager AddOn
*/
// Template-Funktionen laden
require_once __DIR__ . '/lib/template_functions.php';
// JSON-LD Generator-Klasse laden
require_once __DIR__ . '/lib/JsonLdGenerator.php';
require_once __DIR__ . '/lib/LanguageConfig.php';
require_once __DIR__ . '/lib/DynamicJsonLd.php';
// Nur im Frontend
if (!rex::isBackend() && rex_addon::get('jsonld_manager')->isAvailable()) {
// Extension Point für automatische JSON-LD Ausgabe
rex_extension::register('OUTPUT_FILTER', function (rex_extension_point $ep) {
$content = $ep->getSubject();
if (strpos($content, '</head>') !== false) {
$article = rex_article::getCurrent();
if (!$article || !function_exists('jsonld_is_template_output_allowed') || !jsonld_is_template_output_allowed($article)) {
return $content;
}
$jsonLdOutput = '';
$dynamicJsonLdOutput = '';
// Prüfe ob es eine dynamische URL ist (URL-Addon)
if (rex_addon::get('url')->isAvailable()) {
try {
$urlManager = \Url\Url::resolveCurrent();
if ($urlManager) {
// Dynamische URL erkannt - JSON-LD für URL-Profil generieren
$profileId = $urlManager->getProfileId();
$dataId = $urlManager->getDatasetId();
if ($profileId && $dataId) {
$dynamicJsonLdOutput = generateDynamicJsonLd($profileId, $dataId);
}
}
} catch (Exception $e) {
// Fehler beim URL-Parsing ignorieren
}
}
// Standard JSON-LD immer zusätzlich ausgeben
$jsonLdOutput .= jsonld_render();
// Dynamisches URL-JSON-LD zusätzlich anhängen (falls vorhanden)
$jsonLdOutput .= $dynamicJsonLdOutput;
// Legacy-Meta-Daten ausgeben (nach letztem <meta ...> im <head>)
$legacyMeta = trim(rex_config::get('jsonld_manager', 'legacy_meta_raw', ''));
if ($legacyMeta !== '') {
// Nur ausgeben, wenn Template erlaubt ist (wie bei JSON-LD)
if (function_exists('jsonld_is_template_output_allowed') && jsonld_is_template_output_allowed($article)) {
// Suche alle <meta ...> im <head>
$headStart = stripos($content, '<head');
$headEnd = stripos($content, '</head>');
if ($headStart !== false && $headEnd !== false && $headEnd > $headStart) {
$headContent = substr($content, $headStart, $headEnd - $headStart);
// Finde alle <meta ...> Tags
preg_match_all('/<meta[^>]*>/i', $headContent, $metaMatches, PREG_OFFSET_CAPTURE);
if (!empty($metaMatches[0])) {
$lastMeta = end($metaMatches[0]);
$insertPos = $headStart + $lastMeta[1] + strlen($lastMeta[0]);
$content = substr($content, 0, $insertPos) . "\n" . $legacyMeta . "\n" . substr($content, $insertPos);
} else {
// Kein <meta> gefunden, vor </head> einfügen
$content = str_replace('</head>', $legacyMeta . "\n</head>", $content);
}
} else {
// Kein <head> gefunden, vor </head> einfügen
$content = str_replace('</head>', $legacyMeta . "\n</head>", $content);
}
}
}
if (!empty($jsonLdOutput)) {
$content = str_replace('</head>', $jsonLdOutput . '</head>', $content);
}
}
return $content;
});
}
// Extension Point für Cache-Invalidierung bei Artikel-Änderungen
rex_extension::register('ART_UPDATED', function($ep) {
if (class_exists('\FriendsOfRedaxo\JsonLdManager\Frontend\Renderer')) {
$articleId = 0;
$params = $ep->getParams();
if (isset($params['id'])) {
$articleId = (int) $params['id'];
} elseif (isset($params['article_id'])) {
$articleId = (int) $params['article_id'];
}
\FriendsOfRedaxo\JsonLdManager\Frontend\Renderer::clearCache($articleId > 0 ? $articleId : null);
}
});
// Bedingte Menüanzeige für Dynamische URLs
if (rex::isBackend()) {
// CSS/JS nur auf Addon-Seiten im Backend laden
if (rex_be_controller::getCurrentPagePart(1) === 'jsonld_manager') {
rex_view::addCssFile(rex_url::addonAssets('jsonld_manager', 'css/jsonld_manager.css'));
rex_view::addJsFile(rex_url::addonAssets('jsonld_manager', 'js/jsonld_manager.js'));
}
$hideDynamicUrlsSubpage = static function (): void {
$filter = static function ($page) {
if (!$page || !method_exists($page, 'getSubpages') || !method_exists($page, 'setSubpages')) {
return;
}
$subpages = $page->getSubpages();
if (!is_array($subpages)) {
return;
}
foreach ($subpages as $key => $subpage) {
$subpageKey = method_exists($subpage, 'getKey') ? (string) $subpage->getKey() : (string) $key;
$subpageFullKey = method_exists($subpage, 'getFullKey') ? (string) $subpage->getFullKey() : '';
if ($subpageKey === 'dynamic_urls' || $subpageFullKey === 'jsonld_manager/dynamic_urls') {
unset($subpages[$key]);
}
}
$page->setSubpages($subpages);
};
// Root-Page absichern
$filter(rex_be_controller::getPageObject('jsonld_manager'));
// Aktuelle Navigation (inkl. Parent) absichern
$current = rex_be_controller::getCurrentPageObject();
if ($current) {
$filter($current);
if (method_exists($current, 'getParent')) {
$filter($current->getParent());
}
}
};
$shouldHideDynamicUrls = static function (): bool {
if (!rex_addon::get('url')->isAvailable()) {
return true;
}
$profileCount = rex_sql::factory()->getArray('SELECT COUNT(*) as count FROM ' . rex::getTable('url_generator_profile'));
return !$profileCount || (int) $profileCount[0]['count'] === 0;
};
// Harte UI-Fallback-Ausblendung, falls REDAXO-Navigation den Tab dennoch rendert
if ($shouldHideDynamicUrls()) {
rex_view::addCssFile(rex_url::addonAssets('jsonld_manager', 'css/hide_dynamic_urls_tab.css'));
}
rex_extension::register('PACKAGES_INCLUDED', function() use ($hideDynamicUrlsSubpage, $shouldHideDynamicUrls) {
if ($shouldHideDynamicUrls()) {
$hideDynamicUrlsSubpage();
}
});
rex_extension::register('PAGE_PREPARED', function() use ($hideDynamicUrlsSubpage, $shouldHideDynamicUrls) {
if (!$shouldHideDynamicUrls()) {
return;
}
$hideDynamicUrlsSubpage();
// Direkter Aufruf der Seite verhindern, wenn sie nicht verfügbar ist
$requestedPage = rex_request('page', 'string');
if ($requestedPage === 'jsonld_manager/dynamic_urls') {
rex_response::sendRedirect(rex_url::backendPage('jsonld_manager/article'));
}
});
}
rex_extension::register('ART_DELETED', function($ep) {
if (class_exists('\FriendsOfRedaxo\JsonLdManager\Frontend\Renderer')) {
$articleId = 0;
$params = $ep->getParams();
if (isset($params['id'])) {
$articleId = (int) $params['id'];
} elseif (isset($params['article_id'])) {
$articleId = (int) $params['article_id'];
}
\FriendsOfRedaxo\JsonLdManager\Frontend\Renderer::clearCache($articleId > 0 ? $articleId : null);
}
});