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
PostStatusIcon.php
126 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\Dashicon; |
| 10 | use AC\Helper\WpDateFormat; |
| 11 | use AC\Type\Value; |
| 12 | use DateTimeZone; |
| 13 | use WP_Post; |
| 14 | |
| 15 | class PostStatusIcon implements Formatter |
| 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 | $formatted_date = $this->format_date($post->post_date); |
| 42 | |
| 43 | if ($formatted_date !== null) { |
| 44 | switch ($post->post_status) { |
| 45 | case 'private': |
| 46 | return Helper\Html::create()->tooltip( |
| 47 | Dashicon::create('hidden', 'gray')->render(), |
| 48 | sprintf( |
| 49 | '%s <br><em>%s</em>', |
| 50 | __('Private'), |
| 51 | $formatted_date |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | case 'publish': |
| 56 | return Helper\Html::create()->tooltip( |
| 57 | Dashicon::create('yes', 'blue large')->render(), |
| 58 | sprintf( |
| 59 | '%s <br><em>%s</em>', |
| 60 | __('Published'), |
| 61 | $formatted_date |
| 62 | ) |
| 63 | ); |
| 64 | |
| 65 | case 'draft': |
| 66 | return Helper\Html::create()->tooltip( |
| 67 | Dashicon::create('edit', 'green')->render(), |
| 68 | sprintf( |
| 69 | '%s <br><em>%s</em>', |
| 70 | __('Draft'), |
| 71 | $formatted_date |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | case 'pending': |
| 76 | return Helper\Html::create()->tooltip( |
| 77 | Dashicon::create('backup', 'orange')->render(), |
| 78 | sprintf( |
| 79 | '%s <br><em>%s</em>', |
| 80 | __('Pending for review'), |
| 81 | $formatted_date |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | case 'future': |
| 86 | $icon = Helper\Html::create()->tooltip( |
| 87 | Dashicon::create('clock')->render(), |
| 88 | sprintf( |
| 89 | '%s <br><em>%s</em>', |
| 90 | __('Scheduled'), |
| 91 | $formatted_date |
| 92 | ) |
| 93 | ); |
| 94 | |
| 95 | // Missed schedule |
| 96 | if ((time() - mysql2date('G', $post->post_date_gmt)) > 0) { |
| 97 | $icon .= Helper\Html::create()->tooltip( |
| 98 | Dashicon::create('flag', 'gray')->render(), |
| 99 | __('Missed schedule') |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | return $icon; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | private function format_date(string $date): ?string |
| 111 | { |
| 112 | $timestamp = strtotime($date); |
| 113 | |
| 114 | if ($timestamp === false) { |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | return wp_date( |
| 119 | WpDateFormat::date_time(), |
| 120 | $timestamp, |
| 121 | new DateTimeZone('UTC') |
| 122 | ) ?: null; |
| 123 | } |
| 124 | |
| 125 | } |
| 126 |