first commit
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Reading Time
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules
|
||||
*/
|
||||
|
||||
if ( class_exists( 'Powerkit_Module' ) ) {
|
||||
/**
|
||||
* Init module
|
||||
*/
|
||||
class Powerkit_Reading_Time extends Powerkit_Module {
|
||||
|
||||
/**
|
||||
* Register module
|
||||
*/
|
||||
public function register() {
|
||||
$this->name = esc_html__( 'Reading Time', 'powerkit' );
|
||||
$this->desc = esc_html__( 'Let’s you easily add an estimated reading time to your WordPress posts.', 'powerkit' );
|
||||
$this->slug = 'reading_time';
|
||||
$this->type = 'default';
|
||||
$this->category = 'tools';
|
||||
$this->priority = 140;
|
||||
$this->public = true;
|
||||
$this->enabled = true;
|
||||
$this->links = array(
|
||||
array(
|
||||
'name' => esc_html__( 'View documentation', 'powerkit' ),
|
||||
'url' => powerkit_get_setting( 'documentation' ) . '/reading-time/',
|
||||
'target' => '_blank',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize module
|
||||
*/
|
||||
public function initialize() {
|
||||
|
||||
/* Load the required dependencies for this module */
|
||||
|
||||
// Helpers Functions for the module.
|
||||
require_once dirname( __FILE__ ) . '/helpers/helper-powerkit-reading-time.php';
|
||||
|
||||
// Admin and public area.
|
||||
require_once dirname( __FILE__ ) . '/public/class-powerkit-reading-time-public.php';
|
||||
|
||||
new Powerkit_Reading_Time_Public( $this->slug );
|
||||
}
|
||||
}
|
||||
|
||||
new Powerkit_Reading_Time();
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Helpers Reading Time
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/Helper
|
||||
*/
|
||||
|
||||
/**
|
||||
* Calculate Post Reading Time in Minutes
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*/
|
||||
function powerkit_calculate_post_reading_time( $post_id = null ) {
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
$post_content = get_post_field( 'post_content', $post_id );
|
||||
$strip_shortcodes = strip_shortcodes( $post_content );
|
||||
$strip_tags = strip_tags( $strip_shortcodes );
|
||||
|
||||
$str = preg_replace( '/[[:punct:]]/', '', $strip_tags );
|
||||
$str = preg_replace( '/[\s]+/', ' ', $str );
|
||||
$word_count = count( (array) array_filter( (array) explode( ' ', $str ) ) );
|
||||
|
||||
$reading_time = intval( ceil( $word_count / 265 ) );
|
||||
|
||||
// Filter for Reading Time.
|
||||
if ( function_exists( 'iconv_strlen') ) {
|
||||
$reading_time = apply_filters( 'powerkit_calculate_reading_time', $reading_time, iconv_strlen( $strip_tags ) );
|
||||
} else {
|
||||
$reading_time = apply_filters( 'powerkit_calculate_reading_time', $reading_time, mb_strlen( $strip_tags ) );
|
||||
}
|
||||
|
||||
return $reading_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Post Reading Time from Post Meta
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
*/
|
||||
function powerkit_get_post_reading_time( $post_id = null ) {
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
|
||||
// Get existing post meta.
|
||||
$reading_time = get_post_meta( $post_id, '_powerkit_reading_time', true );
|
||||
|
||||
// Calculate and save reading time, if there's no existing post meta.
|
||||
if ( ! $reading_time ) {
|
||||
$reading_time = powerkit_calculate_post_reading_time( $post_id );
|
||||
update_post_meta( $post_id, '_powerkit_reading_time', $reading_time );
|
||||
}
|
||||
|
||||
return $reading_time;
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* The public-facing functionality of the module.
|
||||
*
|
||||
* @link https://codesupply.co
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @package Powerkit
|
||||
* @subpackage Modules/public
|
||||
*/
|
||||
|
||||
/**
|
||||
* The public-facing functionality of the module.
|
||||
*/
|
||||
class Powerkit_Reading_Time_Public extends Powerkit_Module_Public {
|
||||
|
||||
/**
|
||||
* Initialize
|
||||
*/
|
||||
public function initialize() {
|
||||
add_action( 'save_post', array( $this, 'update_post_reading_time' ), 10, 3 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update Post Reading Time on Post Save
|
||||
*
|
||||
* @param int $post_id The post ID.
|
||||
* @param post $post The post object.
|
||||
* @param bool $update Whether this is an existing post being updated or not.
|
||||
*/
|
||||
public function update_post_reading_time( $post_id, $post, $update ) {
|
||||
if ( ! $post_id ) {
|
||||
$post_id = get_the_ID();
|
||||
}
|
||||
$reading_time = powerkit_calculate_post_reading_time( $post_id );
|
||||
|
||||
update_post_meta( $post_id, '_powerkit_reading_time', $reading_time );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user