I'm starting to provide Chinese / English versions of some posts, switch with the Language menu above. 我开始提供部分文章的中文、英文翻译,请使用顶部语言菜单切换。

W3 Total Cache 末尾注释去除

W3 Total Cache 开了资源最小化的效果是非常好的,连 HTML 都给我最小化了,不信看看我的网页源代码,连换行符都被删掉了,最小化效果超级给力。但是末尾的地方,却有个 W3 Total Cache 加的注释。

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Content Delivery Network via xuyh0120.asia

Served from: xuyh0120.asia @ 2013-02-13 20:56:59 by W3 Total Cache -->

对于我这种追求速度的人,这段东西不光让网页无端变大,最重要的是我想到有这个东西就烦。于是,我决定对 W3 Total Cache 开刀,把产生注释的这一段代码删掉。

先到 Google 上查了一下,有人说在 w3-total-cache/lib/W3/Plugin/TotalCache.php 这个文件的 2000 多行的位置。结果我打开文件一看,整个文件才 800 行,哪来的 2000 行?继续搜,无果。只能我自己看看吧。于是我开始翻这个 php 文件,直到我翻到这一段代码:

/**
 * Add footer comment
 */
$date = date_i18n('Y-m-d H:i:s');
$host = (!empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');

if ($this->_config->get_string('common.support') != '' || $this->_config->get_boolean('common.tweeted')) {
    $buffer .= sprintf("\r\n<!-- Served from: %s @ %s by W3 Total Cache -->", w3_escape_comment($host), $date);
} else {
    $strings = array();

    if ($this->_config->get_boolean('minify.enabled') && !$this->_config->get_boolean('minify.debug')) {
        $w3_plugin_minify = w3_instance('W3_Plugin_Minify');

        $strings[] = sprintf("Minified using %s%s", w3_get_engine_name($this->_config->get_string('minify.engine')), ($w3_plugin_minify->minify_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_minify->minify_reject_reason) : ''));
    }

    if ($this->_config->get_boolean('pgcache.enabled') && !$this->_config->get_boolean('pgcache.debug')) {
        $w3_pgcache = w3_instance('W3_PgCache');

        $strings[] = sprintf("Page Caching using %s%s", w3_get_engine_name($this->_config->get_string('pgcache.engine')), ($w3_pgcache->cache_reject_reason != '' ? sprintf(' (%s)', $w3_pgcache->cache_reject_reason) : ''));
    }

    if ($this->_config->get_boolean('dbcache.enabled') &&
            !$this->_config->get_boolean('dbcache.debug')) {
        $db = w3_instance('W3_DbCache');
        $append = (!is_null($db->cache_reject_reason) ?
            sprintf(' (%s)', $db->cache_reject_reason) :
            '');

        if ($db->query_hits) {
            $strings[] = sprintf("Database Caching %d/%d queries in %.3f seconds using %s%s",
$db->query_hits, $db->query_total, $db->time_total,
w3_get_engine_name($this->_config->get_string('dbcache.engine')),
$append);
        } else {
            $strings[] = sprintf("Database Caching using %s%s",
w3_get_engine_name($this->_config->get_string('dbcache.engine')),
$append);
        }
    }

    if (w3_is_dbcluster()) {
        $db_cluster = w3_instance('W3_Enterprise_DbCluster');
        $strings[] = $db_cluster->status_message();
    }

    if ($this->_config->get_boolean('objectcache.enabled') && !$this->_config->get_boolean('objectcache.debug')) {
        $w3_objectcache = w3_instance('W3_ObjectCache');

        $append = ($w3_objectcache->cache_reject_reason != '' ?
            sprintf(' (%s)', $w3_objectcache->cache_reject_reason) :
            '');

        $strings[] = sprintf("Object Caching %d/%d objects using %s%s",
            $w3_objectcache->cache_hits, $w3_objectcache->cache_total,
            w3_get_engine_name($this->_config->get_string('objectcache.engine')),
            $append);
    }

    if ($this->_config->get_boolean('fragmentcache.enabled') && !$this->_config->get_boolean('fragmentcache.debug')) {
        $w3_fragmentcache = w3_instance('W3_Pro_FragmentCache');
        $append = ($w3_fragmentcache->cache_reject_reason != '' ?
            sprintf(' (%s)', $w3_fragmentcache->cache_reject_reason) :'');
        $strings[] = sprintf("Fragment Caching %d/%d fragments using %s%s",
            $w3_fragmentcache->cache_hits, $w3_fragmentcache->cache_total,
            w3_get_engine_name($this->_config->get_string('fragmentcache.engine')),
            $append);
    }

    if ($this->_config->get_boolean('cdn.enabled') && !$this->_config->get_boolean('cdn.debug')) {
        $w3_plugin_cdn = w3_instance('W3_Plugin_Cdn');
        $w3_plugin_cdncommon = w3_instance('W3_Plugin_CdnCommon');
        $cdn = & $w3_plugin_cdncommon->get_cdn();
        $via = $cdn->get_via();

        $strings[] = sprintf("Content Delivery Network via %s%s", ($via ? $via : 'N/A'), ($w3_plugin_cdn->cdn_reject_reason != '' ? sprintf(' (%s)', $w3_plugin_cdn->cdn_reject_reason) : ''));
    }

    if ($this->_config->get_boolean('newrelic.enabled')) {
        $w3_newrelic = w3_instance('W3_Plugin_NewRelic');
        $append = ($w3_newrelic->newrelic_reject_reason != '') ?
            sprintf(' (%s)', $w3_newrelic->newrelic_reject_reason) : '';
        $strings[] = sprintf(__("Application Monitoring using New Relic%s", 'w3-total-cache'), $append);
    }
    $buffer .= "\r\n<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/\r\n";

    if (count($strings)) {
        $buffer .= "\r\n" . implode("\r\n", $strings) . "\r\n";
    }

    $buffer .= sprintf("\r\n Served from: %s @ %s by W3 Total Cache -->", w3_escape_comment($host), $date);
}

“Add footer comment”,这一段代码,你摊上事了。这一段代码就是产生在 HTML 最末端的注释,我当然不能让它存在。

如果你要删,你可以直接把这段代码删掉,但是如果你怕删错了搞坏插件,你可以在这段代码两段分别加一个 /* 和 */,这是 PHP 里的注释符号。如果你今后想把它弄出来,把这两个符号删掉就行了。

本站使用运行在 Vercel 上的 Waline 评论系统,中国大陆访问可能不稳定。