acf = $acf;
$this->fieldMeta = $fieldMeta;
$this->field = $field;
$this->fieldValue = $fieldValue;
}
protected function getImageMarkup(): string
{
$imageUrl = '';
$imageSize = $this->field->imageSize ?: 'full';
switch ($this->fieldMeta->getReturnFormat()) {
case 'id':
$imageUrl = (string)wp_get_attachment_image_url($this->fieldValue, $imageSize);
break;
case 'array':
$sizes = (array)($this->fieldValue['sizes'] ?? []);
$imageUrl = $sizes[$imageSize] ?? '';
$imageUrl = ! $imageUrl ?
($this->fieldValue['url'] ?? '') :
$imageUrl;
break;
case 'url':
$imageUrl = $this->fieldValue;
break;
}
return "
";
}
protected function getFileMarkup(): string
{
$fileUrl = '';
$fileTitle = '';
switch ($this->fieldMeta->getReturnFormat()) {
case 'id':
$fileUrl = (string)wp_get_attachment_url($this->fieldValue);
$fileTitle = get_post($this->fieldValue)->post_title ?? '';
break;
case 'array':
$fileUrl = $this->fieldValue['url'] ?? '';
$fileTitle = $this->fieldValue['title'] ?? '';
break;
case 'url':
$fileUrl = $this->fieldValue;
break;
}
// if the linkLabel is setup, use it instead of default
$fileTitle = $this->field->linkLabel ?: $fileTitle;
// use label in case there are no linkLabel and default
$fileTitle = ! $fileTitle ?
$this->field->label :
$fileTitle;
// last attempt to fill the label. Use the url
$fileTitle = ! $fileTitle ?
$fileUrl :
$fileTitle;
return "" . esc_html($fileTitle) . "";
}
protected function getSelectMarkup(): string
{
$fieldHTML = '';
// select & checkbox could have multiple values
$fieldValues = (is_array($this->fieldValue) &&
! key_exists('label', $this->fieldValue)) ?
$this->fieldValue :
[$this->fieldValue];
$fieldViews = [];
foreach ($fieldValues as $fieldValue) {
switch ($this->fieldMeta->getReturnFormat()) {
case 'value':
$fieldViews[] = $this->fieldMeta->getChoices()[$fieldValue] ?? '';
break;
case 'label':
$fieldViews[] = $fieldValue;
break;
case 'array':
$fieldViews[] = $fieldValue['label'] ?? '';
break;
}
}
if ('radio' === $this->fieldMeta->getType()) {
return implode('', $fieldViews);
}
foreach ($fieldViews as $subFieldView) {
$fieldHTML .= "
" . esc_html($subFieldView) . "
";
}
return $fieldHTML;
}
protected function getTrueFalseMarkup(): string
{
$state = $this->fieldValue ?
'checked' :
'unchecked';
return "";
}
protected function getDatepickerMarkup(): string
{
$displayFormat = $this->fieldMeta->getDisplayFormat();
$returnFormat = $this->fieldMeta->getReturnFormat();
if (! $this->fieldValue ||
! $displayFormat ||
! $returnFormat) {
return '';
}
$date = DateTime::createFromFormat($returnFormat, $this->fieldValue);
return $date->format($displayFormat);
}
protected function getColorPickerMarkup(): string
{
if ('array' !== $this->fieldMeta->getReturnFormat() ||
! is_array($this->fieldValue)) {
return $this->fieldValue;
}
$red = (string)($this->fieldValue['red'] ?? '');
$green = (string)($this->fieldValue['green'] ?? '');
$blue = (string)($this->fieldValue['blue'] ?? '');
$alpha = (string)($this->fieldValue['alpha'] ?? '');
return sprintf('rgba(%s;%s;%s;%s)', esc_html($red), esc_html($green), esc_html($blue), esc_html($alpha));
}
protected function getLinkMarkup(): string
{
$linkUrl = '';
$linkTitle = '';
switch ($this->fieldMeta->getReturnFormat()) {
case 'array':
$linkUrl = $this->fieldValue['url'] ?? '';
$linkTitle = $this->fieldValue['title'] ?? '';
break;
case 'url':
$linkUrl = $this->fieldValue;
break;
}
$target = (isset($this->fieldValue['target']) && $this->fieldValue['target']) ?
'_blank' :
'_self';
// use linkLabel if exists
$linkTitle = $this->field->linkLabel ?: $linkTitle;
// use label if title is empty
$linkTitle = ! $linkTitle ?
$this->field->label :
$linkTitle;
// use url if title is empty
$linkTitle = ! $linkTitle ?
$linkUrl :
$linkTitle;
return "" .
esc_html($linkTitle) . "";
}
protected function getPageLinkMarkup(): string
{
$markup = '';
$links = is_array($this->fieldValue) ?
$this->fieldValue :
[$this->fieldValue];
foreach ($links as $linkUrl) {
$linkTitle = $this->field->linkLabel ?: $this->field->label;
$linkTitle = ! $linkTitle ?
$linkUrl :
$linkTitle;
$markup .= "" .
esc_html($linkTitle) . "";
}
return $markup;
}
public function getMarkup(int $viewId): string
{
// field could be removed or file type could be changed after a view has been saved
if (! $this->fieldMeta->isFieldExist() ||
! in_array($this->fieldMeta->getType(), $this->acf->getFieldTypes(), true)) {
return '';
}
$this->fieldValue = apply_filters(
'acf_views/view/field_value',
$this->fieldValue,
$this->fieldMeta,
$viewId
);
$this->fieldValue = apply_filters(
'acf_views/view/field_value/type=' . $this->fieldMeta->getType(),
$this->fieldValue,
$this->fieldMeta,
$viewId
);
$this->fieldValue = apply_filters(
'acf_views/view/field_value/name=' . $this->fieldMeta->getName(),
$this->fieldValue,
$this->fieldMeta,
$viewId
);
$this->fieldValue = apply_filters(
'acf_views/view/field_value/view_id=' . $viewId,
$this->fieldValue,
$this->fieldMeta,
$viewId
);
if ('true_false' !== $this->fieldMeta->getType() &&
! $this->fieldValue) {
return '';
}
$fieldHTML = $this->fieldValue ?: '';
// transformation for certain types
switch ($this->fieldMeta->getType()) {
case 'image':
$fieldHTML = $this->getImageMarkup();
break;
case 'file':
$fieldHTML = $this->getFileMarkup();
break;
case 'select':
case 'checkbox':
case 'radio':
$fieldHTML = $this->getSelectMarkup();
break;
case 'true_false':
$fieldHTML = $this->getTrueFalseMarkup();
break;
case 'date_picker':
case 'date_time_picker':
case 'time_picker':
$fieldHTML = $this->getDatepickerMarkup();
break;
case 'color_picker':
$fieldHTML = $this->getColorPickerMarkup();
break;
case 'link':
$fieldHTML = $this->getLinkMarkup();
break;
case 'page_link':
$fieldHTML = $this->getPageLinkMarkup();
break;
default:
// convert all types (int, float, string) to string
$fieldHTML = (string)$fieldHTML;
// don't escape output of wysiwyg, as HTML is expected here
if (in_array($this->fieldMeta->getType(), ['wysiwyg'], true)) {
break;
}
$fieldHTML = esc_html($fieldHTML);
break;
}
$fieldHTML = (string)apply_filters(
'acf_views/view/field_markup',
$fieldHTML,
$this->fieldMeta,
$this->fieldValue,
$viewId
);
$fieldHTML = (string)apply_filters(
'acf_views/view/field_markup/name=' . $this->fieldMeta->getName(),
$fieldHTML,
$this->fieldMeta,
$this->fieldValue,
$viewId
);
$fieldHTML = (string)apply_filters(
'acf_views/view/field_markup/type=' . $this->fieldMeta->getType(),
$fieldHTML,
$this->fieldMeta,
$this->fieldValue,
$viewId
);
$fieldHTML = (string)apply_filters(
'acf_views/view/field_markup/view_id=' . $viewId,
$fieldHTML,
$this->fieldMeta,
$this->fieldValue,
$viewId
);
return $fieldHTML;
}
}