<?php
/**
 * Klasa Excerpt
 *
 * @link https://krzysztof-turek.com
 *
 * @package tylkofotografia.pl
 * @version 0.2
 */

    class Excerpt {

        use Singleton;

        function __construct() {
            
        }
        
        public static function improvedExcerpt($text, $length)
        {
            global $shortcode_tags;
            if ('' != $text)
            {
                $text = get_the_content('');
                $text = strip_shortcodes($text);
                $text = apply_filters('the_content', $text);
                $text = str_replace('\]\]\>', ']]&gt;', $text);
                $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                $text = preg_replace('@<figcaption[^>]*?>.*?</figcaption>@si', '', $text);
                $text = strip_tags($text);
                $text = trim($text);
                $words = explode(' ', $text, $length + 1);
        
                if (count($words) > $length)
                {
                    array_pop($words);
                    $text = trim(implode(' ', $words)) . '...';
                }
            }
            $text = preg_replace('/\s\s+/', ' ', $text);
            return $text;
        }
    }