Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Differentiate between page title and opengraph title #55

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
Loading