From 0ec68537c5b8fd470f965d3f345b8c5ea9a32e13 Mon Sep 17 00:00:00 2001 From: danpros Date: Mon, 1 Jan 2024 11:37:47 +0700 Subject: [PATCH] Draft for pages and subpages --- system/admin/admin.php | 209 ++++++++++++++++++++++++---- system/admin/views/add-page.html.php | 2 +- system/admin/views/delete-category.html.php | 3 +- system/admin/views/delete-page.html.php | 3 +- system/admin/views/delete-post.html.php | 4 +- system/admin/views/edit-page.html.php | 14 +- system/admin/views/static-pages.html.php | 91 ++++++------ system/admin/views/user-draft.html.php | 44 ++++++ system/htmly.php | 83 +++++++---- system/includes/functions.php | 46 +++++- 10 files changed, 387 insertions(+), 112 deletions(-) diff --git a/system/admin/admin.php b/system/admin/admin.php index a78f909..1fc36c1 100644 --- a/system/admin/admin.php +++ b/system/admin/admin.php @@ -1,6 +1,8 @@ ' . $post_description . "\n\n" . $content; if (!empty($post_title) && !empty($post_url) && !empty($post_content)) { - - $newfile = $dir . '/' . $post_url . '.md'; - if ($oldfile === $newfile) { - file_put_contents($oldfile, print_r($post_content, true)); - } else { - rename($oldfile, $newfile); + + if(!empty($revertPage)) { + $newfile = $dir . '/draft/' . $post_url . '.md'; + file_put_contents($newfile, print_r($post_content, true)); + if (empty($static)) { + $old = pathinfo($oldfile, PATHINFO_FILENAME); + if(is_dir($dir . '/' . $old)) { + rename($dir . '/' . $old, $dir . '/' . $post_url); + } + } + unlink($oldfile); + } elseif(!empty($publishDraft)) { + $newfile = dirname($dir) . '/' . $post_url . '.md'; file_put_contents($newfile, print_r($post_content, true)); if (empty($static)) { - $path = pathinfo($oldfile); - $old = substr($path['filename'], strrpos($path['filename'], '/')); + $old = pathinfo($oldfile, PATHINFO_FILENAME); if(is_dir($dir . '/' . $old)) { rename($dir . '/' . $old, $dir . '/' . $post_url); } } + unlink($oldfile); + }else { + $newfile = $dir . '/' . $post_url . '.md'; + if ($oldfile === $newfile) { + file_put_contents($oldfile, print_r($post_content, true)); + } else { + rename($oldfile, $newfile); + file_put_contents($newfile, print_r($post_content, true)); + if (empty($static)) { + $old = pathinfo($oldfile, PATHINFO_FILENAME); + if(is_dir($dir . '/' . $old)) { + rename($dir . '/' . $old, $dir . '/' . $post_url); + } + } + } } if (!empty($static)) { @@ -628,7 +672,12 @@ function edit_page($title, $url, $content, $oldfile, $destination = null, $descr } if ($destination == 'post') { - header("Location: $posturl"); + if(!empty($revertPage)) { + $drafturl = site_url() . 'admin/draft'; + header("Location: $drafturl"); + } else { + header("Location: $posturl"); + } } else { $redirect = site_url() . $destination; header("Location: $redirect"); @@ -639,7 +688,6 @@ function edit_page($title, $url, $content, $oldfile, $destination = null, $descr // Add category function add_category($title, $url, $content, $description = null) { - $post_title = safe_html($title); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); $description = safe_html($description); @@ -675,7 +723,7 @@ function add_category($title, $url, $content, $description = null) // Edit category function edit_category($title, $url, $content, $oldfile, $destination = null, $description = null) { - $dir = substr($oldfile, 0, strrpos($oldfile, '/')); + $dir = pathinfo($oldfile, PATHINFO_DIRNAME); $post_title = safe_html($title); $post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url))); @@ -1154,3 +1202,106 @@ function rename_category_folder($new_name, $old_file) } } + +// Return static page. +function find_draft_pages($static = null) +{ + $posts = get_draft_pages(); + + $tmp = array(); + + if (!empty($posts)) { + + foreach ($posts as $index => $v) { + if (stripos($v['basename'], $static . '.md') !== false) { + + $post = new stdClass; + + // The static page URL + $url= $v['filename']; + + $post->url = site_url() . $url; + + $post->file = $v['dirname'] . '/' . $v['basename']; + $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); + + $post->md = $url; + + // Get the contents and convert it to HTML + $content = file_get_contents($post->file); + + // Extract the title and body + $post->title = get_content_tag('t', $content, 'Untitled static page: ' . format_date($post->lastMod, 'l, j F Y, H:i')); + + // Get the contents and convert it to HTML + $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); + + if (config('views.counter') == 'true') { + $post->views = get_views($post->file); + } + + $post->description = get_content_tag("d", $content, get_description($post->body)); + + $word_count = str_word_count(strip_tags($post->body)); + $post->readTime = ceil($word_count / 200); + + $tmp[] = $post; + } + } + } + + return $tmp; +} + +// Return static page. +function find_draft_subpages($static = null, $sub_static = null) +{ + $posts = get_draft_subpages(); + + $tmp = array(); + + if (!empty($posts)) { + + foreach ($posts as $index => $v) { + if (stripos($v['basename'], $sub_static . '.md') !== false) { + + $post = new stdClass; + + if (is_null($static)) { + $static = str_replace('content/static/', '', dirname($v['dirname'])); + } + + // The static page URL + $url= $v['filename']; + $post->url = site_url() . $static . "/" . $url; + + $post->file = $v['dirname'] . '/' . $v['basename']; + $post->lastMod = strtotime(date('Y-m-d H:i:s', filemtime($post->file))); + + $post->md = $url; + + $post->parent = $static; + + // Get the contents and convert it to HTML + $content = file_get_contents($post->file); + + // Extract the title and body + $post->title = get_content_tag('t', $content, 'Untitled static subpage: ' . format_date($post->lastMod, 'l, j F Y, H:i')); + + // Get the contents and convert it to HTML + $post->body = MarkdownExtra::defaultTransform(remove_html_comments($content)); + + $post->views = get_views($post->file); + + $post->description = get_content_tag("d", $content, get_description($post->body)); + + $word_count = str_word_count(strip_tags($post->body)); + $post->readTime = ceil($word_count / 200); + + $tmp[] = $post; + } + } + } + + return $tmp; +} \ No newline at end of file diff --git a/system/admin/views/add-page.html.php b/system/admin/views/add-page.html.php index 656282a..0d74a4f 100644 --- a/system/admin/views/add-page.html.php +++ b/system/admin/views/add-page.html.php @@ -42,7 +42,7 @@
- + diff --git a/system/admin/views/delete-category.html.php b/system/admin/views/delete-category.html.php index 01c153d..1783d32 100644 --- a/system/admin/views/delete-category.html.php +++ b/system/admin/views/delete-category.html.php @@ -21,8 +21,9 @@ if (isset($destination)) { } else { $back = site_url(); } +$info = $p->title . ' (' . $p->file . ')'; ?> -

title);?>

+


diff --git a/system/admin/views/delete-page.html.php b/system/admin/views/delete-page.html.php index 01c153d..1783d32 100644 --- a/system/admin/views/delete-page.html.php +++ b/system/admin/views/delete-page.html.php @@ -21,8 +21,9 @@ if (isset($destination)) { } else { $back = site_url(); } +$info = $p->title . ' (' . $p->file . ')'; ?> -

title);?>

+


diff --git a/system/admin/views/delete-post.html.php b/system/admin/views/delete-post.html.php index f9b6538..a78ea19 100644 --- a/system/admin/views/delete-post.html.php +++ b/system/admin/views/delete-post.html.php @@ -28,9 +28,9 @@ if (isset($destination)) { } else { $back = site_url(); } - +$info = $p->title . ' (' . $p->file . ')'; ?> -

title);?>

+


diff --git a/system/admin/views/edit-page.html.php b/system/admin/views/edit-page.html.php index 8828e8f..fc09c4c 100644 --- a/system/admin/views/edit-page.html.php +++ b/system/admin/views/edit-page.html.php @@ -46,9 +46,9 @@ if ($type == 'is_frontpage') { } else { $destination = 'admin'; } - $dir = substr($url, 0, strrpos($url, '/')); - $oldurl = str_replace($dir . '/', '', $url); - $oldmd = str_replace('.md', '', $oldurl); + $dir = pathinfo($url, PATHINFO_DIRNAME); + $oldurl = pathinfo($url, PATHINFO_BASENAME); + $oldmd = pathinfo($url, PATHINFO_FILENAME); if (isset($p->url)) { $delete = $p->url . '/delete?destination=' . $destination; @@ -118,7 +118,13 @@ $images = get_gallery(); - + + + + + + +
diff --git a/system/admin/views/static-pages.html.php b/system/admin/views/static-pages.html.php index 8e0a408..567b4c8 100644 --- a/system/admin/views/static-pages.html.php +++ b/system/admin/views/static-pages.html.php @@ -3,48 +3,51 @@


- + + + + + + + + + + + + md)); ?> + + + + + + + + -if (isset($_SESSION[config("site.url")]['user'])) { - $posts = get_static_post(null); - if (!empty($posts)) { - echo '
Sub Pages
title;?>views;?> + + md); + foreach ($subPages as $sp):?> + +
+
+ title;?> +
+
+ + views;?> +
+
+ +
+
+ + + +
+
'; - echo ''; - if (config("views.counter") == "true") - echo ''; - echo ''; - $i = 0; - $len = count($posts); - foreach ($posts as $p) { - if ($i == 0) { - $class = 'item first'; - } elseif ($i == $len - 1) { - $class = 'item last'; - } else { - $class = 'item'; - } - $i++; - - echo ''; - echo ''; - if (config("views.counter") == "true") - echo ''; - echo ''; - echo ''; - - $subPages = get_static_sub_post($p->md); - - foreach ($subPages as $sp) { - echo ''; - echo ''; - if (config("views.counter") == "true") - echo ''; - echo ''; - echo ''; - } - } - echo '
' . i18n('Title') . ''.i18n('Views').'' . i18n('Operations') . '
' . $p->title . '' . $p->views . '' . i18n('Add_sub') . ' ' . i18n('Edit') . ' ' . i18n('Delete') . '
» ' . $sp->title . '' . $sp->views . '' . i18n('Edit') . ' ' . i18n('Delete') . '
'; - } -} - - ?> \ No newline at end of file + + + + \ No newline at end of file diff --git a/system/admin/views/user-draft.html.php b/system/admin/views/user-draft.html.php index ed2f5ef..41a76db 100644 --- a/system/admin/views/user-draft.html.php +++ b/system/admin/views/user-draft.html.php @@ -43,3 +43,47 @@ + + +

+
+

:

+ + + + + + + + md)); ?> + + + + + + +
title ?>lastMod) ?>
+ + + +

+
+

: Sub

+ + + + + + + + + parent);?> + + + + + + + +
title ?>lastMod) ?> title;?>
+ \ No newline at end of file diff --git a/system/htmly.php b/system/htmly.php index 0ab6f83..47a092d 100644 --- a/system/htmly.php +++ b/system/htmly.php @@ -616,12 +616,13 @@ post('/add/page', function () { $url = from($_REQUEST, 'url'); $content = from($_REQUEST, 'content'); $description = from($_REQUEST, 'description'); + $draft = from($_REQUEST, 'draft'); if ($proper && !empty($title) && !empty($content) && login()) { if (!empty($url)) { - add_page($title, $url, $content, $description); + add_page($title, $url, $content, $draft, $description); } else { $url = $title; - add_page($title, $url, $content, $description); + add_page($title, $url, $content, $draft, $description); } } else { $message['error'] = ''; @@ -936,6 +937,10 @@ get('/admin/draft', function () { $perpage = config('profile.perpage'); $posts = get_draft($name, $page, $perpage); + + $draftPages = find_draft_pages(); + + $draftSubpages = find_draft_subpages(); $total = get_draftcount($name); @@ -955,6 +960,8 @@ get('/admin/draft', function () { 'page' => $page, 'heading' => i18n('My_draft'), 'posts' => null, + 'draftPages' => $draftPages, + 'draftSubpages' => $draftSubpages, 'about' => $author->about, 'name' => $author->name, 'type' => 'is_admin-draft', @@ -965,7 +972,7 @@ get('/admin/draft', function () { )); die; } - + render('user-draft', array( 'title' => i18n('My_draft') . ' - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -973,6 +980,8 @@ get('/admin/draft', function () { 'heading' => i18n('My_draft'), 'page' => $page, 'posts' => $posts, + 'draftPages' => $draftPages, + 'draftSubpages' => $draftSubpages, 'about' => $author->about, 'name' => $author->name, 'type' => 'is_admin-draft', @@ -1752,11 +1761,6 @@ get('/admin/categories/:category', function ($category) { $total = $desc->count; - if (empty($posts) || $page < 1) { - // a non-existing page - not_found(); - } - render('category-list', array( 'title' => $desc->title . ' - ' . blog_title(), 'description' => $desc->description, @@ -2381,7 +2385,7 @@ get('/post/:name', function ($name) { } else { $blog = ''; } - + $vroot = rtrim(config('views.root'), '/'); $lt = $vroot . '/layout--post--' . $current->ct . '.html.php'; @@ -2961,12 +2965,13 @@ post('/:static/add', function ($static) { $url = from($_REQUEST, 'url'); $content = from($_REQUEST, 'content'); $description = from($_REQUEST, 'description'); + $draft = from($_REQUEST, 'draft'); if ($proper && !empty($title) && !empty($content) && login()) { if (!empty($url)) { - add_sub_page($title, $url, $content, $static, $description); + add_sub_page($title, $url, $content, $static, $draft, $description); } else { $url = $title; - add_sub_page($title, $url, $content, $static, $description); + add_sub_page($title, $url, $content, $static, $draft, $description); } } else { $message['error'] = ''; @@ -3005,11 +3010,16 @@ get('/:static/edit', function ($static) { $post = get_static_post($static); if (!$post) { - not_found(); + $post = find_draft_pages($static); + if (!$post) { + not_found(); + } else { + $post = $post[0]; + } + } else { + $post = $post['current']; } - $post = $post['current']; - render('edit-page', array( 'title' => i18n('Edit') . ': ' . $post->title . ' - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -3042,12 +3052,14 @@ post('/:static/edit', function () { $oldfile = from($_REQUEST, 'oldfile'); $destination = from($_GET, 'destination'); $description = from($_REQUEST, 'description'); + $revertPage = from($_REQUEST, 'revertpage'); + $publishDraft = from($_REQUEST, 'publishdraft'); if ($proper && !empty($title) && !empty($content)) { if (!empty($url)) { - edit_page($title, $url, $content, $oldfile, $destination, $description); + edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description); } else { $url = $title; - edit_page($title, $url, $content, $oldfile, $destination, $description); + edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description); } } else { $message['error'] = ''; @@ -3087,11 +3099,16 @@ get('/:static/delete', function ($static) { $post = get_static_post($static); if (!$post) { - not_found(); + $post = find_draft_pages($static); + if (!$post) { + not_found(); + } else { + $post = $post[0]; + } + } else { + $post = $post['current']; } - $post = $post['current']; - render('delete-page', array( 'title' => i18n('Delete') . ': ' . $post->title . ' - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -3222,11 +3239,16 @@ get('/:static/:sub/edit', function ($static, $sub) { $page = get_static_sub_post($static, $sub); if (!$page) { - not_found(); + $page = find_draft_subpages($static, $sub); + if (!$page) { + not_found(); + } else { + $page = $page[0]; + } + } else { + $page = $page['current']; } - $page = $page['current']; - render('edit-page', array( 'title' => i18n('Edit') . ': ' . $page->title . ' - ' . blog_title(), 'description' => strip_tags(blog_description()), @@ -3259,15 +3281,17 @@ post('/:static/:sub/edit', function ($static, $sub) { $oldfile = from($_REQUEST, 'oldfile'); $destination = from($_GET, 'destination'); $description = from($_REQUEST, 'description'); + $revertPage = from($_REQUEST, 'revertpage'); + $publishDraft = from($_REQUEST, 'publishdraft'); if ($destination === null) { $destination = $static . "/" . $sub; } if ($proper && !empty($title) && !empty($content)) { if (!empty($url)) { - edit_page($title, $url, $content, $oldfile, $destination, $description, $static); + edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description, $static); } else { $url = $title; - edit_page($title, $url, $content, $oldfile, $destination, $description, $static); + edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination, $description, $static); } } else { $message['error'] = ''; @@ -3317,11 +3341,16 @@ get('/:static/:sub/delete', function ($static, $sub) { $page = get_static_sub_post($static, $sub); if (!$page) { - not_found(); + $page = find_draft_subpages($static, $sub); + if (!$page) { + not_found(); + } else { + $page = $page[0]; + } + } else { + $page = $page['current']; } - $page = $page['current']; - render('delete-page', array( 'title' => i18n('Delete') . ': ' . $page->title . ' - ' . blog_title(), 'description' => strip_tags(blog_description()), diff --git a/system/includes/functions.php b/system/includes/functions.php index c54801b..495f084 100644 --- a/system/includes/functions.php +++ b/system/includes/functions.php @@ -110,6 +110,40 @@ function get_draft_posts() return $_draft; } +// Get static page draft. +function get_draft_pages() +{ + static $_draftPage = array(); + if (empty($_draftPage)) { + $tmp = array(); + $tmp = glob('content/static/draft/*.md', GLOB_NOSORT); + if (is_array($tmp)) { + foreach ($tmp as $file) { + $_draftPage[] = pathinfo($file); + } + } + usort($_draftPage, "sortfile_a"); + } + return $_draftPage; +} + +// Get static subpage draft. +function get_draft_subpages() +{ + static $_draftSubpage = array(); + if (empty($_draftSubpage)) { + $tmp = array(); + $tmp = glob('content/static/*/draft/*.md', GLOB_NOSORT); + if (is_array($tmp)) { + foreach ($tmp as $file) { + $_draftSubpage[] = pathinfo($file); + } + } + usort($_draftSubpage, "sortfile_a"); + } + return $_draftSubpage; +} + // Get scheduled posts. function get_scheduled_posts() { @@ -221,7 +255,9 @@ function rebuilt_cache($type = null) $ptmp = glob('content/static/*.md', GLOB_NOSORT); if (is_array($ptmp)) { foreach ($ptmp as $file) { - $page_cache[] = pathinfo($file); + if(strpos($file, '/draft/') === false) { + $page_cache[] = pathinfo($file); + } } } usort($page_cache, "sortfile_a"); @@ -233,7 +269,9 @@ function rebuilt_cache($type = null) $sptmp = glob('content/static/*/*.md', GLOB_NOSORT); if (is_array($sptmp)) { foreach ($sptmp as $file) { - $subpage_cache[] = pathinfo($file); + if(strpos($file, '/draft/') === false) { + $subpage_cache[] = pathinfo($file); + } } } usort($subpage_cache, "sortfile_a"); @@ -1048,6 +1086,8 @@ function default_profile($name) function get_static_post($static = null) { $pages = get_static_pages(); + + $tmp = array(); if (!empty($pages)) { @@ -1084,7 +1124,7 @@ function get_static_post($static = null) $post->readTime = ceil($word_count / 200); $tmp[] = $post; - + } elseif (stripos($v['basename'], $static . '.md') !== false) { // Use the get_posts method to return