BeforeAfter
5 years ago
Pro
5 years ago
ActionIcons.php
5 years ago
AttachmentDisplay.php
5 years ago
BeforeAfter.php
5 years ago
CharacterLimit.php
5 years ago
Comment.php
5 years ago
CommentCount.php
5 years ago
CommentLink.php
5 years ago
CustomField.php
5 years ago
CustomFieldType.php
5 years ago
Date.php
5 years ago
DateTimeFormat.php
5 years ago
ExifData.php
5 years ago
Image.php
5 years ago
Images.php
5 years ago
Label.php
5 years ago
LinkLabel.php
5 years ago
LinkToMenu.php
5 years ago
MediaLink.php
5 years ago
Message.php
5 years ago
Meta.php
5 years ago
MissingImageSize.php
5 years ago
NumberFormat.php
5 years ago
NumberOfItems.php
5 years ago
Password.php
5 years ago
PathScope.php
5 years ago
Post.php
5 years ago
PostFormatIcon.php
5 years ago
PostLink.php
5 years ago
PostStatus.php
5 years ago
PostType.php
5 years ago
Pro.php
5 years ago
Separator.php
5 years ago
StatusIcon.php
5 years ago
StringLimit.php
5 years ago
Taxonomy.php
5 years ago
Term.php
5 years ago
TermLink.php
5 years ago
Time.php
5 years ago
Toggle.php
5 years ago
Type.php
5 years ago
User.php
5 years ago
UserLink.php
5 years ago
Width.php
5 years ago
WordLimit.php
5 years ago
WordsPerMinute.php
5 years ago
Date.php
85 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC\Settings; |
| 6 | |
| 7 | class Date extends Settings\Column\DateTimeFormat { |
| 8 | |
| 9 | private function get_diff_html_label() { |
| 10 | $description = __( 'The difference is returned in a human readable format.', 'codepress-admin-columns' ) . ' <br/>' . |
| 11 | sprintf( __( 'For example: %s.', 'codepress-admin-columns' ), |
| 12 | '"' . $this->format_human_time_diff( strtotime( "-1 hour" ) ) . '" ' |
| 13 | . __( 'or', 'codepress-admin-columns' ) . |
| 14 | ' "' . $this->format_human_time_diff( strtotime( "-2 days" ) ) . '"' |
| 15 | ); |
| 16 | |
| 17 | return $this->get_html_label( __( 'Time Difference', 'codepress-admin-columns' ), '', $description ); |
| 18 | } |
| 19 | |
| 20 | protected function get_custom_format_options() { |
| 21 | $options = [ |
| 22 | 'diff' => $this->get_diff_html_label(), |
| 23 | 'wp_default' => $this->get_default_html_label( __( 'WordPress Date Format', 'codepress-admin-columns' ) ), |
| 24 | ]; |
| 25 | |
| 26 | $formats = [ |
| 27 | 'j F Y', |
| 28 | 'Y-m-d', |
| 29 | 'm/d/Y', |
| 30 | 'd/m/Y', |
| 31 | ]; |
| 32 | |
| 33 | foreach ( $formats as $format ) { |
| 34 | $options[ $format ] = $this->get_html_label_from_date_format( $format ); |
| 35 | } |
| 36 | |
| 37 | return $options; |
| 38 | } |
| 39 | |
| 40 | protected function get_wp_default_format() { |
| 41 | return get_option( 'date_format' ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param string $date |
| 46 | * @param $original_value |
| 47 | * |
| 48 | * @return string |
| 49 | */ |
| 50 | public function format( $date, $original_value ) { |
| 51 | $timestamp = $this->get_timestamp( $date ); |
| 52 | |
| 53 | if ( ! $timestamp ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | if ( 'diff' === $this->get_date_format() ) { |
| 58 | return $this->format_human_time_diff( $timestamp ); |
| 59 | } |
| 60 | |
| 61 | return parent::format( $date, $original_value ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param int $timestamp Unix time stamp |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public function format_human_time_diff( $timestamp ) { |
| 70 | if ( ! $timestamp ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | $current_time = current_time( 'timestamp' ); |
| 75 | |
| 76 | $tpl = __( '%s ago' ); |
| 77 | |
| 78 | if ( $timestamp > $current_time ) { |
| 79 | $tpl = __( 'in %s', 'codepress-admin-columns' ); |
| 80 | } |
| 81 | |
| 82 | return sprintf( $tpl, human_time_diff( $timestamp, $current_time ) ); |
| 83 | } |
| 84 | |
| 85 | } |