From: Ben Sherratt Date: Sat, 4 Jul 2026 18:07:02 +0000 (+0100) Subject: Added support for previewing posts when logged in X-Git-Url: https://git.bts.cx/cx.git/commitdiff_plain/8a7cb090b52893c49c169d4ba1419317251bfaaa Added support for previewing posts when logged in --- diff --git a/cx/cx.php b/cx/cx.php index e4285ee..b18232b 100644 --- a/cx/cx.php +++ b/cx/cx.php @@ -182,7 +182,7 @@ function cx_route($path) { } else { if (count($path_components) >= 1) { $slug = $path_components[0]; - $page_id = cx_posts_find_page_id($slug); + $page_id = cx_posts_find_page_id($slug, include_drafts: cx_admin_logged_in()); if ($page_id) { $template = 'post'; $template_variables['post_id'] = $page_id; @@ -193,7 +193,7 @@ function cx_route($path) { $year = $path_components[0]; $month = $path_components[1]; $slug = $path_components[2]; - $post_id = cx_posts_find_article_id($slug); + $post_id = cx_posts_find_article_id($slug, include_drafts: cx_admin_logged_in()); if ($post_id) { $template = 'post'; $template_variables['post_id'] = $post_id; diff --git a/cx/lib/posts.php b/cx/lib/posts.php index 9fb4e35..3293ace 100644 --- a/cx/lib/posts.php +++ b/cx/lib/posts.php @@ -211,30 +211,43 @@ function cx_posts_find_post($post_id) { return null; } -function cx_posts_find_article_id($post_slug) { +function cx_posts_find_article_id($post_slug, bool $include_drafts = false) { $sql = 'SELECT post_id FROM posts WHERE post_slug == ? - AND post_is_page == FALSE - AND post_is_draft == FALSE - LIMIT 1;'; + AND post_is_page == FALSE'; + + if ($include_drafts == false) { + $sql .= ' AND post_is_draft == FALSE'; + } + + $sql .= ' LIMIT 1'; + + $sql .= ';'; foreach (cx_db_query($sql, $post_slug) as $post) { return $post['post_id']; } return null; + } -function cx_posts_find_page_id($post_slug) { +function cx_posts_find_page_id($post_slug, bool $include_drafts = false) { $sql = 'SELECT post_id FROM posts WHERE post_slug == ? - AND post_is_page == TRUE - AND post_is_draft == FALSE - LIMIT 1;'; + AND post_is_page == TRUE'; + + if ($include_drafts == false) { + $sql .= ' AND post_is_draft == FALSE'; + } + + $sql .= ' LIMIT 1'; + + $sql .= ';'; foreach (cx_db_query($sql, $post_slug) as $post) { return $post['post_id'];