WordsPerMinute.php
2.55 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace AC\Settings\Column;
use AC\Settings;
use AC\View;
class WordsPerMinute extends Settings\Column
implements Settings\FormatValue {
/**
* @var int
*/
private $words_per_minute;
protected function define_options() {
return [
'words_per_minute' => 200,
];
}
public function create_view() {
$setting = $this->create_element( 'number' );
$setting
->set_attributes( [
'min' => 0,
'step' => 1,
'placeholder' => $this->get_words_per_minute(),
] );
$view = new View( [
'label' => __( 'Words per minute', 'codepress-admin-columns' ),
'tooltip' => __( 'Estimated reading time in words per minute.', 'codepress-admin-columns' ) . ' ' . sprintf( __( 'By default: %s', 'codepress-admin-columns' ), $this->get_words_per_minute() ),
'setting' => $setting,
] );
return $view;
}
/**
* @return int
*/
public function get_words_per_minute() {
return absint( $this->words_per_minute );
}
/**
* @param int $words_per_minute
*
* @return $this
*/
public function set_words_per_minute( $words_per_minute ) {
$this->words_per_minute = $words_per_minute;
return $this;
}
/**
* Create a human readable time based on seconds
*
* @param int $seconds
*
* @return string
* @since 3.0
*/
protected function make_human_readable( $seconds ) {
$time = false;
if ( is_numeric( $seconds ) ) {
$minutes = floor( $seconds / 60 );
$seconds = floor( $seconds % 60 );
$time = $minutes;
if ( $minutes && $seconds < 10 ) {
$seconds = '0' . $seconds;
}
if ( '00' != $seconds ) {
$time .= ':' . $seconds;
}
if ( $minutes < 1 ) {
$time = $seconds . ' ' . _n( 'second', 'seconds', $seconds, 'codepress-admin-columns' );
} else {
$time .= ' ' . _n( 'minute', 'minutes', $minutes, 'codepress-admin-columns' );
}
}
return $time;
}
/**
* Return the seconds required to read this string based on average words per minute
*
* @param $string
*
* @return int
*/
protected function get_estimated_reading_time_in_seconds( $string ) {
if ( $this->get_words_per_minute() <= 0 ) {
return false;
}
$word_count = ac_helper()->string->word_count( $string );
if ( ! $word_count ) {
return false;
}
$seconds = (int) floor( ( $word_count / $this->get_words_per_minute() ) * 60 );
// No one can read a word in 0 seconds ;)
if ( $seconds < 1 ) {
$seconds = 1;
}
return $seconds;
}
public function format( $value, $original_value ) {
return $this->make_human_readable( $this->get_estimated_reading_time_in_seconds( $value ) );
}
}