<?php

namespace CaptureRelay;

final class Caption
{
    public static function format(string $credit, ?string $format = null): string
    {
        $credit = trim(wp_strip_all_tags($credit));
        if ($credit === '') {
            return '';
        }
        $template = $format ?? (string) Config::get('caption_format', 'Photo by {credit}');
        if (!str_contains($template, '{credit}')) {
            $template = 'Photo by {credit}';
        }
        return sanitize_text_field(str_replace('{credit}', $credit, $template));
    }
}

