contains(function ($row) use ($value) {
return is_array($row) && (string) ($row['value'] ?? '') === (string) $value;
});
return $known ? esc_html((string) $value) : '';
}
/**
* The label of a choice on the options page; its value when the label is empty.
*/
public static function choiceLabel(array $row): string
{
$label = (string) ($row['label'] ?? '');
return $label !== '' ? $label : (string) ($row['value'] ?? '');
}
/**
* A link `$text`; the text is the URL itself when none is given.
*/
public static function link(string $url, string $text = ''): string
{
return sprintf('%s', esc_url($url), esc_html($text !== '' ? $text : $url));
}
/**
* Whether the shortcode asks for a link (`link="1"`, or a `text` for the link).
*/
public static function wantsLink(array $args): bool
{
return in_array($args['link'] ?? '', ['1', 'yes', 'true'], true) || ($args['text'] ?? '') !== '';
}
/**
* A stored date and/or time (`Y-m-d`, `H:i`, `Y-m-d\TH:i`, in the time zone of the site) in the format of the
* shortcode, or the format of the site. A value that is not a date is shown as it is.
*
* @param mixed $value
* @param string $storedFormat The format the field type stores.
* @param string $siteFormat The format to use when the shortcode has none.
*/
public static function dateTime($value, string $storedFormat, string $siteFormat, array $args): string
{
if (!is_string($value) || $value === '') {
return '';
}
$date = \DateTimeImmutable::createFromFormat('!' . $storedFormat, $value, wp_timezone());
if ($date === false) {
return esc_html($value);
}
$format = ($args['format'] ?? '') !== '' ? (string) $args['format'] : $siteFormat;
return esc_html((string) wp_date($format, $date->getTimestamp()));
}
/**
* The id of an attachment that still exists, or 0.
*
* @param mixed $value
*/
public static function attachmentId($value): int
{
$id = is_numeric($value) ? (int) $value : 0;
return $id > 0 && get_post_type($id) === 'attachment' ? $id : 0;
}
}