This repository has been archived by the owner on Jan 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
readingtime.php
57 lines (49 loc) · 1.62 KB
/
readingtime.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
<?php
/**
* Reading Time
*
* A simple plugin that estimates the reading
* time for any text
*
* Sample Usage:
*
* <?php echo $page->text()->readingtime() ?>
*
* @author Roy Lodder <http://roylodder.com>, Bastian Allgeier <http://getkirby.com>
* @version 2.1.0
*/
function readingtime($content, $params = array()) {
$defaults = array(
'minute' => 'minute',
'minutes' => 'minutes',
'second' => 'second',
'seconds' => 'seconds',
'format' => '{minutesCount} {minutesLabel}, {secondsCount} {secondsLabel}',
'format.alt' => '{secondsCount} {secondsLabel}',
'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 = array(
'minutesCount' => $minutesCount,
'minutesLabel' => $minutesLabel,
'secondsCount' => $secondsCount,
'secondsLabel' => $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;
}
field::$methods['readingtime'] = function($field, $params = array()) {
return readingtime($field->value, $params);
};