$body */ public static function encode(array $body): string { try { // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- wp_json_encode() does not throw on invalid UTF-8; the strict, exception-based encoding is required here and this class must work without WordPress loaded $json = json_encode($body, self::FLAGS); } catch (\JsonException) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception messages go to logs and callers, never echoed; public errors are fixed codes throw new ProviderException(ProviderException::INVALID_REQUEST, false, 'The request contains text that cannot be sent (not valid UTF-8).'); } if (strlen($json) > self::MAX_PAYLOAD_BYTES) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception messages go to logs and callers, never echoed; public errors are fixed codes throw new ProviderException(ProviderException::INVALID_REQUEST, false, 'Request exceeds the local input limit.'); } return $json; } /** * The reference material as the text placed in the data message (same encoding as before Phase 97D). Text that is not valid UTF-8 is refused * before dispatch instead of surfacing as an unclassified error. * * @param list> $context */ public static function contextJson(array $context): string { try { // phpcs:ignore WordPress.WP.AlternativeFunctions.json_encode_json_encode -- strict, exception-based encoding (see encode()) return json_encode($context, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE); } catch (\JsonException) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception messages go to logs and callers, never echoed; public errors are fixed codes throw new ProviderException(ProviderException::INVALID_REQUEST, false, 'The request contains text that cannot be sent (not valid UTF-8).'); } } /** * Both bounds, for an adapter, before anything is handed to a transport. $configured is the site's byte limit (clamped to 1,024..131,072). * * @param array $body the provider-shaped body the adapter is about to send */ public static function assertWithinLimits(ModelRequest $request, array $body, int $configured): void { try { $logical = InputEstimate::bytes($request); } catch (\JsonException) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception messages go to logs and callers, never echoed; public errors are fixed codes throw new ProviderException(ProviderException::INVALID_REQUEST, false, 'The request contains text that cannot be sent (not valid UTF-8).'); } if ($logical > max(1024, min(131072, $configured))) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- exception messages go to logs and callers, never echoed; public errors are fixed codes throw new ProviderException(ProviderException::INVALID_REQUEST, false, 'Context exceeds the configured local byte limit.'); } self::encode($body); // the serialized bound (and the UTF-8 check) on the body that will really be sent } }