• Resolved Ocio132

    (@ocio132)


    hello good I have a separate plugin because if not the fast velocity minify does not work well enough

    <?php
    /**
     * Plugin Name: SuperMinify
     * Plugin URI: https://servicios.ayudawp.com/
      * Description: Superminimiza los c贸digos HTML, CSS y JS de tu web y quita los comentarios HTML (excepto los de IE que est茅n dentro de scripts). Totalmente activar y listo. Si no quieres minimizar alg煤n c贸digo utiliza la tag '<!--superminify-html-compression no compression-->'
     * Version: 1.0
     * Author: Fernando Tellado
     * Author URI: https://tellado.es/
     */
    defined( 'ABSPATH' ) || die( 'No script kiddies please!' );
    class superminify_HTML_Compression
    {
            // Ajustes - Cambiar a false para desactivar
            protected $compress_css = true;
            protected $compress_js = false;
            protected $info_comment = true;
            protected $remove_comments = true;
    
            // Variables
            protected $html;
    
            public function __construct($html)
            {
                    if (!empty($html))
                    {
                            $this->parseHTML($html);
                    }
            }
    
            public function __toString()
            {
                    return $this->html;
            }
    
            protected function minifyHTML($html)
            {
                    $pattern = '/<(?<script>script).*?<\/script\s*>|<(?<style>style).*?<\/style\s*>|<!(?<comment>--).*?-->|<(?<tag>[\/\w.:-]*)(?:".*?"|\'.*?\'|[^\'">]+)*>|(?<text>((<[^!\/\w.:-])?[^<]*)+)|/si';
                    preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
                    $overriding = false;
                    $raw_tag = false;
                    // Variable reused for output
                    $html = '';
                    foreach ( $matches as $token ) {
    
                            $tag = (isset($token['tag'])) ? strtolower($token['tag']) : null;
                            $content = $token[0];
    
                            if ( is_null( $tag ) ) {
    
                                    if ( !empty( $token['script'] ) ) {
    
                                            $strip = $this->compress_js;
    
                                    } else if ( !empty($token['style'] ) ) {
    
                                            $strip = $this->compress_css;
    
                                    } else if ( $content == '<!--superminify-html-compression no compression-->' ) {
    
                                            $overriding = !$overriding;
                                            // Don't print HTML comments
                                            continue;
    
                                    } else if ( $this->remove_comments ) {
    
                                            if ( !$overriding && $raw_tag != 'textarea' ) {
    
                                                    // Remove any HTML comment, except those conditional for MSIE
                                                    $content = preg_replace('/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s', '', $content);
                                            }
                                    }
    
                            } else {
    
                                    if ( $tag == 'pre' || $tag == 'textarea' || $tag == 'script' ) {
    
                                            $raw_tag = $tag;
    
                                    } else if ( $tag == '/pre' || $tag == '/textarea' || $tag == '/script' ) {
    
                                            $raw_tag = false;
    
                                    } else {
    
                                            if ($raw_tag || $overriding) {
    
                                                    $strip = false;
    
                                            } else {
    
                                                    $strip = true;
    
                                                    // Remove any empty attribute except:
                                                    // action, alt, content, src
                                                    $content = preg_replace('/(\s+)(\w++(?<!\baction|\balt|\bcontent|\bsrc)="")/', '$1', $content);
    
                                                    // Remove any space before the end of any closing XHTML tag
                                                    // JavaScript excluded
                                                    $content = str_replace(' />', '/>', $content);
                                            }
    
                                    }
    
                            }
    
                            if ( $strip ) {
    
                                    $content = $this->removeWhiteSpace($content);
                            }
    
                            $html .= $content;
                    }
    
                    return $html;
            }
    
            public function parseHTML($html)
            {
                    $this->html = $this->minifyHTML($html);
            }
    
            protected function removeWhiteSpace($str)
            {
                    $str = str_replace( "\t", ' ', $str );
                    $str = str_replace( "\n",  '', $str );
                    $str = str_replace( "\r",  '', $str );
    
                    while ( stristr($str, '  ' ) ) {
    
                            $str = str_replace('  ', ' ', $str);
                    }
    
                    return $str;
            }
    }
    
    function superminify_html_compression_finish($html) {
    
            return new superminify_HTML_Compression($html);
    }
    
    function superminify_html_compression_start() {
    
            ob_start( 'superminify_html_compression_finish' );
    }
    
    add_action( 'get_header', 'superminify_html_compression_start' );

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘plugin super minify’ is closed to new replies.