Skip to content

Commit 553766a

Browse files
committed
Add seo tag pages to sitemap generator
1 parent e7ccb34 commit 553766a

File tree

8 files changed

+41
-21
lines changed

8 files changed

+41
-21
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,6 @@ release:
449449
- policies:
450450
- note: undeletable pages
451451
- undeletable page toggle only for permission holder
452-
- tag controller
453-
- add tag urls to sitemap.
454452
- documentation
455453
- Kristof: screenshots + banner + packagist + slack + filament plugin store
456454

src/FilamentFlexibleContentBlockPages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ public function settings(): Settings
4242
/**
4343
* Looks up the resource class of a model in ANY panel.
4444
*
45-
* @param string<class-string> $modelClass
46-
* @return string<class-string>|null
45+
* @param class-string $modelClass
46+
* @return class-string|null
4747
*/
4848
public function getModelResource(string $modelClass): ?string
4949
{

src/Http/Controllers/SeoTagController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ private function createModelLabelsLookup(): array
118118

119119
foreach ($enabledModels as $modelClass) {
120120
$resourceClass = FilamentFlexibleContentBlockPages::getModelResource($modelClass);
121-
/** @var resource|null $resourceClass */
121+
/** @var class-string|null $resourceClass */
122122
$labels[$modelClass] = $resourceClass ? $resourceClass::getModelLabel() : class_basename($modelClass);
123123
}
124124

src/Http/Middleware/RedirectsMissingPages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function handle(Request $request, Closure $next)
1919
/** @var Response $response */
2020
$response = parent::handle($request, $next);
2121

22-
if ($request->query->count() > 0 && $this->shouldRedirect($response)) {
22+
if ($request->query->count() > 0 && $this->shouldRedirect($response) && method_exists($response, 'getTargetUrl')) {
2323
// make sure we do not lose the query string:
2424
/** @var RedirectResponse $response */
25-
if (method_exists($response, 'getTargetUrl') && ! Str::contains($response->getTargetUrl(), '?')) {
25+
if (! Str::contains($response->getTargetUrl(), '?')) {
2626
$response->setTargetUrl($response->getTargetUrl().'?'.$request->getQueryString());
2727
}
2828
}

src/Models/Tag.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
88
use Mcamara\LaravelLocalization\Interfaces\LocalizedUrlRoutable;
99
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
10+
use Statikbe\FilamentFlexibleContentBlockPages\Routes\Contracts\HandlesPageRoutes;
1011
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\Linkable;
1112

1213
/**
@@ -67,14 +68,14 @@ public function getViewUrl(?string $locale = null): string
6768
{
6869
$locale = $locale ?? app()->getLocale();
6970

70-
// TODO impl routing & controller
71-
// return LaravelLocalization::getURLFromRouteNameTranslated($locale, 'routes.article_tag_index', ['tag:slug' => $this->translate('slug', $locale)]);
72-
return 'https://www.statik.be';
71+
return route(HandlesPageRoutes::ROUTE_SEO_TAG_PAGE, [
72+
'tag:slug' => $this->getTranslation('slug', $locale) ?? $this->slug,
73+
]);
7374
}
7475

7576
public function getPreviewUrl(?string $locale = null): string
7677
{
77-
return $this->getPreviewUrl($locale);
78+
return $this->getViewUrl($locale);
7879
}
7980

8081
public function getMorphClass()

src/Routes/AbstractPageRouteHelper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@
1010

1111
abstract class AbstractPageRouteHelper implements HandlesPageRoutes
1212
{
13-
const ROUTE_HOME = 'home';
14-
15-
const ROUTE_PAGE = 'filament-flexible-content-block-pages::page_index';
16-
17-
const ROUTE_CHILD_PAGE = 'filament-flexible-content-block-pages::child_page_index';
18-
19-
const ROUTE_GRANDCHILD_PAGE = 'filament-flexible-content-block-pages::grandchild_page_index';
20-
21-
const ROUTE_SEO_TAG_PAGE = 'filament-flexible-content-block-pages::seo_tag_page_index';
22-
2313
public function defineRoutes(): void
2414
{
2515
if (FilamentFlexibleContentBlockPages::config()->areTagPagesEnabled()) {

src/Routes/Contracts/HandlesPageRoutes.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
interface HandlesPageRoutes
99
{
10+
public const ROUTE_PAGE = 'filament-flexible-content-block-pages::page_index';
11+
12+
public const ROUTE_CHILD_PAGE = 'filament-flexible-content-block-pages::child_page_index';
13+
14+
public const ROUTE_SEO_TAG_PAGE = 'filament-flexible-content-block-pages::seo_tag_page_index';
15+
16+
public const ROUTE_HOME = 'home';
17+
18+
public const ROUTE_GRANDCHILD_PAGE = 'filament-flexible-content-block-pages::grandchild_page_index';
19+
1020
/**
1121
* Creates the page and SEO tag routes.
1222
*/

src/Services/SitemapGeneratorService.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Spatie\Sitemap\Tags\Url;
1313
use Statikbe\FilamentFlexibleContentBlockPages\Facades\FilamentFlexibleContentBlockPages;
1414
use Statikbe\FilamentFlexibleContentBlockPages\Models\Page;
15+
use Statikbe\FilamentFlexibleContentBlockPages\Models\Tag;
1516
use Statikbe\FilamentFlexibleContentBlockPages\Services\Contracts\GeneratesSitemap;
1617
use Statikbe\FilamentFlexibleContentBlockPages\Services\Enum\SitemapGeneratorMethod;
1718
use Statikbe\FilamentFlexibleContentBlocks\ContentBlocks\CallToActionBlock;
@@ -69,6 +70,10 @@ protected function generateManually(): void
6970
$this->addLinkableModels();
7071
}
7172

73+
if (FilamentFlexibleContentBlockPages::config()->areTagPagesEnabled()) {
74+
$this->addSeoTagPages();
75+
}
76+
7277
$this->addCustomUrls();
7378
}
7479

@@ -185,6 +190,22 @@ protected function addCustomUrls(): void
185190
}
186191
}
187192

193+
protected function addSeoTagPages(): void
194+
{
195+
$seoTags = Tag::whereRelation('tagType', 'has_seo_pages', true)->get();
196+
197+
foreach ($seoTags as $tag) {
198+
$this->sitemap->add(
199+
Url::create($tag->getViewUrl($this->canonicalLocale))
200+
);
201+
202+
$this->addToSitemap($tag->getViewUrl($this->canonicalLocale),
203+
null, // TODO query the last updated_at of all models.
204+
0.6,
205+
Url::CHANGE_FREQUENCY_WEEKLY);
206+
}
207+
}
208+
188209
protected function getLinkableModels(): array
189210
{
190211
$ctaModels = FilamentFlexibleBlocksConfig::getCallToActionModels(CallToActionBlock::class);

0 commit comments

Comments
 (0)