Integrate HTML compression for TpCache
in Tutorial with 5 comments
Integrate HTML compression for TpCache
in Tutorial with 5 comments

This article is meaningless. Go and read it《 Integrate code compression function for Pinghsu topic 》This is enough

I have been doing HTML code compression for theme integration for a while, which is regarded as a success, but conflicts with TpCache plug-in. Code compression can only be effective after login, but it will return to the original when exiting. Alas, it's a waste of time~

Then tonight, I suddenly wanted to directly integrate HTML code compression in TpCache. The compressed code is the one that directly controls the output to the user, and then I struggled for two hours

Here are the steps to integrate HTML code compression for TpCache

Add Function

Open the Plugin.php , at the end of the document, at } Before, add the following code

Code from the CompressHTML plug-in

 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; }

Modify cache prefunction

find public static function C() , on {} The modification is as follows

Before modification

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

After modification

 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);

Modify Cache Post Function

find public static function S() , on {} The modification is as follows

Before modification

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

After modification

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

last

Save, upload and overwrite your TpCache's Plugin.php

Not much rigorous testing, only passing the pinghsu theme test···

to update

In the step of modifying the cache prefunction, the modification is

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

other

Because it is for self use and the plug-in is someone else's, there is no option to configure the background plug-in

By the way, Lao Gao, the author of TpCache, fixed the bug of not refreshing the cache after logging in a few days ago

also, 🎉 The PageSpeed score of the blog is 94, which means that the image size control, image compression and image cache are invalid

Not much content, so far

Responses
  1. Very powerful. Basically, you have perfected Tpcache

    Reply
  2. Your station is extremely optimized. It feels great to drive for seconds

    Reply
  3. Great...
    It would be better if Lao Gao could add it

    Reply
  4. That's great, mark

    Reply
  5. Good cheating, mark

    Reply