Ajax被Wordpress警告破坏:solve through ob_start()


Ajax broken by Wordpress warnings: solve trough ob_start()?

我正在为WordPress编写一个插件。我的目标是让它为每个人服务。某些页面使用ajax响应。一些用户激活了WP_DEBUG常量,该常量显示来自主题和其他插件的错误和警告。

这破坏了我的Ajax PHPs,因为我包含了Wordpress Core,然后WP可以在一些博客中显示警告!

我解决这个问题的老方法是:

<?php
ob_start();
//The wordpress core require
ob_end_clean();
//Now the ajax response:
echo '0';
?>

然而,一位用户报告了一个非常奇怪的错误:ajax页面从不响应,只在Opera中工作,谷歌Chrome显示:错误330(net::ERR_CONTENT_DECODING_FAILED):

在用户web服务器中挖掘,我发现WP配置、插件或elsewere使用ob_start("ob-gzhandler")。

ob_get_status(true)显示在我工作的Web服务器中:

Array
(
    [0] => Array
        (
            [chunk_size] => 4096
            [type] => 1
            [status] => 0
            [name] => default output handler
            [del] => 1
        )
    [1] => Array
        (
            [chunk_size] => 0
            [size] => 40960
            [block_size] => 10240
            [type] => 1
            [status] => 0
            [name] => default output handler
            [del] => 1
        )
)

在有漏洞的网络服务器中:

Array
(
    [0] => Array
        (
            [chunk_size] => 4096
            [type] => 1
            [status] => 0
            [name] => default output handler
            [del] => 1
        )
    [1] => Array
        (
            [chunk_size] => 0
            [size] => 40960
            [block_size] => 10240
            [type] => 1
            [status] => 0
            [name] => default output handler
            [del] => 1
        )
    [2] => Array
        (
            [chunk_size] => 0
            [size] => 40960
            [block_size] => 10240
            [type] => 1
            [status] => 0
            [name] => ob_gzhandler
            [del] => 1
        )
)

我的用户web服务器中的序列是:

  • 我的代码调用ob_start()
  • 包括WP核心,WP初始化所有并调用ob_start("ob-gzhandler")
  • 当我的代码调用ob_end_clean()时,它失败了

我需要一种安全的方式来隐藏WP警告,但我不能破坏启用GZIP的配置(我怀疑是一些插件)。如果不可能,我更喜欢在警告系统中保留破坏ajax,而忘记缓冲区方法。

我认为一个干净的方法可能是:

$buffers = count(ob_get_status(true));
ob_start();
//The wordpress core require
if (count(ob_get_status(true)) == $buffers + 1) {
ob_end_clean();
} else {
ob_flush();
}
//Now the ajax response:
echo '0';

但我担心PHP版本和配置、WP版本、插件和配置的无限组合会让人崩溃。你认为解决这个问题的好方法是什么?

如果您在插件(或functions.php)所需的任何文件中的open php标记之前留下任何空格或行,它将破坏Wordpress AJAX。

中断AJAX:

 <?php

不会破坏AJAX:

<?php