Attachments.php
5 months ago
Author.php
5 months ago
BeforeMoreContent.php
5 months ago
CommentCount.php
5 months ago
CommentsForPostLink.php
5 months ago
ContentExcerpt.php
5 months ago
DatePublishFormatted.php
5 months ago
Depth.php
5 months ago
DescriptivePostStatus.php
5 months ago
DiscussionStatus.php
5 months ago
Excerpt.php
5 months ago
ExcerptMissingMessage.php
5 months ago
ExcerptRaw.php
5 months ago
FeaturedImage.php
5 months ago
HasCommentStatus.php
5 months ago
IsPasswordProtected.php
5 months ago
IsSticky.php
5 months ago
LastModifiedAuthor.php
5 months ago
Meta.php
5 months ago
ModifiedDate.php
5 months ago
Order.php
5 months ago
PageTemplate.php
5 months ago
Path.php
5 months ago
Permalink.php
5 months ago
PingStatus.php
5 months ago
PostContent.php
5 months ago
PostDate.php
5 months ago
PostDateTimestamp.php
5 months ago
PostFormat.php
5 months ago
PostFormatIcon.php
5 months ago
PostFormatLabel.php
5 months ago
PostLink.php
5 months ago
PostParentId.php
5 months ago
PostReferences.php
5 months ago
PostStatus.php
5 months ago
PostStatusIcon.php
5 months ago
PostTerms.php
5 months ago
PostTermsOriginal.php
5 months ago
PostTitle.php
5 months ago
Property.php
5 months ago
ShortLink.php
5 months ago
Shortcodes.php
5 months ago
Slug.php
5 months ago
PostStatusIcon.php
115 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\Post; |
| 6 | |
| 7 | use AC\Formatter; |
| 8 | use AC\Helper; |
| 9 | use AC\Helper\Date; |
| 10 | use AC\Type\Value; |
| 11 | use DateTimeZone; |
| 12 | use WP_Post; |
| 13 | |
| 14 | class PostStatusIcon implements Formatter |
| 15 | { |
| 16 | |
| 17 | public function format(Value $value): Value |
| 18 | { |
| 19 | $post = $this->get_post_from_value($value); |
| 20 | |
| 21 | if ( ! $post instanceof WP_Post) { |
| 22 | return $value; |
| 23 | } |
| 24 | |
| 25 | return $value->with_value($this->get_status_icon($post)); |
| 26 | } |
| 27 | |
| 28 | private function get_post_from_value(Value $value): ?WP_Post |
| 29 | { |
| 30 | $post_value = $value->get_value(); |
| 31 | |
| 32 | if ($post_value instanceof WP_Post) { |
| 33 | return $post_value; |
| 34 | } |
| 35 | |
| 36 | return get_post($value->get_id()); |
| 37 | } |
| 38 | |
| 39 | private function get_status_icon(WP_Post $post): ?string |
| 40 | { |
| 41 | switch ($post->post_status) { |
| 42 | case 'private' : |
| 43 | return Helper\Html::create()->tooltip( |
| 44 | Helper\Icon::create()->dashicon(['icon' => 'hidden', 'class' => 'gray']), |
| 45 | sprintf( |
| 46 | '%s <br><em>%s</em>', |
| 47 | __('Private'), |
| 48 | $this->format_date($post->post_date) |
| 49 | ) |
| 50 | ); |
| 51 | |
| 52 | case 'publish' : |
| 53 | return Helper\Html::create()->tooltip( |
| 54 | Helper\Icon::create()->dashicon(['icon' => 'yes', 'class' => 'blue large']), |
| 55 | sprintf( |
| 56 | '%s <br><em>%s</em>', |
| 57 | __('Published'), |
| 58 | $this->format_date($post->post_date) |
| 59 | ) |
| 60 | ); |
| 61 | |
| 62 | case 'draft' : |
| 63 | return Helper\Html::create()->tooltip( |
| 64 | Helper\Icon::create()->dashicon(['icon' => 'edit', 'class' => 'green']), |
| 65 | sprintf( |
| 66 | '%s <br><em>%s</em>', |
| 67 | __('Draft'), |
| 68 | $this->format_date($post->post_date) |
| 69 | ) |
| 70 | ); |
| 71 | |
| 72 | case 'pending' : |
| 73 | return Helper\Html::create()->tooltip( |
| 74 | Helper\Icon::create()->dashicon(['icon' => 'backup', 'class' => 'orange']), |
| 75 | sprintf( |
| 76 | '%s <br><em>%s</em>', |
| 77 | __('Pending for review'), |
| 78 | $this->format_date($post->post_date) |
| 79 | ) |
| 80 | ); |
| 81 | |
| 82 | case 'future' : |
| 83 | $icon = Helper\Html::create()->tooltip( |
| 84 | Helper\Icon::create()->dashicon(['icon' => 'clock']), |
| 85 | sprintf( |
| 86 | '%s <br><em>%s</em>', |
| 87 | __('Scheduled'), |
| 88 | $this->format_date($post->post_date) |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | // Missed schedule |
| 93 | if ((time() - mysql2date('G', $post->post_date_gmt)) > 0) { |
| 94 | $icon .= Helper\Html::create()->tooltip( |
| 95 | Helper\Icon::create()->dashicon(['icon' => 'flag', 'class' => 'gray']), |
| 96 | __('Missed schedule') |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return $icon; |
| 101 | default: |
| 102 | return null; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private function format_date(string $date): string |
| 107 | { |
| 108 | return wp_date( |
| 109 | Date::create()->get_date_time_format(), |
| 110 | strtotime($date), |
| 111 | new DateTimeZone('UTC') |
| 112 | ) ?: ''; |
| 113 | } |
| 114 | |
| 115 | } |