Commit 662e3781 authored by Administrator's avatar Administrator 💬

fix: prev/next navigation links in lessons

/spend 10h
parent 28efacec
Pipeline #14447 failed with stage
...@@ -381,18 +381,18 @@ class M3P2Plugin extends Plugin ...@@ -381,18 +381,18 @@ class M3P2Plugin extends Plugin
// Found one, let's add it // Found one, let's add it
$other_lessons[$page_lesson['slug']] = $page_lesson; $other_lessons[$page_lesson['slug']] = $page_lesson;
// And the prev/next navigation // And the prev/next navigation (the old way)
if ($current_found) { // if ($current_found) {
if ( ! $next_found) { // if ( ! $next_found) {
$next_lesson_slug = $page_lesson['slug']; // $next_lesson_slug = $page_lesson['slug'];
$next_found = true; // $next_found = true;
} // }
} else { // } else {
if ( ! $previous_found) { // if ( ! $previous_found) {
$previous_lesson_slug = $page_lesson['slug']; // $previous_lesson_slug = $page_lesson['slug'];
$previous_found = true; // $previous_found = true;
} // }
} // }
} }
continue; continue;
...@@ -406,6 +406,24 @@ class M3P2Plugin extends Plugin ...@@ -406,6 +406,24 @@ class M3P2Plugin extends Plugin
} }
} }
$redirects = array_values($this->getPagesWithRedirects());
$amountOfRedirects = count($redirects);
$redirect_parent_index = -1;
foreach ($redirects as $k => $redirect) {
if ($this->endsWith($redirect->redirect(), "/lessons/" . $lesson_slug)) {
$redirect_parent_index = $k;
break;
}
}
if ($redirect_parent_index > -1) {
if ($redirect_parent_index > 0) {
$previous_lesson_slug = $this->extractLessonSlug($redirects[$redirect_parent_index - 1]->redirect());
}
if ($redirect_parent_index < $amountOfRedirects - 1) {
$next_lesson_slug = $this->extractLessonSlug($redirects[$redirect_parent_index + 1]->redirect());
}
}
$translated_lessons = []; // lang => slug $translated_lessons = []; // lang => slug
$translated_lessons_contenders = []; // lang => [ slug => likelihood ] $translated_lessons_contenders = []; // lang => [ slug => likelihood ]
...@@ -464,4 +482,54 @@ class M3P2Plugin extends Plugin ...@@ -464,4 +482,54 @@ class M3P2Plugin extends Plugin
$event->page = $page; $event->page = $page;
$event->stopPropagation(); $event->stopPropagation();
} }
public function getPagesWithRedirects()
{
/** @var Pages $pages */
$pages = $this->grav['pages'];
$target_pages = array_filter($pages->instances(), function (Page $p) {
$page_config = $p->header();
if (empty($p->redirect())) {
return false;
}
return true;
});
return $target_pages;
}
protected function mbEndsWith($haystack, $needle, $case=true): bool
{
$expectedPosition = mb_strlen($haystack) - mb_strlen($needle);
if ($case) {
return mb_strrpos($haystack, $needle, 0) === $expectedPosition;
}
return mb_strripos($haystack, $needle, 0) === $expectedPosition;
}
protected function endsWith($haystack, $needle, $case=true): bool
{
$expectedPosition = strlen($haystack) - strlen($needle);
if ($case) {
return strrpos($haystack, $needle, 0) === $expectedPosition;
}
return strripos($haystack, $needle, 0) === $expectedPosition;
}
protected function extractLessonSlug(string $url) : string
{
$matches = array();
if (false !== preg_match("!/lessons/(?P<slug>[^#?&]+)!", $url, $matches)) {
return $matches['slug'];
}
return "";
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment