Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autosave to drafts feature #757

Merged
merged 5 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions autosave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
<?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 (!empty($content)) {
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);
}
} else {
$response = "No content to save.";
}
echo $response;
} else {
$login = site_url() . 'login';
header("location: $login");
}
?>
6 changes: 5 additions & 1 deletion lang/en_US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ rss_settings = "RSS settings"
read_more_text = "Read more text"
read_more_text_placeholder = "Read more"
reading = "Reading"
writing = "Writing"
reading_settings = "Reading Settings"
writing_settings = "Writing Settings"
recaptcha = "reCAPTCHA"
recent_posts = "Recent posts"
recent_posts_widget_at_most = "Recent posts widget at most"
Expand Down Expand Up @@ -292,4 +294,6 @@ verify_code = "Verify the MFA code"
verify_password = "Verify current password"
manualsetupkey = "You can also manually add the setup key"
mfa_error = "MFA code is not correct"
disablemfa = "Disable MFA"
disablemfa = "Disable MFA"
enable_auto_save = "Enable Auto Save to Drafts"
explain_autosave = "When enabled, new posts or pages will automatically be saved as a draft every 60 seconds after you start writing."
7 changes: 6 additions & 1 deletion system/admin/views/add-content.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<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 = [
Expand Down Expand Up @@ -86,7 +87,7 @@ function extractLast( term ) {
<?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">
Expand Down Expand Up @@ -177,6 +178,7 @@ function extractLast( term ) {
<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>
Expand Down Expand Up @@ -311,3 +313,6 @@ function loadImages(page) {
$('#insertImageDialogURL').val($(e.target).attr('src'));
});
</script>
<?php if (config('autosave.enable') == 'true' ) {
echo '<script src="'.site_url().'system/resources/js/save_draft.js"></script>';
} ?>
7 changes: 6 additions & 1 deletion system/admin/views/add-page.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down Expand Up @@ -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');?>"/>
Expand Down Expand Up @@ -137,3 +139,6 @@ function loadImages(page) {
$('#insertImageDialogURL').val($(e.target).attr('src'));
});
</script>
<?php if (config('autosave.enable') == 'true' ) {
echo '<script src="'.site_url().'system/resources/js/save_draft.js"></script>';
} ?>
1 change: 1 addition & 0 deletions system/admin/views/config-custom.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
1 change: 1 addition & 0 deletions system/admin/views/config-metatags.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link active" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
1 change: 1 addition & 0 deletions system/admin/views/config-performance.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link active" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
1 change: 1 addition & 0 deletions system/admin/views/config-reading.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link active" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
1 change: 1 addition & 0 deletions system/admin/views/config-widget.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link active" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
43 changes: 43 additions & 0 deletions system/admin/views/config-writing.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php if (!defined('HTMLY')) die('HTMLy'); ?>
<h2><?php echo i18n('Writing_Settings');?></h2>
<br>
<nav>
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link active" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
<a class="nav-item nav-link" id="nav-custom-tab" href="<?php echo site_url();?>admin/config/custom"><?php echo i18n('Custom');?></a>
</div>
</nav>
<br><br>
<form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo get_csrf(); ?>">
<div class="form-group row">
<label class="col-sm-2 col-form-label"><?php echo i18n('Enable_auto_save');?></label>
<div class="col-sm-10">
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-autosave.enable" id="autosave.enable1" value="true" <?php if (config('autosave.enable') === 'true'):?>checked<?php endif;?>>
<label class="form-check-label" for="autosave.enable1">
<?php echo i18n('Enable');?>
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-autosave.enable" id="autosave.enable2" value="false" <?php if (config('autosave.enable') === 'false'):?>checked<?php endif;?>>
<label class="form-check-label" for="autosave.enable2">
<?php echo i18n('Disable');?>
</label>
</div>
</div>
<small><em><?php echo i18n('explain_autosave');?></em></small>
</div>
</div>
<div class="form-group row">
<div class="col-sm-10">
<button type="submit" class="btn btn-primary"><?php echo i18n('Save_Config');?></button>
</div>
</div>
</form>
1 change: 1 addition & 0 deletions system/admin/views/config.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<div class="nav nav-tabs" id="nav-tab">
<a class="nav-item nav-link active" id="nav-general-tab" href="<?php echo site_url();?>admin/config"><?php echo i18n('General');?></a>
<a class="nav-item nav-link" id="nav-profile-tab" href="<?php echo site_url();?>admin/config/reading"><?php echo i18n('Reading');?></a>
<a class="nav-item nav-link" id="nav-writing-tab" href="<?php echo site_url();?>admin/config/writing"><?php echo i18n('Writing');?></a>
<a class="nav-item nav-link" id="nav-widget-tab" href="<?php echo site_url();?>admin/config/widget"><?php echo i18n('Widget');?></a>
<a class="nav-item nav-link" id="nav-metatags-tab" href="<?php echo site_url();?>admin/config/metatags"><?php echo i18n('Metatags');?></a>
<a class="nav-item nav-link" id="nav-performance-tab" href="<?php echo site_url();?>admin/config/performance"><?php echo i18n('Performance');?></a>
Expand Down
3 changes: 2 additions & 1 deletion system/configList.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@
"default.image",
"favicon.image",
"views.root",
"views.layout"
"views.layout",
"autosave.enable"
]
Loading