Automattic\WooCommerce\Blocks\Domain

BlockRegistrationContext::is_woocommerce_rest_request │ private │ WC 1.0

Whether the request targets a WooCommerce-owned REST namespace other than the Store API, in either pretty (/wp-json/<namespace>) or plain (?rest_route=/<namespace>) permalink form.

Метод класса: BlockRegistrationContext{}

Хуков нет.

Возвращает

bool.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->is_woocommerce_rest_request(): bool;

Код BlockRegistrationContext::is_woocommerce_rest_request() WC 11.3.0

private function is_woocommerce_rest_request(): bool {
	if ( empty( $_SERVER['REQUEST_URI'] ) ) {
		return false;
	}

	// WooCommerce-owned namespaces that render no blocks; mirrors wc_rest_should_load_namespace() (add new
	// versions here too). Store API (wc/store) is handled above; the trailing slash prevents matching a
	// longer, unrelated namespace.
	$namespaces = array(
		'wc/v1/',
		'wc/v2/',
		'wc/v3/',
		'wc/v4/',
		'wc/private/',
		'wc-admin/',
		'wc-analytics/',
		'wc-telemetry/',
	);

	// Match against the path only (a leading slash anchors the prefix) so a REST-like query argument such as
	// /some-page/?arg=/wp-json/wc/v3 is not mistaken for a REST 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 );
	$rest_prefix = '/' . trailingslashit( rest_get_url_prefix() );

	// Pretty permalinks: /wp-json/<namespace>...
	if ( is_string( $path ) ) {
		foreach ( $namespaces as $namespace ) {
			if ( false !== strpos( $path, $rest_prefix . $namespace ) ) {
				return true;
			}
		}
	}

	// Plain permalinks: ?rest_route=/<namespace>...
	// 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'] ) ) ), '/' );
		foreach ( $namespaces as $namespace ) {
			if ( 0 === strpos( $rest_route, '/' . $namespace ) ) {
				return true;
			}
		}
	}

	return false;
}