🖥️ Code
function my_custom_endpoint(WP_REST_Request $request)
{
$plugin_file = $request->get_param("plugin_file");
if(!class_exists('Plugin_Upgrader'))
{
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
}
ob_start();
$upgrader = new Plugin_Upgrader();
$result = $upgrader->upgrade($plugin_file);
$logs = ob_get_clean();
// I m testing here if the upgrade is successfull before return the response
return rest_ensure_response(array("logs" => $logs));
}
â›” The issue
I create a custom WordPress REST endpoint to update some plugin from another website.
The method Plugin_Upgrader::upgrade
write a lot of strings in STDOUT, I wanna catch in $logs
but when I try buffering it with ob_start
I still get it in my http response.
<divclass="wrap">
<h1></h1>
<p>downloading_package</p>
<p>L’authenticitéde<spanclass="code">complianz-gdpr.6.5.4.zip</span>
n’apaspuêtrevérifiéecaraucunesignaturen’aététrouvée.</p>
<p>unpack_package</p>
<p>Installationdeladernièreversion…</p>
<p>remove_old</p>
<p>process_success</p>
</div>
<b>Warning</b>: Cannotmodifyheaderinformation-headersalreadysentby(outputstartedatC: \xampp\htdocs\oust\wp-includes\functions.php: 5349)in<b>C: \xampp\htdocs\oust\wp-includes\rest-api\class-wp-rest-server.php</b>online<b>1769</b><br/>
{
'logs' => false
}
🇫🇷 French Logs Sorry
PHP ob_get_clean: If output buffering isn't active then false is returned.
This return seems to be the cause of all my problems, but I don't understand why buffering is deactivate after $upgrader->upgrade($plugin_file)
call (I tried replace it with simple echo and the buffering work)..
🙆‍♂️ Expected
{
"logs": "<divclass="wrap"><h1></h1><p>downloading_package</p><p>L’authenticitĂ©de<spanclass="code">complianz-gdpr.6.5.4.zip</span>n’apaspuĂŞtrevĂ©rifiĂ©ecaraucunesignaturen’aĂ©tĂ©trouvĂ©e.</p><p>unpack_package</p><p>Installationdeladernièreversion…</p><p>remove_old</p><p>process_success</p></div>"
}
Thanks in advance for your time and suggestions ❤️
ob_start
or when you're including the various files to get the plugin upgrader class. If there's nothing of note then it's best we can see that to avoid repeated unnecessary questions$_POST
like this:$plugin_file = $_POST['plugin_file'];
is there a reason for this? Would it not be safer/more compatible to do this:function my_custom_endpoint( \WP_REST_Request $request) : \WP_REST_Response|\WP_Error {
and$plugin_file = $request['plugin_file'];
? The REST API will take care of figuring out if it's GET or POST, using$_POST
directly means none of the validation or sanitisation features will workob_start
to before theif
statement that requires the file? I can't find anything in the WP source that messes or uses output buffers for upgrading, is it possible somewhere in your code a filter is flushing the output buffer?skin
attribute with an upgrader skin of your own that has no output, the purpose of the upgrader skin is to do all the output and display, the upgrader class itself doesn't output anything, though that doesn't answer your output buffering question