-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collections & taxonomies sitemaps with rapidez/sitemap (#93)
- Loading branch information
1 parent
01e1904
commit eeef1be
Showing
8 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Actions; | ||
|
||
use Statamic\Facades\Site; | ||
use Rapidez\Statamic\Jobs\GenerateStoreSitemapJob; | ||
|
||
class GenerateSitemapsAction | ||
{ | ||
public static function generate(): void | ||
{ | ||
Site::all()->each(function ($site) { | ||
GenerateStoreSitemapJob::dispatchSync($site); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Facades\Storage; | ||
use Rapidez\Sitemap\Actions\GenerateSitemapAction; | ||
use Statamic\Sites\Site; | ||
use Statamic\Entries\Collection; | ||
use Statamic\Entries\Entry; | ||
|
||
class GenerateCollectionSitemapJob implements ShouldQueue, ShouldBeUnique | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable; | ||
|
||
public function __construct( | ||
protected Site $site, | ||
protected Collection $collection, | ||
) {} | ||
|
||
public function handle(GenerateSitemapAction $sitemapGenerator): void | ||
{ | ||
$storeId = $this->site->attribute('magento_store_id'); | ||
$path = trim(config('rapidez-sitemap.path', 'rapidez-sitemaps'), '/'); | ||
$storageDirectory = $path.'/'.$storeId.'/'; | ||
|
||
$sitemapPath = $storageDirectory | ||
. config('rapidez.statamic.sitemap.prefix') | ||
. 'collection_' | ||
. $this->collection->handle() | ||
. '.xml'; | ||
|
||
$sitemapContent = $sitemapGenerator->createSitemapUrlset($this->generateCollectionSitemap()); | ||
$storageDisk = Storage::disk(config('rapidez-sitemap.disk', 'public')); | ||
$storageDisk->put($sitemapPath, $sitemapContent); | ||
} | ||
|
||
protected function generateCollectionSitemap() : array | ||
{ | ||
$entries = $this->collection->queryEntries()->where('site', $this->site->handle())->whereStatus('published')->get(); | ||
return $entries->map(fn (Entry $entry) => [ | ||
'loc' => $entry->url(), | ||
'lastmod' => $entry->lastModified() | ||
])->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Facades\Storage; | ||
use Statamic\Facades\Collection as StatamicCollection; | ||
use Statamic\Facades\Taxonomy as TaxonomyFacade; | ||
use Statamic\Sites\Site; | ||
use Statamic\Entries\Collection; | ||
use TorMorten\Eventy\Facades\Eventy; | ||
|
||
class GenerateStoreSitemapJob implements ShouldQueue, ShouldBeUnique | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable; | ||
|
||
public function __construct( | ||
protected Site $site | ||
) {} | ||
|
||
public function handle(): void | ||
{ | ||
$this->createCollectionSitemaps(); | ||
$this->createTaxonomySitemaps(); | ||
$this->addSitemapFilter(); | ||
} | ||
|
||
protected function createCollectionSitemaps() : void | ||
{ | ||
$collections = StatamicCollection::all(); | ||
$collections = $collections->filter(fn (Collection $collection) => $collection->route($this->site->handle())); | ||
|
||
foreach ($collections as $collection) { | ||
/* @var Collection $collection */ | ||
if ($collection->queryEntries()->where('site', $this->site->handle())->whereStatus('published')->count()) { | ||
GenerateCollectionSitemapJob::dispatchSync($this->site, $collection); | ||
} | ||
} | ||
} | ||
|
||
protected function createTaxonomySitemaps() : void | ||
{ | ||
$taxonomies = TaxonomyFacade::all() | ||
->filter(function ($taxonomy) { | ||
$terms = $taxonomy->queryTerms() | ||
->where('site', $this->site->handle()) | ||
->get(); | ||
|
||
return $terms | ||
->filter(function ($term) { | ||
return view()->exists($term->template()); | ||
})->isNotEmpty(); | ||
}); | ||
|
||
foreach ($taxonomies as $taxonomy) { | ||
/* @var Taxonomy $taxonomy */ | ||
GenerateTermSitemapJob::dispatchSync($this->site, $taxonomy); | ||
} | ||
} | ||
|
||
protected function addSitemapFilter() : void | ||
{ | ||
$storeId = $this->site->attribute('magento_store_id'); | ||
$storageDisk = Storage::disk(config('rapidez-sitemap.disk', 'public')); | ||
$path = trim(config('rapidez-sitemap.path', 'rapidez-sitemaps'), '/'); | ||
$storageDirectory = $path.'/'.$storeId.'/'; | ||
|
||
$sitemapPrefix = config('rapidez.statamic.sitemap.prefix'); | ||
|
||
$sitemaps = collect($storageDisk->files($storageDirectory)) | ||
->filter(fn($item) => str_starts_with($item, $storageDirectory . $sitemapPrefix)) | ||
->map(fn($item) => [ | ||
'loc' => url($item), | ||
'lastmod' => $storageDisk->lastModified($item) | ||
? date('Y-m-d H:i:s', $storageDisk->lastModified($item)) | ||
: null | ||
]) | ||
->toArray(); | ||
|
||
Eventy::addFilter('rapidez.sitemap.' . $storeId, fn($rapidezSitemaps) => array_merge($rapidezSitemaps, $sitemaps)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Rapidez\Statamic\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Support\Facades\Storage; | ||
use Rapidez\Sitemap\Actions\GenerateSitemapAction; | ||
use Statamic\Facades\Taxonomy as TaxonomyFacade; | ||
use Statamic\Sites\Site; | ||
use Statamic\Taxonomies\LocalizedTerm; | ||
use Statamic\Taxonomies\Taxonomy; | ||
|
||
class GenerateTermSitemapJob implements ShouldQueue, ShouldBeUnique | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable; | ||
|
||
public function __construct( | ||
protected Site $site, | ||
protected Taxonomy $taxonomy, | ||
) {} | ||
|
||
public function handle(GenerateSitemapAction $sitemapGenerator): void | ||
{ | ||
$storeId = $this->site->attribute('magento_store_id'); | ||
$path = trim(config('rapidez-sitemap.path', 'rapidez-sitemaps'), '/'); | ||
$storageDirectory = $path.'/'.$storeId.'/'; | ||
|
||
$sitemapPath = $storageDirectory | ||
. config('rapidez.statamic.sitemap.prefix') | ||
. 'taxonomy_' | ||
. $this->taxonomy->handle() | ||
. '.xml'; | ||
|
||
$sitemapContent = $sitemapGenerator->createSitemapUrlset($this->generateTermSitemap()); | ||
$storageDisk = Storage::disk(config('rapidez-sitemap.disk', 'public')); | ||
$storageDisk->put($sitemapPath, $sitemapContent); | ||
} | ||
|
||
|
||
protected function generateTermSitemap() : array | ||
{ | ||
$terms = $this->publishedTerms(); | ||
return $terms->map(fn (LocalizedTerm $term) => [ | ||
'loc' => $term->absoluteUrl(), | ||
'lastmod' => $term->lastModified() | ||
])->toArray(); | ||
} | ||
|
||
protected function publishedTerms() | ||
{ | ||
return TaxonomyFacade::all() | ||
->flatMap(fn ($taxonomy) => $taxonomy->queryTerms() | ||
->where('site', $this->site->handle()) | ||
->whereNotNull('uri') | ||
->get() | ||
) | ||
->filter | ||
->published() | ||
->filter(function ($term) { | ||
return view()->exists($term->template()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters