-
Notifications
You must be signed in to change notification settings - Fork 1
/
theme.php
449 lines (416 loc) · 20.1 KB
/
theme.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php global $Wcms; ?>
<!DOCTYPE html>
<?php
if ( method_exists( $Wcms, 'getSiteLanguage' ) ) {
// WonderCMS 3.4.3+
echo '<html lang="' . htmlentities( $Wcms->getSiteLanguage() ) . '" data-bs-theme="auto">';
} else {
// WonderCMS < 3.4.3
echo '<html lang="en" data-bs-theme="auto">';
}
?>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php
echo $Wcms->get('config', 'siteTitle') . ' - ' . $Wcms->page('title');
?></title>
<meta name="description" content="<?php echo $Wcms->page('description') ?>">
<meta name="keywords" content="<?php echo $Wcms->page('keywords') ?>">
<meta name="title" content="<?php echo $Wcms->get('config', 'siteTitle') ?> - <?php echo $Wcms->page('title') ?>" />
<meta property="og:url" content="<?php echo $this->url() ?>" />
<meta property="og:type" content="website" />
<meta property="og:site_name" content="<?php echo $Wcms->get('config', 'siteTitle') ?>" />
<meta property="og:title" content="<?php echo $Wcms->page('title') ?>" />
<meta name="twitter:site" content="<?php echo $this->url() ?>" />
<meta name="twitter:title" content="<?php echo $Wcms->get('config', 'siteTitle') ?> - <?php echo $Wcms->page('title') ?>" />
<meta name="twitter:description" content="<?php echo $Wcms->page('description') ?>" />
<?php
// Admin CSS
echo $Wcms->css();
// Theme CSS
echo '<link rel="stylesheet" href="' . $Wcms->asset('css/style.css') . '">';
?>
</head>
<body>
<!-- Disable Bootstrap's "animation" on "collapsing" for the navbar -->
<style>
.collapsing {
transition-delay: 0s !important;
transition-duration: 0s !important;
transition: none !important;
display: none !important;
}
</style>
<script>
<?php
/**
* It's not always best practice to put a script tag here since it
* will block rendering of the page marginally, but this is a very
* minor script block that is used to set the correct Bootstrap 5
* "color mode" so it matches the visitor browser's setting (i.e.
* "Dark mode" or "Light mode") without getting a brief flash
* effect every time the page loads
**/
?>
(() => {
'use strict'
const colorMode = window.matchMedia("(prefers-color-scheme: dark)").matches ?
"dark" :
"light";
document.querySelector("html").setAttribute("data-bs-theme", colorMode);
})()
</script>
<div class="container p-1 wcmsbs5-body">
<?php echo $Wcms->alerts() ?>
<div class="settings-margin">
<?php echo $Wcms->settings() ?>
</div>
<?php
/**
* Deconstruct possible tokens
*/
if ( ! empty( $_POST['token'] ) ) {
$thisToken = $_POST['token'];
} else {
$thisToken = '';
}
/**
* Deconstruct search parameter, if there is one.
*/
if ( ! empty( $_POST['searchtext'] ) ) {
$searchString = trim( urldecode( mb_substr( $_POST['searchtext'], 0, 64 ) ) );
} else {
$searchString = '';
}
/**
* Make sure we have a valid token if there's a search parameter
*/
if ( ! empty( $searchString ) && ! $Wcms->hashVerify( $thisToken ) ) {
// No match, ignore search
$searchString = '';
}
/**
* Perform search, after pre-conditioning the pages in question
*/
// ----
// Walk through menu items, excluding empty/hidden/invisible items
function iterateItems( $theItems ) {
$items = array();
foreach( $theItems as $item ) {
if ( empty( $item->visibility ) || $item->visibility != 'show' ) {
return( $items );
}
if ( empty ( $item->slug ) ) {
return( $items );
}
$items[$item->slug] = $item->slug;
$subpages = $item->subpages;
$visibleSubpage = $subpages && in_array( 'show', array_column( (array)$subpages, 'visibility' ) );
if ( $visibleSubpage ) {
$items['subpages'] = iterateItems( $subpages, $items );
}
}
return( $items );
}
// ----
// Find menu items
$allItems = $Wcms->db->config->menuItems;
$ourItems = iterateItems( $allItems );
// Find corresponding pages
$allPages = (array)$Wcms->db->pages;
$ourPages = array_intersect_key( $allPages, $ourItems );
$GLOBALS['searchpages'] = array();
// ----
// Walk through visible pages, process search string and slugs
// The use of $GLOBALS[] isn't pretty, duly noted :-)
function pageWalk( $item, $key, $slug ) {
if ( isset( $item->content ) ) {
// Remove HTML, etc
$content = preg_replace( '/<(|\/)(?!\?).*?(|\/)>/', '', $item->content );
} else {
$content = '';
}
$GLOBALS['searchpages'][] = array(
'slug' => $slug . '/' . $key,
'title' => $item->title,
'content' => $content,
);
if ( $item->subpages ) {
array_walk_recursive( $item->subpages, 'pageWalk', $slug . '/' . $key );
}
}
// ----
$matchingContent = array();
if ( ! empty( $searchString ) ) {
// Flatten or findings
if ( array_walk_recursive( $ourPages, 'pageWalk', '' ) ) {
/*
error_log('Final result-------------------------------------');
error_log(print_r($GLOBALS['searchpages'] , true));
*/
foreach( $GLOBALS['searchpages'] as $page ) {
if ( mb_stristr( $page['title'], $searchString ) !== false ) {
$matchingContent[] = array(
'slug' => $page['slug'],
'title' => $page['title'],
'content' => '',
);
} else {
$match = mb_stristr( $page['content'], $searchString );
if ( $match !== false ) {
$matchingContent[] = array(
'slug' => $page['slug'],
'title' => $page['title'],
'content' => '<mark>' . $searchString . '</mark>' .
mb_substr( $match, mb_strlen( $searchString ), 128 ),
);
}
}
}// foreach
} else {
// Something didn't work out
error_log( __FILE__ . '(' . __LINE__ . '): Unable to process pages' );
}
/**
* See if the SimpleBlog plugin is present, in which case we need to search there too.
*
* TODO: This could possibly be improved or be done differently if WonderCMS 3.4.3+ is
* installed since it has a installedPlugins() function. I'll leave this as it
* is for now though.
*/
$simpleBlogData = $Wcms->dataPath . '/simpleblog.json';
if ( file_exists( $Wcms->rootDir . '/plugins/simple-blog/simple-blog.php' ) ) {
if ( file_exists( $simpleBlogData ) ) {
$blogPosts = json_decode( file_get_contents( $simpleBlogData ), true );
if ( ! empty( $blogPosts['posts'] ) && is_array( $blogPosts['posts'] ) ) {
foreach( $blogPosts['posts'] as $slug => $post ) {
if ( mb_stristr( $post['title'], $searchString ) !== false ) {
$matchingContent[] = array(
'slug' => '/blog/' . $slug,
'title' => $post['title'],
'content' => '',
);
} else {
// Remove HTML, etc
$content = preg_replace( '/<(|\/)(?!\?).*?(|\/)>/', '', $post['body'] );
$match = mb_stristr( $content, $searchString );
if ( $match !== false ) {
$matchingContent[] = array(
'slug' => '/blog/' . $slug,
'title' => $post['title'],
'content' => '<mark>' . $searchString . '</mark>' .
mb_substr( $match, mb_strlen( $searchString ), 128 ),
);
} elseif ( ! empty( $post['description'] ) ) {
// Also check "description" part of post
$content = preg_replace( '/<(|\/)(?!\?).*?(|\/)>/', '', $post['description'] );
$match = mb_stristr( $content, $searchString );
if ( $match !== false ) {
$matchingContent[] = array(
'slug' => '/blog/' . $slug,
'title' => $post['title'],
'content' => '<mark>' . $searchString . '</mark>' .
mb_substr( $match, mb_strlen( $searchString ), 128 ),
);
}
}
}
}// foreach
} else {
// Something didn't work out
error_log( __FILE__ . '(' . __LINE__ . '): Unable to process SimpleBlog data in "' . $simpleBlogData . '"' );
}
}
}
} // ! empty( $searchString )
?>
<div class="container sticky-top pb-5">
<nav class="navbar wcmsbs5-navbar justify-content-start align-items-start" role="navigation">
<div class="mx-0">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#bs5navBar" aria-controls="bs5navBar" aria-expanded="false" aria-label="Toggle navigation">
<small><span class="navbar-toggler-icon"></span></small>
</button>
</div>
<div class="navbar-text text-truncate ms-1 flex-fill w-50 p-1 wcmsbs5-navbar-site-title">
<a class="h4 text-decoration-none wcmsbs5-navbar-site-title-text"
href="<?php echo $Wcms->url(); ?>"
title="<?php echo htmlentities( $Wcms->get( 'config', 'siteTitle' ) ); ?>" >
<?php echo htmlentities( $Wcms->get( 'config', 'siteTitle' ) ); ?>
</a>
</div>
<div class="navbar-text text-right ms-1 w-25 p-0 align-middle wcmsbs5-navbar-search">
<form method="post" name="searchform" id="searchform" role="search" action="<?php echo htmlentities( $Wcms->getCurrentPageUrl() ); ?>">
<input type="hidden" name="token" value="<?php echo htmlentities( $Wcms->getToken() ); ?>" />
<div class="input-group d-flex justify-content-end w-100">
<input class="form-control p-1" name="searchtext" id="searchtext" type="search" aria-label="Search" maxlength="200" value="<?php echo htmlentities( $searchString ); ?>" />
<button class="btn btn btn-outline-secondary btn-sm d-none d-lg-inline-block" name="searchbutton" id="searchbutton" type="button">Search</button>
</div>
</form>
</div>
<div class="collapse navbar-collapse p-2 mt-2 bg-secondary-subtle rounded-bottom border border-3 border-secondary-subtle w-75" id="bs5navBar">
<ul class="navbar-nav ms-1 mt-3 text-truncate">
<?php echo $Wcms->menu(); ?>
</ul>
</div>
</nav>
</div>
<?php
/**
* If we're searching, display result of search
*/
if ( ! empty( $searchString ) ) {
?>
<div class="container wcmsbs5-maincontainer">
<div class="bg-body-tertiary text-body p-5 rounded wcmsbs5-maincontent">
<?php
if ( empty( $matchingContent ) ) {
echo '<p class="wcmsbs5-notfound">' .
'No content matches the search criteria' . ' 🤔' .
'</p>';
} else {
foreach( $matchingContent as $m ) {
echo '<a class="text-decoration-none" href="' . $Wcms->url( ltrim( $m['slug'], '/' ) ) . '">';
echo '<div class="mt-4 wcmsbs5-match text-wrap">'.
'<h5 class="wcmsbs5-match-title">➡️ '.
htmlentities( $m['title'] ) .
'</h5>';
if ( ! empty( $m['content'] ) ) {
echo '<p class="wcmsbs5-match-content text-wrap">'.
$m['content'] .
'</p>';
}
echo '</div>';
echo '</a>';
}// foreach
}
?>
</div>
</div>
<?php
} else {
/**
* Display title of current page, unless we're on a page called
* "blog" and the plugin "Simple Blog" is installed. I really don't
* like kludges, but "Simple Blog" is a nice plugin, so we'll be
* nice. And "Simple Blog" does not like it when we display the
* actual page('title') since it seems to use that for other
* things.
*/
$hideTitle = false;
$simpleBlogSlug = 'blog';
$pathTest = $Wcms->currentPageTree;
if ( array_shift( $pathTest ) === $simpleBlogSlug ) {
if ( file_exists( $Wcms->rootDir . '/plugins/simple-blog/simple-blog.php' ) ) {
$hideTitle = true;
}
}
if ( ! $hideTitle ) {
?>
<div class="container wcmsbs5-pagetitle">
<h1 class="h2 text-body-secondary" title="<?php echo htmlentities( $Wcms->page('title') ); ?>">
<?php
// The page title
// Not sure if I should use $Wcms->currentPage or
// page('title') here, but currentPage seems to be
// a slug ...
echo htmlentities( $Wcms->page('title') );
?>
</h1>
</div>
<?php
}
?>
<div class="container wcmsbs5-maincontainer">
<div class="bg-body-tertiary text-body p-5 rounded wcmsbs5-maincontent">
<?php
/**
* This hack will refrain us from outputting our "better" blog post
* cards if we're currently logged in as the Simple Blog plugin
* makes some assumptions regarding what it can modify and output.
*/
if ( $hideTitle && ! $Wcms->loggedIn ) {
echo '<div class="wcmsbs5-blogcards">';
}
// The page text
echo $Wcms->page('content');
// Closing the "hack" (see above)
if ( $hideTitle && ! $Wcms->loggedIn ) {
echo '</div">';
}
?>
</div>
</div>
<?php
}
?>
<div class="container mt-5 wcmsbs5-subsidecontainer">
<div class="bg-success-subtle p-5 text-center rounded wcmsbs5-subsidecontent">
<?php
// The page text
echo $Wcms->block('subside');
?>
</div>
</div>
<footer class="container-fluid mt-5 wcmsbs5-footercontainer">
<div class="bg-body-secondary container p-2 text-center rounded wcmsbs5-footercontent">
<?php echo $Wcms->footer() ?>
</div>
</footer>
<?php
echo $Wcms->js();
?>
</div>
<script>
(() => {
'use strict'
// Set theme to the user's preferred color scheme
function updateBootstrapTheme() {
const colorMode = window.matchMedia("(prefers-color-scheme: dark)").matches ?
"dark" :
"light";
document.querySelector("html").setAttribute("data-bs-theme", colorMode);
}
// Submit search form to ourselves
function wcmsBS5search() {
let e = document.getElementById("searchtext");
if (e && ! e.value.length ) {
e.focus();
} else {
e = document.getElementById("searchform");
if (e) {
e.submit();
}
}
}
function wcmsBS5setup() {
// Update theme when the preferred scheme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateBootstrapTheme);
// Setup button click handler for search form
let searchButton = document.getElementById("searchbutton");
if (searchButton) {
searchButton.addEventListener("click", wcmsBS5search);
}
// Setup search field handler for ENTER key
let e = document.getElementById("searchtext");
if (e) {
if (searchButton) {
e.addEventListener("keydown", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
searchButton.click();
}
});
}
}
}
if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {
wcmsBS5setup();
} else {
document.addEventListener("DOMContentLoaded", wcmsBS5setup);
}
})()
</script>
</body>
</html>