richtabor / post-reading-time-block Goto Github PK
View Code? Open in Web Editor NEWLicense: GNU General Public License v2.0
License: GNU General Public License v2.0
There’s the option to change font colors but not the font size.
delete please
Here's a zip for the beta release of the block plugin:
I just wrote about this plugin on the Tavern. :) In it, I mentioned the need for an "hours" format for long-long-form works. Since I literally built something for this just recently on another system, I figured I'd at least share the code. Feel free to use use if you feel like this would be a useful feature. It probably needs a little cleanup. :)
The Str::nText()
method call would be the equivalent of WP's _n()
function. $this->content()
would be the post content. I think everything else should be easily portable.
Also, I'm using number_format()
to get the thousands separator (WP should have a i18n function for that).
public function readingTime( int $words_per_min = 200 ): string
{
$words_per_hour = $words_per_min * 60;
// Strip tags and get the word count from the content.
$count = str_word_count( strip_tags( $this->content() ) );
// Get the floor for hours. Otherwise, it will round up to
// `1.0`. But, get the ceiling for minutes.
$time_hours = intval( floor( $count / $words_per_hour ) );
$time_mins = intval( ceil( $count / $words_per_min ) );
// If there are no hours, just return the minutes.
if ( 0 >= $time_hours ) {
return sprintf(
Str::nText( '%d Minute', '%d Minutes', $time_mins ),
number_format( $time_mins )
);
}
// Subtract the hours by minute from the total minutes.
$time_mins = $time_mins - ( $time_hours * 60 );
// Set up text for hours.
$text_hours = sprintf(
Str::nText( '%d Hour', '%d Hours', $time_hours ),
number_format( $time_hours )
);
// Set up text for minutes.
$text_mins = sprintf(
Str::nText( '%d Minute', '%d Minutes', $time_mins ),
number_format( $time_mins )
);
// If no minutes left after subtraction of hours, return hours.
if ( 0 >= $time_mins ) {
return $text_hours;
}
// Merge hours + minutes text.
return sprintf( '%s, %s', $text_hours, $text_mins );
}
I’m using this plugin on a site with Chinese articles and noticed that it doesn’t get the word count, so I modified the code to get the string length for Chinese characters and calculate base on the average Chinese reading speed. I’m unsure of how multilingual support is planned so I’ll just sharing the modification here.
$content = get_post_field( 'post_content', $post_ID );
$word_count = mb_strlen( strip_tags( $content ), 'UTF-8' );
$readingtime = ceil( $word_count / 400 );
$text = __( '阅读时长', 'post-reading-time-block' );
$timetext = __( '分钟', 'post-reading-time-block' );
$totalreadingtime = $text . ' ' . $readingtime . ' ' . $timetext;
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.