forked from php-embed/Embed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPublishedTime.php
More file actions
60 lines (53 loc) · 1.62 KB
/
Copy pathPublishedTime.php
File metadata and controls
60 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
declare(strict_types = 1);
namespace Embed\Detectors;
use Datetime;
class PublishedTime extends Detector
{
public function detect(): ?Datetime
{
$oembed = $this->extractor->getOEmbed();
$metas = $this->extractor->getMetas();
$ld = $this->extractor->getLinkedData();
return $oembed->time('pubdate')
?: $metas->time(
'article:published_time',
'created',
'date',
'datepublished',
'music:release_date',
'video:release_date',
'newsrepublic:publish_date'
)
?: $ld->time(
'pagePublished',
'datePublished'
)
?: $this->detectFromPath()
?: $metas->time(
'pagerender',
'pub_date',
'publication-date',
'lp.article:published_time',
'lp.article:modified_time',
'publish-date',
'rc.datecreation',
'timestamp',
'sailthru.date',
'article:modified_time',
'dcterms.date'
);
}
/**
* Some sites using WordPress have the published time in the url
* For example: mysite.com/2020/05/19/post-title
*/
private function detectFromPath(): ?Datetime
{
$path = $this->extractor->getUri()->getPath();
if (preg_match('#/(19|20)\d{2}/[0-1]?\d/[0-3]?\d/#', $path, $matches)) {
return date_create_from_format('/Y/m/d/', $matches[0]) ?: null;
}
return null;
}
}