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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Filter replaces hard-coded addition of draft posts to navigation
- Filter url passed to chapter navigation items

## [v2.3.0] - 2026-04-29

Expand Down
80 changes: 74 additions & 6 deletions spec/chapter_navigation.spec.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Kahlan\Arg;

describe(\LongReadPlugin\ChapterNavigation::class, function () {
beforeEach(function () {
$this->chapterNavigation = new \LongReadPlugin\ChapterNavigation();
Expand All @@ -15,7 +13,18 @@
'ID' => 123,
'post_title' => 'Chapter One'
];
allow('apply_filters')->toBeCalled()->andReturn(['publish']);
$chapterUrlCallArgs = [];
allow('apply_filters')->toBeCalled()->andRun(function (string $filterName, $value, $chapterPostId = null) use (&$chapterUrlCallArgs) {
if ($filterName === 'long_read_plugin_post_status') {
return ['publish'];
}

if ($filterName === 'long_read_plugin_chapter_url') {
$chapterUrlCallArgs[] = $chapterPostId;
}

return $value;
});
allow('get_post_parent')->toBeCalled()->andReturn(false);
allow('get_posts')->toBeCalled()->andReturn([
(object) [
Expand All @@ -39,6 +48,9 @@
allow('get_permalink')->toBeCalled()->andReturn('http://chapter-two-link', 'http://chapter-three-link');

$result = $this->chapterNavigation->getItems();
expect($chapterUrlCallArgs[0])->toEqual(123);
expect($chapterUrlCallArgs[1])->toEqual(456);
expect($chapterUrlCallArgs[2])->toEqual(789);

expect(count($result))->toEqual(3);
expect($result[0]->title)->toEqual('Chapter One');
Expand All @@ -61,10 +73,21 @@
'ID' => 123,
'post_title' => 'Chapter One'
];
allow('apply_filters')->toBeCalled()->andReturn(['publish']);
$chapterUrlCallArgs = [];
allow('apply_filters')->toBeCalled()->andRun(function (string $filterName, $value, $chapterPostId = null) use (&$chapterUrlCallArgs) {
if ($filterName === 'long_read_plugin_post_status') {
return ['publish'];
}

if ($filterName === 'long_read_plugin_chapter_url') {
$chapterUrlCallArgs[] = $chapterPostId;
}

return $value;
});
allow('get_post_parent')->toBeCalled()->andReturn($parentPost);
allow('get_posts')->toBeCalled()->andReturn([
$post = (object) [
(object) [
'ID' => 456,
'post_title' => 'Chapter Two'
],
Expand All @@ -85,6 +108,9 @@
allow('get_permalink')->toBeCalled()->andReturn('http://chapter-one-link', 'http://chapter-three-link');

$result = $this->chapterNavigation->getItems();
expect($chapterUrlCallArgs[0])->toEqual(123);
expect($chapterUrlCallArgs[1])->toEqual(456);
expect($chapterUrlCallArgs[2])->toEqual(789);

expect(count($result))->toEqual(3);
expect($result[0]->title)->toEqual('Chapter One');
Expand All @@ -108,7 +134,13 @@
'post_title' => 'Chapter One',
'post_status' => 'publish'
];
allow('apply_filters')->toBeCalled()->andReturn(['publish', 'draft']);
allow('apply_filters')->toBeCalled()->andRun(function (string $filterName, $value) {
if ($filterName === 'long_read_plugin_post_status') {
return ['publish', 'draft'];
}

return $value;
});
allow('get_post_parent')->toBeCalled()->andReturn($parentPost);
allow('get_posts')->toBeCalled()->andReturn([
(object) [
Expand Down Expand Up @@ -138,5 +170,41 @@
expect(count($result))->toEqual(3);
});
});

context('when long read chapter URL filter changes an item URL', function () {
it('returns the filtered chapter URL in the navigation item', function () {
global $post;
$post = (object) [
'ID' => 123,
'post_title' => 'Chapter One'
];
allow('apply_filters')->toBeCalled()->andRun(function (string $filterName, $value, $chapterPostId = null) {
if ($filterName === 'long_read_plugin_post_status') {
return ['publish'];
}

if (
$filterName === 'long_read_plugin_chapter_url'
&& $chapterPostId === 456
) {
return 'http://filtered-chapter-two-link';
}

return $value;
});
allow('get_post_parent')->toBeCalled()->andReturn(false);
allow('get_posts')->toBeCalled()->andReturn([
(object) [
'ID' => 456,
'post_title' => 'Chapter Two'
]
]);
allow('get_permalink')->toBeCalled()->andReturn('http://chapter-two-link');

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

expect($result[1]->url)->toEqual('http://filtered-chapter-two-link');
});
});
});
});
3 changes: 2 additions & 1 deletion src/ChapterNavigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public function getItems(): array
'order' => 'ASC'
]);
array_unshift($chapterPosts, $parentPost);
$currentPostId = $post->ID;
$chapterNavigationItems = [];
foreach ($chapterPosts as $chapterPost) {
$chapterNavigationItems[] = (object) [
'title' => $chapterPost->post_title,
'url' => $chapterPost->ID == $post->ID ? null : get_permalink($chapterPost)
'url' => apply_filters('long_read_plugin_chapter_url', $chapterPost->ID == $currentPostId ? null : get_permalink($chapterPost), $chapterPost->ID)
];
}
return $chapterNavigationItems;
Expand Down
Loading