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