1
0
Fork 0
mirror of https://github.com/archtechx/laravel-seo.git synced 2025-12-12 09:54:03 +00:00

OpenGraph improvements (#17)

* Allow setting tag('og:title', ...)

* og:type overriding

* Add type()
This commit is contained in:
Samuel Štancl 2022-03-30 18:01:21 +02:00 committed by GitHub
parent 2480e532bf
commit 754b3936d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 5 deletions

View file

@ -1,6 +1,10 @@
@if(seo('title')) @if(seo('title'))
<title>@seo('title')</title> <title>@seo('title')</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')" /> <meta property="og:title" content="@seo('title')" />
@endunless
@endif @endif
@if(seo('description')) @if(seo('description'))
@ -8,7 +12,11 @@
<meta name="description" content="@seo('description')" /> <meta name="description" content="@seo('description')" />
@endif @endif
@if(seo('type'))
<meta property="og:type" content="@seo('type')" />
@else
<meta property="og:type" content="website" /> <meta property="og:type" content="website" />
@endif
@if(seo('site')) <meta property="og:site_name" content="@seo('site')"> @endif @if(seo('site')) <meta property="og:site_name" content="@seo('site')"> @endif

View file

@ -42,5 +42,10 @@
} }
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"prefer-stable": true "prefer-stable": true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
} }

View file

@ -13,6 +13,7 @@ use Illuminate\Support\Str;
* @method $this url(string $url = null, ...$args) Set the canonical URL. * @method $this url(string $url = null, ...$args) Set the canonical URL.
* @method $this site(string $site = null, ...$args) Set the site name. * @method $this site(string $site = null, ...$args) Set the site name.
* @method $this image(string $url = null, ...$args) Set the cover image. * @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 twitter(enabled $bool = true, ...$args) Enable the Twitter extension.
* @method $this twitterSite(string $username = null, ...$args) Set the Twitter author. * @method $this twitterSite(string $username = null, ...$args) Set the Twitter author.
* @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title. * @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title.
@ -53,7 +54,7 @@ class SEOManager
protected function getKeys(): array protected function getKeys(): array
{ {
return collect([ return collect([
'site', 'title', 'image', 'description', 'url', 'site', 'title', 'image', 'description', 'url', 'type',
'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description', 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description',
]) ])
->merge(array_keys($this->defaults)) ->merge(array_keys($this->defaults))
@ -201,6 +202,18 @@ class SEOManager
return $this->tags; 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. */ /** Add a head tag. */
public function rawTag(string $key, string $tag = null): static public function rawTag(string $key, string $tag = null): static
{ {

View file

@ -127,3 +127,22 @@ test('canonical url can be changed', function () {
->toContain('<meta property="og:url" content="http://foo.com/bar" />') ->toContain('<meta property="og:url" content="http://foo.com/bar" />')
->toContain('<link rel="canonical" href="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');
});