acorn icon indicating copy to clipboard operation
acorn copied to clipboard

🩹 fix: Relay status code set by WordPress

Open tombroucke opened this issue 2 months ago • 4 comments

Fixes #484 WordPress sets a 404 statuscode in the WP::handle_404() method, but that got overwritten by the default status code of the Response class. We can fetch the status set by WP using http_response_code(), and pass that in to the sendHeaders() method. This will relay all status codes, but I'm assuming if WP throws a 301, we should follow that.

Tests are running fine, but this should probably be tested manually.

tombroucke avatar Oct 16 '25 11:10 tombroucke

Thanks for the PR! With these changes:

❯ curl -I http://example.test/asdf
HTTP/1.1 404 Not Found 
❯ curl -I http://example.test/asdf/
HTTP/1.1 404 OK

"404 OK" probably isn't what we want?

retlehs avatar Oct 16 '25 15:10 retlehs

I didn't see the response text because of HTTP/2. If we use the setStatusCode() method, it will also update the statusText.

tombroucke avatar Oct 16 '25 15:10 tombroucke

nice find! does https://github.com/roots/acorn/blob/8f4d458f9d87c6f2cfb433468370d0d32b7eb0d2/src/Roots/Acorn/Application/Concerns/Bootable.php#L166 still end up being necessary?

Log1x avatar Oct 18 '25 22:10 Log1x

nice find! does

https://github.com/roots/acorn/blob/8f4d458f9d87c6f2cfb433468370d0d32b7eb0d2/src/Roots/Acorn/Application/Concerns/Bootable.php#L166

still end up being necessary?

I did some testing and I found this is no longer necessary. The status code is always set twice, the first time (line 166) is not always correct, the second time (line 237) is.

However, I did noticed that the "Poor man's wp cron" requests don't fire the send_headers action, so the status code is only set once (from line 166). We should probably add site_url('wp-cron.php') to the $except collection to have wordpress handle these requests.

$except = collect([
    admin_url(),
    wp_login_url(),
    wp_registration_url(),
    rest_url(),
    site_url('wp-cron.php'),
])->map(fn ($url) => parse_url($url, PHP_URL_PATH))->unique()->filter();

tombroucke avatar Oct 24 '25 08:10 tombroucke