Skip to content

Commit

Permalink
Better handle pages that have all nodes in arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
brianlmoon committed Jun 27, 2023
1 parent 59b3c75 commit 06dc9a1
Show file tree
Hide file tree
Showing 4 changed files with 1,307 additions and 1,350 deletions.
31 changes: 17 additions & 14 deletions src/LinkedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,6 @@ protected function fetchData(): array
$ldjson = json_decode($node, true);
if (!empty($ldjson)) {

if (empty($data)) {
$data = $ldjson;
} elseif (isset($ldjson['mainEntityOfPage'])) {
$url = '';
if (is_string($ldjson['mainEntityOfPage'])) {
$url = $ldjson['mainEntityOfPage'];
} elseif (isset($ldjson['mainEntityOfPage']['@id'])) {
$url = $ldjson['mainEntityOfPage']['@id'];
}
if (!empty($url) && $url == $request_uri) {
$data = $ldjson;
}
}

// some pages with multiple ld+json blocks will put
// each block into an array (Flickr does this). Most
// appear to put an object in each ld+json block. To
Expand All @@ -106,6 +92,23 @@ protected function fetchData(): array
$ldjson = [$ldjson];
}

foreach ($ldjson as $node) {
if (empty($data)) {
$data = $node;
} elseif (isset($node['mainEntityOfPage'])) {
$url = '';
if (is_string($node['mainEntityOfPage'])) {
$url = $node['mainEntityOfPage'];
} elseif (isset($node['mainEntityOfPage']['@id'])) {
$url = $node['mainEntityOfPage']['@id'];
}
if (!empty($url) && $url == $request_uri) {
$data = $node;
}
}
}


$this->allData = array_merge($this->allData, $ldjson);
}
}
Expand Down

0 comments on commit 06dc9a1

Please sign in to comment.