Allow to change the post permalink prefix

Example: /blog/post-slug
This commit is contained in:
danpros 2024-09-06 14:19:25 +07:00
commit a473482da8
5 changed files with 60 additions and 57 deletions

View file

@ -74,26 +74,51 @@ Please install and enable the GD extension to use the thumbnail feature.
</nav> </nav>
<br><br> <br><br>
<form method="POST"> <form method="POST">
<input type="hidden" name="csrf_token" value="<?php echo get_csrf(); ?>"> <input type="hidden" name="csrf_token" value="<?php echo get_csrf(); ?>">
<h4><?php echo i18n('Permalink');?></h4>
<hr>
<div class="form-group row"> <div class="form-group row">
<label class="col-sm-2 col-form-label"><?php echo i18n('Permalink');?></label> <label class="col-sm-2 col-form-label"><?php echo i18n('Enable_blog_URL');?></label>
<div class="col-sm-10"> <div class="col-sm-10">
<div class="col-sm-10"> <div class="col-sm-10">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="radio" name="-config-permalink.type" id="permalink.type1" value="default" <?php if (config('permalink.type') === 'default'):?>checked<?php endif;?>> <input class="form-check-input" type="radio" name="-config-blog.enable" id="blog.enable1" value="true" <?php if (config('blog.enable') === 'true'):?>checked<?php endif;?>>
<label class="form-check-label" for="permalink.type1"> <label class="form-check-label" for="blog.enable1">
<?php echo i18n('year_month_your_post_slug');?> <?php echo i18n('Enable');?>
</label> </label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="radio" name="-config-permalink.type" id="permalink.type1" value="post" <?php if (config('permalink.type') === 'post'):?>checked<?php endif;?>> <input class="form-check-input" type="radio" name="-config-blog.enable" id="blog.enable2" value="false" <?php if (config('blog.enable') === 'false'):?>checked<?php endif;?>>
<label class="form-check-label" for="permalink.type2"> <label class="form-check-label" for="blog.enable2">
<?php echo i18n('post_your_post_slug');?> <?php echo i18n('Disable');?>
</label> </label>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div class="form-group row">
<label for="blog.path" class="col-sm-2 col-form-label">Blog Path</label>
<div class="col-sm-10">
<input type="text" name="-config-blog.path" class="form-control" id="blog.path" placeholder="blog" value="<?php echo config('blog.path');?>">
</div>
</div>
<div class="form-group row">
<label for="blog.string" class="col-sm-2 col-form-label">Blog String</label>
<div class="col-sm-10">
<input type="text" name="-config-blog.string" class="form-control" id="blog.string" placeholder="Blog" value="<?php echo config('blog.string');?>">
</div>
</div>
<div class="form-group row">
<label for="custom.permalink" class="col-sm-2 col-form-label"><?php echo i18n('Permalink');?> Prefix</label>
<div class="col-sm-10">
<input type="text" name="-config-permalink.type" class="form-control" id="permalink.type" value="<?php echo permalink_type();?>" placeholder="default">
<p class="title-format" style="margin-bottom:5px;"><code>default</code> <?php echo i18n('year_month_your_post_slug');?></p>
<p class="title-format" style="margin-bottom:5px;"><code>post</code> <?php echo i18n('post_your_post_slug');?></p>
</div>
</div>
<br>
<h4><?php echo i18n('Metatags');?></h4>
<hr>
<div class="form-group row"> <div class="form-group row">
<label for="description.char" class="col-sm-2 col-form-label"><?php echo i18n('Meta_description_character');?></label> <label for="description.char" class="col-sm-2 col-form-label"><?php echo i18n('Meta_description_character');?></label>
<div class="col-sm-10"> <div class="col-sm-10">

View file

@ -35,37 +35,6 @@
</div> </div>
</div> </div>
</div> </div>
<div class="form-group row">
<label class="col-sm-2 col-form-label"><?php echo i18n('Enable_blog_URL');?></label>
<div class="col-sm-10">
<div class="col-sm-10">
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-blog.enable" id="blog.enable1" value="true" <?php if (config('blog.enable') === 'true'):?>checked<?php endif;?>>
<label class="form-check-label" for="blog.enable1">
<?php echo i18n('Enable');?>
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="-config-blog.enable" id="blog.enable2" value="false" <?php if (config('blog.enable') === 'false'):?>checked<?php endif;?>>
<label class="form-check-label" for="blog.enable2">
<?php echo i18n('Disable');?>
</label>
</div>
</div>
</div>
</div>
<div class="form-group row">
<label for="blog.path" class="col-sm-2 col-form-label">Blog Path</label>
<div class="col-sm-10">
<input type="text" name="-config-blog.path" class="form-control" id="blog.path" placeholder="blog" value="<?php echo config('blog.path');?>">
</div>
</div>
<div class="form-group row">
<label for="blog.string" class="col-sm-2 col-form-label">Blog String</label>
<div class="col-sm-10">
<input type="text" name="-config-blog.string" class="form-control" id="blog.string" placeholder="Blog" value="<?php echo config('blog.string');?>">
</div>
</div>
<div class="form-group row"> <div class="form-group row">
<label for="posts.perpage" class="col-sm-2 col-form-label"><?php echo i18n('Posts_in_front_page_show_at_most');?></label> <label for="posts.perpage" class="col-sm-2 col-form-label"><?php echo i18n('Posts_in_front_page_show_at_most');?></label>
<div class="col-sm-10"> <div class="col-sm-10">

View file

@ -48,11 +48,11 @@ $timestamp = $time->format("Y-m-d H:i:s");
// The post date // The post date
$postdate = strtotime($timestamp); $postdate = strtotime($timestamp);
// The post URL // The post URL
if (config('permalink.type') == 'post') { if (permalink_type() == 'default') {
$delete = site_url() . 'post/' . $oldmd . '/delete?destination=' . $destination; $delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination;
} else { } else {
// The post URL // The post URL
$delete = site_url() . date('Y/m', $postdate) . '/' . $oldmd . '/delete?destination=' . $destination; $delete = site_url() . permalink_type() . '/' . $oldmd . '/delete?destination=' . $destination;
} }
$tags = tag_cloud(true); $tags = tag_cloud(true);

View file

@ -3486,7 +3486,7 @@ get('/feed/opml', function () {
}); });
// Show blog post without year-month // Show blog post without year-month
get('/post/:name', function ($name) { get('/'. permalink_type() .'/:name', function ($name) {
if (isset($_GET['search'])) { if (isset($_GET['search'])) {
$search = _h($_GET['search']); $search = _h($_GET['search']);
@ -3494,7 +3494,7 @@ get('/post/:name', function ($name) {
header("Location: $url"); header("Location: $url");
} }
if (config('permalink.type') != 'post') { if (permalink_type() == 'default') {
$post = find_post(null, null, $name); $post = find_post(null, null, $name);
if (is_null($post)) { if (is_null($post)) {
not_found(); not_found();
@ -3607,7 +3607,7 @@ get('/post/:name', function ($name) {
}); });
// Edit blog post // Edit blog post
get('/post/:name/edit', function ($name) { get('/'. permalink_type() .'/:name/edit', function ($name) {
if (login()) { if (login()) {
@ -3677,7 +3677,7 @@ get('/post/:name/edit', function ($name) {
}); });
// Get edited data from blog post // Get edited data from blog post
post('/post/:name/edit', function () { post('/'. permalink_type() .'/:name/edit', function () {
if(!login()) { if(!login()) {
$login = site_url() . 'login'; $login = site_url() . 'login';
header("location: $login"); header("location: $login");
@ -3825,7 +3825,7 @@ post('/post/:name/edit', function () {
}); });
// Delete blog post // Delete blog post
get('/post/:name/delete', function ($name) { get('/'. permalink_type() .'/:name/delete', function ($name) {
if (login()) { if (login()) {
@ -3886,7 +3886,7 @@ get('/post/:name/delete', function ($name) {
}); });
// Get deleted data from blog post // Get deleted data from blog post
post('/post/:name/delete', function () { post('/'. permalink_type() .'/:name/delete', function () {
$proper = is_csrf_proper(from($_REQUEST, 'csrf_token')); $proper = is_csrf_proper(from($_REQUEST, 'csrf_token'));
if ($proper && login()) { if ($proper && login()) {
@ -4682,8 +4682,8 @@ get('/:year/:month/:name', function ($year, $month, $name) {
header("Location: $url"); header("Location: $url");
} }
if (config('permalink.type') == 'post') { if (permalink_type() !== 'default') {
$redir = site_url() . 'post/' . $name; $redir = site_url() . permalink_type() . '/' . $name;
header("location: $redir", TRUE, 301); header("location: $redir", TRUE, 301);
} }

View file

@ -399,7 +399,6 @@ function get_posts($posts, $page = 1, $perpage = 0)
$auto = config('toc.automatic'); $auto = config('toc.automatic');
$counter = config('views.counter'); $counter = config('views.counter');
$caption = config('fig.captions'); $caption = config('fig.captions');
$permalink = config('permalink.type');
if ($counter == 'true') { if ($counter == 'true') {
$viewsFile = "content/data/views.json"; $viewsFile = "content/data/views.json";
@ -469,10 +468,10 @@ function get_posts($posts, $page = 1, $perpage = 0)
// The archive per day // The archive per day
$post->archive = site_url() . 'archive/' . date('Y-m', $post->date); $post->archive = site_url() . 'archive/' . date('Y-m', $post->date);
if ($permalink == 'post') { if (permalink_type() == 'default') {
$post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]);
} else {
$post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]);
} else {
$post->url = site_url() . permalink_type() . '/' . str_replace('.md', '', $arr[2]);
} }
$post->slug = str_replace('.md', '', $arr[2]); $post->slug = str_replace('.md', '', $arr[2]);
@ -3085,10 +3084,10 @@ function sitemap_post_path($posts, $page = 1, $perpage = 0)
$post->archiveyear = site_url() . 'archive/' . date('Y', $post->date); $post->archiveyear = site_url() . 'archive/' . date('Y', $post->date);
// The post URL // The post URL
if (config('permalink.type') == 'post') { if (permalink_type() == 'default') {
$post->url = site_url() . 'post/' . str_replace('.md', '', $arr[2]);
} else {
$post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]); $post->url = site_url() . date('Y/m', $post->date) . '/' . str_replace('.md', '', $arr[2]);
} else {
$post->url = site_url() . permalink_type() . '/' . str_replace('.md', '', $arr[2]);
} }
$tmp[] = $post; $tmp[] = $post;
@ -3492,6 +3491,16 @@ function is_index()
} }
} }
// Return post permalink type
function permalink_type()
{
$permalink = config('permalink.type');
if (!is_null($permalink) && !empty($permalink)) {
return strtolower($permalink);
}
return 'default';
}
// Return blog path index // Return blog path index
function blog_path() function blog_path()
{ {