feat: ✨ add new Post wrapper and Blog overview page
This commit is contained in:
parent
b420a6eafa
commit
a2c6842b71
8 changed files with 384 additions and 6 deletions
166
www/post.php
Normal file
166
www/post.php
Normal file
|
@ -0,0 +1,166 @@
|
|||
|
||||
<?php
|
||||
|
||||
class PostData {
|
||||
public static $default_banners = [];
|
||||
public static $markdown_engine = null;
|
||||
|
||||
public $data;
|
||||
|
||||
public $html_data;
|
||||
public $raw_data;
|
||||
|
||||
public static function _generate_404($sql_row) {
|
||||
$sql_row ??= [
|
||||
'post_id' => -1,
|
||||
'post_path' => '/404.md',
|
||||
'post_title' => '404 Page',
|
||||
'post_metadata' => [
|
||||
'type' => '404'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function _deduce_type($path) {
|
||||
$ext = pathinfo($path, PATHINFO_EXTENSION);
|
||||
|
||||
if(preg_match("/\.(\w+)\.md$/", $path, $ext_match)) {
|
||||
$ext = $ext_match[1];
|
||||
}
|
||||
|
||||
$ext_mapping = [
|
||||
'' => 'directory',
|
||||
'md' => 'text/markdown',
|
||||
'png' => 'image',
|
||||
'jpg' => 'image',
|
||||
'jpeg' => 'image'
|
||||
];
|
||||
|
||||
return $ext_mapping[$ext] ?? '?';
|
||||
}
|
||||
|
||||
public static function _deduce_icon($type) {
|
||||
$icon_mapping = [
|
||||
'' => 'question',
|
||||
'text/markdown' => 'markdown',
|
||||
'directory' => 'folder',
|
||||
'gallery' => 'images',
|
||||
'image' => 'image'
|
||||
];
|
||||
|
||||
return $icon_mapping[$type] ?? 'unknown';
|
||||
}
|
||||
|
||||
function __construct($post_handler, $sql_row) {
|
||||
$this->post_handler = $post_handler;
|
||||
|
||||
$this->content_html = null;
|
||||
|
||||
$sql_meta = null;
|
||||
if(!isset($sql_row) or !$sql_row['found']) {
|
||||
$sql_row = $this->_generate_404($sql_row);
|
||||
$sql_meta = $sql_row['post_metadata'];
|
||||
}
|
||||
|
||||
|
||||
$sql_meta = $sql_row['post_metadata'];
|
||||
if(is_string($sql_meta)) {
|
||||
$sql_meta = json_decode($sql_meta, true);
|
||||
}
|
||||
|
||||
unset($sql_meta['settings']);
|
||||
|
||||
$this->sql_row = $sql_row;
|
||||
$this->sql_meta = $sql_meta ?? [];
|
||||
$sql_settings = json_decode($sql_row['post_settings_cache'], true) ?? [];
|
||||
|
||||
$data = [
|
||||
'id' => $sql_row['post_id'],
|
||||
'path' => $sql_row['post_path'],
|
||||
'url' => 'https://' . $sql_row['host'] . $sql_row['post_path'],
|
||||
'created_at' => $sql_row['post_create_time'] ?? '',
|
||||
'updated_at' => $sql_row['post_update_time'] ?? '',
|
||||
'view_count' => $sql_row['post_access_count'] ?? 0
|
||||
];
|
||||
|
||||
$data['title'] = $sql_meta['title'] ?? $sql_row['title'];
|
||||
unset($sql_meta['title']);
|
||||
|
||||
$data['tags'] = $sql_meta['tags'] ?? [];
|
||||
unset($sql_meta['tags']);
|
||||
|
||||
$data['type'] = $sql_meta['type']
|
||||
?? self::_deduce_type($sql_row['post_path']);
|
||||
unset($sql_meta['type']);
|
||||
|
||||
$data['icon'] = $sql_meta['icon']
|
||||
?? self::_deduce_icon($data['type']);
|
||||
unset($sql_meta['icon']);
|
||||
|
||||
if(isset($sql_meta['media_url'])) {
|
||||
$data['media_url'] = $sql_meta['media_url'];
|
||||
$data['thumb_url'] = $sql_meta['thumb_url'] ?? $data['media_url'];
|
||||
|
||||
unset($sql_meta['media_url']);
|
||||
unset($sql_meta['thumb_url']);
|
||||
}
|
||||
|
||||
$data['banners'] = $sql_meta['banners']
|
||||
?? $sql_settings['banners']
|
||||
?? self::$default_banners;
|
||||
|
||||
unset($sql_meta['banners']);
|
||||
unset($sql_settings['banners']);
|
||||
|
||||
$data['preview_image'] = $sql_meta['preview_image'] ?? $data['banners'][0]['src'] ?? null;
|
||||
unset($sql_meta['preview_image']);
|
||||
|
||||
$data['brief'] = $sql_meta['brief']
|
||||
?? $data['title'];
|
||||
unset($sql_meta['brief']);
|
||||
|
||||
$data['excerpt'] = $sql_meta['excerpt']
|
||||
?? substr($sql_row['post_content'], 0, 256);
|
||||
|
||||
$data['metadata'] = $sql_meta;
|
||||
$data['settings'] = $sql_settings;
|
||||
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function __get($name) {
|
||||
if($name == 'html') {
|
||||
return $this->get_html();
|
||||
}
|
||||
if($name == 'markdown') {
|
||||
return $this->sql_row['post_content'];
|
||||
}
|
||||
if($name == 'json') {
|
||||
return $this->to_json();
|
||||
}
|
||||
|
||||
return $this->data[$name];
|
||||
}
|
||||
|
||||
public function get_html() {
|
||||
$fn = self::$markdown_engine;
|
||||
$this->content_html ??= $fn($this);
|
||||
|
||||
return $this->content_html;
|
||||
}
|
||||
|
||||
public function to_json($with_markdown = false, $with_html = false) {
|
||||
$out_data = $this->data;
|
||||
|
||||
if($with_markdown) {
|
||||
$out_data['markdown'] = $this->sql_row['post_content'];
|
||||
}
|
||||
if($with_html) {
|
||||
$out_data['html'] = $this->get_html();
|
||||
}
|
||||
|
||||
return json_encode($out_data);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue