Skip to content

Commit

Permalink
Improve sitemaps (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni authored Jul 20, 2024
1 parent 2af4adc commit a8d893d
Show file tree
Hide file tree
Showing 31 changed files with 490 additions and 547 deletions.
14 changes: 6 additions & 8 deletions resources/views/sitemaps/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" advanced-seo-version="{{ $version }}">
@foreach ($sitemaps as $sitemap)
@if ($sitemap->urls()->count() >= 1)
<sitemap>
<loc>{{ $sitemap->url() }}</loc>
<sitemap>
<loc>{{ $sitemap['url'] }}</loc>

@if($sitemap->lastmod())
<lastmod>{{ $sitemap->lastmod() }}</lastmod>
@endif
</sitemap>
@endif
@isset($sitemap['lastmod'])
<lastmod>{{ $sitemap['lastmod'] }}</lastmod>
@endisset
</sitemap>
@endforeach
</sitemapindex>
File renamed without changes.
51 changes: 51 additions & 0 deletions src/Actions/IncludeInSitemap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Aerni\AdvancedSeo\Actions;

use Aerni\AdvancedSeo\Concerns\AsAction;
use Aerni\AdvancedSeo\Concerns\EvaluatesIndexability;
use Aerni\AdvancedSeo\Facades\Seo;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Taxonomies\Taxonomy;
use Statamic\Contracts\Taxonomies\Term;
use Statamic\Facades\Blink;

class IncludeInSitemap
{
use AsAction;
use EvaluatesIndexability;

public function handle(Entry|Term|Taxonomy $model, ?string $locale = null): bool
{
$locale ??= $model->locale();

return Blink::once("{$model->id()}::{$locale}", fn () => match (true) {
$model instanceof Entry => $this->includeEntryOrTermInSitemap($model),
$model instanceof Term => $this->includeEntryOrTermInSitemap($model),
$model instanceof Taxonomy => $this->includeTaxonomyInSitemap($model, $locale)
});
}

protected function includeEntryOrTermInSitemap(Entry|Term $model): bool
{
return ! $this->isExcludedFromSitemap($model, $model->locale)
&& $this->isIndexableEntryOrTerm($model)
&& $model->seo_sitemap_enabled
&& $model->seo_canonical_type == 'current';
}

protected function includeTaxonomyInSitemap(Taxonomy $taxonomy, string $locale): bool
{
return ! $this->isExcludedFromSitemap($taxonomy, $locale)
&& $this->isIndexableSite($locale);
}

protected function isExcludedFromSitemap(Entry|Term|Taxonomy $model, string $locale): bool
{
$excluded = Seo::find('site', 'indexing')
?->in($locale)
?->value('excluded_'.EvaluateModelType::handle($model)) ?? [];

return in_array(EvaluateModelHandle::handle($model), $excluded);
}
}
109 changes: 0 additions & 109 deletions src/Actions/Indexable.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/Concerns/AsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Aerni\AdvancedSeo\Concerns;

trait AsAction
{
/**
* @return static
*/
public static function make()
{
return app(static::class);
}

/**
* @see static::handle()
*
* @param mixed ...$arguments
* @return mixed
*/
public static function run(...$arguments)
{
return static::make()->handle(...$arguments);
}
}
27 changes: 27 additions & 0 deletions src/Concerns/EvaluatesContextType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aerni\AdvancedSeo\Concerns;

use Statamic\Contracts\Entries\Collection;
use Statamic\Tags\Context;
use Statamic\Taxonomies\Taxonomy;

trait EvaluatesContextType
{
protected function contextIsEntryOrTerm(Context $context): bool
{
return $context->value('is_entry') || $context->value('is_term');
}

protected function contextIsTaxonomy(Context $context): bool
{
return $context->get('page') instanceof Taxonomy
&& $context->get('page')->collection() === null;
}

protected function contextIsCollectionTaxonomy(Context $context): bool
{
return $context->get('page') instanceof Taxonomy
&& $context->get('page')->collection() instanceof Collection;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Aerni\AdvancedSeo\View\Concerns;
namespace Aerni\AdvancedSeo\Concerns;

use Aerni\AdvancedSeo\Facades\Seo;
use Statamic\Contracts\Entries\Entry;
Expand All @@ -10,6 +10,8 @@

trait EvaluatesIndexability
{
use EvaluatesContextType;

protected function isIndexable(Context|Entry|LocalizedTerm|Site $model): bool
{
return match (true) {
Expand All @@ -22,7 +24,7 @@ protected function isIndexable(Context|Entry|LocalizedTerm|Site $model): bool

protected function isIndexableContext(Context $context): bool
{
$model = $this->contextIsEntryOrTerm()
$model = $this->contextIsEntryOrTerm($context)
? $context->get('id')->augmentable()
: $context->get('site');

Expand All @@ -37,13 +39,10 @@ protected function isIndexableEntryOrTerm(Entry|LocalizedTerm $model): bool
&& ! $model->seo_noindex; // Models with noindex should not be indexed.
}

protected function isIndexableSite(string $locale): bool
protected function isIndexableSite(string $site): bool
{
if (! $this->crawlingIsEnabled()) {
return false;
}

return ! Seo::find('site', 'indexing')?->in($locale)?->noindex;
return $this->crawlingIsEnabled()
&& ! Seo::find('site', 'indexing')?->in($site)?->noindex;
}

protected function crawlingIsEnabled(): bool
Expand Down
4 changes: 1 addition & 3 deletions src/Contracts/SitemapUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ public function priority(): string|self|null;

public function site(): string|self;

public function isCanonicalUrl(): bool;

public function toArray(): ?array;
public function canonicalTypeIsCurrent(): bool;
}
2 changes: 1 addition & 1 deletion src/Facades/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Sitemap extends Facade
{
protected static function getFacadeAccessor()
{
return \Aerni\AdvancedSeo\Sitemap\SitemapRepository::class;
return \Aerni\AdvancedSeo\Sitemaps\SitemapRepository::class;
}
}
13 changes: 6 additions & 7 deletions src/GraphQL/Fields/SitemapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

namespace Aerni\AdvancedSeo\GraphQL\Fields;

use Aerni\AdvancedSeo\Facades\Sitemap;
use Aerni\AdvancedSeo\GraphQL\Types\SeoSitemapType;
use Aerni\AdvancedSeo\Sitemap\SitemapIndex;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Collection;
use Rebing\GraphQL\Support\Field;
use Statamic\Facades\GraphQL;

Expand Down Expand Up @@ -37,12 +36,12 @@ public function args(): array

public function type(): Type
{
return GraphQl::listOf(GraphQL::type(SeoSitemapType::NAME));
return GraphQL::listOf(GraphQL::type(SeoSitemapType::NAME));
}

public function resolve($root, $args, $context, ResolveInfo $info): ?Collection
public function resolve($root, $args, $context, ResolveInfo $info): ?array
{
$sitemaps = (new SitemapIndex)->{"{$info->fieldName}Sitemaps"}();
$sitemaps = Sitemap::{"{$info->fieldName}Sitemaps"}();

if ($baseUrl = $args['baseUrl'] ?? null) {
$sitemaps = $sitemaps->each(fn ($sitemap) => $sitemap->baseUrl($baseUrl));
Expand All @@ -55,9 +54,9 @@ public function resolve($root, $args, $context, ResolveInfo $info): ?Collection
$sitemapUrls = $sitemaps->flatMap->urls();

if ($site = $args['site'] ?? null) {
$sitemapUrls = $sitemapUrls->where('site', $site);
$sitemapUrls = $sitemapUrls->filter(fn ($url) => $url->site() === $site);
}

return $sitemapUrls->isNotEmpty() ? $sitemapUrls : null;
return $sitemapUrls->isNotEmpty() ? $sitemapUrls->toArray() : null;
}
}
Loading

0 comments on commit a8d893d

Please sign in to comment.