Skip to content

Commit 754b393

Browse files
authored
OpenGraph improvements (#17)
* Allow setting tag('og:title', ...) * og:type overriding * Add type()
1 parent 2480e53 commit 754b393

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

assets/views/components/meta.blade.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
@if(seo('title'))
22
<title>@seo('title')</title>
3-
<meta property="og:title" content="@seo('title')" />
3+
4+
@unless(seo()->hasTag('og:title'))
5+
{{-- If an og:title tag is provided directly, it's included in the @foreach below --}}
6+
<meta property="og:title" content="@seo('title')" />
7+
@endunless
48
@endif
59

6-
@if(seo('description'))
10+
@if(seo('description'))
711
<meta property="og:description" content="@seo('description')" />
812
<meta name="description" content="@seo('description')" />
913
@endif
1014

11-
<meta property="og:type" content="website" />
15+
@if(seo('type'))
16+
<meta property="og:type" content="@seo('type')" />
17+
@else
18+
<meta property="og:type" content="website" />
19+
@endif
1220

1321
@if(seo('site')) <meta property="og:site_name" content="@seo('site')"> @endif
1422

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@
4242
}
4343
},
4444
"minimum-stability": "dev",
45-
"prefer-stable": true
45+
"prefer-stable": true,
46+
"config": {
47+
"allow-plugins": {
48+
"pestphp/pest-plugin": true
49+
}
50+
}
4651
}

src/SEOManager.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @method $this url(string $url = null, ...$args) Set the canonical URL.
1414
* @method $this site(string $site = null, ...$args) Set the site name.
1515
* @method $this image(string $url = null, ...$args) Set the cover image.
16+
* @method $this type(string $type = null, ...$args) Set the page type.
1617
* @method $this twitter(enabled $bool = true, ...$args) Enable the Twitter extension.
1718
* @method $this twitterSite(string $username = null, ...$args) Set the Twitter author.
1819
* @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title.
@@ -53,7 +54,7 @@ public function all(): array
5354
protected function getKeys(): array
5455
{
5556
return collect([
56-
'site', 'title', 'image', 'description', 'url',
57+
'site', 'title', 'image', 'description', 'url', 'type',
5758
'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description',
5859
])
5960
->merge(array_keys($this->defaults))
@@ -201,6 +202,18 @@ public function tags(): array
201202
return $this->tags;
202203
}
203204

205+
/** Has a specific tag been set? */
206+
public function hasRawTag(string $key): bool
207+
{
208+
return isset($this->tags[$key]) && ($this->tags[$key] !== null);
209+
}
210+
211+
/** Has a specific meta tag been set? */
212+
public function hasTag(string $property): bool
213+
{
214+
return $this->hasRawTag("meta.{$property}");
215+
}
216+
204217
/** Add a head tag. */
205218
public function rawTag(string $key, string $tag = null): static
206219
{

tests/Pest/ManagerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,22 @@
127127
->toContain('<meta property="og:url" content="http://foo.com/bar" />')
128128
->toContain('<link rel="canonical" href="http://foo.com/bar" />');
129129
});
130+
131+
test('og:title can be overridden using a tag', function () {
132+
seo()->title('foo')
133+
->tag('og:title', 'bar');
134+
135+
expect(meta())
136+
->toContain('<title>foo</title>')
137+
->toContain('<meta property="og:title" content="bar" />');
138+
});
139+
140+
test('type can be overridden using the type method', function () {
141+
expect(meta())->toContain('<meta property="og:type" content="website" />'); // default
142+
143+
seo()->type('foo');
144+
145+
expect(meta())
146+
->toContain('<meta property="og:type" content="foo" />') // overridden
147+
->not()->toContain('website');
148+
});

0 commit comments

Comments
 (0)