acf = $acf; $this->fieldMeta = $fieldMeta; $this->field = $field; $this->fieldValue = $fieldValue; } // content types protected function getImageMarkup($fieldValue): string { $imageUrl = ''; $imageSize = $this->field->imageSize ?: 'full'; switch ($this->fieldMeta->getReturnFormat()) { case 'id': $imageUrl = (string)wp_get_attachment_image_url($fieldValue, $imageSize); break; case 'array': $sizes = (array)($fieldValue['sizes'] ?? []); $imageUrl = $sizes[$imageSize] ?? ''; $imageUrl = ! $imageUrl ? ($fieldValue['url'] ?? '') : $imageUrl; break; case 'url': $imageUrl = $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 getGalleryMarkup(): string { $markup = ''; $images = (array)($this->fieldValue ?: []); foreach ($images as $image) { $markup .= $this->getImageMarkup($image); } return $markup; } // choice types 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; } } // only one value is possible for them if (in_array($this->fieldMeta->getType(), ['radio', 'button_group',], true)) { return implode('', $fieldViews); } foreach ($fieldViews as $subFieldView) { $fieldHTML .= "
" . esc_html($subFieldView) . "
"; } return $fieldHTML; } protected function getTrueFalseMarkup(): string { $state = $this->fieldValue ? 'checked' : 'unchecked'; return "
"; } // jquery types protected function getMapMarkup(): string { $mapMarkup = ''; // always should be an array if (! is_array($this->fieldValue)) { return $mapMarkup; } $zoom = $this->fieldValue['zoom'] ?? '16'; $lat = $this->fieldValue['lat'] ?? ''; $lng = $this->fieldValue['lng'] ?? ''; // zoom gets from every specific map (when admin zoom out and save a page, the zoom is also saved) $mapMarkup .= sprintf( '
', $zoom ); $mapMarkup .= sprintf( '
', esc_attr($lat), esc_attr($lng) ); $mapMarkup .= '
'; return $mapMarkup; } 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)); } // relation types protected function getLinkMarkup($fieldValue, string $returnFormat): string { $linkUrl = ''; $linkTitle = ''; switch ($returnFormat) { case 'array': $linkUrl = $fieldValue['url'] ?? ''; $linkTitle = $fieldValue['title'] ?? ''; break; case 'url': $linkUrl = (string)$fieldValue; break; } $target = (isset($fieldValue['target']) && $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 getPostObjectMarkup(): string { $postObjectMarkup = ''; $posts = is_array($this->fieldValue) ? $this->fieldValue : [$this->fieldValue]; foreach ($posts as $post) { // the 'get_' functions below work for the both 'object' and 'id' return formats $linkLabel = $this->field->linkLabel ?: get_the_title($post); $postObjectMarkup .= $this->getLinkMarkup([ 'url' => (string)get_the_permalink($post), 'title' => $linkLabel, ], 'array'); } return $postObjectMarkup; } 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; } protected function getTaxonomyMarkup(): string { $taxonomyMarkup = ''; // always should be an array if (! is_array($this->fieldValue)) { return $taxonomyMarkup; } foreach ($this->fieldValue as $term) { $term = 'id' === $this->fieldMeta->getReturnFormat() ? get_term($term) : $term; $linkLabel = ''; $linkUrl = ''; // don't use simple 'continue' as markup needs for tests if ($term) { $linkLabel = $this->field->linkLabel ?: $term->name; $linkUrl = (string)get_term_link($term); } $taxonomyMarkup .= $this->getLinkMarkup([ 'url' => $linkUrl, 'title' => $linkLabel, ], 'array'); } return $taxonomyMarkup; } protected function getUserMarkup(): string { $userMarkup = ''; // multiple option is available $users = (is_array($this->fieldValue) && ! key_exists('display_name', $this->fieldValue)) ? $this->fieldValue : [$this->fieldValue,]; foreach ($users as $user) { $linkLabel = ''; $linkUrl = ''; switch ($this->fieldMeta->getReturnFormat()) { case 'object': $linkLabel = $user->display_name ?? ''; $linkUrl = $user->user_url ?? ''; break; case 'id': $userObject = get_user_by('ID', $user); $linkLabel = $userObject->display_name ?? ''; $linkUrl = $userObject->user_url ?? ''; break; case 'array': $linkLabel = $user['display_name'] ?? ''; $linkUrl = $user['user_url'] ?? ''; break; } // use linkLabel if available $linkLabel = $this->field->linkLabel ?: $linkLabel; $userMarkup .= $this->getLinkMarkup([ 'url' => $linkUrl, 'title' => $linkLabel, ], 'array'); } return $userMarkup; } // 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()) { //// content types case 'image': $fieldHTML = $this->getImageMarkup($this->fieldValue); break; case 'file': $fieldHTML = $this->getFileMarkup(); break; case 'gallery': $fieldHTML = $this->getGalleryMarkup(); break; //// choice types case 'select': case 'checkbox': case 'radio': case 'button_group': $fieldHTML = $this->getSelectMarkup(); break; case 'true_false': $fieldHTML = $this->getTrueFalseMarkup(); break; //// relational case 'link': $fieldHTML = $this->getLinkMarkup($this->fieldValue, $this->fieldMeta->getReturnFormat()); break; case 'page_link': $fieldHTML = $this->getPageLinkMarkup(); break; case 'post_object': case 'relationship': $fieldHTML = $this->getPostObjectMarkup(); break; case 'taxonomy': $fieldHTML = $this->getTaxonomyMarkup(); break; case 'user': $fieldHTML = $this->getUserMarkup(); break; //// jquery types case 'google_map': $fieldHTML = $this->getMapMarkup(); break; case 'date_picker': case 'date_time_picker': case 'time_picker': $fieldHTML = $this->getDatepickerMarkup(); break; case 'color_picker': $fieldHTML = $this->getColorPickerMarkup(); break; //// others 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', 'oembed',], 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; } }