Support checking for a REST API request before the query is parsed#916
Support checking for a REST API request before the query is parsed#916TimothyBJacobs wants to merge 1 commit intoWordPress:masterfrom
Conversation
peterwilsoncc
left a comment
There was a problem hiding this comment.
I've added a couple of comments to this, having some trouble working out how to test locally so I be sure about the parsing. As it comes from the main WP class I assume it's pretty well battle tested though.
| } | ||
|
|
||
| /** | ||
| * Determines whether the current request is a REST API request. |
There was a problem hiding this comment.
CS Nit
| * Determines whether the current request is a REST API request. | |
| * Determine whether the current request is a REST API request. |
| // This intentionally doesn't use wp_using_themes(). We are trying to check if wp() would be called for this request | ||
| // in a front-end context which is typically indicated by the WP_USE_THEMES constant. |
There was a problem hiding this comment.
/*
* CS: multiline
* comment format
*/
🔢
| $index = 'index.php'; | ||
| } | ||
|
|
||
| $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; |
There was a problem hiding this comment.
I know this is a mostly a copy-paste from WP but a couple of tiny things could be tidied up along the way.
| $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; | |
| $path_info = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : ''; |
🔢
| $pathinfo = str_replace( '%', '%25', $pathinfo ); | ||
|
|
||
| list( $req_uri ) = explode( '?', $_SERVER['REQUEST_URI'] ); | ||
| $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); |
There was a problem hiding this comment.
Core helper if it's defined when this runs.
| $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); | |
| $home_path = trim( wp_parse_url( home_url(), PHP_URL_PATH ), '/' ); |
There was a problem hiding this comment.
If the return value of home_url() does not contain a path (e.g. "https://example.com") then in PHP 8.1+ this results in a "trim(): Passing null to parameter #1 ($string) of type string is deprecated" warning.
Thus, this should probably be replaced with something like:
$home_url_path = wp_parse_url( home_url(), PHP_URL_PATH );
$home_path = $home_url_path ? trim( $home_url_path, '/' ) : '';
|
While the purpose of this PR is different, in terms of https://core.trac.wordpress.org/ticket/42061 it is now superseded by #5658. We may want to open another ticket for the specific point of checking for a REST API request prior to parsing the query, as that's a more specific use-case than what was mostly discussed on the ticket. |
Trac ticket: https://core.trac.wordpress.org/ticket/42061
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.