Added autosave feature to posts/pages

Added a feature to automatically save posts/pages every 60 seconds as a draft to avoid lost work. Includes a notification in the bottom right of the screen. Still needs a bit more work to avoid saving multiple drafts under different file names. Only auto saves for new posts/pages, not edits.
This commit is contained in:
KuJoe 2024-05-18 03:22:31 -04:00
commit 25fee781cf
5 changed files with 292 additions and 2 deletions

211
autosave.php Normal file
View file

@ -0,0 +1,211 @@
<?php
define('HTMLY', true);
require 'system/vendor/autoload.php';
if (login()) {
// Automatically Save Draft
function auto_save_page($title, $url, $content, $draft, $description = null)
{
$post_title = safe_html($title);
if (empty($url)) {
$url = $title;
}
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$description = safe_html($description);
if ($description !== null) {
if (!empty($description)) {
$post_description = "\n<!--d " . $description . " d-->";
} else {
$post_description = "\n<!--d " . $content . " d-->";
}
} else {
$post_description = "";
}
$posts = get_static_pages();
$timestamp = date('YmdHis');
foreach ($posts as $index => $v) {
if (strtolower($v['basename']) === strtolower($post_url . '.md')) {
$post_url = $post_url .'-'. $timestamp;
} else {
$post_url = $post_url;
}
}
$post_content = '<!--t ' . $post_title . ' t-->' . $post_description . "\n\n" . $content;
if (!empty($post_title) && !empty($post_url) && !empty($post_content)) {
$filename = $post_url . '.md';
$dir = 'content/static/';
$dirDraft = 'content/static/draft/';
if (!is_dir($dirDraft)) {
mkdir($dirDraft, 0775, true);
}
file_put_contents($dirDraft . $filename, print_r($post_content, true), LOCK_EX);
return "Auto Saved";
}
}
// Automatically Save Draft
function auto_save_post($title, $tag, $url, $content, $user, $draft, $category, $type, $description = null, $media = null, $dateTime = null)
{
$tag = explode(',', preg_replace("/\s*,\s*/", ",", rtrim($tag, ',')));
$tag = array_filter(array_unique($tag));
$tagslang = "content/data/tags.lang";
if (file_exists($tagslang)) {
$taglang = array_flip(unserialize(file_get_contents($tagslang)));
$tflip = array_intersect_key($taglang, array_flip($tag));
$post_tag = array();
$post_tagmd = array();
foreach ($tag as $t) {
if (array_key_exists($t, $tflip)) {
foreach ($tflip as $tfp => $tf){
if($t == $tfp) {
$post_tag[] = $tf;
$post_tagmd[] = $tfp;
}
}
} else {
$post_tag[] = $t;
$post_tagmd[] = $t;
}
}
$post_tag = safe_tag(implode(',', $post_tag));
$post_tagmd = safe_html(implode(',', $post_tagmd));
} else {
$post_tag = safe_tag(implode(',', $tag));
$post_tagmd = safe_html(implode(',', $tag));
}
$post_date = date('Y-m-d-H-i-s', strtotime($dateTime));
$post_title = safe_html($title);
if (empty($url)) {
$url = $title;
}
$post_tag = strtolower(preg_replace(array('/[^a-zA-Z0-9,. \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($post_tag)));
$post_url = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($url)));
$category = strtolower(preg_replace(array('/[^a-zA-Z0-9 \-\p{L}]/u', '/[ -]+/', '/^-|-$/'), array('', '-', ''), remove_accent($category)));
$description = safe_html($description);
$post_t = explode(',', $post_tag);
$pret_t = explode(',', $post_tagmd);
$tags = tag_cloud(true);
$timestamp = date('YmdHis');
$combine = array_combine($pret_t, $post_t);
$inter = array_intersect_key($tags, array_flip($post_t));
$newtag = array();
foreach ($combine as $tag => $v) {
if (array_key_exists($v, $tags)) {
foreach ($inter as $in => $i){
if($v == $in) {
if (strtolower($tag) == strtolower(tag_i18n($in))) {
$newtag[$v]= $tag;
} else {
$newtag[$v.'-'. $timestamp]= $tag;
}
}
}
} else {
$newtag[$v] = $tag;
}
}
$post_tag = implode(',', array_keys($newtag));
$posts = get_blog_posts();
foreach ($posts as $index => $v) {
$arr = explode('_', $v['basename']);
if (strtolower($arr[2]) === strtolower($post_url . '.md')) {
$post_url = $post_url .'-'. $timestamp;
} else {
$post_url = $post_url;
}
}
if ($description !== null) {
if (!empty($description)) {
$post_description = "\n<!--d " . $description . " d-->";
} else {
$post_description = "\n<!--d " . get_description($content) . " d-->";
}
} else {
$post_description = "";
}
if ($tag !== null) {
$tagmd = "\n<!--tag " . $post_tagmd . " tag-->";
} else {
$tagmd = "";
}
if ($media!== null) {
$post_media = "\n<!--" .$type. " " . preg_replace('/\s\s+/', ' ', strip_tags($media)) . " " .$type. "-->";
} else {
$post_media = "";
}
$post_content = "<!--t " . $post_title . " t-->" . $post_description . $tagmd . $post_media . "\n\n" . $content;
if (!empty($post_title) && !empty($post_tag) && !empty($post_url) && !empty($post_content)) {
$filename = $post_date . '_' . $post_tag . '_' . $post_url . '.md';
$dir = 'content/' . $user . '/blog/' . $category. '/draft/';
if (is_dir($dir)) {
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
} else {
mkdir($dir, 0775, true);
file_put_contents($dir . $filename, print_r($post_content, true), LOCK_EX);
}
save_tag_i18n($post_tag, $post_tagmd);
rebuilt_cache('all');
return "Auto Saved";
}
}
$title = $_POST['title'];
$url = $_POST['url'];
$content = $_POST['content'];
$description = $_POST['description'];
$draft = 'true';
$posttype = $_POST['posttype'];
if ($posttype == 'is_page') {
$response = auto_save_page($title, $url, $content, $draft, $description);
} else {
$user = $_SESSION[site_url()]['user'];
$tag = $_POST['tag'];
$category = $_POST['category'];
$dateTime = $_POST['dateTime'];
if ($posttype == 'is_image') {
$type = 'image';
$media = $_POST['pimage'];
} elseif ($posttype == 'is_video') {
$type = 'video';
$media = $_POST['pvideo'];
} elseif ($posttype == 'is_link') {
$type = 'link';
$media = $_POST['plink'];
} elseif ($posttype == 'is_quote') {
$type = 'quote';
$media = $_POST['pquote'];
} elseif ($posttype == 'is_audio') {
$type = 'audio';
$media = $_POST['paudio'];
} elseif ($posttype == 'is_post') {
$type = 'post';
$media = null;
}
$response = auto_save_post($title, $tag, $url, $content, $user, $draft, $category, $type, $description, $media, $dateTime);
}
echo $response;
} else {
$login = site_url() . 'login';
header("location: $login");
}
?>

View file

@ -35,6 +35,7 @@ $images = image_gallery(null, 1, 40);
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Editor.js"></script>
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Extra.js"></script>
<link rel="stylesheet" href="<?php echo site_url() ?>system/resources/css/jquery-ui.css">
<link rel="stylesheet" href="<?php echo site_url() ?>system/resources/css/autosave.css">
<script>
$( function() {
var availableTags = [
@ -86,7 +87,7 @@ $( function() {
<?php if (isset($error)) { ?>
<div class="error-message"><?php echo $error ?></div>
<?php } ?>
<div class="notice" id="response"></div>
<div class="row">
<div class="wmd-panel" style="width:100%;">
<form method="POST">
@ -177,6 +178,7 @@ $( function() {
<div class="row">
<div class="col-sm-6">
<div>
<input type="hidden" id="pType" name="posttype" value="<?php echo $type; ?>">
<label for="wmd-input"><?php echo i18n('Content');?> <span class="required">*</span></label>
<div id="wmd-button-bar" class="wmd-button-bar"></div>
<textarea id="wmd-input" class="form-control wmd-input <?php if (isset($postContent)) { if (empty($postContent)) { echo 'error'; } } ?>" name="content" cols="20" rows="15"><?php if (isset($postContent)) { echo $postContent;} ?></textarea><br>
@ -311,3 +313,4 @@ $('.img-container').on("click", ".the-img", function(e) {
$('#insertImageDialogURL').val($(e.target).attr('src'));
});
</script>
<script src="<?php echo site_url() ?>system/resources/js/save_draft.js"></script>

View file

@ -8,11 +8,12 @@
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Editor.js"></script>
<script type="text/javascript" src="<?php echo site_url() ?>system/admin/editor/js/Markdown.Extra.js"></script>
<link rel="stylesheet" href="<?php echo site_url() ?>system/resources/css/jquery-ui.css">
<link rel="stylesheet" href="<?php echo site_url() ?>system/resources/css/autosave.css">
<?php if (isset($error)) { ?>
<div class="error-message"><?php echo $error ?></div>
<?php } ?>
<div class="notice" id="response"></div>
<div class="row">
<div class="wmd-panel" style="width:100%;">
<form method="POST">
@ -40,6 +41,7 @@
<div id="wmd-button-bar" class="wmd-button-bar"></div>
<textarea id="wmd-input" class="form-control wmd-input <?php if (isset($postContent)) {if (empty($postContent)) {echo 'error';}} ?>" name="content" cols="20" rows="10"><?php if (isset($postContent)) {echo $postContent;} ?></textarea>
<br>
<input type="hidden" id="pType" name="posttype" value="<?php echo $type; ?>">
<input type="hidden" name="csrf_token" value="<?php echo get_csrf() ?>">
<?php if ($type == 'is_page') :?>
<input type="submit" name="submit" class="btn btn-primary submit" value="<?php echo i18n('Publish');?>"/> <input type="submit" name="draft" class="btn btn-primary draft" value="<?php echo i18n('Save_as_draft');?>"/>
@ -137,3 +139,4 @@ $('.img-container').on("click", ".the-img", function(e) {
$('#insertImageDialogURL').val($(e.target).attr('src'));
});
</script>
<script src="<?php echo site_url() ?>system/resources/js/save_draft.js"></script>

View file

@ -0,0 +1,16 @@
.notice {
position: fixed;
bottom: 20px;
right: 20px;
padding: 10px 20px;
background-color: #f0f9ff;
border: 1px solid #e0e0e0;
border-radius: 5px;
box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.1);
font-size: 14px;
color: #333;
z-index: 999;
display: flex;
align-items: center;
display: none;
}

View file

@ -0,0 +1,57 @@
const response = document.getElementById("response");
function updateData() {
var title = $("#pTitle").val();
var url = $("#pURL").val();
var content = $("#wmd-input").val();
var description = $("#pMeta").val();
var tag = $("#pTag").val();
var category = $("#pCategory").val();
var posttype = $("#pType").val();
var pimage = $("#pImage").val();
var paudio = $("#pAudio").val();
var pvideo = $("#pVideo").val();
var pquote = $("#pQuote").val();
var plink = $("#pLink").val();
var pDate = $("#pDate").val();
var pTime = $("#pTime").val();
var dateTime = pDate + " " + pTime;
// Prepare data to send to PHP
var data = {
title: title,
url: url,
content: content,
description: description,
tag: tag,
category: category,
posttype: posttype,
pimage: pimage,
paudio: paudio,
pvideo: pvideo,
pquote: pquote,
plink: plink,
dateTime: dateTime
};
$.ajax({
url: base_path + 'autosave.php',
type: "POST",
data: data,
success: function(response) {
$("#response").html(response);
$("#response").fadeIn(600, function() {
$("#response").css("display", "block");
});
setTimeout(function() {
$("#response").fadeOut(600, function() {
$("#response").css("display", "none");
});
}, 3000);
}
});
}
$(document).ready(function() {
setInterval(updateData, 60000);
});