SO网友:Tyler V.
W3 Total Cache插件更改W3 Total Cache/lib/W3/plugin/Cdn中各种文件的URL。函数*ob\\u callback*中的php。它使用一系列回调来修改输出缓冲区。代码如下所示:
w3\\u total\\u缓存。php调用$root->run();
W3\\u根::运行调用$plugin->run()
对于中的每个插件$this->_loaded_plugins
W3\\u Plugin\\u TotalCache::run启动一个输出缓冲区,该缓冲区调用W3\\u Plugin\\u TotalCache::ob\\u callbacks(),它运行存储在中的任何回调$GLOBALS[\'_w3tc_ob_callbacks\']
,不幸的是,随后运行的回调在这一行中是硬编码的:$buffer = w3tc_do_ob_callbacks(array(\'minify\', \'newrelic\', \'cdn\', \'browsercache\', \'pagecache\'), $buffer);
因为这是硬编码的,如果您需要修改包含的内容和不包含的内容,那么您必须更改它们的回调。
Example:
我有一个导出JSON的插件,W3 Total Cache的CDN方面没有更改JSON请求的任何URL。结果是我的输出没有通过w3_is_xml($buffer)
测验我通过将单个CDN回调转换为多个来修复它,如下所示:
// Modify the output buffer callbacks of W3 Total Cache to work with the JSON API
if (!empty($GLOBALS[\'_w3tc_ob_callbacks\']) && isset($GLOBALS[\'_w3tc_ob_callbacks\'][\'cdn\'])) {
// Back-up the original value of $GLOBALS[\'_w3tc_ob_callbacks\'][\'cdn\']
// This should be W3_Plugin_Cdn::ob_callback
$this->cdn_ob_callback = $GLOBALS[\'_w3tc_ob_callbacks\'][\'cdn\'];
// Replace $GLOBALS[\'_w3tc_ob_callbacks\'][\'cdn\'] with out own method
// which will call the original callback in between two of our own
$GLOBALS[\'_w3tc_ob_callbacks\'][\'cdn\'] = array($this, \'do_multiple_cdn_ob_callbacks\');
}
然后做我需要的更改,确保在中间调用他们原来的回调。public function do_multiple_cdn_ob_callbacks(&$buffer) {
// Frist run our own callback to add an XML string to the buffer
// so that the content passes the w3_is_xml($buffer) test
$buffer = $this->w3_total_cache_ob_callback_start($buffer);
// Next run the original callback, which will replace the asset URLs
$buffer = call_user_func($this->cdn_ob_callback, $buffer);
// Finally, run another callback of our own to remove the XML string
$buffer = $this->w3_total_cache_ob_callback_end($buffer);
return $buffer;
}