acf = $acf; $this->fieldMeta = $fieldMeta; $this->field = $field; $this->fieldValue = $fieldValue; } protected function getImageMarkup(): string { $imageUrl = ''; switch ($this->fieldMeta->getReturnFormat()) { case 'id': $imageUrl = function_exists('wp_get_attachment_image_url') ? (string)wp_get_attachment_image_url($this->fieldValue, 'full') : ''; break; case 'array': $imageUrl = (is_array($this->fieldValue) && key_exists('url', $this->fieldValue)) ? $this->fieldValue['url'] : ''; break; case 'url': $imageUrl = $this->fieldValue; break; } return ""; } protected function getFileMarkup(): string { $fileUrl = ''; $fileTitle = ''; switch ($this->fieldMeta->getReturnFormat()) { case 'id': $fileUrl = function_exists('wp_get_attachment_url') ? (string)wp_get_attachment_url($this->fieldValue) : ''; $fileTitle = (! $this->field->label && function_exists('get_post')) ? (get_post($this->fieldValue)->post_title ?? '') : $this->field->label; break; case 'array': $fileUrl = $this->fieldValue['url'] ?? ''; $fileTitle = ! $this->field->label ? ($this->fieldValue['title'] ?? '') : $this->field->label; break; case 'url': $fileUrl = $this->fieldValue; $fileTitle = $this->field->label ?: $fileUrl; break; } 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)); } public function getMarkup(): 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) || ('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; } return (string)$fieldHTML; } }