-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for FAQPage schema (#77)
* add support for FaqPage schema * add documentation * update readme * Formatting * Update README.md * Update README.md * WIP --------- Co-authored-by: Ralph J. Smit <[email protected]>
- Loading branch information
1 parent
c51735f
commit e25a2fa
Showing
4 changed files
with
151 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace RalphJSmit\Laravel\SEO\Schema; | ||
|
||
use Illuminate\Support\HtmlString; | ||
use RalphJSmit\Laravel\SEO\Support\SEOData; | ||
|
||
/** | ||
* @see https://developers.google.com/search/docs/appearance/structured-data/faqpage | ||
*/ | ||
class FaqPageSchema extends Schema | ||
{ | ||
public string $type = 'FAQPage'; | ||
|
||
public array $questions = []; | ||
|
||
public function addQuestion( | ||
string $name, | ||
string $acceptedAnswer | ||
): static { | ||
$this->questions[] = [ | ||
'@type' => 'Question', | ||
'name' => $name, | ||
'acceptedAnswer' => [ | ||
'@type' => 'Answer', | ||
'text' => $acceptedAnswer, | ||
], | ||
]; | ||
|
||
return $this; | ||
} | ||
|
||
public function initializeMarkup(SEOData $SEOData, array $markupBuilders): void | ||
{ | ||
// | ||
} | ||
|
||
public function generateInner(): HtmlString | ||
{ | ||
$inner = collect([ | ||
'@context' => 'https://schema.org', | ||
'@type' => $this->type, | ||
'mainEntity' => $this->questions, | ||
]) | ||
->pipeThrough($this->markupTransformers) | ||
->toJson(); | ||
|
||
return new HtmlString($inner); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
use RalphJSmit\Laravel\SEO\Schema\FaqPageSchema; | ||
use RalphJSmit\Laravel\SEO\SchemaCollection; | ||
use RalphJSmit\Laravel\SEO\Tests\Fixtures\Page; | ||
|
||
use function Pest\Laravel\get; | ||
|
||
it('does not render by default the JSON-LD Schema markup: FaqPageTest', function () { | ||
get(route('seo.test-plain')) | ||
->assertDontSee('"application/ld+json"') | ||
->assertDontSee('"@type": "FAQPage"'); | ||
}); | ||
|
||
it('can correctly render the JSON-LD Schema markup: FaqPageTest', function () { | ||
config()->set('seo.title.suffix', ' | Laravel SEO'); | ||
|
||
$page = Page::create([]); | ||
|
||
$page::$overrides = [ | ||
'title' => 'Test FAQ', | ||
'enableTitleSuffix' => true, | ||
'url' => 'https://example.com/test/faq', | ||
'schema' => SchemaCollection::initialize()->addFaqPage(function (FaqPageSchema $faqPage): FaqPageSchema { | ||
return $faqPage | ||
->addQuestion(name: 'Can this package add FaqPage to the schema?', acceptedAnswer: 'Yes!') | ||
->addQuestion(name: 'Does it support multiple questions?', acceptedAnswer: 'Of course.'); | ||
}), | ||
]; | ||
|
||
get(route('seo.test-page', ['page' => $page])) | ||
->assertSee('"application/ld+json"', false) | ||
->assertSee( | ||
'<script type="application/ld+json">' . | ||
json_encode([ | ||
'@context' => 'https://schema.org', | ||
'@type' => 'FAQPage', | ||
'mainEntity' => [ | ||
[ | ||
'@type' => 'Question', | ||
'name' => 'Can this package add FaqPage to the schema?', | ||
'acceptedAnswer' => [ | ||
'@type' => 'Answer', | ||
'text' => 'Yes!', | ||
], | ||
], | ||
[ | ||
'@type' => 'Question', | ||
'name' => 'Does it support multiple questions?', | ||
'acceptedAnswer' => [ | ||
'@type' => 'Answer', | ||
'text' => 'Of course.', | ||
], | ||
], | ||
], | ||
]) . '</script>', | ||
false | ||
); | ||
}); |