WooCommerce::is_store_api_request
Returns true if the request is a store REST API request.
Method of the class: WooCommerce{}
No Hooks.
Returns
bool.
Usage
$WooCommerce = new WooCommerce(); $WooCommerce->is_store_api_request();
WooCommerce::is_store_api_request() WooCommerce::is store api request code WC 11.1.2
public function is_store_api_request() {
if ( empty( $_SERVER['REQUEST_URI'] ) ) {
return false;
}
// Pretty permalinks: the Store API namespace is part of the path, e.g. /wp-json/wc/store/v1/cart. Match the
// path only (a leading slash anchors the prefix) so a REST-like query argument such as
// /some-page/?arg=/wp-json/wc/store/ is not mistaken for a Store API request.
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$path = wp_parse_url( '/' . ltrim( (string) wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ), PHP_URL_PATH );
if ( is_string( $path ) && false !== strpos( $path, '/' . trailingslashit( rest_get_url_prefix() ) . 'wc/store/' ) ) {
return true;
}
// Plain permalinks: the route is passed as a query parameter, e.g. ?rest_route=/wc/store/v1/cart.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the route only, no state change.
if ( isset( $_GET['rest_route'] ) && is_string( $_GET['rest_route'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the route only, no state change.
$rest_route = '/' . ltrim( rawurldecode( sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ) ), '/' );
if ( 0 === strpos( $rest_route, '/wc/store/' ) ) {
return true;
}
}
return false;
}