* fix: prevent long strings from overflowing public layout * fix: fall back to default hero copy and tighten hero line-height * fix: skip blank testimonials and guard empty portfolio metadata * feat: add branded 419, 429 and 503 error pages * fix: reset submit state on bfcache restore, swap label while sending, trap mobile menu focus * chore: track hardening task status * fix: restore contact submit state after bfcache * fix: allow contact links to wrap long unbroken strings
151 lines
5.0 KiB
PHP
151 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Application\Data;
|
|
|
|
use App\Models\PortfolioCase;
|
|
use App\Models\SiteSetting;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
final readonly class PageMeta
|
|
{
|
|
/**
|
|
* @param array<string, mixed>|null $jsonLd
|
|
*/
|
|
public function __construct(
|
|
public string $title,
|
|
public string $description,
|
|
public string $canonical,
|
|
public string $ogType = 'website',
|
|
public ?string $ogImageUrl = null,
|
|
public ?string $ogImageAlt = null,
|
|
public ?array $jsonLd = null,
|
|
public string $siteName = '',
|
|
public string $robots = 'index, follow',
|
|
) {}
|
|
|
|
/**
|
|
* @param array<string, mixed>|null $jsonLd
|
|
*/
|
|
public static function forPage(
|
|
string $canonical,
|
|
SiteSetting $settings,
|
|
?string $title = null,
|
|
?string $description = null,
|
|
?string $ogImageUrl = null,
|
|
?string $ogImageAlt = null,
|
|
string $ogType = 'website',
|
|
?array $jsonLd = null,
|
|
): self {
|
|
return new self(
|
|
title: filled($title) ? (string) $title : self::defaultTitle($settings),
|
|
description: filled($description) ? (string) $description : self::defaultDescription($settings),
|
|
canonical: $canonical,
|
|
ogType: $ogType,
|
|
ogImageUrl: $ogImageUrl ?? self::defaultOgImageUrl($settings),
|
|
ogImageAlt: $ogImageAlt ?? $settings->default_og_image_alt,
|
|
jsonLd: $jsonLd,
|
|
siteName: (string) $settings->brand_name,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed>|null $jsonLd
|
|
*/
|
|
public static function forCase(
|
|
PortfolioCase $case,
|
|
string $canonical,
|
|
SiteSetting $settings,
|
|
?array $jsonLd = null,
|
|
): self {
|
|
$title = filled($case->meta_title) ? (string) $case->meta_title : (string) $case->title;
|
|
$description = filled($case->meta_description)
|
|
? (string) $case->meta_description
|
|
: (filled($case->summary) ? (string) $case->summary : self::defaultDescription($settings));
|
|
|
|
$ogImageUrl = filled($case->cover_image_path)
|
|
? url(Storage::disk('public')->url((string) $case->cover_image_path))
|
|
: self::defaultOgImageUrl($settings);
|
|
|
|
$ogImageAlt = filled($case->cover_image_alt)
|
|
? (string) $case->cover_image_alt
|
|
: $settings->default_og_image_alt;
|
|
|
|
return new self(
|
|
title: $title,
|
|
description: $description,
|
|
canonical: $canonical,
|
|
ogType: 'article',
|
|
ogImageUrl: $ogImageUrl,
|
|
ogImageAlt: $ogImageAlt,
|
|
jsonLd: $jsonLd,
|
|
siteName: (string) $settings->brand_name,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Build the metadata for branded error pages.
|
|
*
|
|
* Error pages carry no canonical, are excluded from search indexes and use
|
|
* the page name suffixed with the brand name as their title.
|
|
*/
|
|
public static function forErrorPage(
|
|
SiteSetting $settings,
|
|
int $status = 404,
|
|
): self {
|
|
[$title, $description] = match ($status) {
|
|
500 => ['Algo deu errado', 'Não foi possível concluir o pedido. Tente novamente em instantes.'],
|
|
419 => ['Sessão expirada', 'Sua sessão expirou. Volte e tente enviar novamente.'],
|
|
429 => ['Muitas solicitações', 'Você enviou muitas solicitações em pouco tempo. Aguarde um instante e tente novamente.'],
|
|
503 => ['Em manutenção', 'Estamos realizando uma breve manutenção. Tente novamente em instantes.'],
|
|
default => ['Página não encontrada', 'A página que você procura não existe ou foi movida.'],
|
|
};
|
|
|
|
return new self(
|
|
title: trim($title).' - '.$settings->brand_name,
|
|
description: $description,
|
|
canonical: '',
|
|
robots: 'noindex, nofollow',
|
|
siteName: (string) $settings->brand_name,
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Append the brand name to a page title when it is not already present.
|
|
*/
|
|
public static function withBrandSuffix(string $title, SiteSetting $settings): string
|
|
{
|
|
$brand = filled($settings->default_meta_title)
|
|
? (string) $settings->default_meta_title
|
|
: (string) $settings->brand_name;
|
|
|
|
if (str_contains($title, $brand)) {
|
|
return $title;
|
|
}
|
|
|
|
return trim($title).' - '.$brand;
|
|
}
|
|
|
|
private static function defaultTitle(SiteSetting $settings): string
|
|
{
|
|
return filled($settings->default_meta_title)
|
|
? (string) $settings->default_meta_title
|
|
: (string) $settings->brand_name;
|
|
}
|
|
|
|
private static function defaultDescription(SiteSetting $settings): string
|
|
{
|
|
return (string) ($settings->default_meta_description ?? '');
|
|
}
|
|
|
|
private static function defaultOgImageUrl(SiteSetting $settings): ?string
|
|
{
|
|
if (! filled($settings->default_og_image_path)) {
|
|
return null;
|
|
}
|
|
|
|
return url(Storage::disk('public')->url((string) $settings->default_og_image_path));
|
|
}
|
|
}
|