Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
werner-freytag committed May 17, 2022
2 parents a2b9d01 + 6b76048 commit 1732640
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor
/.php-cs-fixer.cache
38 changes: 30 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Sitemap Addon for Statamic

![Statamic 3.0+](https://img.shields.io/badge/Statamic-3.0+-FF269E?style=for-the-badge&link=https://statamic.com)

Automatically adds a sitemap to your Statamic v3 web site. The default path is <your web site>/sitemap.xml

## Installation
Expand Down Expand Up @@ -38,14 +40,34 @@ If you prefer another view engine, it is also possible. For example to use Antle

An array of sitemap **entries** is passed to the view template. Each **entry** has these properties:

* **path**: The page path (without host name)
* **loc**: The absolute url
* **lastmod**: A date object of the last modification date
* **changefreq**: The entry property `change_frequency`, if available
* **priority**: The entry property `property`, if available

The missing values can be overriden in the template.
* **loc**: The absolute url
* **path**: The relative path
* **lastmod**: A `DateTime` object of the last modification date
* **changefreq**: A string like 'daily', 'weekly' (optional)
* **priority**: A string presenting a float value between 0 and 1 (optional)

### Dynamically adding entries (optional)

You may add entries dynamically by providing a closure that returns an array to the `addEntries` method.

```php
use Pecotamic\Sitemap\Sitemap;
use Pecotamic\Sitemap\SitemapEntry;

class AppServiceProvider extends Provider
{
public function boot()
{
Sitemap::addEntries(static function () {
return [
new SitemapEntry('/hidden-page', new \DateTime('2020-02-20')),
new SitemapEntry('/about-me', new \DateTime('now'), 'daily', '1.0'),
];
});
}
}
```

## Credits

Thanks for code contribution to Prageeth Silva.
Thanks for code contribution to [Prageeth Silva](/prageeth) and [Poh Nean](/pohnean).
58 changes: 47 additions & 11 deletions src/Models/Sitemap.php → src/Generator.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
<?php

namespace Pecotamic\Sitemap\Models;
namespace Pecotamic\Sitemap;

use Illuminate\Support\Facades\Facade;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;
use Statamic\Facades\Taxonomy;
use Statamic\Fields\Value;

class Sitemap
class Generator extends Facade
{
protected $extraEntries;

public function __construct()
{
$this->extraEntries = collect();
}

/**
* @return SitemapEntry[]|array
*/
public static function entries(): array
public function entries(): array
{
$sitemap = new static;

$entries = collect();

// collect
if (config('pecotamic.sitemap.include_entries', true)) {
$entries = $entries->merge($sitemap->publishedEntries(config('pecotamic.sitemap.entry_types')));
$entries = $entries->merge($this->publishedEntries(config('pecotamic.sitemap.entry_types')));
}
if (config('pecotamic.sitemap.include_terms', true)) {
$entries = $entries->merge($sitemap->publishedTerms());
$entries = $entries->merge($this->publishedTerms());
}
if (config('pecotamic.sitemap.include_collection_terms', true)) {
$entries = $entries->merge($sitemap->publishedCollectionTerms());
$entries = $entries->merge($this->publishedCollectionTerms());
}

// filter by current site
Expand All @@ -42,7 +48,8 @@ public static function entries(): array
$entries = $entries->filter($callback);
}

return $entries
// find entries
$entries = $entries
->map(function ($entry) {
$properties = self::sitemapProperties($entry);

Expand All @@ -56,13 +63,33 @@ public static function entries(): array

return new SitemapEntry($properties['loc'], $properties['lastmod'], $properties['changefreq'], $properties['priority']);
})
->values()
->sortBy(function (SitemapEntry $entry) {
->values();

// add extra sitemap entries
$extraEntries = $this->extraEntries
->flatMap(static function ($closure) {
return $closure();
})
->map(static function (SitemapEntry $entry) {
$entry->loc = self::absoluteUrl($entry->loc);
return $entry;
});

return $entries
->merge($extraEntries)
->sortBy(static function (SitemapEntry $entry) {
return substr_count(rtrim($entry->path, '/'), '/');
})
->toArray();
}

public function addEntries($closure): self
{
$this->extraEntries[] = $closure;

return $this;
}

protected function publishedEntries(?array $entryTypes = null): \Illuminate\Support\Collection
{
return Collection::all()
Expand All @@ -86,6 +113,15 @@ protected static function isAbsoluteUrl(string $url): bool
return preg_match('#^https?://#', $url);
}

protected static function absoluteUrl(string $url): string
{
if (self::isAbsoluteUrl($url)) {
return $url;
}

return Site::current()->absoluteUrl() . $url;
}

protected function publishedTerms()
{
return Taxonomy::all()
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Controllers/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\View\View;
use Pecotamic\Sitemap\Models\Sitemap;
use Pecotamic\Sitemap\Models\SitemapEntry;
use Pecotamic\Sitemap\Sitemap;
use Pecotamic\Sitemap\SitemapEntry;
use Statamic\Facades\Site;
use Statamic\Support\Str;
use Statamic\View\Antlers\Engine as AntlersEngine;
Expand Down
13 changes: 13 additions & 0 deletions src/Sitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Pecotamic\Sitemap;

use Illuminate\Support\Facades\Facade;

class Sitemap extends Facade
{
protected static function getFacadeAccessor()
{
return Generator::class;
}
}
2 changes: 1 addition & 1 deletion src/Models/SitemapEntry.php → src/SitemapEntry.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Pecotamic\Sitemap\Models;
namespace Pecotamic\Sitemap;

class SitemapEntry
{
Expand Down

0 comments on commit 1732640

Please sign in to comment.