Attachments.php
2 days ago
Author.php
2 days ago
BeforeMoreContent.php
2 days ago
CommentCount.php
2 days ago
CommentsForPostLink.php
2 days ago
ContentExcerpt.php
2 days ago
DatePublishFormatted.php
2 days ago
Depth.php
2 days ago
DescriptivePostStatus.php
2 days ago
DiscussionStatus.php
2 days ago
Excerpt.php
2 days ago
ExcerptMissingMessage.php
2 days ago
ExcerptRaw.php
2 days ago
FeaturedImage.php
2 days ago
HasCommentStatus.php
2 days ago
IsPasswordProtected.php
2 days ago
IsSticky.php
2 days ago
LastModifiedAuthor.php
2 days ago
Meta.php
2 days ago
ModifiedDate.php
2 days ago
Order.php
2 days ago
PageTemplate.php
2 days ago
Path.php
2 days ago
Permalink.php
2 days ago
PingStatus.php
2 days ago
PostContent.php
2 days ago
PostDate.php
2 days ago
PostDateTimestamp.php
2 days ago
PostFormat.php
2 days ago
PostFormatIcon.php
2 days ago
PostFormatLabel.php
2 days ago
PostLink.php
2 days ago
PostParentId.php
2 days ago
PostReferences.php
2 days ago
PostStatus.php
2 days ago
PostStatusIcon.php
2 days ago
PostTerms.php
2 days ago
PostTermsOriginal.php
2 days ago
PostTitle.php
2 days ago
Property.php
2 days ago
ShortLink.php
2 days ago
Shortcodes.php
2 days ago
Slug.php
2 days ago
PostTitle.php
45 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\Post; |
| 6 | |
| 7 | use AC\Exception\ValueNotFoundException; |
| 8 | use AC\Formatter; |
| 9 | use AC\Helper; |
| 10 | use AC\Type\Value; |
| 11 | use WP_Post; |
| 12 | |
| 13 | class PostTitle implements Formatter |
| 14 | { |
| 15 | private bool $use_file_name_for_attachments; |
| 16 | |
| 17 | public function __construct(bool $use_file_name_for_attachments = true) |
| 18 | { |
| 19 | $this->use_file_name_for_attachments = $use_file_name_for_attachments; |
| 20 | } |
| 21 | |
| 22 | public function format(Value $value): Value |
| 23 | { |
| 24 | $post = get_post($value->get_id()); |
| 25 | |
| 26 | if (! $post) { |
| 27 | throw ValueNotFoundException::from_id($value->get_id()); |
| 28 | } |
| 29 | |
| 30 | return $value->with_value( |
| 31 | $this->get_title($post) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | private function get_title(WP_Post $post): string |
| 36 | { |
| 37 | if ($this->use_file_name_for_attachments && 'attachment' === $post->post_type) { |
| 38 | return Helper\LocalFile::create()->get_file_name($post->ID) ?: ''; |
| 39 | } |
| 40 | |
| 41 | return get_the_title($post); |
| 42 | } |
| 43 | |
| 44 | } |
| 45 |