$data * @param array $ops * @return array */ public static function apply(array $data, array $ops): array { foreach ($ops as $op) { if (!is_array($op) || !isset($op['field']) || !is_string($op['field'])) { continue; } $data[$op['field']] = $op['value'] ?? null; } return $data; } /** * Apply ops to a buffered response body. Anything that is not a JSON object * comes back byte-identical — an HTML page must never be rewritten by a * field transform meant for an API response. * * @param array $ops */ public static function applyToJsonBuffer(string $buffer, array $ops): string { if ($ops === [] || $buffer === '') { return $buffer; } $decoded = json_decode($buffer, true); // A JSON list has nothing to reshape (only objects carry the keys ops name). if (!is_array($decoded) || array_keys($decoded) === range(0, count($decoded) - 1)) { return $buffer; } $encoded = wp_json_encode(self::apply($decoded, $ops)); return is_string($encoded) ? $encoded : $buffer; } }