sitemap.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Data;
  4. use Grav\Common\Page\Page;
  5. use Grav\Common\Plugin;
  6. use Grav\Common\Uri;
  7. use Grav\Common\Page\Pages;
  8. use RocketTheme\Toolbox\Event\Event;
  9. class SitemapPlugin extends Plugin
  10. {
  11. /**
  12. * @var array
  13. */
  14. protected $sitemap = array();
  15. /**
  16. * @return array
  17. */
  18. public static function getSubscribedEvents()
  19. {
  20. return [
  21. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  22. 'onBlueprintCreated' => ['onBlueprintCreated', 0]
  23. ];
  24. }
  25. /**
  26. * Enable sitemap only if url matches to the configuration.
  27. */
  28. public function onPluginsInitialized()
  29. {
  30. if ($this->isAdmin()) {
  31. $this->active = false;
  32. return;
  33. }
  34. /** @var Uri $uri */
  35. $uri = $this->grav['uri'];
  36. $route = $this->config->get('plugins.sitemap.route');
  37. if ($route && $route == $uri->path()) {
  38. $this->enable([
  39. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  40. 'onPagesInitialized' => ['onPagesInitialized', 0],
  41. 'onPageInitialized' => ['onPageInitialized', 0],
  42. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  43. ]);
  44. }
  45. }
  46. /**
  47. * Generate data for the sitemap.
  48. */
  49. public function onPagesInitialized()
  50. {
  51. require_once __DIR__ . '/classes/sitemapentry.php';
  52. /** @var Pages $pages */
  53. $pages = $this->grav['pages'];
  54. $routes = array_unique($pages->routes());
  55. ksort($routes);
  56. $ignores = (array) $this->config->get('plugins.sitemap.ignores');
  57. foreach ($routes as $route => $path) {
  58. $page = $pages->get($path);
  59. $header = $page->header();
  60. $page_ignored = isset($header->sitemap['ignore']) ? $header->sitemap['ignore'] : false;
  61. if ($page->published() && $page->routable() && !preg_match(sprintf("@^(%s)$@i", implode('|', $ignores)), $page->route()) && !$page_ignored) {
  62. $entry = new SitemapEntry();
  63. $entry->location = $page->canonical();
  64. $entry->lastmod = date('Y-m-d', $page->modified());
  65. // optional changefreq & priority that you can set in the page header
  66. $entry->changefreq = (isset($header->sitemap['changefreq'])) ? $header->sitemap['changefreq'] : $this->config->get('plugins.sitemap.changefreq');
  67. $entry->priority = (isset($header->sitemap['priority'])) ? $header->sitemap['priority'] : $this->config->get('plugins.sitemap.priority');
  68. if (count($this->config->get('system.languages.supported', [])) > 0) {
  69. $entry->translated = $page->translatedLanguages(true);
  70. foreach($entry->translated as $lang => $page_route) {
  71. $page_route = $page->rawRoute();
  72. if ($page->home()) {
  73. $page_route = '/';
  74. }
  75. $entry->translated[$lang] = $page_route;
  76. }
  77. }
  78. $this->sitemap[$route] = $entry;
  79. }
  80. }
  81. $rootUrl = $this->grav['uri']->rootUrl(true) . $pages->base();
  82. $additions = (array) $this->config->get('plugins.sitemap.additions');
  83. foreach ($additions as $addition) {
  84. $entry = new SitemapEntry();
  85. $entry->location = $rootUrl . $addition['location'];
  86. $entry->lastmod = $addition['lastmod'];
  87. $this->sitemap[] = $entry;
  88. }
  89. }
  90. public function onPageInitialized()
  91. {
  92. // set a dummy page
  93. $page = new Page;
  94. $page->init(new \SplFileInfo(__DIR__ . '/pages/sitemap.md'));
  95. unset($this->grav['page']);
  96. $this->grav['page'] = $page;
  97. }
  98. /**
  99. * Add current directory to twig lookup paths.
  100. */
  101. public function onTwigTemplatePaths()
  102. {
  103. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  104. }
  105. /**
  106. * Set needed variables to display the sitemap.
  107. */
  108. public function onTwigSiteVariables()
  109. {
  110. $twig = $this->grav['twig'];
  111. $twig->template = 'sitemap.xml.twig';
  112. $twig->twig_vars['sitemap'] = $this->sitemap;
  113. }
  114. /**
  115. * Extend page blueprints with feed configuration options.
  116. *
  117. * @param Event $event
  118. */
  119. public function onBlueprintCreated(Event $event)
  120. {
  121. static $inEvent = false;
  122. /** @var Data\Blueprint $blueprint */
  123. $blueprint = $event['blueprint'];
  124. if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
  125. $inEvent = true;
  126. $blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
  127. $extends = $blueprints->get('sitemap');
  128. $blueprint->extend($extends, true);
  129. $inEvent = false;
  130. }
  131. }
  132. }