Skip to content

Commit

Permalink
Merge branch 'release/4.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Sep 22, 2023
2 parents 82ae2d9 + c7bfe6f commit d8df127
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# v4.0.0
## 09/22/2023

1. [](#new)
* Added support for Google News tags [#93](https://github.com/getgrav/grav-plugin-sitemap/pull/93)
* Enhanced Google News to allow restriction to paths and configurable max-age in days
1. [](#improved)
* Added YAML syntax highlighting in README.md [#101](https://github.com/getgrav/grav-plugin-sitemap/pull/101)

# v3.0.2
## 06/14/2022

Expand Down
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Grav Sitemap Plugin

`Sitemap` is a [Grav](http://github.com/getgrav/grav) Plugin that generates a [map of your pages](http://en.wikipedia.org/wiki/Site_map) in `XML` format that is easily understandable and indexable by Search engines.
`Sitemap` is a [Grav](https://github.com/getgrav/grav) Plugin that generates a [map of your pages](https://en.wikipedia.org/wiki/Site_map) in `XML` format that is easily understandable and indexable by Search engines.

# Installation

Installing the Sitemap plugin can be done in one of two ways. Our GPM (Grav Package Manager) installation method enables you to quickly and easily install the plugin with a simple terminal command, while the manual method enables you to do so via a zip file.

## GPM Installation (Preferred)

The simplest way to install this plugin is via the [Grav Package Manager (GPM)](http://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:
The simplest way to install this plugin is via the [Grav Package Manager (GPM)](https://learn.getgrav.org/advanced/grav-gpm) through your system's Terminal (also called the command line). From the root of your Grav install type:

bin/gpm install sitemap

This will install the Sitemap plugin into your `/user/plugins` directory within Grav. Its files can be found under `/your/site/grav/user/plugins/sitemap`.

## Manual Installation

To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `sitemap`. You can find these files either on [GitHub](https://github.com/getgrav/grav-plugin-sitemap) or via [GetGrav.org](http://getgrav.org/downloads/plugins#extras).
To install this plugin, just download the zip version of this repository and unzip it under `/your/site/grav/user/plugins`. Then, rename the folder to `sitemap`. You can find these files either on [GitHub](https://github.com/getgrav/grav-plugin-sitemap) or via [GetGrav.org](https://getgrav.org/downloads/plugins#extras).

You should now have all the plugin files under

/your/site/grav/user/plugins/sitemap

>> NOTE: This plugin is a modular component for Grav which requires [Grav](http://github.com/getgrav/grav), the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) plugins, and a theme to be installed in order to operate.
>> NOTE: This plugin is a modular component for Grav which requires [Grav](https://github.com/getgrav/grav), the [Error](https://github.com/getgrav/grav-plugin-error) and [Problems](https://github.com/getgrav/grav-plugin-problems) plugins, and a theme to be installed in order to operate.

# Usage
Expand All @@ -31,7 +31,7 @@ The `sitemap` plugin works out of the box. You can just go directly to `http://y

## Config Defaults

```
```yaml
enabled: true
route: '/sitemap'
ignore_external: true
Expand All @@ -41,9 +41,14 @@ ignores:
- /blog/blog-post-to-ignore
- /ignore-this-route
- /ignore-children-of-this-route/.*
include_news_tags: false
news_max_age_days: 2
news_enabled_paths:
- /blog
whitelist:
html_support: false
urlset: 'http://www.sitemaps.org/schemas/sitemap/0.9'
urlnewsset: 'http://www.google.com/schemas/sitemap-news/0.9'
short_date_format: true
include_changefreq: true
changefreq: daily
Expand All @@ -64,7 +69,7 @@ additions:
You can ignore your own pages by providing a list of routes to ignore. You can also use a page's Frontmatter to signal that the sitemap should ignore it:
```
```yaml
sitemap:
ignore: true
```
Expand All @@ -75,11 +80,17 @@ The latest Sitemap `v3.0` includes all new multi-language support utilizing the

This is handled automatically based on your Grav multi-language System configuration.

### News Support

New in version 4.0 of the plugin is support for Google's [**News Sitemap Extension**](https://developers.google.com/search/docs/crawling-indexing/sitemaps/news-sitemap) that uses a specific tags under a `<news:news></news:news>` tag to provide Google News specific data. When enabled, the news extensions will be enabled when an item is in one of the configured news paths (`/` by default, so all), and if the published date is not older than the configured `max age` (default of 2 per Googles recommendations).

The output of the news tags is controlled by an overridable `sitemap-extensions/news.html.twig` template

## Images

You can add images to the sitemap by adding an entry in the page's Frontmatter.

```
```yaml
sitemap:
images:
your_image:
Expand Down Expand Up @@ -108,7 +119,7 @@ You can copy and extend this Twig template in your theme to customize it for you

You can manually add URLs to the sitemap using the Admin settings, or by adding entries to your `sitemap.yaml` with this format:

```
```yaml
additions:
-
location: /something-special
Expand All @@ -122,7 +133,7 @@ Note that Regex support is available: Just append `.*` to a path to ignore all o

If you have some dynamic content being added to your site via another plugin, or perhaps a 3rd party API, you can now add them dynamically to the sitemap with a simple event:

Make sure you are subscribed to the `` event then add simply add your entry to the sitemap like this:
Make sure you are subscribed to the `onSitemapProcessed` event then add simply add your entry to the sitemap like this:

```php
public function onSitemapProcessed(\RocketTheme\Toolbox\Event\Event $e)
Expand Down
35 changes: 34 additions & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Sitemap
type: plugin
slug: sitemap
version: 3.0.2
version: 4.0.0
description: "Provide automatically generated **XML sitemaps** with this very useful, but simple to configure, Grav plugin."
icon: map-marker
author:
Expand Down Expand Up @@ -110,6 +110,39 @@ form:
label: PLUGIN_SITEMAP.URLSET
help: PLUGIN_SITEMAP.URLSET_HELP

include_news_tags:
type: toggle
label: PLUGIN_SITEMAP.INCLUDE_NEWS_TAGS
help: PLUGIN_SITEMAP.INCLUDE_NEWS_TAGS_HELP
highlight: 1
default: 0
options:
1: PLUGIN_ADMIN.ENABLED
0: PLUGIN_ADMIN.DISABLED
validate:
type: bool

urlnewsset:
type: text
default: 'http://www.google.com/schemas/sitemap-news/0.9'
label: PLUGIN_SITEMAP.URLNEWSSET
help: PLUGIN_SITEMAP.URLNEWSSET_HELP

news_max_age_days:
type: number
default: 2
size: x-small
label: PLUGIN_SITEMAP.NEWS_MAX_AGE_DAYS
append: Days
validate:
type: int

news_enabled_paths:
type: array
label: PLUGIN_SITEMAP.NEWS_TAG_PATHS
value_only: true
placeholder_value: '/news'

short_date_format:
type: toggle
label: PLUGIN_ADMIN.SHORT_DATE_FORMAT
Expand Down
44 changes: 44 additions & 0 deletions classes/SitemapEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class SitemapEntry
public $images;
public $hreflangs = [];

public int $timestamp;
public string $rawroute;
public string $longdate;
public string $shortdate;

/**
* SitemapEntry constructor.
*
Expand Down Expand Up @@ -258,5 +263,44 @@ public function setHreflangs(array $hreflangs): SitemapEntry
return $this;
}

public function getTimestamp(): int
{
return $this->timestamp;
}

public function setTimestamp(int $timestamp): void
{
$this->timestamp = $timestamp;
}

public function getRawroute(): string
{
return $this->rawroute;
}

public function setRawroute(string $rawroute): void
{
$this->rawroute = $rawroute;
}

public function getLongdate(): string
{
return $this->longdate;
}

public function setLongdate(string $longdate): void
{
$this->longdate = $longdate;
}

public function getShortdate(): string
{
return $this->shortdate;
}

public function setShortdate(string $shortdate): void
{
$this->shortdate = $shortdate;
}

}
5 changes: 5 additions & 0 deletions languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ en:
IGNORE_REDIRECT_HELP: 'Ignores pages that have a custom "redirect" entry in the header'
URLSET: 'URLSet'
URLSET_HELP: 'The URLSet XML Namespace, don''t change this!'
URLNEWSSET: 'URLNewsSet'
URLNEWSSET_HELP: 'The URLNewsSet XML Namespace, don''t change this!'
INCLUDE_NEWS_TAGS: 'Include News Tags'
NEWS_MAX_AGE_DAYS: 'News Max Age (Days)'
NEWS_TAG_PATHS: 'News Tag Paths'
MULTILANG_ENABLED: 'Multi-Lang Features'
MULTILANG_ENABLED_HELP: 'Enables support for multilanguage features'
INCLUDE_CHANGEFREQ: 'Include Change Frequency'
Expand Down
14 changes: 14 additions & 0 deletions sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ public function onTwigInitialized()
$this->grav['twig']->twig()->addFunction(
new TwigFunction('sort_sitemap_entries_by_language', [$this, 'sortSitemapEntriesByLanguage'])
);
$this->grav['twig']->twig()->addFunction(
new TwigFunction('timestamp_within_days', [$this, 'timestampWithinDays'])
);
}

/**
Expand Down Expand Up @@ -255,6 +258,13 @@ public function sortSitemapEntriesByLanguage()
return $entries;
}

public function timestampWithinDays(int $timestamp, int $days): bool
{
$now = time();
$days_ago = $now - ($days * 24 * 60 * 60);
return $timestamp >= $days_ago;
}

protected function addRouteData($pages, $lang)
{
$routes = array_unique($pages->routes());
Expand Down Expand Up @@ -285,6 +295,10 @@ protected function addRouteData($pages, $lang)
'translated' => in_array($lang, $page_languages),
'location' => $location,
'lastmod' => date($this->datetime_format, $page->modified()),
'longdate' => date('Y-m-d\TH:i:sP', $page->date()),
'shortdate' => date('Y-m-d', $page->date()),
'timestamp' => $page->date(),
'rawroute' => $page->rawRoute(),
];

if ($this->include_change_freq) {
Expand Down
5 changes: 5 additions & 0 deletions sitemap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ route: '/sitemap'
ignore_external: true
ignore_protected: true
ignore_redirect: true
include_news_tags: false
news_max_age_days: 2
news_enabled_paths:
- /blog
ignores:
whitelist:
html_support: false
urlset: 'http://www.sitemaps.org/schemas/sitemap/0.9'
urlnewsset: 'http://www.google.com/schemas/sitemap-news/0.9'
short_date_format: true
include_changefreq: true
changefreq: daily
Expand Down
8 changes: 8 additions & 0 deletions templates/sitemap-extensions/news.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<news:news>
<news:publication>
<news:name>{{ site.title }}</news:name>
<news:language>{{ entry.lang }}</news:language>
</news:publication>
<news:publication_date>{{ entry.shortdate }}</news:publication_date>
<news:title>{{ entry.title }}</news:title>
</news:news>
9 changes: 9 additions & 0 deletions templates/sitemap.xml.twig
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="{{ uri.rootUrl }}/user/plugins/sitemap/sitemap.xsl"?>
<urlset xmlns="{{ config.plugins.sitemap.urlset }}"
{% if config.plugins.sitemap.include_news_tags %}
xmlns:news="{{ config.plugins.sitemap.urlnewsset }}"
{% endif %}
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
{% for entry in sitemap %}
<url>
<loc>{{ entry.location|e }}</loc>
{% if config.plugins.sitemap.include_news_tags and
timestamp_within_days(entry.timestamp, config.plugin.sitemap.news_max_age_days|default(2)) and
entry.rawroute|starts_with(config.plugins.sitemap.news_enabled_paths)
%}
{% include 'sitemap-extensions/news.html.twig' %}
{% endif %}
{% for hreflang in entry.hreflangs %}
<xhtml:link rel="alternate" hreflang="{{ hreflang.hreflang }}" href="{{ hreflang.href }}" />
{% endfor %}
Expand Down

0 comments on commit d8df127

Please sign in to comment.