PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.3.2
Admin Columns v4.3.2
7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Settings / Column / WordsPerMinute.php
codepress-admin-columns / classes / Settings / Column Last commit date
BeforeAfter 5 years ago Pro 5 years ago ActionIcons.php 5 years ago AttachmentDisplay.php 5 years ago BeforeAfter.php 5 years ago CharacterLimit.php 5 years ago Comment.php 5 years ago CommentCount.php 5 years ago CommentLink.php 5 years ago CustomField.php 5 years ago CustomFieldType.php 5 years ago Date.php 5 years ago DateTimeFormat.php 5 years ago ExifData.php 5 years ago Image.php 5 years ago Images.php 5 years ago Label.php 5 years ago LinkLabel.php 5 years ago LinkToMenu.php 5 years ago MediaLink.php 5 years ago Message.php 5 years ago Meta.php 5 years ago MissingImageSize.php 5 years ago NumberFormat.php 5 years ago NumberOfItems.php 5 years ago Password.php 5 years ago PathScope.php 5 years ago Post.php 5 years ago PostFormatIcon.php 5 years ago PostLink.php 5 years ago PostStatus.php 5 years ago PostType.php 5 years ago Pro.php 5 years ago Separator.php 5 years ago StatusIcon.php 5 years ago StringLimit.php 5 years ago Taxonomy.php 5 years ago Term.php 5 years ago TermLink.php 5 years ago Time.php 5 years ago Toggle.php 5 years ago Type.php 5 years ago User.php 5 years ago UserLink.php 5 years ago Width.php 5 years ago WordLimit.php 5 years ago WordsPerMinute.php 5 years ago
WordsPerMinute.php
125 lines
1 <?php
2
3 namespace AC\Settings\Column;
4
5 use AC\Settings;
6 use AC\View;
7
8 class WordsPerMinute extends Settings\Column
9 implements Settings\FormatValue {
10
11 /**
12 * @var int
13 */
14 private $words_per_minute;
15
16 protected function define_options() {
17 return [
18 'words_per_minute' => 200,
19 ];
20 }
21
22 public function create_view() {
23 $setting = $this->create_element( 'number' );
24 $setting
25 ->set_attributes( [
26 'min' => 0,
27 'step' => 1,
28 'placeholder' => $this->get_words_per_minute(),
29 ] );
30
31 $view = new View( [
32 'label' => __( 'Words per minute', 'codepress-admin-columns' ),
33 'tooltip' => __( 'Estimated reading time in words per minute.', 'codepress-admin-columns' ) . ' ' . sprintf( __( 'By default: %s', 'codepress-admin-columns' ), $this->get_words_per_minute() ),
34 'setting' => $setting,
35 ] );
36
37 return $view;
38 }
39
40 /**
41 * @return int
42 */
43 public function get_words_per_minute() {
44 return absint( $this->words_per_minute );
45 }
46
47 /**
48 * @param int $words_per_minute
49 *
50 * @return $this
51 */
52 public function set_words_per_minute( $words_per_minute ) {
53 $this->words_per_minute = $words_per_minute;
54
55 return $this;
56 }
57
58 /**
59 * Create a human readable time based on seconds
60 *
61 * @param int $seconds
62 *
63 * @return string
64 * @since 3.0
65 */
66 protected function make_human_readable( $seconds ) {
67 $time = false;
68
69 if ( is_numeric( $seconds ) ) {
70 $minutes = floor( $seconds / 60 );
71 $seconds = floor( $seconds % 60 );
72
73 $time = $minutes;
74
75 if ( $minutes && $seconds < 10 ) {
76 $seconds = '0' . $seconds;
77 }
78
79 if ( '00' != $seconds ) {
80 $time .= ':' . $seconds;
81 }
82
83 if ( $minutes < 1 ) {
84 $time = $seconds . ' ' . _n( 'second', 'seconds', $seconds, 'codepress-admin-columns' );
85 } else {
86 $time .= ' ' . _n( 'minute', 'minutes', $minutes, 'codepress-admin-columns' );
87 }
88 }
89
90 return $time;
91 }
92
93 /**
94 * Return the seconds required to read this string based on average words per minute
95 *
96 * @param $string
97 *
98 * @return int
99 */
100 protected function get_estimated_reading_time_in_seconds( $string ) {
101 if ( $this->get_words_per_minute() <= 0 ) {
102 return false;
103 }
104
105 $word_count = ac_helper()->string->word_count( $string );
106
107 if ( ! $word_count ) {
108 return false;
109 }
110
111 $seconds = (int) floor( ( $word_count / $this->get_words_per_minute() ) * 60 );
112
113 // No one can read a word in 0 seconds ;)
114 if ( $seconds < 1 ) {
115 $seconds = 1;
116 }
117
118 return $seconds;
119 }
120
121 public function format( $value, $original_value ) {
122 return $this->make_human_readable( $this->get_estimated_reading_time_in_seconds( $value ) );
123 }
124
125 }