Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Chapter navigation now includes draft posts for users with edit permissions

## [v2.2.0] - 2026-04-07

### Changed
Expand Down
46 changes: 46 additions & 0 deletions spec/chapter_navigation.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'ID' => 123,
'post_title' => 'Chapter One'
];
allow('current_user_can')->toBeCalled();
allow('get_post_parent')->toBeCalled()->andReturn(false);
allow('get_posts')->toBeCalled()->andReturn([
(object) [
Expand All @@ -28,6 +29,7 @@
]);
expect('get_posts')->toBeCalled()->once()->with([
'post_parent' => 123,
'post_status' => ['publish'],
'post_type' => 'any',
'posts_per_page' => -1,
'orderby' => 'menu_order',
Expand Down Expand Up @@ -58,6 +60,7 @@
'ID' => 123,
'post_title' => 'Chapter One'
];
allow('current_user_can')->toBeCalled();
allow('get_post_parent')->toBeCalled()->andReturn($parentPost);
allow('get_posts')->toBeCalled()->andReturn([
$post = (object) [
Expand All @@ -71,6 +74,7 @@
]);
expect('get_posts')->toBeCalled()->once()->with([
'post_parent' => 123,
'post_status' => ['publish'],
'post_type' => 'any',
'posts_per_page' => -1,
'orderby' => 'menu_order',
Expand All @@ -89,5 +93,47 @@
expect($result[2]->url)->toEqual('http://chapter-three-link');
});
});

context('when the current logged in user can edit posts', function () {
it('includes draft posts in the query and returns them in the result', function () {
global $post;
$post = (object) [
'ID' => 1011,
'post_title' => 'Chapter Four'
];
$parentPost = (object) [
'ID' => 123,
'post_title' => 'Chapter One',
'post_status' => 'publish'
];
allow('current_user_can')->toBeCalled()->andReturn(true);
allow('get_post_parent')->toBeCalled()->andReturn($parentPost);
allow('get_posts')->toBeCalled()->andReturn([
(object) [
'ID' => 456,
'post_title' => 'Chapter Two',
'post_status' => 'publish'
],
(object) [
'ID' => 789,
'post_title' => 'Chapter Three',
'post_status' => 'draft'
]
]);
expect('get_posts')->toBeCalled()->once()->with([
'post_parent' => 123,
'post_status' => ['publish', 'draft'],
'post_type' => 'any',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC'
]);
allow('get_permalink')->toBeCalled();

$result = $this->chapterNavigation->getItems();

expect(count($result))->toEqual(3);
});
});
});
});
5 changes: 5 additions & 0 deletions src/ChapterNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ class ChapterNavigation
{
public function getItems(): array
{
$postStatus = ['publish'];
if(current_user_can('edit_posts')) {
array_push($postStatus, 'draft');
}
global $post;
$potentialParent = get_post_parent($post);
$parentPost = $potentialParent ? $potentialParent : $post;
$chapterPosts = get_posts([
'post_parent' => $parentPost->ID,
'post_status' => $postStatus,
'post_type' => 'any',
'posts_per_page' => -1,
'orderby' => 'menu_order',
Expand Down
Loading