WooCommerce::is_rest_api_request │ public │ WC 1.0

Returns true if the request is a non-legacy REST API request.

Legacy REST requests should still run some extra code for backwards compatibility.

This method cannot be replaced with core's wp_is_serving_rest_request(): that function reads the REST_REQUEST constant, which is only defined once rest_api_loaded() runs on parse_request — long after this method is first called during plugin bootstrap (e.g. to decide whether to load the frontend includes). Sniffing the request URI is the only signal available that early. Code that runs after parse_request should prefer wp_is_serving_rest_request(), or wp_is_rest_endpoint(), which also covers internal REST requests dispatched during a regular page load.

Method of the class: WooCommerce{}

Hooks from the method

Returns

bool.

Usage

$WooCommerce = new WooCommerce();
$WooCommerce->is_rest_api_request();

WooCommerce::is_rest_api_request() code WC 11.1.2

public function is_rest_api_request() {
	if ( empty( $_SERVER['REQUEST_URI'] ) ) {
		return false;
	}

	// Pretty permalinks: the REST prefix is part of the path, e.g. /wp-json/wc/v3/products.
	// Plain permalinks: the route is passed as a query parameter instead, e.g. ?rest_route=/wc/v3/products
	// (also used by Jetpack-signed REST requests regardless of the permalink structure).
	// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Recommended
	$rest_prefix         = trailingslashit( rest_get_url_prefix() );
	$is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ) || ! empty( $_GET['rest_route'] );
	// phpcs:enable

	/**
	 * Whether this is a REST API request.
	 *
	 * @since 3.6.0
	 */
	return apply_filters( 'woocommerce_is_rest_api_request', $is_rest_api_request );
}