Skip to content

Commit

Permalink
Better approach
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Jul 19, 2024
1 parent 358e927 commit 17af81e
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 42 deletions.
2 changes: 0 additions & 2 deletions src/Contracts/SitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ public function priority(): string|self|null;
public function site(): string|self;

public function isCanonicalUrl(): bool;

public function toArray(): ?array;
}
11 changes: 5 additions & 6 deletions src/Http/Controllers/Web/SitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ public function index(): Response

$sitemaps = Cache::remember('advanced-seo::sitemaps::index', Sitemap::cacheExpiry(), function () {
return Sitemap::all()
->filter(fn ($sitemap) => $sitemap->urls()->isNotEmpty())
->toArray();
->filter(fn ($sitemap) => $sitemap->urls()->isNotEmpty());
});

throw_unless($sitemaps, new NotFoundHttpException);
throw_if($sitemaps->isEmpty(), new NotFoundHttpException);

return response()
->view('advanced-seo::sitemaps.index', [
'sitemaps' => $sitemaps,
'sitemaps' => $sitemaps->toArray(),
'version' => Addon::get('aerni/advanced-seo')->version(),
])
->header('Content-Type', 'text/xml')
Expand All @@ -44,11 +43,11 @@ public function show(string $type, string $handle): Response
fn () => Sitemap::find($id)?->urls()
);

throw_unless($urls, new NotFoundHttpException);
throw_if($urls->isEmpty(), new NotFoundHttpException);

return response()
->view('advanced-seo::sitemaps.show', [
'urls' => $urls,
'urls' => $urls->toArray(),
'version' => Addon::get('aerni/advanced-seo')->version(),
])
->header('Content-Type', 'text/xml')
Expand Down
2 changes: 1 addition & 1 deletion src/Sitemaps/BaseSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function url(): string

public function lastmod(): ?string
{
return $this->urls()->sortByDesc('lastmod')->first()['lastmod'] ?? null;
return $this->urls()->sortByDesc('lastmod')->first()?->lastmod();
}

public function clearCache(): void
Expand Down
14 changes: 3 additions & 11 deletions src/Sitemaps/BaseSitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace Aerni\AdvancedSeo\Sitemaps;

use Aerni\AdvancedSeo\Contracts\SitemapUrl;
use Illuminate\Contracts\Support\Arrayable;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Taxonomies\Taxonomy;
use Statamic\Contracts\Taxonomies\Term;
use Statamic\Facades\URL;
use Statamic\Sites\Site;

abstract class BaseSitemapUrl implements SitemapUrl
abstract class BaseSitemapUrl implements SitemapUrl, Arrayable
{
abstract public function loc(): string|self;

Expand All @@ -29,17 +30,8 @@ public function isCanonicalUrl(): bool
return true;
}

/**
* We need to cast the data to an array so that we can get the correct url of taxonomies in the view.
* That's because we are temporarily setting the current site in \Aerni\AdvancedSeo\Sitemaps\TaxonomySitemapUrl::class,
* which won't have any effect if we are directly accessing the data methods in the view instead.
*/
public function toArray(): ?array
public function toArray(): array
{
if (! $this->isCanonicalUrl()) {
return null;
}

return [
'loc' => $this->loc(),
'alternates' => $this->alternates(),
Expand Down
4 changes: 2 additions & 2 deletions src/Sitemaps/Collections/CollectionSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function urls(): Collection
}

return $this->urls = $this->entries()
->map(fn ($entry) => (new EntrySitemapUrl($entry, $this))->toArray())
->filter();
->map(fn ($entry) => new EntrySitemapUrl($entry, $this))
->filter(fn ($url) => $url->isCanonicalUrl());
}

protected function entries(): Collection
Expand Down
2 changes: 1 addition & 1 deletion src/Sitemaps/Custom/CustomSitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public function add(CustomSitemapUrl $item): self

public function urls(): Collection
{
return $this->urls->map->toArray();
return $this->urls;
}
}
16 changes: 8 additions & 8 deletions src/Sitemaps/Taxonomies/TaxonomySitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Aerni\AdvancedSeo\Sitemaps\Taxonomies;

use Aerni\AdvancedSeo\Actions\IncludeInSitemap;
use Aerni\AdvancedSeo\Sitemaps\BaseSitemap;
use Illuminate\Support\Collection;
use Aerni\AdvancedSeo\Sitemaps\BaseSitemap;
use Statamic\Contracts\Taxonomies\Taxonomy;
use Aerni\AdvancedSeo\Actions\IncludeInSitemap;

class TaxonomySitemap extends BaseSitemap
{
Expand All @@ -21,32 +21,32 @@ public function urls(): Collection
->merge($this->termUrls())
->merge($this->collectionTaxonomyUrls())
->merge($this->collectionTermUrls())
->filter()
->values();
->filter(fn ($url) => $url->isCanonicalUrl());
}

protected function taxonomyUrls(): Collection
{
return $this->taxonomies()
->map(fn ($taxonomy, $site) => (new TaxonomySitemapUrl($taxonomy, $site, $this))->toArray());
->map(fn ($taxonomy, $site) => new TaxonomySitemapUrl($taxonomy, $site, $this))
->values();
}

protected function termUrls(): Collection
{
return $this->terms()
->map(fn ($term) => (new TermSitemapUrl($term, $this))->toArray());
->map(fn ($term) => new TermSitemapUrl($term, $this));
}

protected function collectionTaxonomyUrls(): Collection
{
return $this->collectionTaxonomies()
->map(fn ($item) => (new CollectionTaxonomySitemapUrl($item['taxonomy'], $item['site'], $this))->toArray());
->map(fn ($item) => new CollectionTaxonomySitemapUrl($item['taxonomy'], $item['site'], $this));
}

protected function collectionTermUrls(): Collection
{
return $this->collectionTerms()
->map(fn ($term) => (new CollectionTermSitemapUrl($term, $this))->toArray());
->map(fn ($term) => new CollectionTermSitemapUrl($term, $this));
}

public function taxonomies(): Collection
Expand Down
13 changes: 2 additions & 11 deletions src/Sitemaps/Taxonomies/TaxonomySitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,14 @@

class TaxonomySitemapUrl extends BaseSitemapUrl
{
protected string $initialSite;

public function __construct(protected Taxonomy $taxonomy, protected string $site, protected TaxonomySitemap $sitemap)
{
$this->initialSite = Site::current()->handle();

// We need to set the site so that we can get to correct URL of the taxonomy.
Site::setCurrent($site);
}

public function __destruct()
{
Site::setCurrent($this->initialSite);
}

public function loc(): string
{
Site::setCurrent($this->site);

return $this->absoluteUrl($this->taxonomy);
}

Expand Down

0 comments on commit 17af81e

Please sign in to comment.