-
Notifications
You must be signed in to change notification settings - Fork 0
/
reading-time.php
executable file
·49 lines (42 loc) · 1.52 KB
/
reading-time.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
<?php
/**
* Fansoro Reading Time Plugin
*
* (c) Romanenko Sergey / Awilum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
function readingTime($content, array $params = [])
{
$defaults = [
'minute' => 'minute',
'minutes' => 'minutes',
'second' => 'second',
'seconds' => 'seconds',
'format' => '{minutes_count} {minutes_label}, {seconds_count} {seconds_label}',
'format.alt' => '{seconds_count} {seconds_label}',
'format.alt.enable' => false
];
$options = array_merge($defaults, $params);
$words = str_word_count(strip_tags($content));
$minutesCount = floor($words / 200);
$secondsCount = floor($words % 200 / (200 / 60));
$minutesLabel = ($minutesCount <= 1) ? $options['minute'] : $options['minutes'];
$secondsLabel = ($secondsCount <= 1) ? $options['second'] : $options['seconds'];
$replace = [
'minutes_count' => $minutesCount,
'minutes_label' => $minutesLabel,
'seconds_count' => $secondsCount,
'seconds_label' => $secondsLabel,
];
if ($minutesCount < 1 and $options['format.alt.enable'] === true) {
$result = $options['format.alt'];
} else {
$result = $options['format'];
}
foreach ($replace as $key => $value) {
$result = str_replace('{' . $key . '}', $value, $result);
}
return $result;
}