sitemap.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Grav\Plugin;
  3. use Composer\Autoload\ClassLoader;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Data;
  6. use Grav\Common\Page\Page;
  7. use Grav\Common\Plugin;
  8. use Grav\Common\Uri;
  9. use Grav\Common\Page\Pages;
  10. use Grav\Common\Utils;
  11. use Grav\Plugin\Sitemap\SitemapEntry;
  12. use RocketTheme\Toolbox\Event\Event;
  13. class SitemapPlugin extends Plugin
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $sitemap = array();
  19. /**
  20. * @return array
  21. */
  22. public static function getSubscribedEvents()
  23. {
  24. return [
  25. 'onPluginsInitialized' => [
  26. ['autoload', 100000], // TODO: Remove when plugin requires Grav >=1.7
  27. ['onPluginsInitialized', 0],
  28. ],
  29. 'onBlueprintCreated' => ['onBlueprintCreated', 0]
  30. ];
  31. }
  32. /**
  33. * Composer autoload.
  34. *is
  35. * @return ClassLoader
  36. */
  37. public function autoload(): ClassLoader
  38. {
  39. return require __DIR__ . '/vendor/autoload.php';
  40. }
  41. /**
  42. * Enable sitemap only if url matches to the configuration.
  43. */
  44. public function onPluginsInitialized()
  45. {
  46. if ($this->isAdmin()) {
  47. $this->active = false;
  48. return;
  49. }
  50. /** @var Uri $uri */
  51. $uri = $this->grav['uri'];
  52. $route = $this->config->get('plugins.sitemap.route');
  53. if ($route && $route == $uri->path()) {
  54. $this->enable([
  55. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  56. 'onPagesInitialized' => ['onPagesInitialized', 0],
  57. 'onPageInitialized' => ['onPageInitialized', 0],
  58. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  59. ]);
  60. }
  61. }
  62. /**
  63. * Generate data for the sitemap.
  64. */
  65. public function onPagesInitialized()
  66. {
  67. // get grav instance and current language
  68. $grav = Grav::instance();
  69. $current_lang = $grav['language']->getLanguage() ?: 'en';
  70. /** @var Pages $pages */
  71. $pages = $this->grav['pages'];
  72. $routes = array_unique($pages->routes());
  73. ksort($routes);
  74. $ignores = (array) $this->config->get('plugins.sitemap.ignores');
  75. $ignore_external = $this->config->get('plugins.sitemap.ignore_external');
  76. $ignore_protected = $this->config->get('plugins.sitemap.ignore_protected');
  77. foreach ($routes as $route => $path) {
  78. $page = $pages->get($path);
  79. $header = $page->header();
  80. $external_url = $ignore_external ? isset($header->external_url) : false;
  81. $protected_page = $ignore_protected ? isset($header->access) : false;
  82. $page_ignored = $protected_page || $external_url || (isset($header->sitemap['ignore']) ? $header->sitemap['ignore'] : false);
  83. $page_languages = $page->translatedLanguages();
  84. $lang_available = (empty($page_languages) || array_key_exists($current_lang, $page_languages));
  85. if ($page->published() && $page->routable() && !preg_match(sprintf("@^(%s)$@i", implode('|', $ignores)), $page->route()) && !$page_ignored && $lang_available ) {
  86. $entry = new SitemapEntry();
  87. $entry->location = $page->canonical();
  88. $entry->lastmod = date('Y-m-d', $page->modified());
  89. // optional changefreq & priority that you can set in the page header
  90. $entry->changefreq = (isset($header->sitemap['changefreq'])) ? $header->sitemap['changefreq'] : $this->config->get('plugins.sitemap.changefreq');
  91. $entry->priority = (isset($header->sitemap['priority'])) ? $header->sitemap['priority'] : $this->config->get('plugins.sitemap.priority');
  92. if (count($this->config->get('system.languages.supported', [])) > 0) {
  93. $entry->translated = $page->translatedLanguages(true);
  94. foreach($entry->translated as $lang => $page_route) {
  95. $page_route = $page->rawRoute();
  96. if ($page->home()) {
  97. $page_route = '';
  98. }
  99. $entry->translated[$lang] = $page_route;
  100. }
  101. }
  102. $this->sitemap[$route] = $entry;
  103. }
  104. }
  105. $additions = (array) $this->config->get('plugins.sitemap.additions');
  106. foreach ($additions as $addition) {
  107. if (isset($addition['location'])) {
  108. $location = Utils::url($addition['location'], true);
  109. $entry = new SitemapEntry($location,$addition['lastmod']??null,$addition['changefreq']??null, $addition['priority']??null);
  110. $this->sitemap[$location] = $entry;
  111. }
  112. }
  113. $this->grav->fireEvent('onSitemapProcessed', new Event(['sitemap' => &$this->sitemap]));
  114. }
  115. public function onPageInitialized($event)
  116. {
  117. $page = $event['page'] ?? null;
  118. $route = $this->config->get('plugins.sitemap.route');
  119. if (is_null($page) || $page->route() !== $route) {
  120. // set a dummy page
  121. $page = new Page;
  122. $page->init(new \SplFileInfo(__DIR__ . '/pages/sitemap.md'));
  123. unset($this->grav['page']);
  124. $this->grav['page'] = $page;
  125. $twig = $this->grav['twig'];
  126. $twig->template = 'sitemap.xml.twig';
  127. }
  128. }
  129. /**
  130. * Add current directory to twig lookup paths.
  131. */
  132. public function onTwigTemplatePaths()
  133. {
  134. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  135. }
  136. /**
  137. * Set needed variables to display the sitemap.
  138. */
  139. public function onTwigSiteVariables()
  140. {
  141. $twig = $this->grav['twig'];
  142. $twig->twig_vars['sitemap'] = $this->sitemap;
  143. }
  144. /**
  145. * Extend page blueprints with feed configuration options.
  146. *
  147. * @param Event $event
  148. */
  149. public function onBlueprintCreated(Event $event)
  150. {
  151. static $inEvent = false;
  152. /** @var Data\Blueprint $blueprint */
  153. $blueprint = $event['blueprint'];
  154. if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
  155. if (!in_array($blueprint->getFilename(), array_keys($this->grav['pages']->modularTypes()))) {
  156. $inEvent = true;
  157. $blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
  158. $extends = $blueprints->get('sitemap');
  159. $blueprint->extend($extends, true);
  160. $inEvent = false;
  161. }
  162. }
  163. }
  164. }