From 5437dd80b243b7d81f8d044f9c565f826e54714d Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 18 Dec 2023 20:59:54 +0000 Subject: [PATCH 1/6] Add migration stub to have an opengraph title column --- .../add_opengraph_title_column.php.stub | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 database/migrations/add_opengraph_title_column.php.stub diff --git a/database/migrations/add_opengraph_title_column.php.stub b/database/migrations/add_opengraph_title_column.php.stub new file mode 100644 index 0000000..5ab44b5 --- /dev/null +++ b/database/migrations/add_opengraph_title_column.php.stub @@ -0,0 +1,22 @@ +string('opengraph_title')->nullable(); + }); + } + + public function down(): void + { + Schema::table('seo', function (Blueprint $table) { + $table->dropColumn('opengraph_title'); + }); + } +}; From a744875ba8fb769b794d99b97e95f3215e0af559 Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 18 Dec 2023 21:04:00 +0000 Subject: [PATCH 2/6] Add opengraph title to SEO data and the model --- src/Models/SEO.php | 3 ++- src/Support/SEOData.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 23e26c2..1dac37b 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -46,7 +46,8 @@ public function prepareForUsage(): SEOData type: $overrides->type ?? null, locale: $overrides->locale ?? null, robots: $overrides->robots ?? $this->robots, - canonical_url: $overrides->canonical_url ?? $this->canonical_url, + canonical_url: $overrides->canonical_url ?? $this->canonical_url ?? $overrides->title ?? $this->title, + openGraphTitle: $overrides->openGraphTitle ?? $this->opengraph_title, ); } } \ No newline at end of file diff --git a/src/Support/SEOData.php b/src/Support/SEOData.php index 95955a3..5d0e7c2 100644 --- a/src/Support/SEOData.php +++ b/src/Support/SEOData.php @@ -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(); From b35ce943a89b386811d60c4bd6ea35bb8818364c Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 18 Dec 2023 21:11:50 +0000 Subject: [PATCH 3/6] Add og:title special handling --- src/Models/SEO.php | 2 +- src/Tags/OpenGraphTags.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 1dac37b..2ccc8bc 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -46,7 +46,7 @@ public function prepareForUsage(): SEOData type: $overrides->type ?? null, locale: $overrides->locale ?? null, robots: $overrides->robots ?? $this->robots, - canonical_url: $overrides->canonical_url ?? $this->canonical_url ?? $overrides->title ?? $this->title, + canonical_url: $overrides->canonical_url ?? $this->canonical_url, openGraphTitle: $overrides->openGraphTitle ?? $this->opengraph_title, ); } diff --git a/src/Tags/OpenGraphTags.php b/src/Tags/OpenGraphTags.php index 399ba8d..a2799a7 100644 --- a/src/Tags/OpenGraphTags.php +++ b/src/Tags/OpenGraphTags.php @@ -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)); } From 073866712f963201c7b5f88992397d8a0ec32469 Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 18 Dec 2023 22:00:57 +0000 Subject: [PATCH 4/6] Add usage of openGraphTitle in tags --- src/TagManager.php | 4 ++++ src/Tags/TwitterCardTags.php | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/TagManager.php b/src/TagManager.php index 168aa07..4fc7349 100644 --- a/src/TagManager.php +++ b/src/TagManager.php @@ -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) ) { diff --git a/src/Tags/TwitterCardTags.php b/src/Tags/TwitterCardTags.php index cb6b5bd..524a8e6 100644 --- a/src/Tags/TwitterCardTags.php +++ b/src/Tags/TwitterCardTags.php @@ -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)); } From 2a56f2bb245a28a54c930610061aa5e5828816e4 Mon Sep 17 00:00:00 2001 From: Voltra Date: Mon, 18 Dec 2023 22:01:53 +0000 Subject: [PATCH 5/6] Add tests for opengraph_title column --- tests/Feature/Tags/OpenGraphTagsTest.php | 15 +++++++++++++++ .../Tags/TwitterCardSummaryTagsTest.php | 19 ++++++++++++++++++- tests/TestCase.php | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/Feature/Tags/OpenGraphTagsTest.php b/tests/Feature/Tags/OpenGraphTagsTest.php index 2a985fd..6759204 100644 --- a/tests/Feature/Tags/OpenGraphTagsTest.php +++ b/tests/Feature/Tags/OpenGraphTagsTest.php @@ -122,3 +122,18 @@ get(route('seo.test-plain')) ->assertSee('', false); }); + +it('uses opengraph_title over title', function() { + config()->set('seo.title.suffix', ' | Laravel SEO'); + + $page = Page::create(); + $page->seo->update([ + 'opengraph_title' => 'My OG title', + 'title' => 'My page title', + ]); + + $page->refresh(); + + get(route('seo.test-page', ['page' => $page])) + ->assertSee('', false); +}); diff --git a/tests/Feature/Tags/TwitterCardSummaryTagsTest.php b/tests/Feature/Tags/TwitterCardSummaryTagsTest.php index 6aba7b8..66e3501 100644 --- a/tests/Feature/Tags/TwitterCardSummaryTagsTest.php +++ b/tests/Feature/Tags/TwitterCardSummaryTagsTest.php @@ -91,4 +91,21 @@ })->with([ ['images/twitter-72x72.jpg'], ['images/twitter-4721x4721.jpg'], -]); \ No newline at end of file +]); + + + +it('uses opengraph_title over title', function() { + config()->set('seo.title.suffix', ' | Laravel SEO'); + + $page = Page::create(); + $page->seo->update([ + 'opengraph_title' => 'My OG title', + 'title' => 'My page title', + ]); + + $page->refresh(); + + get(route('seo.test-page', ['page' => $page])) + ->assertSee('', false); +}); \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index e9ff2c7..b6a0ff4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -32,6 +32,7 @@ public function getEnvironmentSetUp($app) Schema::enableForeignKeyConstraints(); ( include __DIR__ . '/../database/migrations/create_seo_table.php.stub' )->up(); + ( include __DIR__ . '/../database/migrations/add_opengraph_title_column.php.stub' )->up(); ( include __DIR__ . '/../tests/Fixtures/migrations/create_pages_table.php' )->up(); } } From 5742e70e45c0647952a4d9dd16ee8c6466a38fc4 Mon Sep 17 00:00:00 2001 From: Voltra Date: Wed, 20 Dec 2023 19:19:47 +0000 Subject: [PATCH 6/6] Drop use of migration for opengraph title --- .../add_opengraph_title_column.php.stub | 22 ------------------- src/Models/SEO.php | 2 +- tests/Feature/Tags/OpenGraphTagsTest.php | 6 +++-- .../Tags/TwitterCardSummaryTagsTest.php | 6 +++-- tests/TestCase.php | 1 - 5 files changed, 9 insertions(+), 28 deletions(-) delete mode 100644 database/migrations/add_opengraph_title_column.php.stub diff --git a/database/migrations/add_opengraph_title_column.php.stub b/database/migrations/add_opengraph_title_column.php.stub deleted file mode 100644 index 5ab44b5..0000000 --- a/database/migrations/add_opengraph_title_column.php.stub +++ /dev/null @@ -1,22 +0,0 @@ -string('opengraph_title')->nullable(); - }); - } - - public function down(): void - { - Schema::table('seo', function (Blueprint $table) { - $table->dropColumn('opengraph_title'); - }); - } -}; diff --git a/src/Models/SEO.php b/src/Models/SEO.php index 2ccc8bc..94e0051 100644 --- a/src/Models/SEO.php +++ b/src/Models/SEO.php @@ -47,7 +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 ?? $this->opengraph_title, + openGraphTitle: $overrides->openGraphTitle ?? null, ); } } \ No newline at end of file diff --git a/tests/Feature/Tags/OpenGraphTagsTest.php b/tests/Feature/Tags/OpenGraphTagsTest.php index 6759204..974c5b5 100644 --- a/tests/Feature/Tags/OpenGraphTagsTest.php +++ b/tests/Feature/Tags/OpenGraphTagsTest.php @@ -123,12 +123,14 @@ ->assertSee('', false); }); -it('uses opengraph_title over title', function() { +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([ - 'opengraph_title' => 'My OG title', 'title' => 'My page title', ]); diff --git a/tests/Feature/Tags/TwitterCardSummaryTagsTest.php b/tests/Feature/Tags/TwitterCardSummaryTagsTest.php index 66e3501..36584dd 100644 --- a/tests/Feature/Tags/TwitterCardSummaryTagsTest.php +++ b/tests/Feature/Tags/TwitterCardSummaryTagsTest.php @@ -95,12 +95,14 @@ -it('uses opengraph_title over title', function() { +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([ - 'opengraph_title' => 'My OG title', 'title' => 'My page title', ]); diff --git a/tests/TestCase.php b/tests/TestCase.php index b6a0ff4..e9ff2c7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -32,7 +32,6 @@ public function getEnvironmentSetUp($app) Schema::enableForeignKeyConstraints(); ( include __DIR__ . '/../database/migrations/create_seo_table.php.stub' )->up(); - ( include __DIR__ . '/../database/migrations/add_opengraph_title_column.php.stub' )->up(); ( include __DIR__ . '/../tests/Fixtures/migrations/create_pages_table.php' )->up(); } }