Skip to content

Commit

Permalink
Sitemap 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Jul 9, 2020
1 parent facb250 commit fa4f929
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 16 deletions.
39 changes: 39 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
# v2.0.0, 2020-07-03

### Features

* **routes:** add ability to set custom routes
```yaml
route: sitemap.xml
```
* **core:** add ability to set ignore list
```yaml
ignore:
- blog/blog-post-to-ignore
- ignore-this-entry
```
* **core:** add ability to set additions list
```yaml
additions:
-
loc: something-special
lastmod: '2020-04-16'
changefreq: hourly
priority: 0.3
```
* **core:** add ability to set default values for entries
```yaml
default:
changefreq: daily
priority: !!float 1
```
* **core:** add new event `onSitemapAfterInitialized`
* **core:** add sitemap.xsl template
* **core:** improved and refactored sitemap.html template

# v1.8.0, 2020-05-15
* Updates for Flextype 0.9.8

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ sitemap:
ignore: true
```
### Set default values entry(entries)
### Set default values for entry(entries)
```yaml
sitemap:
Expand Down
72 changes: 67 additions & 5 deletions app/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Flextype;

use Flextype\Component\Arr\Arr;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

Expand All @@ -11,6 +12,14 @@
*/
class SitemapController extends Container {

/**
* Current $sitemap data array
*
* @var array
* @access public
*/
public $sitemap = [];

/**
* Index page
*
Expand All @@ -21,23 +30,76 @@ class SitemapController extends Container {
public function index(Request $request, Response $response) : Response
{
$sitemap = [];
$entries = $this->entries->fetch('', ['recursive' => true, 'order_by' => ['field' => 'published_at', 'direction' => 'desc']]);
$entries = $this->entries->fetch('', ['recursive' => true,
'order_by' => ['field' => 'modified_at',
'direction' => 'desc']]);

foreach ($entries as $entry) {

if (!((isset($entry['visibility']) && ($entry['visibility'] === 'draft' || $entry['visibility'] === 'hidden')) ||
(isset($entry['routable']) && ($entry['routable'] === false)))) {
$sitemap[] = $entry;
// Check entry visibility field
if (isset($entry['visibility']) && ($entry['visibility'] === 'draft' || $entry['visibility'] === 'hidden')) {
continue;
}

// Check entry routable field
if (isset($entry['routable']) && ($entry['routable'] === false)) {
continue;
}

// Check entry sitemap.ignore field
if (isset($entry['sitemap']['ignore']) && ($entry['sitemap']['ignore'] === true)) {
continue;
}

// Check entry changefreq field
if (isset($entry['sitemap']['changefreq'])) {
$entry['changefreq'] = $entry['sitemap']['changefreq'];
} else {
$entry['changefreq'] = $this->registry->get('plugins.sitemap.settings.default.changefreq');
}

// Check entry priority field
if (isset($entry['sitemap']['priority'])) {
$entry['priority'] = $entry['sitemap']['priority'];
} else {
$entry['priority'] = $this->registry->get('plugins.sitemap.settings.default.priority');
}

// Check ignore list
if (in_array($entry['slug'], (array) $this->registry->get('plugins.sitemap.settings.ignore'))) {
continue;
}

// Prepare data
$entry_to_add['loc'] = $entry['slug'];
$entry_to_add['lastmod'] = $entry['modified_at'];
$entry_to_add['changefreq'] = $entry['changefreq'];
$entry_to_add['priority'] = $entry['priority'];

// Add entry to sitemap
$sitemap[] = $entry_to_add;
}

// Additions
$additions = (array) $this->registry->get('plugins.sitemap.settings.additions');
foreach ($additions as $addition) {
$sitemap[] = $addition;
}

// Set entry to the SitemapController class property $sitemap
$this->sitemap = $sitemap;

// Run event onSitemapAfterInitialized
$this->emitter->emit('onSitemapAfterInitialized');

// Set response header
$response = $response->withHeader('Content-Type', 'application/xml');

return $this->twig->render(
$response,
'plugins/sitemap/templates/index.html',
[
'sitemap' => $sitemap
'sitemap' => $this->sitemap
]);
}
}
6 changes: 5 additions & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
name: Sitemap
version: 1.8.0
version: 2.0.0
description: Provide automatically generated XML sitemap.
author:
name: Sergey Romanenko
email: [email protected]
url: https://flextype.org
homepage: https://github.com/flextype-plugins/sitemap
documentation: https://github.com/flextype-plugins/sitemap
changelog: https://github.com/flextype-plugins/sitemap/blob/master/CHANGELOG.md
bugs: https://github.com/flextype-plugins/sitemap/issues
icon: fas fa-map-marked-alt
keywords: sitemap, plugin, xml, map, index
license: MIT

dependencies:
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace Flextype;

$app->get('/sitemap.xml', 'SitemapController:index')->setName('sitemap.index');
$app->get('/' . $flextype->registry->get('plugins.sitemap.settings.route'), 'SitemapController:index')->setName('sitemap.index');
18 changes: 18 additions & 0 deletions settings.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
# enabled: true or false to disable the plugin
enabled: true

# Sitemap plugin priority
priority: 95

# Route
route: sitemap.xml

# Ignore list
ignore:

# Addition list
additions:

# Default values for entries
default:
changefreq: daily
priority: !!float 1
23 changes: 15 additions & 8 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in sitemap %}
<url>
<loc>{{ base_url() }}/{{ page.slug }}</loc>
<lastmod>{{ page.date }}</lastmod>
<changefreq>{% if page.changefreq %}{{ page.changefreq }}{% else %}1.0{% endif %}</changefreq>
<priority>1.0</priority>
</url>
<?xml-stylesheet type="text/xsl" href="{{ url() }}/project/plugins/sitemap/templates/sitemap.xsl"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
{% for entry in sitemap %}
<url>
<loc>{{ url() }}/{{ entry.loc|e }}</loc>
{% if entry.lastmod %}
<lastmod>{{ entry.lastmod|date(registry.get('flextype.settings.date_display_format')) }}</lastmod>
{% endif %}
{% if entry.changefreq %}
<changefreq>{{ entry.changefreq }}</changefreq>
{% endif %}
{% if entry.priority %}
<priority>{{ entry.priority|number_format(1) }}</priority>
{% endif %}
</url>
{% endfor %}
</urlset>
122 changes: 122 additions & 0 deletions templates/sitemap.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:s="http://www.sitemaps.org/schemas/sitemap/0.9"
exclude-result-prefixes="s"
>
<xsl:template match="/">
<html>
<head>
<meta name="robots" content="noindex" />
<title>
XML Sitemap
</title>
<style type="text/css">
@import url('//cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css');
@import url('//fonts.googleapis.com/css?family=Roboto:400');
html {
font-smooth: auto;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
-webkit-text-size-adjust: 100%;
background-color: #fff
}

body {
font-family: 'Roboto', sans-serif;
font-weight: 400;
font-size: 20px;
line-height: 1.8em;
letter-spacing: 0;
text-align: left;
color: #333;
}

body {
overflow: auto;
padding: 20px
}

a,
a:link,
a:visited {
text-decoration: none;
border-bottom: dotted 1px #333;
color: #333;
}

.table {
margin: 0 auto;
}

.table th {
border: solid 1px #cbcbcb !important;
text-align: center;
background: #fff;
}

.table {
border-collapse: collapse;
border-spacing: 0;
empty-cells: show;
border: 1px solid #cbcbcb;
}

.table caption {
color: #000;
padding: 1em 0;
text-align: center;
}

.table td,
.table th {
border: 1px solid #cbcbcb;
border-width: 1px;
font-size: inherit;
margin: 0;
overflow: visible;
padding: 0.5em 1em;
}

.table thead {
background-color: #e0e0e0;
color: #000;
text-align: left;
vertical-align: bottom;
}
</style>
</head>
<body>
<table class="table" border="0">
<thead>
<tr>
<th colspan="5">Sitemap</th>
</tr>
<tr>
<th></th>
<th>Location</th>
<th>Last Modified</th>
<th>Update Frequency</th>
<th>Priority</th>
</tr>
</thead>
<tfoot>
</tfoot>
<tbody>
<xsl:for-each select="s:urlset/s:url">
<xsl:sort select="s:loc" />
<tr>
<xsl:variable name="loc"><xsl:value-of select="s:loc"/></xsl:variable>
<xsl:variable name="pno"><xsl:value-of select="position()"/></xsl:variable>
<td><xsl:value-of select="$pno"/></td>
<td><a href="{$loc}"><xsl:value-of select="s:loc"/></a></td>
<td><xsl:value-of select="s:lastmod"/></td>
<td><xsl:value-of select="s:changefreq"/></td>
<td><xsl:value-of select="s:priority"/></td>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

0 comments on commit fa4f929

Please sign in to comment.