Fix canonical url not working
This commit is contained in:
parent
946f64851c
commit
e3bbd3d0ae
1 changed files with 39 additions and 0 deletions
39
src/classes/emebd-uri-interface.php
Normal file
39
src/classes/emebd-uri-interface.php
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the canonical URL
|
||||||
|
*
|
||||||
|
* @see https://github.com/oscarotero/Embed/issues/478
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Embed\Detectors;
|
||||||
|
|
||||||
|
use Psr\Http\Message\UriInterface;
|
||||||
|
|
||||||
|
class Url extends Detector
|
||||||
|
{
|
||||||
|
public function detect(): UriInterface
|
||||||
|
{
|
||||||
|
$oembed = $this->extractor->getOEmbed();
|
||||||
|
|
||||||
|
return $oembed->url('url')
|
||||||
|
?: $oembed->url('web_page')
|
||||||
|
?: $this->getCanonical()
|
||||||
|
?: $this->extractor->getUri();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCanonical(): ?UriInterface
|
||||||
|
{
|
||||||
|
$document = $this->extractor->getDocument();
|
||||||
|
|
||||||
|
foreach ($document->select('.//link[@canonical]')->nodes() as $node) {
|
||||||
|
$href = $node->getAttribute('href');
|
||||||
|
|
||||||
|
if ($href) {
|
||||||
|
return $this->extractor->resolveUri($href);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue