previousnext.php (3203B)
1 <?php 2 // Previousnext extension, https://github.com/annaesvensson/yellow-previousnext 3 4 class YellowPreviousnext { 5 const VERSION = "0.9.1"; 6 public $yellow; // access to API 7 8 // Handle initialisation 9 public function onLoad($yellow) { 10 $this->yellow = $yellow; 11 $this->yellow->system->setDefault("previousnextPagePrevious", "1"); 12 $this->yellow->system->setDefault("previousnextPageNext", "1"); 13 } 14 15 // Handle page content element 16 public function onParseContentElement($page, $name, $text, $attributes, $type) { 17 $output = null; 18 if ($name=="previousnext" && ($type=="block" || $type=="inline")) { 19 $pages = $this->getRelatedPages($page); 20 $page->setLastModified($pages->getModified()); 21 $pagePrevious = $pageNext = null; 22 if ($this->yellow->system->get("previousnextPagePrevious")) $pagePrevious = $pages->getPagePrevious($page); 23 if ($this->yellow->system->get("previousnextPageNext")) $pageNext = $pages->getPageNext($page); 24 if ($pagePrevious!=null || $pageNext!=null) { 25 $output = "<div class=\"previousnext\" role=\"navigation\" aria-label=\"".$this->yellow->language->getTextHtml("previousnextNavigation")."\">\n"; 26 $output .= "<p>"; 27 if ($pagePrevious!=null) { 28 $text = preg_replace("/@title/i", $pagePrevious->get("title"), $this->yellow->language->getText("previousnextPagePrevious")); 29 $output .= "<a class=\"previous\" href=\"".$pagePrevious->getLocation(true)."\">".htmlspecialchars($text)."</a>"; 30 } 31 if ($pageNext!=null) { 32 if ($pagePrevious) $output .= " "; 33 $text = preg_replace("/@title/i", $pageNext->get("title"), $this->yellow->language->getText("previousnextPageNext")); 34 $output .= "<a class=\"next\" href=\"".$pageNext->getLocation(true)."\">".htmlspecialchars($text)."</a>"; 35 } 36 $output .= "</p>\n"; 37 $output .="</div>\n"; 38 } 39 } 40 return $output; 41 } 42 43 // Handle page extra data 44 public function onParsePageExtra($page, $name) { 45 $output = null; 46 if ($name=="previousnext" || $name=="link") { 47 $output = $this->onParseContentElement($page, "previousnext", "", "", "block"); 48 } 49 return $output; 50 } 51 52 // Return related pages 53 public function getRelatedPages($page) { 54 switch ($page->get("layout")) { 55 case "blog": if ($this->yellow->system->get("blogStartLocation")=="auto") { 56 $pages = $page->getSiblings(); 57 } else { 58 $pages = $this->yellow->content->index(); 59 } 60 $pages->filter("layout", "blog")->sort("published", true); 61 break; 62 case "blog-start": $pages = $this->yellow->content->clean(); break; 63 default: $pages = $page->getSiblings(); 64 } 65 return $pages; 66 } 67 }