BeforeAfter
8 years ago
ActionIcons.php
8 years ago
AttachmentDisplay.php
8 years ago
BeforeAfter.php
8 years ago
CharacterLimit.php
8 years ago
CommentCount.php
8 years ago
CustomField.php
8 years ago
CustomFieldType.php
8 years ago
Date.php
8 years ago
ExifData.php
8 years ago
Image.php
8 years ago
Images.php
8 years ago
Label.php
8 years ago
LinkLabel.php
8 years ago
LinkToMenu.php
8 years ago
Message.php
8 years ago
Meta.php
8 years ago
MissingImageSize.php
8 years ago
NumberOfItems.php
8 years ago
Password.php
8 years ago
PathScope.php
8 years ago
Post.php
8 years ago
PostFormatIcon.php
8 years ago
PostLink.php
8 years ago
PostType.php
8 years ago
Separator.php
8 years ago
StatusIcon.php
8 years ago
StringLimit.php
8 years ago
Taxonomy.php
8 years ago
Term.php
8 years ago
Toggle.php
8 years ago
Type.php
8 years ago
User.php
8 years ago
Width.php
8 years ago
WordLimit.php
8 years ago
WordsPerMinute.php
8 years ago
Date.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | class AC_Settings_Column_Date extends AC_Settings_Column |
| 8 | implements AC_Settings_FormatValueInterface { |
| 9 | |
| 10 | private $date_format; |
| 11 | |
| 12 | protected function set_name() { |
| 13 | $this->name = 'date'; |
| 14 | } |
| 15 | |
| 16 | protected function define_options() { |
| 17 | return array( |
| 18 | 'date_format' => 'wp_default', |
| 19 | ); |
| 20 | } |
| 21 | |
| 22 | public function create_view() { |
| 23 | |
| 24 | $setting = $this |
| 25 | ->create_element( 'text' ) |
| 26 | ->set_attribute( 'placeholder', $this->get_default() ); |
| 27 | |
| 28 | $view = new AC_View( array( |
| 29 | 'setting' => $setting, |
| 30 | 'date_format' => $this->get_date_format(), |
| 31 | 'date_options' => $this->get_date_options(), |
| 32 | 'label' => __( 'Date Format', 'codepress-admin-columns' ), |
| 33 | 'tooltip' => __( 'This will determine how the date will be displayed.', 'codepress-admin-columns' ), |
| 34 | ) ); |
| 35 | |
| 36 | $view->set_template( 'settings/setting-date' ); |
| 37 | |
| 38 | return $view; |
| 39 | } |
| 40 | |
| 41 | protected function get_html_label( $args ) { |
| 42 | $defaults = array( |
| 43 | 'label' => false, |
| 44 | 'date_format' => false, |
| 45 | 'description' => false, |
| 46 | ); |
| 47 | |
| 48 | $data = (object) wp_parse_args( $args, $defaults ); |
| 49 | |
| 50 | $label = ''; |
| 51 | |
| 52 | if ( $data->label ) { |
| 53 | $label .= '<span class="ac-setting-input-date__value">' . $data->label . '</span>'; |
| 54 | } |
| 55 | if ( $data->date_format ) { |
| 56 | $label .= '<code>' . $data->date_format . '</code>'; |
| 57 | } |
| 58 | if ( $data->description ) { |
| 59 | $label .= '<span class="ac-setting-input-date__more hidden">' . $data->description . '</span>'; |
| 60 | } |
| 61 | |
| 62 | return $label; |
| 63 | } |
| 64 | |
| 65 | protected function get_date_options() { |
| 66 | |
| 67 | $options = array( |
| 68 | 'diff' => $this->get_html_label( array( |
| 69 | 'label' => __( 'Time Difference', 'codepress-admin-columns' ), |
| 70 | 'description' => __( 'The difference is returned in a human readable format.', 'codepress-admin-columns' ) . ' <br/>' . sprintf( __( 'For example: %s.', 'codepress-admin-columns' ), '"' . $this->format_human_time_diff( strtotime( "-1 hour" ) ) . '" ' . __( 'or' ) . ' "' . $this->format_human_time_diff( strtotime( "-2 days" ) ) . '"' ), |
| 71 | ) |
| 72 | ), |
| 73 | ); |
| 74 | |
| 75 | $default_args = array( |
| 76 | 'label' => __( 'WordPress Date Format', 'codepress-admin-columns' ), |
| 77 | 'date_format' => $this->get_wp_date_format(), |
| 78 | ); |
| 79 | |
| 80 | if ( current_user_can( 'manage_options' ) ) { |
| 81 | $default_args['description'] = sprintf( __( 'The %s can be changed in %s.', 'codepress-admin-columns' ), $default_args['label'], ac_helper()->html->link( admin_url( 'options-general.php' ) . '#date_format_custom_radio', strtolower( __( 'General Settings' ) ) ) ); |
| 82 | } |
| 83 | |
| 84 | $options['wp_default'] = $this->get_html_label( $default_args ); |
| 85 | |
| 86 | $formats = array( |
| 87 | 'j F Y', |
| 88 | 'Y-m-d', |
| 89 | 'm/d/Y', |
| 90 | 'd/m/Y', |
| 91 | ); |
| 92 | |
| 93 | foreach ( $formats as $format ) { |
| 94 | $options[ $format ] = $this->get_html_label( array( 'label' => date_i18n( $format ), 'date_format' => $format ) ); |
| 95 | } |
| 96 | |
| 97 | $custom_label = $this->get_html_label( array( |
| 98 | 'label' => __( 'Custom:', 'codepress-admin-columns' ), |
| 99 | 'description' => sprintf( __( 'Learn more about %s.', 'codepress-admin-columns' ), ac_helper()->html->link( 'http://codex.wordpress.org/Formatting_Date_and_Time', __( 'date and time formatting', 'codepress-admin-columns' ) ), array( 'target' => '_blank' ) ), |
| 100 | ) |
| 101 | ); |
| 102 | $custom_label .= '<input type="text" class="ac-setting-input-date__custom" value="' . esc_attr( $this->get_date_format() ) . '" disabled>'; |
| 103 | $custom_label .= '<span class="ac-setting-input-date__example"></span>'; |
| 104 | |
| 105 | $options['custom'] = $custom_label; |
| 106 | |
| 107 | return $options; |
| 108 | } |
| 109 | |
| 110 | private function get_wp_date_format() { |
| 111 | return get_option( 'date_format' ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @return mixed |
| 116 | */ |
| 117 | public function get_date_format() { |
| 118 | return $this->date_format; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param mixed $date_format |
| 123 | * |
| 124 | * @return bool |
| 125 | */ |
| 126 | public function set_date_format( $date_format ) { |
| 127 | $this->date_format = trim( $date_format ); |
| 128 | |
| 129 | return true; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param string $date |
| 134 | * |
| 135 | * @return string |
| 136 | */ |
| 137 | public function format( $date, $original_value ) { |
| 138 | if ( ! $date || ! is_scalar( $date ) ) { |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | $date_format = $this->get_date_format(); |
| 143 | |
| 144 | if ( ! $date_format ) { |
| 145 | $date_format = $this->get_default(); |
| 146 | } |
| 147 | |
| 148 | $timestamp = strtotime( $date ); |
| 149 | |
| 150 | switch ( $date_format ) { |
| 151 | |
| 152 | case 'wp_default' : |
| 153 | $date = date_i18n( $this->get_wp_date_format(), $timestamp ); |
| 154 | |
| 155 | break; |
| 156 | case 'diff' : |
| 157 | $date = $this->format_human_time_diff( $timestamp ); |
| 158 | |
| 159 | break; |
| 160 | default : |
| 161 | |
| 162 | $date = date_i18n( $this->get_date_format(), $timestamp ); |
| 163 | } |
| 164 | |
| 165 | return $date; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @param int $timestamp Unix time stamp |
| 170 | * |
| 171 | * @return string |
| 172 | */ |
| 173 | public function format_human_time_diff( $timestamp ) { |
| 174 | if ( ! $timestamp ) { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | $current_time = current_time( 'timestamp' ); |
| 179 | |
| 180 | $tpl = __( '%s ago' ); |
| 181 | |
| 182 | if ( $timestamp > $current_time ) { |
| 183 | $tpl = __( 'in %s', 'codepress-admin-columns' ); |
| 184 | } |
| 185 | |
| 186 | return sprintf( $tpl, human_time_diff( $timestamp, $current_time ) ); |
| 187 | } |
| 188 | |
| 189 | } |
| 190 |