| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Shortcodes; |
| 4 |
use WPDeveloper\BetterDocs\Core\Shortcode; |
| 5 |
|
| 6 |
class ReadingTime extends Shortcode { |
| 7 |
public function get_name() { |
| 8 |
return 'betterdocs_reading_time'; |
| 9 |
} |
| 10 |
|
| 11 |
public function get_style_depends() { |
| 12 |
return ['reading-time']; |
| 13 |
} |
| 14 |
|
| 15 |
public function default_attributes() { |
| 16 |
return [ |
| 17 |
'content' => __( "Reading Time", 'betterdocs' ), |
| 18 |
'the_content' => get_the_content() |
| 19 |
]; |
| 20 |
} |
| 21 |
|
| 22 |
public function render( $atts, $content = null ) { |
| 23 |
$this->views( 'shortcodes/reading-time' ); |
| 24 |
} |
| 25 |
|
| 26 |
public function view_params() { |
| 27 |
$single_doc_content = $this->attributes['the_content']; |
| 28 |
$content_without_html_tags = strip_tags( $single_doc_content ); |
| 29 |
preg_match_all('/<[^>]*>|[\p{L}\p{M}]+/u', $content_without_html_tags, $matches ); |
| 30 |
$reading_total_words = ! empty( $matches[0] ) ? count( $matches[0] ) : count( [] ); |
| 31 |
|
| 32 |
//lets assume an adult reads 200 words per minute, based on this information calculate the reading time |
| 33 |
$minutes = floor( $reading_total_words / 200 ); |
| 34 |
$seconds = floor( $reading_total_words % 200 / ( 200 / 60 ) ); |
| 35 |
|
| 36 |
$calculate_time = $minutes . ' ' . __( 'min', 'betterdocs' ) . ' ' . ( $seconds != 0 ? $seconds . ' ' . __( 'seconds', 'betterdocs' ) . ' ' . __( 'read', 'betterdocs' ) : __( 'read', 'betterdocs' ) ); |
| 37 |
|
| 38 |
return [ |
| 39 |
'time' => $calculate_time |
| 40 |
]; |
| 41 |
} |
| 42 |
} |
| 43 |
|