self::summary($data)], '', '', $tool->untrusted, ); } /** * Keep a value within the budget; a larger one is cut and says so. * * @since 0.1.0 */ public static function cut(mixed $result): mixed { $json = wp_json_encode($result); if (!is_string($json) || strlen($json) <= self::BUDGET) { return $result; } return [ 'truncated' => true, 'note' => sprintf('The result was %d bytes; only the first %d are shown.', strlen($json), self::BUDGET), 'partial' => substr($json, 0, self::BUDGET), ]; } /** * A short, neutral description of a result for the trace card: how many items or fields came back. * * @since 0.1.0 */ public static function summary(mixed $data): string { if (is_array($data) && ($data['truncated'] ?? false) === true) { return __('Large result, shortened', 'maze-graph'); } if (is_array($data) && array_is_list($data)) { /* translators: %d: number of items. */ return sprintf(_n('%d item', '%d items', count($data), 'maze-graph'), count($data)); } if (is_array($data)) { foreach (['items', 'plugins', 'themes', 'posts', 'comments', 'terms', 'events', 'notes', 'tests'] as $key) { if (isset($data[$key]) && is_array($data[$key])) { /* translators: %d: number of items. */ return sprintf(_n('%d item', '%d items', count($data[$key]), 'maze-graph'), count($data[$key])); } } /* translators: %d: number of fields. */ return sprintf(_n('%d field', '%d fields', count($data), 'maze-graph'), count($data)); } return $data === null ? __('No result', 'maze-graph') : __('1 value', 'maze-graph'); } }