feat: implement structured data support for blog and page components, enhancing SEO and user experience through JSON-LD integration
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\Page;
|
||||
|
||||
class PageStructuredData extends StructuredData
|
||||
{
|
||||
public static function forPage(?Page $page, string $pageUrl, ?string $name = null, ?string $description = null): array
|
||||
{
|
||||
$pageName = $name ?: ($page
|
||||
? (method_exists($page, 'translate')
|
||||
? ($page->translate('meta_title') ?: $page->translate('title'))
|
||||
: ($page->meta_title ?? $page->title ?? self::siteName()))
|
||||
: self::siteName());
|
||||
|
||||
$pageDescription = $description ?: ($page
|
||||
? (method_exists($page, 'translate')
|
||||
? ($page->translate('meta_description') ?: ($page->excerpt ?? null))
|
||||
: ($page->meta_description ?? $page->excerpt ?? null))
|
||||
: setting('seo_meta_description'));
|
||||
|
||||
$webPageExtra = [];
|
||||
|
||||
if ($page?->updated_at) {
|
||||
$webPageExtra['dateModified'] = $page->updated_at->toIso8601String();
|
||||
}
|
||||
|
||||
if ($page?->is_homepage) {
|
||||
$webPageExtra['@type'] = 'WebPage';
|
||||
}
|
||||
|
||||
$graph = [
|
||||
self::websiteNode(),
|
||||
self::organizationNode(),
|
||||
self::webPageNode($pageUrl, $pageName, $pageDescription, $webPageExtra),
|
||||
self::breadcrumbNode($pageUrl, self::breadcrumbItems($page, $pageUrl, $pageName)),
|
||||
];
|
||||
|
||||
return self::wrap($graph);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{name: string, item: string}>
|
||||
*/
|
||||
protected static function breadcrumbItems(?Page $page, string $pageUrl, string $pageName): array
|
||||
{
|
||||
$items = [
|
||||
['name' => __('pages.breadcrumb_home'), 'item' => self::siteUrl()],
|
||||
];
|
||||
|
||||
if ($page && !$page->is_homepage) {
|
||||
$items[] = ['name' => $pageName, 'item' => $pageUrl];
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user