Improve Autosave

This commit is contained in:
danpros 2024-05-27 07:37:53 +07:00
commit a813ed3b38
10 changed files with 308 additions and 393 deletions

View file

@ -120,8 +120,11 @@ function remove_accent($str)
}
// Add content
function add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description = null, $media = null, $dateTime = null)
function add_content($title, $tag, $url, $content, $user, $draft, $category, $type, $description = null, $media = null, $dateTime = null, $autoSave = null)
{
if (!is_null($autoSave)) {
$draft = 'draft';
}
$tag = explode(',', preg_replace("/\s*,\s*/", ",", rtrim($tag, ',')));
$tag = array_filter(array_unique($tag));
$tagslang = "content/data/tags.lang";
@ -237,11 +240,23 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
}
if (empty($draft)) {
$draftFile = 'content/' . $user . '/blog/' . $category. '/draft/' . $filename;
if (file_exists($draftFile)) {
unlink($draftFile);
}
}
save_tag_i18n($post_tag, $post_tagmd);
rebuilt_cache('all');
clear_post_cache($post_date, $post_tag, $post_url, $dir . $filename, $category, $type);
if (!is_null($autoSave)) {
return "Auto Saved";
}
if (empty($draft)) {
if (date('Y-m-d-H-i-s') >= $post_date) {
@ -258,7 +273,7 @@ function add_content($title, $tag, $url, $content, $user, $draft, $category, $ty
}
// Edit content
function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publishDraft, $category, $type, $destination = null, $description = null, $date = null, $media = null)
function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publishDraft, $category, $type, $destination = null, $description = null, $date = null, $media = null, $autoSave = null)
{
$tag = explode(',', preg_replace("/\s*,\s*/", ",", rtrim($tag, ',')));
$tag = array_filter(array_unique($tag));
@ -449,7 +464,7 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
rebuilt_cache('all');
clear_post_cache($dt, $post_tag, $post_url, $oldfile, $category, $type);
$old_filename = pathinfo($oldfile, PATHINFO_FILENAME);
$old_ex = explode('_', $old_filename);
$old_url = $old_ex[2];
@ -465,6 +480,10 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
}
}
}
if (!is_null($autoSave)) {
return "Auto Saved";
}
if ($destination == 'post') {
if(!empty($revertPost)) {
@ -498,8 +517,11 @@ function edit_content($title, $tag, $url, $content, $oldfile, $revertPost, $publ
}
// Add static page
function add_page($title, $url, $content, $draft, $description = null)
function add_page($title, $url, $content, $draft, $description = null, $autoSave = null)
{
if (!is_null($autoSave)) {
$draft = 'draft';
}
$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);
@ -536,6 +558,10 @@ function add_page($title, $url, $content, $draft, $description = null)
mkdir($dir, 0775, true);
}
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
$draftFile = $dirDraft . $filename;
if (file_exists($draftFile)) {
unlink($draftFile);
}
} else {
if (!is_dir($dirDraft)) {
mkdir($dirDraft, 0775, true);
@ -546,6 +572,10 @@ function add_page($title, $url, $content, $draft, $description = null)
rebuilt_cache('all');
clear_page_cache($post_url);
if (!is_null($autoSave)) {
return "Auto Saved";
}
if (empty($draft)) {
$redirect = site_url() . 'admin/pages';
header("Location: $redirect");
@ -557,8 +587,11 @@ function add_page($title, $url, $content, $draft, $description = null)
}
// Add static sub page
function add_sub_page($title, $url, $content, $static, $draft, $description = null)
function add_sub_page($title, $url, $content, $static, $draft, $description = null, $autoSave = null)
{
if (!is_null($autoSave)) {
$draft = 'draft';
}
$post = find_page($static);
$static = pathinfo($post['current']->md, PATHINFO_FILENAME);
$post_title = safe_html($title);
@ -597,12 +630,20 @@ function add_sub_page($title, $url, $content, $static, $draft, $description = nu
mkdir($dir, 0775, true);
}
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
$draftFile = $dirDraft . $filename;
if (file_exists($draftFile)) {
unlink($draftFile);
}
} else {
if (!is_dir($dirDraft)) {
mkdir($dirDraft, 0775, true);
}
file_put_contents($dirDraft . $filename, print_r($post_content, true), LOCK_EX);
}
if (!is_null($autoSave)) {
return "Auto Saved";
}
rebuilt_cache('all');
clear_page_cache($post_url);
@ -612,7 +653,7 @@ function add_sub_page($title, $url, $content, $static, $draft, $description = nu
}
// Edit static page and sub page
function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination = null, $description = null, $static = null)
function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft, $destination = null, $description = null, $static = null, $autoSave = null)
{
$dir = pathinfo($oldfile, PATHINFO_DIRNAME);
$fn = explode('.', pathinfo($oldfile, PATHINFO_FILENAME));
@ -638,7 +679,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
}
$post_content = '<!--t ' . $post_title . ' t-->' . $post_description . "\n\n" . $content;
if (!empty($post_title) && !empty($post_url) && !empty($post_content)) {
if(!empty($revertPage)) {
@ -655,7 +696,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
}
}
unlink($oldfile);
} elseif(!empty($publishDraft)) {
} elseif (!empty($publishDraft)) {
$newfile = dirname($dir) . '/' . $post_url . '.md';
file_put_contents($newfile, print_r($post_content, true), LOCK_EX);
if (empty($static)) {
@ -665,7 +706,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
}
}
unlink($oldfile);
}else {
} else {
$newfile = $dir . '/' . $post_url . '.md';
if ($oldfile === $newfile) {
file_put_contents($oldfile, print_r($post_content, true), LOCK_EX);
@ -697,7 +738,7 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
}
rebuilt_cache('all');
clear_page_cache($post_url);
clear_page_cache($post_url);
if (!empty($static)) {
$posturl = site_url() . $static .'/'. $pu;
@ -746,6 +787,10 @@ function edit_page($title, $url, $content, $oldfile, $revertPage, $publishDraft,
}
if (!is_null($autoSave)) {
return "Auto Saved";
}
if ($destination == 'post') {
if(!empty($revertPage)) {
$drafturl = site_url() . 'admin/draft';