DateFormat
2 days ago
FieldTypeConfigurator
2 days ago
Media
2 days ago
Post
2 days ago
Pro
2 days ago
ActionIcons.php
2 days ago
AttachmentDisplay.php
2 days ago
BaseComponentFactory.php
2 days ago
BeforeAfter.php
2 days ago
BooleanValues.php
2 days ago
CharacterLimit.php
2 days ago
ColumnInfo.php
2 days ago
CommentDisplay.php
2 days ago
CommentLink.php
2 days ago
CommentStatus.php
2 days ago
CustomField.php
2 days ago
CustomFieldFactory.php
2 days ago
DateFormat.php
2 days ago
DateSaveFormat.php
2 days ago
ExifData.php
2 days ago
FieldType.php
2 days ago
FieldTypeFactory.php
2 days ago
FieldTypeFactoryBuilder.php
2 days ago
FileDisplay.php
2 days ago
FileLink.php
2 days ago
FilePreviewSize.php
2 days ago
HiddenInput.php
2 days ago
HiddenLabel.php
2 days ago
ImageSize.php
2 days ago
ImageSizeBase.php
2 days ago
IncludeMissingSizes.php
2 days ago
InputNameAware.php
2 days ago
IsLink.php
2 days ago
IsLinkable.php
2 days ago
IsMultiple.php
2 days ago
Label.php
2 days ago
LinkLabel.php
2 days ago
LinkToMenu.php
2 days ago
LinkablePostProperty.php
2 days ago
MediaLink.php
2 days ago
Message.php
2 days ago
ModalDisplay.php
2 days ago
Name.php
2 days ago
NumberFormat.php
2 days ago
NumberOfItems.php
2 days ago
Password.php
2 days ago
PathScope.php
2 days ago
PostExtendedProperty.php
2 days ago
PostLink.php
2 days ago
PostProperty.php
2 days ago
PostStatus.php
2 days ago
PostStatusIcon.php
2 days ago
PostType.php
2 days ago
PostTypeFactory.php
2 days ago
RelatedUserMetaField.php
2 days ago
SelectOptions.php
2 days ago
Separator.php
2 days ago
SerializedArrayKeys.php
2 days ago
SerializedDisplay.php
2 days ago
StringLimit.php
2 days ago
Taxonomy.php
2 days ago
TaxonomyFactory.php
2 days ago
TermLink.php
2 days ago
TermProperty.php
2 days ago
UseIcon.php
2 days ago
UserLink.php
2 days ago
UserLinkFactory.php
2 days ago
UserProperty.php
2 days ago
VideoDisplay.php
2 days ago
Width.php
2 days ago
WordLimit.php
2 days ago
WordsPerMinute.php
2 days ago
DateFormat.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting\ComponentFactory; |
| 6 | |
| 7 | use AC\Formatter; |
| 8 | use AC\FormatterCollection; |
| 9 | use AC\Helper; |
| 10 | use AC\Setting\Children; |
| 11 | use AC\Setting\Component; |
| 12 | use AC\Setting\ComponentCollection; |
| 13 | use AC\Setting\Config; |
| 14 | use AC\Setting\Control\Input; |
| 15 | use AC\Setting\Control\Input\Custom; |
| 16 | use AC\Setting\Control\Input\OptionFactory; |
| 17 | use AC\Setting\Control\OptionCollection; |
| 18 | |
| 19 | abstract class DateFormat extends BaseComponentFactory |
| 20 | { |
| 21 | private string $source_format; |
| 22 | |
| 23 | public function __construct(string $source_format = 'U') |
| 24 | { |
| 25 | $this->source_format = $source_format; |
| 26 | } |
| 27 | |
| 28 | abstract protected function get_date_options(): OptionCollection; |
| 29 | |
| 30 | abstract protected function get_default_option(): string; |
| 31 | |
| 32 | public function with_source_format(string $source_format): self |
| 33 | { |
| 34 | $clone = clone $this; |
| 35 | $clone->source_format = $source_format; |
| 36 | |
| 37 | return $clone; |
| 38 | } |
| 39 | |
| 40 | protected function get_label(Config $config): ?string |
| 41 | { |
| 42 | return __('Display Date Format', 'codepress-admin-columns'); |
| 43 | } |
| 44 | |
| 45 | protected function get_description(Config $config): ?string |
| 46 | { |
| 47 | return sprintf( |
| 48 | __('Learn more about %s.', 'codepress-admin-columns'), |
| 49 | sprintf( |
| 50 | '<a target="_blank" href="%s">%s</a>', |
| 51 | 'https://wordpress.org/support/article/formatting-date-and-time/', |
| 52 | __('date and time formatting', 'codepress-admin-columns') |
| 53 | ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | protected function get_input(Config $config): ?Input |
| 58 | { |
| 59 | return new Custom( |
| 60 | 'date_format', |
| 61 | null, |
| 62 | [ |
| 63 | 'wp_date_format' => Helper\WpDateFormat::date(), |
| 64 | 'wp_date_info' => sprintf( |
| 65 | __('The %s can be changed in %s.', 'codepress-admin-columns'), |
| 66 | __('WordPress Date Format', 'codepress-admin-columns'), |
| 67 | Helper\Html::create()->link( |
| 68 | admin_url('options-general.php') . '#date_format_custom_radio', |
| 69 | strtolower(__('General Settings')) |
| 70 | ) |
| 71 | ), |
| 72 | ] |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | protected function get_children(Config $config): ?Children |
| 77 | { |
| 78 | return new Children( |
| 79 | new ComponentCollection([ |
| 80 | new Component( |
| 81 | null, |
| 82 | null, |
| 83 | OptionFactory::create_radio( |
| 84 | 'date_format', |
| 85 | $this->get_date_options(), |
| 86 | (string)$config->get('date_format') ?: $this->get_default_option() |
| 87 | ) |
| 88 | ), |
| 89 | ]) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | protected function get_date_formatter(string $output_format): Formatter |
| 94 | { |
| 95 | switch ($output_format) { |
| 96 | case 'diff': |
| 97 | return new Formatter\Date\TimeDifference($this->source_format); |
| 98 | case 'wp_default': |
| 99 | return new Formatter\Date\LocalizedDateFormat( |
| 100 | Helper\WpDateFormat::date(), |
| 101 | $this->source_format |
| 102 | ); |
| 103 | default: |
| 104 | return new Formatter\Date\LocalizedDateFormat( |
| 105 | $output_format, |
| 106 | $this->source_format, |
| 107 | ); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | protected function add_formatters(Config $config, FormatterCollection $formatters): void |
| 112 | { |
| 113 | $formatters->add( |
| 114 | $this->get_date_formatter( |
| 115 | (string)$config->get('date_format') ?: $this->get_default_option() |
| 116 | ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 |