给TpCache集成HTML压缩

in Tutorials with 5 comments

前段时间一直在搞主题集成HTML代码压缩,算是集成成功了,但跟TpCache插件发生了冲突,代码压缩只能在登录后才有效,退出时又打回原样,唉,好浪费时间~

然后今晚突然想在TpCache里直接集成HTML代码压缩好了,直接控制输出给用户的就是压缩过的代码,然后折腾了两个小时,直接成功

下面是给TpCache集成HTML代码压缩的步骤

添加函数

打开TpCache文件夹里的Plugin.php,在文档的最后,在}之前,添加下面的代码

代码来自CompressHTML插件

public static function compress_html($html_source) {
    $chunks = preg_split('/(<!--<nocompress>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE);
    $compress = '';
    foreach ($chunks as $c) {
        if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') {
            $c = substr($c, 19, strlen($c) - 19 - 20);
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') {
            $c = substr($c, 12, strlen($c) - 12 - 13);
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') {
            $compress .= $c;
            continue;
        } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) {
            $tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY);
            $c = '';
            foreach ($tmps as $tmp) {
                if (strpos($tmp, '//') !== false) {
                    if (substr(trim($tmp), 0, 2) == '//') {
                        continue;
                    }
                    $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY);
                    $is_quot = $is_apos = false;
                    foreach ($chars as $key => $char) {
                        if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) {
                            $is_quot = !$is_quot;
                        } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) {
                            $is_apos = !$is_apos;
                        } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) {
                            $tmp = substr($tmp, 0, $key);
                            break;
                        }
                    }
                }
                $c .= $tmp;
            }
        }
        $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c);
        $c = preg_replace('/\\s{2,}/', ' ', $c);
        $c = preg_replace('/>\\s</', '> <', $c);
        $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c);
        $c = preg_replace('/<!--[^!]*-->/', '', $c);
        $compress .= $c;
    }
    return $compress;
}

修改缓存前置函数

找到public static function C(),在{}修改,修改情况如下

修改前

if (self::$plugin_config->is_debug) echo "Hit!\n";
if ($data['html']) echo $data['html'];
$end = microtime(true);

修改后

if (self::$plugin_config->is_debug) echo "Hit!\n";
if ($data['html']){ $data['html'] = self::compress_html($data['html']); echo $data['html'];}
$end = microtime(true);

修改缓存后置函数

找到public static function S(),在{}修改,修改情况如下

修改前

$data = array();
$data['c_time'] = time();
$data['html'] = $html;

修改后

$data = array();
$data['c_time'] = time();
$data['html'] = self::compress_html($html);

最后

保存,上传覆盖你的TpCache的Plugin.php

没有做太多的严谨性测试,仅在pinghsu主题测试通过···

更新

修改缓存前置函数的那步的修改地方变为

if ($data['html']){ $data['html'] = self::compress_html($data['html']); echo $data['html'];}

其他

因为是自用,插件又是别人的,所以就没给配置后台插件设置选项了

对了,还有,TpCache的作者,老高前几天修复了登录后评论没刷新缓存的bug

还有,🎉 博客的PageSpeed打分到94了~ 就差图片大小控制,图片压缩和图片缓存失效时间了

内容不多,至此

Responses
  1. 很强势,基本上你已经把 Tpcache 弄的很完善了

    Reply
  2. 你这个站优化得极好啊,秒开的感觉很爽

    Reply
  3. 厉害了。。。
    要是老高能加进去就更好了

    Reply
  4. 厉害了,mark

    Reply
  5. 好流弊,mark

    Reply