Skip to content

Commit 06dc9a1

Browse files
committed
Better handle pages that have all nodes in arrays
1 parent 59b3c75 commit 06dc9a1

File tree

4 files changed

+1307
-1350
lines changed

4 files changed

+1307
-1350
lines changed

src/LinkedData.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ protected function fetchData(): array
8383
$ldjson = json_decode($node, true);
8484
if (!empty($ldjson)) {
8585

86-
if (empty($data)) {
87-
$data = $ldjson;
88-
} elseif (isset($ldjson['mainEntityOfPage'])) {
89-
$url = '';
90-
if (is_string($ldjson['mainEntityOfPage'])) {
91-
$url = $ldjson['mainEntityOfPage'];
92-
} elseif (isset($ldjson['mainEntityOfPage']['@id'])) {
93-
$url = $ldjson['mainEntityOfPage']['@id'];
94-
}
95-
if (!empty($url) && $url == $request_uri) {
96-
$data = $ldjson;
97-
}
98-
}
99-
10086
// some pages with multiple ld+json blocks will put
10187
// each block into an array (Flickr does this). Most
10288
// appear to put an object in each ld+json block. To
@@ -106,6 +92,23 @@ protected function fetchData(): array
10692
$ldjson = [$ldjson];
10793
}
10894

95+
foreach ($ldjson as $node) {
96+
if (empty($data)) {
97+
$data = $node;
98+
} elseif (isset($node['mainEntityOfPage'])) {
99+
$url = '';
100+
if (is_string($node['mainEntityOfPage'])) {
101+
$url = $node['mainEntityOfPage'];
102+
} elseif (isset($node['mainEntityOfPage']['@id'])) {
103+
$url = $node['mainEntityOfPage']['@id'];
104+
}
105+
if (!empty($url) && $url == $request_uri) {
106+
$data = $node;
107+
}
108+
}
109+
}
110+
111+
109112
$this->allData = array_merge($this->allData, $ldjson);
110113
}
111114
}

0 commit comments

Comments
 (0)