Skip to content

Commit 5810407

Browse files
Changed injection of structured data
2 parents 47c3c83 + 51cb90c commit 5810407

File tree

6 files changed

+129
-130
lines changed

6 files changed

+129
-130
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ You can install this addon via Composer:
2525
composer require justbetter/statamic-structured-data
2626
```
2727

28+
After installing make sure to load the Structured Data tag in your head.
29+
30+
``` blade
31+
{!! Statamic::tag('structured-data:head')->fetch() !!}
32+
```
33+
2834
## Configuration
2935

3036
Make sure to publish the config by running:
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Justbetter\StatamicStructuredData\Actions;
4+
5+
use Justbetter\StatamicStructuredData\Services\StructuredDataService;
6+
use Statamic\Entries\Entry;
7+
use Statamic\Facades\Entry as EntryFacade;
8+
use Statamic\Facades\Site;
9+
use Statamic\Facades\Term;
10+
use Statamic\Facades\URL;
11+
use Statamic\Structures\Page;
12+
use Statamic\Taxonomies\LocalizedTerm;
13+
14+
class InjectStructuredDataAction
15+
{
16+
public function __construct(protected StructuredDataService $structuredDataService) {}
17+
18+
public function execute(): ?string
19+
{
20+
$entry = $this->getCurrentEntry();
21+
22+
if ($entry) {
23+
return $this->handleEntry($entry);
24+
}
25+
26+
$term = $this->getCurrentTerm();
27+
28+
if ($term) {
29+
return $this->handleTaxonomy($term);
30+
}
31+
32+
return null;
33+
}
34+
35+
protected function handleEntry(Entry|Page $entry): ?string
36+
{
37+
if ($entry instanceof Page) {
38+
$entry = $entry->entry();
39+
}
40+
41+
$enabledCollections = config('justbetter.structured-data.collections', []);
42+
43+
if (! in_array($entry?->collection()?->handle(), $enabledCollections)) {
44+
return null;
45+
}
46+
47+
return $this->handleScripts($entry);
48+
}
49+
50+
protected function handleTaxonomy(LocalizedTerm $term): ?string
51+
{
52+
$enabledTaxonomies = config('justbetter.structured-data.taxonomies', []);
53+
54+
if (! in_array($term?->taxonomy()?->handle(), $enabledTaxonomies)) {
55+
return null;
56+
}
57+
58+
return $this->handleScripts($term);
59+
}
60+
61+
protected function handleScripts(mixed $item): ?string
62+
{
63+
$scripts = $this->structuredDataService->getJsonLdScripts($item);
64+
65+
if (! $scripts ?? false) {
66+
return null;
67+
}
68+
69+
return implode("\n", $scripts);
70+
}
71+
72+
protected function getCurrentEntry(): Page|Entry|null
73+
{
74+
$url = URL::getCurrent();
75+
76+
return EntryFacade::findByUri($url, Site::current()->handle());
77+
}
78+
79+
protected function getCurrentTerm(): ?LocalizedTerm
80+
{
81+
$url = URL::getCurrent();
82+
83+
return Term::findByUri($url, Site::current()->handle());
84+
}
85+
}

src/Http/Middleware/InjectStructuredData.php

Lines changed: 0 additions & 114 deletions
This file was deleted.

src/Listeners/AddStructuredDataTab.php renamed to src/Listeners/AddStructuredDataTabListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Statamic\Events\TermBlueprintFound;
77
use Statamic\Fields\Blueprint;
88

9-
class AddStructuredDataTab
9+
class AddStructuredDataTabListener
1010
{
1111
public function handle(EntryBlueprintFound|TermBlueprintFound $event): void
1212
{

src/ServiceProvider.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use Justbetter\StatamicStructuredData\Fieldtypes\StructuredDataBuilder;
99
use Justbetter\StatamicStructuredData\Fieldtypes\StructuredDataObjectBuilder;
1010
use Justbetter\StatamicStructuredData\Fieldtypes\StructuredDataPreview;
11-
use Justbetter\StatamicStructuredData\Http\Middleware\InjectStructuredData;
12-
use Justbetter\StatamicStructuredData\Listeners\AddStructuredDataTab;
11+
use Justbetter\StatamicStructuredData\Listeners\AddStructuredDataTabListener;
12+
use Justbetter\StatamicStructuredData\Tags\StructuredData;
1313
use Statamic\Events\EntryBlueprintFound;
1414
use Statamic\Events\TermBlueprintFound;
1515
use Statamic\Facades\Blueprint;
@@ -29,36 +29,35 @@ class ServiceProvider extends AddonServiceProvider
2929
'publicDirectory' => 'resources/dist',
3030
];
3131

32+
protected $tags = [
33+
StructuredData::class,
34+
];
35+
3236
protected $fieldtypes = [
3337
StructuredDataBuilder::class,
3438
StructuredDataPreview::class,
3539
StructuredDataObjectBuilder::class,
3640
AvailableVariablesFieldtype::class,
3741
];
3842

39-
protected $middlewareGroups = [
40-
'web' => [
41-
InjectStructuredData::class,
42-
],
43-
];
44-
45-
public function bootAddon()
43+
public function bootAddon(): void
4644
{
4745
$this->bootCollections()
4846
->bootTaxonomies()
4947
->bootConfig()
48+
->bootActions()
5049
->bootEvents();
5150
}
5251

53-
public function bootEvents()
52+
public function bootEvents(): self
5453
{
55-
Event::listen(EntryBlueprintFound::class, AddStructuredDataTab::class);
56-
Event::listen(TermBlueprintFound::class, AddStructuredDataTab::class);
54+
Event::listen(EntryBlueprintFound::class, AddStructuredDataTabListener::class);
55+
Event::listen(TermBlueprintFound::class, AddStructuredDataTabListener::class);
5756

5857
return $this;
5958
}
6059

61-
public function bootCollections()
60+
public function bootCollections(): self
6261
{
6362
if ($this->app->runningInConsole() || Collection::find('structured_data_templates')) {
6463
return $this;
@@ -80,7 +79,7 @@ public function bootCollections()
8079
return $this;
8180
}
8281

83-
public function bootTaxonomies()
82+
public function bootTaxonomies(): self
8483
{
8584
if ($this->app->runningInConsole() || Taxonomy::find('structured_data_objects')) {
8685
return $this;
@@ -102,7 +101,7 @@ public function bootTaxonomies()
102101
return $this;
103102
}
104103

105-
protected function bootConfig()
104+
protected function bootConfig(): self
106105
{
107106
$this->publishes([
108107
__DIR__.'/../config/structured-data.php' => config_path('justbetter/structured-data.php'),

src/Tags/StructuredData.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Justbetter\StatamicStructuredData\Tags;
4+
5+
use Justbetter\StatamicStructuredData\Actions\InjectStructuredDataAction;
6+
use Statamic\Tags\Tags;
7+
8+
class StructuredData extends Tags
9+
{
10+
protected static $handle = 'structured-data';
11+
12+
protected $action;
13+
14+
public function __construct(InjectStructuredDataAction $action)
15+
{
16+
$this->action = $action;
17+
}
18+
19+
public function head(): ?string
20+
{
21+
return $this->action->execute();
22+
}
23+
}

0 commit comments

Comments
 (0)