Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
19 changes: 18 additions & 1 deletion src/Eccube/EventListener/TwigInitializeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Eccube\Repository\PageLayoutRepository;
use Eccube\Repository\PageRepository;
use Eccube\Request\Context;
use Eccube\Service\SiteStructuredDataService;
use Eccube\Service\SystemService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
Expand All @@ -41,6 +42,13 @@

class TwigInitializeListener implements EventSubscriberInterface
{
/**
* サイト共通の構造化データ(WebSite / Organization)を出力するルート名.
*
* トップページと「当サイトについて」の 2 ページに限定する.
*/
private const SITE_STRUCTURED_DATA_ROUTES = ['homepage', 'help_about'];

/**
* @var bool 初期化済かどうか.
*/
Expand All @@ -49,7 +57,7 @@ class TwigInitializeListener implements EventSubscriberInterface
/**
* TwigInitializeListener constructor.
*/
public function __construct(protected Environment $twig, protected BaseInfoRepository $baseInfoRepository, protected PageRepository $pageRepository, protected PageLayoutRepository $pageLayoutRepository, protected BlockPositionRepository $blockPositionRepository, protected DeviceTypeRepository $deviceTypeRepository, private readonly AuthorityRoleRepository $authorityRoleRepository, private EccubeConfig $eccubeConfig, protected Context $requestContext, private readonly MobileDetect $mobileDetector, private readonly UrlGeneratorInterface $router, private readonly LayoutRepository $layoutRepository, protected SystemService $systemService)
public function __construct(protected Environment $twig, protected BaseInfoRepository $baseInfoRepository, protected PageRepository $pageRepository, protected PageLayoutRepository $pageLayoutRepository, protected BlockPositionRepository $blockPositionRepository, protected DeviceTypeRepository $deviceTypeRepository, private readonly AuthorityRoleRepository $authorityRoleRepository, private EccubeConfig $eccubeConfig, protected Context $requestContext, private readonly MobileDetect $mobileDetector, private readonly UrlGeneratorInterface $router, private readonly LayoutRepository $layoutRepository, protected SystemService $systemService, private readonly SiteStructuredDataService $siteStructuredDataService)
{
}

Expand Down Expand Up @@ -177,6 +185,15 @@ public function setFrontVariables(RequestEvent $event): void
$this->twig->addGlobal('title', $Page->getName());
$this->twig->addGlobal('isMaintenance', $this->systemService->isMaintenanceMode());
$this->twig->addGlobal('isDebugMode', env('APP_DEBUG'));
// サイト共通の構造化データ(WebSite / Organization)はトップページと「当サイトについて」にだけ出力する。
// Google の Organization ドキュメントが「トップページか組織を説明する単一ページを推奨。
// サイトの全ページに含める必要はない」としているため、対象外では組み立て自体を行わない。
$this->twig->addGlobal(
'site_json_ld',
in_array($route, self::SITE_STRUCTURED_DATA_ROUTES, true)
? $this->siteStructuredDataService->createWebSiteJsonLd($this->baseInfoRepository->get())
: []
);
}

public function setAdminGlobals(RequestEvent $event): void
Expand Down
3 changes: 3 additions & 0 deletions src/Eccube/Resource/template/default/default_frame.twig
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,8 @@ file that was distributed with this source code.
{{ include('snippet.twig', { snippets: plugin_snippets }) }}
{% endif %}
<script src="{{ asset('assets/js/customize.js', 'user_data') }}"></script>
{% if site_json_ld is defined and site_json_ld %}
<script type="application/ld+json">{{ site_json_ld|json_ld }}</script>
{% endif %}
Comment thread
nanasess marked this conversation as resolved.
</body>
</html>
11 changes: 3 additions & 8 deletions src/Eccube/Service/ProductStructuredDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
class ProductStructuredDataService
{
use StructuredDataDescriptionTrait;

/**
* 画像が無い場合のフォールバック画像ファイル名.
*/
Expand Down Expand Up @@ -124,15 +126,8 @@ private function buildDescription(Product $Product): string
if ($description === null || $description === '') {
$description = $Product->getDescriptionDetail();
}
if ($description === null || $description === '') {
return '';
}

$description = strip_tags($description);
$description = preg_replace('/\s+/u', ' ', $description) ?? $description;
$description = trim($description);

return mb_substr($description, 0, 300);
return $this->normalizeDescription($description);
}

/**
Expand Down
280 changes: 280 additions & 0 deletions src/Eccube/Service/SiteStructuredDataService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Service;

use Eccube\Common\EccubeConfig;
use Eccube\Entity\BaseInfo;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

/**
* サイト共通の構造化データ(JSON-LD / schema.org WebSite・Organization)を組み立てる Service.
*
* 店舗設定(BaseInfo)から WebSite / Organization の連想配列を組み立てて返す。
* 戻り値は EccubeExtension の `json_ld` フィルタ経由で出力し、値に含まれる
* `" < > &` などを機械的にエスケープする(テンプレート文字列補間による JSON 破壊・XSS を防ぐ)。
*
* 値が空の任意プロパティは出力しない(ProductStructuredDataService と同じ方針)。
*/
class SiteStructuredDataService
{
use StructuredDataDescriptionTrait;

/**
* ロゴに用いる画像ファイル(帳票 PDF と共通の店舗ロゴ).
*
* ファビコン(ICO)は Google Images の対応形式に含まれないため使用しない.
*/
private const LOGO_FILE = 'assets/pdf/logo.png';

public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Packages $packages,
private readonly EccubeConfig $eccubeConfig,
) {
}

/**
* サイト共通の JSON-LD 構造を組み立てて返す.
*
* WebSite と Organization は入れ子にせず `@graph` で並列のトップレベルノードとして出力し,
* `WebSite.publisher` から `@id` で Organization を参照する
* (schema.org の `author` は「作品の著者」であり、サイトを発行している組織は `publisher` が適切.
* また `@context` を `@graph` の外側に 1 つだけ置くことで重複定義も避ける).
*
* @return array<string, mixed>
*/
public function createWebSiteJsonLd(BaseInfo $BaseInfo): array
{
return [
'@context' => 'https://schema.org',
'@graph' => [
$this->createWebSiteNode($BaseInfo),
$this->createOrganizationJsonLd($BaseInfo),
],
];
}

/**
* WebSite ノードを組み立てて返す(`@context` は持たない).
*
* @return array<string, mixed>
*/
private function createWebSiteNode(BaseInfo $BaseInfo): array
{
$siteUrl = $this->generateAbsoluteUrl('homepage');

$data = [
'@type' => 'WebSite',
'@id' => $siteUrl.'#website',
'name' => (string) $BaseInfo->getShopName(),
];
$this->addIfNotEmpty($data, 'alternateName', $BaseInfo->getShopNameEng());
$data['url'] = $siteUrl;
$this->addIfNotEmpty($data, 'description', $this->normalizeDescription($BaseInfo->getGoodTraded()));
$data['potentialAction'] = [
'@type' => 'SearchAction',
'target' => [
'@type' => 'EntryPoint',
'urlTemplate' => $this->generateAbsoluteUrl('product_list').'?name={search_term_string}',
],
'query-input' => 'required name=search_term_string',
];
$data['publisher'] = ['@id' => $siteUrl.'#organization'];

return $data;
}

/**
* Organization の JSON-LD 構造を組み立てて返す(`@context` は持たない).
*
* @return array<string, mixed>
*/
public function createOrganizationJsonLd(BaseInfo $BaseInfo): array
{
$siteUrl = $this->generateAbsoluteUrl('homepage');

$data = [
'@type' => 'Organization',
'@id' => $siteUrl.'#organization',
'url' => $siteUrl,
'logo' => [
'@type' => 'ImageObject',
'contentUrl' => $this->buildLogoUrl(),
],
'name' => (string) $BaseInfo->getShopName(),
];
$this->addIfNotEmpty($data, 'alternateName', $BaseInfo->getShopNameEng());
$this->addIfNotEmpty($data, 'legalName', $BaseInfo->getCompanyName());
$this->addIfNotEmpty($data, 'description', $this->normalizeDescription($BaseInfo->getMessage()));
// email01 は送信元(From)かつ全送信メールの BCC 先で、送信専用や店舗内部の運用アドレスが
// 入る前提の項目なので公開しない。公開して良い連絡先は email02(問い合わせ専用)。
$this->addIfNotEmpty($data, 'email', $BaseInfo->getEmail02());

// phone_number は PhoneNumberType の TruncateHyphenListener と Assert\Type('digit') により
// ハイフンなしの数字列(例 0312345678)で保存される。国番号 +81 を前置すると国内トランク
// プレフィックスの 0 が残った不正な値(+81-0312345678)になるため、保存値をそのまま出力する。
$phoneNumber = $BaseInfo->getPhoneNumber();
if ($phoneNumber !== null && $phoneNumber !== '') {
$data['telephone'] = $phoneNumber;
}
Comment thread
nanasess marked this conversation as resolved.

$address = $this->buildAddress($BaseInfo);
if ($address !== null) {
$data['address'] = $address;
}

$contactPoint = $this->buildContactPoint($BaseInfo, $phoneNumber);
if ($contactPoint !== null) {
$data['contactPoint'] = $contactPoint;
}

$invoiceRegistrationNumber = $BaseInfo->getInvoiceRegistrationNumber();
if ($invoiceRegistrationNumber !== null && $invoiceRegistrationNumber !== '') {
$data['iso6523Code'] = '0221:'.$invoiceRegistrationNumber;
}

return $data;
}

/**
* PostalAddress 構造を組み立てる(住所要素が1つも無ければ null).
*
* @return array<string, mixed>|null
*/
private function buildAddress(BaseInfo $BaseInfo): ?array
{
$address = ['@type' => 'PostalAddress'];
$this->addIfNotEmpty($address, 'streetAddress', $BaseInfo->getAddr02());
$this->addIfNotEmpty($address, 'addressLocality', $BaseInfo->getAddr01());
$this->addIfNotEmpty($address, 'addressRegion', $BaseInfo->getPref()?->getName());
$this->addIfNotEmpty($address, 'postalCode', $BaseInfo->getPostalCode());

// @type 以外に住所要素が無ければ出力しない
if (count($address) === 1) {
return null;
}

$address['addressCountry'] = 'JP';

return $address;
}

/**
* ContactPoint 構造を組み立てる(連絡手段が1つも無ければ null).
*
* @return array<string, mixed>|null
*/
private function buildContactPoint(BaseInfo $BaseInfo, ?string $phoneNumber): ?array
{
// 参照先が email02(問い合わせ専用メールアドレス)と contact(お問い合わせフォーム)なので
// 用途は問い合わせ窓口で確定している
$contactPoint = [
'@type' => 'ContactPoint',
'contactType' => 'customer support',
];
if ($phoneNumber !== null && $phoneNumber !== '') {
$contactPoint['telephone'] = $phoneNumber;
}
$this->addIfNotEmpty($contactPoint, 'email', $BaseInfo->getEmail02());

// telephone / email が無ければ url だけの ContactPoint は出力しない
if (!isset($contactPoint['telephone']) && !isset($contactPoint['email'])) {
return null;
}

$contactPoint['url'] = $this->generateAbsoluteUrl('contact');

return $contactPoint;
}

/**
* 店舗ロゴの絶対URLを生成する.
*
* 参照先は帳票 PDF と同じ店舗ロゴで、優先順も `OrderPdfService` に揃える.
* user_data に配置されていればそれを使い、無ければ既定ロゴ(管理画面テンプレート同梱)へフォールバックする.
*/
private function buildLogoUrl(): string
{
$userDataLogo = $this->eccubeConfig->get('eccube_html_dir').'/user_data/'.self::LOGO_FILE;

if (file_exists($userDataLogo)) {
return $this->generateAbsoluteAssetUrl(self::LOGO_FILE, 'user_data');
}

return $this->generateAbsoluteAssetUrl(self::LOGO_FILE, 'admin');
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
}

/**
* ルート名から絶対URLを生成する.
*/
private function generateAbsoluteUrl(string $route): string
{
return $this->urlGenerator->generate($route, [], UrlGeneratorInterface::ABSOLUTE_URL);
}

/**
* アセットの絶対URLを生成する.
*
* Packages::getUrl() は既定の設定ではルート相対パス(/html/user_data/...)を返すため,
* 構造化データではスキーム込みホストを前置して絶対URLにする
* (ProductStructuredDataService の画像URLと同じ方針).
* asset パッケージに base_urls(CDN 等)が設定されている場合は既に絶対URLなのでそのまま返す.
* ただしプロトコル相対URL(`//cdn.example.com/...`)はスキームを持たず、JSON-LD 上は
* `@base` の無い相対 IRI として解決先が曖昧になるため、スキームを補って絶対URLにする.
*/
private function generateAbsoluteAssetUrl(string $path, string $package): string
{
$url = $this->packages->getUrl($path, $package);

if (str_starts_with($url, 'http://') || str_starts_with($url, 'https://')) {
return $url;
}

if (str_starts_with($url, '//')) {
return $this->urlGenerator->getContext()->getScheme().':'.$url;
}

return $this->getSchemeAndHttpHost().$url;
}
Comment thread
nanasess marked this conversation as resolved.

/**
* ルーティングコンテキストからスキーム込みホスト(例: https://example.com)を返す.
*/
private function getSchemeAndHttpHost(): string
{
$context = $this->urlGenerator->getContext();
$scheme = $context->getScheme();
$host = $context->getHost();
$port = $scheme === 'https' ? $context->getHttpsPort() : $context->getHttpPort();

if (($scheme === 'http' && $port !== 80) || ($scheme === 'https' && $port !== 443)) {
$host .= ':'.$port;
}

return $scheme.'://'.$host;
}

/**
* 値が null / 空文字でない場合のみ連想配列へ追加する.
*
* @param array<string, mixed> $data
*/
private function addIfNotEmpty(array &$data, string $key, ?string $value): void
{
if ($value !== null && $value !== '') {
$data[$key] = $value;
}
}
}
Loading
Loading