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
DateTimeFormat.php
199 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC\Settings; |
| 6 | use AC\View; |
| 7 | |
| 8 | abstract class DateTimeFormat extends Settings\Column |
| 9 | implements Settings\FormatValue { |
| 10 | |
| 11 | const NAME = 'date'; |
| 12 | |
| 13 | private $date_format; |
| 14 | |
| 15 | protected function set_name() { |
| 16 | $this->name = self::NAME; |
| 17 | } |
| 18 | |
| 19 | protected function define_options() { |
| 20 | return [ |
| 21 | 'date_format' => 'wp_default', |
| 22 | ]; |
| 23 | } |
| 24 | |
| 25 | abstract protected function get_custom_format_options(); |
| 26 | |
| 27 | abstract protected function get_wp_default_format(); |
| 28 | |
| 29 | /** |
| 30 | * @param string $label |
| 31 | * @param string $date_format |
| 32 | * @param string $description |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | protected function get_default_html_label( $label, $date_format = '', $description = '' ) { |
| 37 | if ( ! $date_format ) { |
| 38 | $date_format = $this->get_wp_default_format(); |
| 39 | } |
| 40 | |
| 41 | if ( ! $description && current_user_can( 'manage_options' ) ) { |
| 42 | $description = sprintf( |
| 43 | __( 'The %s can be changed in %s.', 'codepress-admin-columns' ), |
| 44 | $label, |
| 45 | ac_helper()->html->link( admin_url( 'options-general.php' ) . '#date_format_custom_radio', strtolower( __( 'General Settings' ) ) ) |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | return $this->get_html_label( $label, $date_format, $description ); |
| 50 | } |
| 51 | |
| 52 | public function create_view() { |
| 53 | $setting = $this |
| 54 | ->create_element( 'text' ) |
| 55 | ->set_attribute( 'placeholder', $this->get_default() ); |
| 56 | |
| 57 | $view = new View( [ |
| 58 | 'setting' => $setting, |
| 59 | 'date_format' => $this->get_date_format(), |
| 60 | 'date_options' => $this->get_date_options(), |
| 61 | 'label' => __( 'Date Format', 'codepress-admin-columns' ), |
| 62 | 'tooltip' => __( 'This will determine how the date will be displayed.', 'codepress-admin-columns' ), |
| 63 | ] ); |
| 64 | |
| 65 | $view->set_template( 'settings/setting-date' ); |
| 66 | |
| 67 | return $view; |
| 68 | } |
| 69 | |
| 70 | public function get_html_label_from_date_format( $date_format ) { |
| 71 | return ac_helper()->date->format_date( $date_format, null, ac_helper()->date->timezone() ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param array $formats |
| 76 | * |
| 77 | * @return array |
| 78 | */ |
| 79 | protected function get_formatted_date_options( $formats ) { |
| 80 | $options = []; |
| 81 | |
| 82 | foreach ( $formats as $format ) { |
| 83 | $options[ $format ] = $this->get_html_label( ac_helper()->date->format_date( $format, null, ac_helper()->date->timezone() ), $format ); |
| 84 | } |
| 85 | |
| 86 | return $options; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param string $label |
| 91 | * @param string $date_format |
| 92 | * @param string $description |
| 93 | * |
| 94 | * @return string |
| 95 | */ |
| 96 | protected function get_html_label( $label, $date_format = '', $description = '' ) { |
| 97 | $output = '<span class="ac-setting-input-date__value">' . $label . '</span>'; |
| 98 | |
| 99 | if ( $date_format ) { |
| 100 | $output .= '<code>' . $date_format . '</code>'; |
| 101 | } |
| 102 | |
| 103 | if ( $description ) { |
| 104 | $output .= '<span data-help class="ac-setting-input-date__more hidden">' . $description . '</span>'; |
| 105 | } |
| 106 | |
| 107 | return $output; |
| 108 | } |
| 109 | |
| 110 | protected function get_date_options() { |
| 111 | $options = $this->get_custom_format_options(); |
| 112 | |
| 113 | $custom_label = $this->get_html_label( |
| 114 | __( 'Custom:', 'codepress-admin-columns' ), |
| 115 | '', |
| 116 | sprintf( __( 'Learn more about %s.', 'codepress-admin-columns' ), ac_helper()->html->link( 'https://wordpress.org/support/article/formatting-date-and-time/', __( 'date and time formatting', 'codepress-admin-columns' ), [ 'target' => '_blank' ] ) ) |
| 117 | ); |
| 118 | |
| 119 | $custom_label .= '<input type="text" class="ac-setting-input-date__custom" data-custom-date value="' . esc_attr( $this->get_date_format() ) . '" disabled>'; |
| 120 | $custom_label .= '<span class="ac-setting-input-date__example"></span>'; |
| 121 | |
| 122 | $options['custom'] = $custom_label; |
| 123 | |
| 124 | return $options; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return mixed |
| 129 | */ |
| 130 | public function get_date_format() { |
| 131 | $date_format = $this->date_format; |
| 132 | |
| 133 | if ( ! $date_format ) { |
| 134 | $date_format = $this->get_default(); |
| 135 | } |
| 136 | |
| 137 | return $date_format; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param mixed $date_format |
| 142 | * |
| 143 | * @return bool |
| 144 | */ |
| 145 | public function set_date_format( $date_format ) { |
| 146 | $this->date_format = trim( $date_format ); |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param $date |
| 153 | * |
| 154 | * @return false|int |
| 155 | */ |
| 156 | protected function get_timestamp( $date ) { |
| 157 | if ( empty( $date ) ) { |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if ( ! is_scalar( $date ) ) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | if ( is_numeric( $date ) ) { |
| 166 | return $date; |
| 167 | } |
| 168 | |
| 169 | return strtotime( $date ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param string $date |
| 174 | * @param $original_value |
| 175 | * |
| 176 | * @return string |
| 177 | */ |
| 178 | public function format( $date, $original_value ) { |
| 179 | $timestamp = $this->get_timestamp( $date ); |
| 180 | |
| 181 | if ( ! $timestamp ) { |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | $date_format = $this->get_date_format(); |
| 186 | |
| 187 | switch ( $date_format ) { |
| 188 | case 'wp_default' : |
| 189 | $date = ac_helper()->date->format_date( $this->get_wp_default_format(), $timestamp ); |
| 190 | |
| 191 | break; |
| 192 | default : |
| 193 | $date = ac_helper()->date->format_date( $date_format, $timestamp ); |
| 194 | } |
| 195 | |
| 196 | return $date; |
| 197 | } |
| 198 | |
| 199 | } |