Skip to content

Commit

Permalink
Differentiate between page title and opengraph title (#55)
Browse files Browse the repository at this point in the history
* Add migration stub to have an opengraph title column

* Add opengraph title to SEO data and the model

* Add og:title special handling

* Add usage of openGraphTitle in tags

* Add tests for opengraph_title column

* Drop use of migration for opengraph title
  • Loading branch information
Voltra authored Dec 20, 2023
1 parent 27fe4c0 commit eebd11c
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Models/SEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function prepareForUsage(): SEOData
locale: $overrides->locale ?? null,
robots: $overrides->robots ?? $this->robots,
canonical_url: $overrides->canonical_url ?? $this->canonical_url,
openGraphTitle: $overrides->openGraphTitle ?? null,
);
}
}
1 change: 1 addition & 0 deletions src/Support/SEOData.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct(
public ?string $locale = null,
public ?string $robots = null,
public ?string $canonical_url = null,
public ?string $openGraphTitle = null,
) {
if ( $this->locale === null ) {
$this->locale = app()->getLocale();
Expand Down
4 changes: 4 additions & 0 deletions src/TagManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function fillSEOData(SEOData $SEOData = null): SEOData

if ( $SEOData->enableTitleSuffix ) {
$SEOData->title .= config('seo.title.suffix');

if ($SEOData->openGraphTitle) {
$SEOData->openGraphTitle .= config('seo.title.suffix');
}
}

if ( $SEOData->image && ! filter_var($SEOData->image, FILTER_VALIDATE_URL) ) {
Expand Down
4 changes: 3 additions & 1 deletion src/Tags/OpenGraphTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public static function initialize(SEOData $SEOData): static
{
$collection = new static();

if ( $SEOData->title ) {
if ( $SEOData->openGraphTitle ) {
$collection->push(new OpenGraphTag('title', $SEOData->openGraphTitle));
} else if ( $SEOData->title ) {
$collection->push(new OpenGraphTag('title', $SEOData->title));
}

Expand Down
4 changes: 3 additions & 1 deletion src/Tags/TwitterCardTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static function initialize(SEOData $SEOData): ?static
$collection->push(new TwitterCardTag('card', 'summary'));
}

if ( $SEOData->title ) {
if ( $SEOData->openGraphTitle ) {
$collection->push(new TwitterCardTag('title', $SEOData->openGraphTitle));
} else if ( $SEOData->title ) {
$collection->push(new TwitterCardTag('title', $SEOData->title));
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Tags/OpenGraphTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,20 @@
get(route('seo.test-plain'))
->assertSee('<meta property="og:locale" content="en_GB">', false);
});

it('uses openGraphTitle over title', function() {
config()->set('seo.title.suffix', ' | Laravel SEO');

$page = Page::create();
$page::$overrides = [
'openGraphTitle' => 'My OG title',
];
$page->seo->update([
'title' => 'My page title',
]);

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta property="og:title" content="My OG title | Laravel SEO">', false);
});
21 changes: 20 additions & 1 deletion tests/Feature/Tags/TwitterCardSummaryTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@
})->with([
['images/twitter-72x72.jpg'],
['images/twitter-4721x4721.jpg'],
]);
]);



it('uses openGraphTitle over title', function() {
config()->set('seo.title.suffix', ' | Laravel SEO');

$page = Page::create();
$page::$overrides = [
'openGraphTitle' => 'My OG title',
];
$page->seo->update([
'title' => 'My page title',
]);

$page->refresh();

get(route('seo.test-page', ['page' => $page]))
->assertSee('<meta name="twitter:title" content="My OG title | Laravel SEO">', false);
});

0 comments on commit eebd11c

Please sign in to comment.