Giter VIP home page Giter VIP logo

post-reading-time-block's People

Contributors

richtabor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

post-reading-time-block's Issues

Support for Chinese characters

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;

Showing "hours" for longer works

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.