id = $id; $this->fieldsCache = $fieldsCache; $this->isBlock = $isBlock; } public static function getFields(): array { return [ self::FIELD_TITLE, self::FIELD_THUMBNAIL, self::FIELD_AUTHOR, self::FIELD_DATE, self::FIELD_MODIFIED, self::FIELD_EXCERPT, self::FIELD_TITLE_LINK, self::FIELD_LINK, ]; } public function getTitle(): string { return get_the_title($this->id); } public function getExcerpt(): string { return get_the_excerpt($this->id); } public function getLink(): string { return get_the_permalink($this->id); } public function getTitleLink(): array { // array according to the ACF 'link' with 'array' return format return [ 'url' => $this->getLink(), 'title' => $this->getTitle(), ]; } public function getThumbnailId(): int { return (int)get_post_thumbnail_id($this->id); } public function getAuthorName(): string { $value = get_post_field('post_author', $this->id); $value = get_user_by('ID', $value); return $value->display_name ?? ''; } public function getDate(): string { return (string)get_the_date('', $this->id); } public function getModifiedDate(): string { return (string)get_the_modified_date('', $this->id); } public function getId(): int { return $this->id; } protected function getPostField(string $fieldName) { $value = ''; switch ($fieldName) { case self::FIELD_TITLE: $value = $this->getTitle(); break; case self::FIELD_THUMBNAIL: $value = $this->getThumbnailId(); break; case self::FIELD_AUTHOR: $value = $this->getAuthorName(); break; case self::FIELD_DATE: $value = $this->getDate(); break; case self::FIELD_MODIFIED: $value = $this->getModifiedDate(); break; case self::FIELD_EXCERPT: $value = $this->getExcerpt(); break; case self::FIELD_TITLE_LINK: $value = $this->getTitleLink(); break; case self::FIELD_LINK: $value = $this->getLink(); break; } return $value; } public function getFieldValue(string $fieldName) { if (isset($this->fieldsCache[$fieldName])) { return $this->fieldsCache[$fieldName]; } $value = ''; if (in_array($fieldName, $this->getFields(), true)) { $value = $this->getPostField($fieldName); } else { if (function_exists('get_field')) { $value = ! $this->isBlock ? get_field($fieldName, $this->id) : get_field($fieldName); } } $this->fieldsCache[$fieldName] = $value; return $value; } }