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 | } |