id = $id; $this->fieldsCache = $fieldsCache; } public static function getFields(): array { return [ self::FIELD_TITLE, self::FIELD_THUMBNAIL, self::FIELD_AUTHOR, self::FIELD_DATE, self::FIELD_MODIFIED, ]; } public function getTitle(): string { return function_exists('get_the_title') ? get_the_title($this->id) : ''; } public function getThumbnailId(): int { return function_exists('get_post_thumbnail_id') ? (int)get_post_thumbnail_id($this->id) : 0; } public function getAuthorName(): string { $value = function_exists('get_post_field') ? get_post_field('post_author', $this->id) : ''; $value = function_exists('get_user_by') ? get_user_by('ID', $value) : ''; return $value->display_name ?? ''; } public function getDate(): string { return function_exists('get_the_date') ? (string)get_the_date('', $this->id) : ''; } public function getModifiedDate(): string { return function_exists('get_the_modified_date') ? (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; } 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 { $value = function_exists('get_field') ? get_field($fieldName, $this->id) : ''; } $this->fieldsCache[$fieldName] = $value; return $value; } }