Skip to content

Commit fc00be2

Browse files
authored
Merge pull request #880 from haxtheweb/fix/jsonld-organization-complete
[haxcms-php] Organization JSON-LD: the site description and its Contact page
2 parents 0766a25 + cb60645 commit fc00be2

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎system/backend/php/lib/HAXCMSSite.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,26 @@ public function respondWithRequestedPageVariant()
27432743
}
27442744
return false;
27452745
}
2746+
/**
2747+
* The absolute URL of the site's Contact page for structured data: the
2748+
* published page whose slug is "contact", or '' when there is none.
2749+
* $toAbsolute turns a site-relative slug into an absolute URL.
2750+
*/
2751+
private function getContactPageUrlForStructuredData($toAbsolute)
2752+
{
2753+
$items = isset($this->manifest->items) && is_array($this->manifest->items) ? $this->manifest->items : array();
2754+
foreach ($items as $item) {
2755+
if (!is_object($item) || !isset($item->slug) || !is_string($item->slug)) {
2756+
continue;
2757+
}
2758+
$slug = trim($item->slug, '/');
2759+
if (strtolower($slug) === 'contact' && SiteRouteUtils::isItemPublished($item)) {
2760+
return $toAbsolute($slug);
2761+
}
2762+
}
2763+
return '';
2764+
}
2765+
27462766
/**
27472767
* Whether a page is published: what the shell and the page variants,
27482768
* served without a login, may show (SiteRouteUtils::isItemPublished).
@@ -3469,6 +3489,21 @@ public function getSiteMetadata($page = NULL, $domain = NULL, $cdn = '') {
34693489
if (count($siteSameAs) > 0) {
34703490
$publisherNode['sameAs'] = $siteSameAs;
34713491
}
3492+
// what the site is, in its own words
3493+
$siteDescription = isset($this->manifest->description) ? trim((string) $this->manifest->description) : '';
3494+
if ($siteDescription != '') {
3495+
$publisherNode['description'] = $siteDescription;
3496+
}
3497+
// how to reach the site: its published Contact page (slug "contact"),
3498+
// never the author's email
3499+
$contactUrl = $this->getContactPageUrlForStructuredData($toAbsoluteStructuredDataUrl);
3500+
if ($contactUrl != '') {
3501+
$publisherNode['contactPoint'] = array(
3502+
'@type' => 'ContactPoint',
3503+
'contactType' => 'customer support',
3504+
'url' => $contactUrl,
3505+
);
3506+
}
34723507
if ($authorNode !== null) {
34733508
$publisherNode['founder'] = array(
34743509
'@id' => $authorNode['@id'],

‎system/backend/php/tests/Unit/HAXCMSSiteMetadataTest.php‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,34 @@ public function testGetSiteMetadataJsonLdSameAsOnOrganizationAndPerson(): void
551551
$this->assertArrayNotHasKey('sameAs', $this->graphNodeByType($plain, 'Organization'));
552552
}
553553

554+
public function testGetSiteMetadataJsonLdOrganizationDescriptionAndContactPoint(): void
555+
{
556+
// same behavior as haxcms-nodejs: the site's description, and its
557+
// published Contact page (never the author's email) as contactPoint
558+
$this->site->manifest->description = ' A site about units. ';
559+
$contact = $this->site->manifest->items[1];
560+
$contact->slug = '/contact/';
561+
$contact->metadata->published = true;
562+
$org = $this->graphNodeByType($this->extractJsonLd($this->site->getSiteMetadata($this->homePage())), 'Organization');
563+
$this->assertSame('A site about units.', $org['description']);
564+
$this->assertSame(
565+
array('@type' => 'ContactPoint', 'contactType' => 'customer support', 'url' => 'https://meta.example.com/contact'),
566+
$org['contactPoint']
567+
);
568+
$this->assertDoesNotMatchRegularExpression('/mailto:|[\w.+-]+@[\w-]+\.[\w.]+/', json_encode($org));
569+
// an unpublished Contact page is not offered
570+
$contact->metadata->published = false;
571+
$org = $this->graphNodeByType($this->extractJsonLd($this->site->getSiteMetadata($this->homePage())), 'Organization');
572+
$this->assertArrayNotHasKey('contactPoint', $org);
573+
// no description and no Contact page: neither key
574+
$this->site->manifest->description = '';
575+
$contact->slug = 'elsewhere';
576+
$contact->metadata->published = true;
577+
$org = $this->graphNodeByType($this->extractJsonLd($this->site->getSiteMetadata($this->homePage())), 'Organization');
578+
$this->assertArrayNotHasKey('description', $org);
579+
$this->assertArrayNotHasKey('contactPoint', $org);
580+
}
581+
554582
public function testGetSiteMetadataJsonLdOmitsPersonWithoutAuthor(): void
555583
{
556584
$this->site->manifest->author = '';

0 commit comments

Comments
 (0)