-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow-between.php
67 lines (49 loc) · 1.94 KB
/
show-between.php
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
61
62
63
64
65
66
<?
kirbytext::$pre[] = function($kirbytext, $text) {
$timeZone = c::get('show-between.timezone', date_default_timezone_get());
$fromKeyword = c::get('show-between.from', 'from');
$untilKeyword = c::get('show-between.until', 'until');
$fromOrUntilRegex = '('.$fromKeyword.'|'.$untilKeyword.')';
$dateRegex = '(\d{4}-\d{2}-\d{2})';
$regex = '!\('.$fromOrUntilRegex.': ?'.$dateRegex.'( '.$fromOrUntilRegex.': ?'.$dateRegex.')? (…|\.{3})\)(.*?)\((…|\.{3}) ?'.$fromOrUntilRegex.'\)!is';
$text = preg_replace_callback($regex, function($matches) use($kirbytext, $timeZone, $fromKeyword, $untilKeyword) {
$fromSet = false;
$untilSet = false;
$fromMatch = 2;
$untilMatch = 5;
if($matches[1] === $fromKeyword) {
$fromSet = true;
}
if($matches[1] === $untilKeyword) {
$untilSet = true;
$untilMatch = 2;
}
if($matches[4] === $untilKeyword) {
$untilSet = true;
}
if($matches[4] === $fromKeyword) {
$fromSet = true;
$fromMatch = 5;
}
$now = new DateTime('now', new DateTimeZone($timeZone));
$dateFrom = new DateTime($matches[$fromMatch].' 00:00:00', new DateTimeZone($timeZone));
$diffFrom = $now->diff($dateFrom);
$untilBeforeFrom = false;
$diffUntil = false;
if($untilSet === true) {
$dateUntil = new DateTime($matches[$untilMatch].' 00:00:00', new DateTimeZone($timeZone));
$dateUntil->add(new DateInterval('P1D'));
$dateUntil->sub(new DateInterval('PT1S'));
$diffUntil = $dateUntil->diff($now);
$untilBeforeFrom = ($dateFrom->diff($dateUntil)->invert === 1);
}
$fromCase = ($fromSet && $diffFrom->invert);
$untilCase = ($untilSet && $diffUntil->invert);
if($fromCase && $untilCase || (!$untilSet && $fromCase) || (!$fromSet && $untilCase) || ($untilBeforeFrom && ($fromCase || $untilCase))) {
return $matches[7];
} else {
return '';
}
}, $text);
return $text;
};