<?php
/**
 * @package WordPress
 * @subpackage Campoal
 */


/**
* Apply custom excerpt length to default post excerpt
*
* @since Campoal 1.0.0
*/
if( !function_exists('conikal_custom_excerpt_length') ):
	function conikal_custom_excerpt_length( $length ) {
		return 30;
	}
endif;
add_filter( 'excerpt_length', 'conikal_custom_excerpt_length', 999 );



/**
* Custom post excerpt length and get excerpt by post id
*
* @since Campoal 1.0.0
*/
if( !function_exists('conikal_get_excerpt_by_id') ):
	function conikal_get_excerpt_by_id($post_id, $excerpt_length = 30, $content = '') {
		global $post;
		$post_id = empty($post_id) ? $post->ID : $post_id;

		if (has_excerpt($post_id)) {
			$the_excerpt 	= get_the_excerpt($post_id);
		} else if ($content != '') {
			$the_excerpt = strip_tags(strip_shortcodes($content));
		} else {
			$the_post = get_post($post_id);
			$the_excerpt = $the_post->post_content;
			$the_excerpt = strip_tags(strip_shortcodes($the_excerpt));
		}
		$words = explode(' ', $the_excerpt, $excerpt_length + 1);

		if (count($words) > $excerpt_length) {
			array_pop($words);
			array_push($words, '...');
		}
		$the_excerpt = implode(' ', $words);

		return $the_excerpt;
	}
endif;


/**
* Calculator reading time for a post
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_reading_time')) :
	function conikal_reading_time($post_id = '') {
		global $post;
		if (empty($posd_id)) {
			$posd_id = $post->ID;
		}

	    $content = get_post_field( 'post_content', $post_id );
	    $word_count = str_word_count( strip_tags( $content ) );
	    $readingtime = ceil($word_count / 200);
	    $timer = esc_html__('min read', 'campoal');
		$totalreadingtime = sprintf( _n('%s min read', '%s mins read', $readingtime, 'campoal'), $readingtime );

	    return $totalreadingtime;
	}
endif;
?>