merge master in prod
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
<?php
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Composer\Autoload\ClassLoader;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Data;
|
||||
use Grav\Common\Page\Page;
|
||||
use Grav\Common\Plugin;
|
||||
use Grav\Common\Uri;
|
||||
use Grav\Common\Page\Pages;
|
||||
use Grav\Common\Utils;
|
||||
use Grav\Plugin\Sitemap\SitemapEntry;
|
||||
use RocketTheme\Toolbox\Event\Event;
|
||||
|
||||
class SitemapPlugin extends Plugin
|
||||
@@ -21,11 +25,24 @@ class SitemapPlugin extends Plugin
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 0],
|
||||
'onPluginsInitialized' => [
|
||||
['autoload', 100000], // TODO: Remove when plugin requires Grav >=1.7
|
||||
['onPluginsInitialized', 0],
|
||||
],
|
||||
'onBlueprintCreated' => ['onBlueprintCreated', 0]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Composer autoload.
|
||||
*is
|
||||
* @return ClassLoader
|
||||
*/
|
||||
public function autoload(): ClassLoader
|
||||
{
|
||||
return require __DIR__ . '/vendor/autoload.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable sitemap only if url matches to the configuration.
|
||||
*/
|
||||
@@ -56,7 +73,9 @@ class SitemapPlugin extends Plugin
|
||||
*/
|
||||
public function onPagesInitialized()
|
||||
{
|
||||
require_once __DIR__ . '/classes/sitemapentry.php';
|
||||
// get grav instance and current language
|
||||
$grav = Grav::instance();
|
||||
$current_lang = $grav['language']->getLanguage() ?: 'en';
|
||||
|
||||
/** @var Pages $pages */
|
||||
$pages = $this->grav['pages'];
|
||||
@@ -64,13 +83,21 @@ class SitemapPlugin extends Plugin
|
||||
ksort($routes);
|
||||
|
||||
$ignores = (array) $this->config->get('plugins.sitemap.ignores');
|
||||
$ignore_external = $this->config->get('plugins.sitemap.ignore_external');
|
||||
$ignore_protected = $this->config->get('plugins.sitemap.ignore_protected');
|
||||
|
||||
foreach ($routes as $route => $path) {
|
||||
$page = $pages->get($path);
|
||||
$header = $page->header();
|
||||
$page_ignored = isset($header->sitemap['ignore']) ? $header->sitemap['ignore'] : false;
|
||||
$external_url = $ignore_external ? isset($header->external_url) : false;
|
||||
$protected_page = $ignore_protected ? isset($header->access) : false;
|
||||
$page_ignored = $protected_page || $external_url || (isset($header->sitemap['ignore']) ? $header->sitemap['ignore'] : false);
|
||||
$page_languages = $page->translatedLanguages();
|
||||
$lang_available = (empty($page_languages) || array_key_exists($current_lang, $page_languages));
|
||||
|
||||
if ($page->published() && $page->routable() && !preg_match(sprintf("@^(%s)$@i", implode('|', $ignores)), $page->route()) && !$page_ignored) {
|
||||
|
||||
if ($page->published() && $page->routable() && !preg_match(sprintf("@^(%s)$@i", implode('|', $ignores)), $page->route()) && !$page_ignored && $lang_available ) {
|
||||
|
||||
$entry = new SitemapEntry();
|
||||
$entry->location = $page->canonical();
|
||||
$entry->lastmod = date('Y-m-d', $page->modified());
|
||||
@@ -85,7 +112,7 @@ class SitemapPlugin extends Plugin
|
||||
foreach($entry->translated as $lang => $page_route) {
|
||||
$page_route = $page->rawRoute();
|
||||
if ($page->home()) {
|
||||
$page_route = '/';
|
||||
$page_route = '';
|
||||
}
|
||||
|
||||
$entry->translated[$lang] = $page_route;
|
||||
@@ -96,26 +123,33 @@ class SitemapPlugin extends Plugin
|
||||
}
|
||||
}
|
||||
|
||||
$rootUrl = $this->grav['uri']->rootUrl(true) . $pages->base();
|
||||
$additions = (array) $this->config->get('plugins.sitemap.additions');
|
||||
|
||||
foreach ($additions as $addition) {
|
||||
$entry = new SitemapEntry();
|
||||
$entry->location = $rootUrl . $addition['location'];
|
||||
$entry->lastmod = $addition['lastmod'];
|
||||
|
||||
$this->sitemap[] = $entry;
|
||||
if (isset($addition['location'])) {
|
||||
$location = Utils::url($addition['location'], true);
|
||||
$entry = new SitemapEntry($location,$addition['lastmod']??null,$addition['changefreq']??null, $addition['priority']??null);
|
||||
$this->sitemap[$location] = $entry;
|
||||
}
|
||||
}
|
||||
|
||||
$this->grav->fireEvent('onSitemapProcessed', new Event(['sitemap' => &$this->sitemap]));
|
||||
}
|
||||
|
||||
public function onPageInitialized()
|
||||
public function onPageInitialized($event)
|
||||
{
|
||||
// set a dummy page
|
||||
$page = new Page;
|
||||
$page->init(new \SplFileInfo(__DIR__ . '/pages/sitemap.md'));
|
||||
$page = $event['page'] ?? null;
|
||||
$route = $this->config->get('plugins.sitemap.route');
|
||||
|
||||
unset($this->grav['page']);
|
||||
$this->grav['page'] = $page;
|
||||
if (is_null($page) || $page->route() !== $route) {
|
||||
// set a dummy page
|
||||
$page = new Page;
|
||||
$page->init(new \SplFileInfo(__DIR__ . '/pages/sitemap.md'));
|
||||
unset($this->grav['page']);
|
||||
$this->grav['page'] = $page;
|
||||
|
||||
$twig = $this->grav['twig'];
|
||||
$twig->template = 'sitemap.xml.twig';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,7 +166,6 @@ class SitemapPlugin extends Plugin
|
||||
public function onTwigSiteVariables()
|
||||
{
|
||||
$twig = $this->grav['twig'];
|
||||
$twig->template = 'sitemap.xml.twig';
|
||||
$twig->twig_vars['sitemap'] = $this->sitemap;
|
||||
}
|
||||
|
||||
@@ -148,11 +181,13 @@ class SitemapPlugin extends Plugin
|
||||
/** @var Data\Blueprint $blueprint */
|
||||
$blueprint = $event['blueprint'];
|
||||
if (!$inEvent && $blueprint->get('form/fields/tabs', null, '/')) {
|
||||
$inEvent = true;
|
||||
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
|
||||
$extends = $blueprints->get('sitemap');
|
||||
$blueprint->extend($extends, true);
|
||||
$inEvent = false;
|
||||
if (!in_array($blueprint->getFilename(), array_keys($this->grav['pages']->modularTypes()))) {
|
||||
$inEvent = true;
|
||||
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
|
||||
$extends = $blueprints->get('sitemap');
|
||||
$blueprint->extend($extends, true);
|
||||
$inEvent = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user