ContentAPI voor PHP content bestanden + handleiding in admin
- Nieuwe ContentAPI class beschikbaar als $api in PHP content bestanden met methodes: getAllPages, getPage, getMenu, getConfig, buildUrl, etc. - Admin handleiding pagina op /admin/guide met taalwisselaar - Zijbalk link naar handleiding in admin menu - Dubbele alert in config pagina verwijderd - Handleidingen (nl/en) uitgebreid met Content API referentie
This commit is contained in:
@@ -783,7 +783,8 @@ class CodePressCMS {
|
||||
$title = $metadata['title'] ?? '';
|
||||
|
||||
ob_start();
|
||||
// Make metadata available to the included file
|
||||
// Make API and metadata available to the included file
|
||||
$api = new ContentAPI($this);
|
||||
$pageMetadata = $metadata;
|
||||
include $filePath;
|
||||
$content = ob_get_clean();
|
||||
@@ -1011,7 +1012,6 @@ class CodePressCMS {
|
||||
$this->pluginManager->doAction('onBeforeRender');
|
||||
|
||||
$menu = $this->getMenu();
|
||||
$breadcrumb = $this->generateBreadcrumb();
|
||||
|
||||
// Get homepage title
|
||||
$homepageTitle = $this->getHomepageTitle();
|
||||
@@ -1026,6 +1026,10 @@ class CodePressCMS {
|
||||
// Get layout from page metadata
|
||||
$layout = $page['layout'] ?? 'sidebar-content';
|
||||
|
||||
// Determine if sidebar toggle should be shown
|
||||
$hasSidebar = $layout !== 'content' && !empty(trim($sidebarContent));
|
||||
$breadcrumb = $this->generateBreadcrumb($hasSidebar);
|
||||
|
||||
// Prepare template data
|
||||
$templateData = [
|
||||
'site_title' => $this->config['site_title'],
|
||||
@@ -1154,11 +1158,15 @@ class CodePressCMS {
|
||||
/**
|
||||
* Generate breadcrumb navigation HTML
|
||||
*
|
||||
* @param bool $hasSidebar Whether sidebar content exists and should show toggle
|
||||
* @return string Breadcrumb HTML
|
||||
*/
|
||||
public function generateBreadcrumb() {
|
||||
public function generateBreadcrumb($hasSidebar = true) {
|
||||
// Sidebar toggle button (shown before home icon in breadcrumb)
|
||||
$sidebarToggle = '<li class="breadcrumb-item sidebar-toggle-item"><button type="button" class="sidebar-toggle-btn" onclick="toggleSidebar()" title="Toggle Sidebar" aria-label="Toggle Sidebar" aria-expanded="true"><i class="bi bi-layout-sidebar-inset"></i></button></li>';
|
||||
$sidebarToggle = '';
|
||||
if ($hasSidebar) {
|
||||
$sidebarToggle = '<li class="breadcrumb-item sidebar-toggle-item"><button type="button" class="sidebar-toggle-btn" onclick="toggleSidebar()" title="Toggle Sidebar" aria-label="Toggle Sidebar" aria-expanded="true"><i class="bi bi-layout-sidebar-inset"></i></button></li>';
|
||||
}
|
||||
|
||||
if (isset($_GET['search'])) {
|
||||
return '<nav aria-label="breadcrumb"><ol class="breadcrumb">' . $sidebarToggle . '<li class="breadcrumb-item"><a href="/' . $this->currentLanguage . '"><i class="bi bi-house"></i></a></li><li class="breadcrumb-item"> > </li><li class="breadcrumb-item active">' . $this->t('search') . '</li></ol></nav>';
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
class ContentAPI
|
||||
{
|
||||
private CodePressCMS $cms;
|
||||
|
||||
public function __construct(CodePressCMS $cms)
|
||||
{
|
||||
$this->cms = $cms;
|
||||
}
|
||||
|
||||
public function getAllPages(): array
|
||||
{
|
||||
return $this->cms->getAllPageTitles();
|
||||
}
|
||||
|
||||
public function getPage(string $path): ?array
|
||||
{
|
||||
$contentDir = $this->cms->config['content_dir'];
|
||||
$path = preg_replace('/\.(md|php|html)$/', '', $path);
|
||||
$filePath = $contentDir . '/' . $path;
|
||||
|
||||
$extensions = ['md', 'php', 'html'];
|
||||
$actualPath = null;
|
||||
foreach ($extensions as $ext) {
|
||||
if (file_exists($filePath . '.' . $ext)) {
|
||||
$actualPath = $filePath . '.' . $ext;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$actualPath || !file_exists($actualPath)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$content = file_get_contents($actualPath);
|
||||
$extension = pathinfo($actualPath, PATHINFO_EXTENSION);
|
||||
|
||||
switch ($extension) {
|
||||
case 'md':
|
||||
$result = $this->cms->parseMarkdown($content, $actualPath);
|
||||
break;
|
||||
case 'php':
|
||||
$result = $this->cms->parsePHP($actualPath);
|
||||
$result['content'] = $this->cms->processContent($result['content']);
|
||||
break;
|
||||
case 'html':
|
||||
$result = $this->cms->parseHTML($content, $actualPath);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'title' => $result['title'] ?? '',
|
||||
'content' => $result['content'] ?? '',
|
||||
'path' => $path,
|
||||
'layout' => $result['layout'] ?? 'sidebar-content',
|
||||
'metadata' => $result['metadata'] ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
public function getMenu(): array
|
||||
{
|
||||
return $this->cms->getMenu();
|
||||
}
|
||||
|
||||
public function getConfig(string $key, $default = null)
|
||||
{
|
||||
$keys = explode('.', $key);
|
||||
$value = $this->cms->config;
|
||||
|
||||
foreach ($keys as $k) {
|
||||
if (!isset($value[$k])) {
|
||||
return $default;
|
||||
}
|
||||
$value = $value[$k];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getCurrentLanguage(): string
|
||||
{
|
||||
return $this->cms->currentLanguage;
|
||||
}
|
||||
|
||||
public function buildUrl(string $page = 'index', ?string $lang = null, array $params = []): string
|
||||
{
|
||||
$lang = $lang ?? $this->getCurrentLanguage();
|
||||
$query = 'page=' . urlencode($page) . '&lang=' . urlencode($lang);
|
||||
if (!empty($params)) {
|
||||
$query .= '&' . http_build_query($params);
|
||||
}
|
||||
return '?' . $query;
|
||||
}
|
||||
|
||||
public function pageExists(string $path): bool
|
||||
{
|
||||
$contentDir = $this->cms->config['content_dir'];
|
||||
$path = preg_replace('/\.(md|php|html)$/', '', $path);
|
||||
$basePath = $contentDir . '/' . $path;
|
||||
|
||||
return file_exists($basePath . '.md')
|
||||
|| file_exists($basePath . '.php')
|
||||
|| file_exists($basePath . '.html');
|
||||
}
|
||||
|
||||
public function getCurrentPageTitle(): string
|
||||
{
|
||||
$page = $this->cms->getPage();
|
||||
return $page['title'] ?? '';
|
||||
}
|
||||
|
||||
public function getCurrentPagePath(): string
|
||||
{
|
||||
return $_GET['page'] ?? $this->cms->config['default_page'];
|
||||
}
|
||||
|
||||
public function isHomepage(): bool
|
||||
{
|
||||
$defaultPage = $this->cms->config['default_page'] ?? 'index';
|
||||
$currentPage = $_GET['page'] ?? $defaultPage;
|
||||
return $currentPage === $defaultPage;
|
||||
}
|
||||
|
||||
public function t(string $key): string
|
||||
{
|
||||
return $this->cms->t($key);
|
||||
}
|
||||
|
||||
public function getSiteTitle(): string
|
||||
{
|
||||
return $this->cms->config['site_title'] ?? 'CodePress';
|
||||
}
|
||||
|
||||
public function getAvailableLanguages(): array
|
||||
{
|
||||
return $this->cms->getAvailableLanguages();
|
||||
}
|
||||
|
||||
public function getSearchResults(): array
|
||||
{
|
||||
if (isset($_GET['search'])) {
|
||||
return $this->cms->searchResults;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public function isSearching(): bool
|
||||
{
|
||||
return isset($_GET['search']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user