From aea0773df8c17bfedb4e0181b86da9ba84ad3c78 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Wed, 8 Jun 2022 14:46:26 +0200 Subject: [PATCH] Add pretty url for blog post --- .htaccess | 2 +- src/classes/blog.php | 13 +++++++++++++ src/classes/page.php | 1 + src/pages/blog.php | 2 +- src/pages/changelog.php | 1 + src/pages/post.php | 4 ++-- 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/.htaccess b/.htaccess index a76aa6d6..8c7a9ed4 100644 --- a/.htaccess +++ b/.htaccess @@ -17,7 +17,7 @@ RewriteRule ^wishlist/([0-9a-f]{40})$ /?page=wishlist&hash=$1 [QSA,L] # Blog Post - RewriteRule ^blog/([a-z\-0-9]+)$ /?page=blog&slug=$1 [QSA,L] + RewriteRule ^blog/([a-z\-0-9]+)$ /?page=post&slug=$1 [QSA,L] ##-- When caching of gzipped JS and CSS files is used, enable this setting diff --git a/src/classes/blog.php b/src/classes/blog.php index eef82bde..f2bfa9ec 100644 --- a/src/classes/blog.php +++ b/src/classes/blog.php @@ -33,6 +33,19 @@ class Blog return $post; } + public static function getPostBySlug(string $slug): \stdClass + { + $posts = self::get(self::ENDPOINT_POSTS); + + foreach ($posts as $post) { + if ($slug === $post->slug) { + return $post; + } + } + + throw new \Exception('No post found with the slug "' . $slug . '".'); + } + public static function getPosts(): array { $posts = self::get(self::ENDPOINT_POSTS); diff --git a/src/classes/page.php b/src/classes/page.php index f379fa3b..f7cc1d32 100644 --- a/src/classes/page.php +++ b/src/classes/page.php @@ -122,6 +122,7 @@ class Page $ignorePower = array( 'home', 'blog', + 'post', 'install', 'login', 'maintenance', diff --git a/src/pages/blog.php b/src/pages/blog.php index 0e24874f..3e823a9c 100644 --- a/src/pages/blog.php +++ b/src/pages/blog.php @@ -32,7 +32,7 @@ $posts = Blog::getPosts(); ); $mediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : ''; $categoriesHTML = Blog::getCategoriesHTML($post->categories); - $postLink = '/?page=post&id=' . $post->id; + $postLink = '/?page=post&slug=' . $post->slug; ?>
diff --git a/src/pages/changelog.php b/src/pages/changelog.php index f79193a8..f6c97775 100644 --- a/src/pages/changelog.php +++ b/src/pages/changelog.php @@ -45,6 +45,7 @@ $page->navigation();
  • +

diff --git a/src/pages/post.php b/src/pages/post.php index 83c06017..92c0413a 100644 --- a/src/pages/post.php +++ b/src/pages/post.php @@ -8,8 +8,8 @@ namespace wishthis; -$postID = $_SESSION['_GET']['id']; -$post = Blog::getPost($postID); +$postSlug = $_SESSION['_GET']['slug']; +$post = Blog::getPostBySlug($postSlug); $postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : ''; $page = new Page(__FILE__, 'Post');