normalise($author); } } // If no author-page and h-entry is a permalink then use the rel-author link if ($authorPage === null && $permalink && isset($mf['rels']['author'])) { $authorPage = is_array($mf['rels']['author']) ? $mf['rels']['author'][0] : $mf['rels']['author']; } if ($authorPage === null) { return false; } return $this->findAuthorOnPage($authorPage); } /** * Fetch the author-page and look for a representative h-card on it. * * @throws AuthorshipParserException */ private function findAuthorOnPage(string $authorPage): array|false { try { $response = Http::get($authorPage); } catch (ConnectionException) { throw new AuthorshipParserException('Unable to connect to the author’s page'); } if ($response->failed()) { throw new AuthorshipParserException('Unable to get the content from the author’s page'); } $authorMf2 = parse($response->body(), $authorPage); $hCards = array_filter( $authorMf2['items'] ?? [], fn (array $item): bool => in_array('h-card', $item['type'] ?? [], true) ); // Use the first h-card where url == uid == author-page foreach ($hCards as $hCard) { if ( in_array($authorPage, $hCard['properties']['url'] ?? [], true) && in_array($authorPage, $hCard['properties']['uid'] ?? [], true) ) { return $this->normalise($hCard); } } // Else the first h-card with a url that matches a rel-me link on the page $relMeLinks = $authorMf2['rels']['me'] ?? []; foreach ($hCards as $hCard) { foreach ($hCard['properties']['url'] ?? [] as $url) { if (in_array($url, $relMeLinks, true)) { return $this->normalise($hCard); } } } // Else the first h-card with url == author-page foreach ($hCards as $hCard) { if (in_array($authorPage, $hCard['properties']['url'] ?? [], true)) { return $this->normalise($hCard); } } return false; } private function normalise(array|string $author): array|string { if (is_array($author) && array_key_exists(0, $author)) { $author = $author[0]; } if (is_string($author)) { return $author; } unset($author['value'], $author['children']); return $author; } }