Automattic\WooCommerce\Api\Infrastructure

MetadataController::can_query_metadataprivate staticWC 1.0

Whether the principal may run the _apiMetadata query.

Tri-tier ladder, deliberately fail-closed:

  1. If the principal declares can_query_metadata(): bool, use it.
    Plugins distinguish metadata-query access from native
    introspection access by declaring this method.
    1. Else if the principal declares can_introspect(): bool, fall
      back to it — one switch then gates both metadata and
      introspection, which is the common case.
    2. Else (neither method declared) deny. Plugin authors that don't
      opt their principal in get a locked-down endpoint rather than
      leaking schema shape and gate descriptors by default.

The principal-derived decision is then passed through the woocommerce_graphql_can_query_metadata filter so sites can grant or revoke access without subclassing the principal — useful for per-request rules (specific IPs, headers, query parameters, etc.).

Fail-closed contract: null principal denies before the filter is consulted; either method's return is checked with === true; any throw from the principal method or the filter denies; the filter must likewise return strictly true to allow.

Method of the class: MetadataController{}

Returns

null. Nothing (null).

Usage

$result = MetadataController::can_query_metadata( ?object $principal ): bool;
?object $principal(required)
.

MetadataController::can_query_metadata() code WC 11.0.1

private static function can_query_metadata( ?object $principal ): bool {
	if ( null === $principal ) {
		return false;
	}

	try {
		if ( method_exists( $principal, 'can_query_metadata' ) ) {
			$allowed = true === $principal->can_query_metadata();
		} elseif ( method_exists( $principal, 'can_introspect' ) ) {
			$allowed = true === $principal->can_introspect();
		} else {
			$allowed = false;
		}

		/**
		 * Filters whether the current principal may run the `_apiMetadata` query.
		 *
		 * The filter receives the principal-derived decision (see the tri-tier
		 * ladder in {@see MetadataController::can_query_metadata()}) and must
		 * return strictly `true` to grant access; any other return value
		 * denies. The filter is not invoked when principal resolution failed
		 * (i.e. when the resolver receives a null principal) — that case
		 * denies outright.
		 *
		 * @since 10.9.0
		 *
		 * @internal
		 *
		 * @param bool   $allowed   Whether the principal may query `_apiMetadata`.
		 * @param object $principal The resolved principal.
		 */
		$allowed = apply_filters( 'woocommerce_graphql_can_query_metadata', $allowed, $principal );
	} catch ( \Throwable $e ) {
		return false;
	}

	return true === $allowed;
}