Skip to content

Commit

Permalink
OpenGraph improvements (#17)
Browse files Browse the repository at this point in the history
* Allow setting tag('og:title', ...)

* og:type overriding

* Add type()
  • Loading branch information
stancl authored Mar 30, 2022
1 parent 2480e53 commit 754b393
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
14 changes: 11 additions & 3 deletions assets/views/components/meta.blade.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
@if(seo('title'))
<title>@seo('title')</title>
<meta property="og:title" content="@seo('title')" />

@unless(seo()->hasTag('og:title'))
{{-- If an og:title tag is provided directly, it's included in the @foreach below --}}
<meta property="og:title" content="@seo('title')" />
@endunless
@endif

@if(seo('description'))
@if(seo('description'))
<meta property="og:description" content="@seo('description')" />
<meta name="description" content="@seo('description')" />
@endif

<meta property="og:type" content="website" />
@if(seo('type'))
<meta property="og:type" content="@seo('type')" />
@else
<meta property="og:type" content="website" />
@endif

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

Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,10 @@
}
},
"minimum-stability": "dev",
"prefer-stable": true
"prefer-stable": true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
15 changes: 14 additions & 1 deletion src/SEOManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @method $this url(string $url = null, ...$args) Set the canonical URL.
* @method $this site(string $site = null, ...$args) Set the site name.
* @method $this image(string $url = null, ...$args) Set the cover image.
* @method $this type(string $type = null, ...$args) Set the page type.
* @method $this twitter(enabled $bool = true, ...$args) Enable the Twitter extension.
* @method $this twitterSite(string $username = null, ...$args) Set the Twitter author.
* @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title.
Expand Down Expand Up @@ -53,7 +54,7 @@ public function all(): array
protected function getKeys(): array
{
return collect([
'site', 'title', 'image', 'description', 'url',
'site', 'title', 'image', 'description', 'url', 'type',
'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description',
])
->merge(array_keys($this->defaults))
Expand Down Expand Up @@ -201,6 +202,18 @@ public function tags(): array
return $this->tags;
}

/** Has a specific tag been set? */
public function hasRawTag(string $key): bool
{
return isset($this->tags[$key]) && ($this->tags[$key] !== null);
}

/** Has a specific meta tag been set? */
public function hasTag(string $property): bool
{
return $this->hasRawTag("meta.{$property}");
}

/** Add a head tag. */
public function rawTag(string $key, string $tag = null): static
{
Expand Down
19 changes: 19 additions & 0 deletions tests/Pest/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,22 @@
->toContain('<meta property="og:url" content="http://foo.com/bar" />')
->toContain('<link rel="canonical" href="http://foo.com/bar" />');
});

test('og:title can be overridden using a tag', function () {
seo()->title('foo')
->tag('og:title', 'bar');

expect(meta())
->toContain('<title>foo</title>')
->toContain('<meta property="og:title" content="bar" />');
});

test('type can be overridden using the type method', function () {
expect(meta())->toContain('<meta property="og:type" content="website" />'); // default

seo()->type('foo');

expect(meta())
->toContain('<meta property="og:type" content="foo" />') // overridden
->not()->toContain('website');
});

0 comments on commit 754b393

Please sign in to comment.