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
NumberFormat.php
194 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC\Settings; |
| 6 | use AC\View; |
| 7 | |
| 8 | class NumberFormat extends Settings\Column |
| 9 | implements Settings\FormatValue { |
| 10 | |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $number_format; |
| 15 | |
| 16 | /** |
| 17 | * @var int |
| 18 | */ |
| 19 | private $number_decimals; |
| 20 | |
| 21 | /** |
| 22 | * @var string |
| 23 | */ |
| 24 | private $number_decimal_point; |
| 25 | |
| 26 | /** |
| 27 | * @var string |
| 28 | */ |
| 29 | private $number_thousands_separator; |
| 30 | |
| 31 | protected function define_options() { |
| 32 | return [ |
| 33 | 'number_format' => '', |
| 34 | 'number_decimals' => 0, |
| 35 | 'number_decimal_point' => '.', |
| 36 | 'number_thousands_separator' => '', |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | private function get_decimals_setting() { |
| 41 | $decimals = $this->create_element( 'number', 'number_decimals' ); |
| 42 | $decimals->set_attribute( 'placeholder', $this->get_default( 'number_decimals' ) ) |
| 43 | ->set_attribute( 'step', 1 ) |
| 44 | ->set_attribute( 'max', 20 ) |
| 45 | ->set_attribute( 'min', 0 ); |
| 46 | $decimals_view = new View( [ |
| 47 | 'label' => __( 'Decimals', 'codepress-admin-columns' ), |
| 48 | 'setting' => $decimals, |
| 49 | ] ); |
| 50 | |
| 51 | return $decimals_view; |
| 52 | } |
| 53 | |
| 54 | private function get_decimal_point_setting() { |
| 55 | $decimal_point = $this->create_element( 'text', 'number_decimal_point' ); |
| 56 | $decimal_point->set_attribute( 'placeholder', $this->get_default( 'number_decimal_point' ) ); |
| 57 | $decimal_point_view = new View( [ |
| 58 | 'label' => __( 'Decimal point', 'codepress-admin-columns' ), |
| 59 | 'setting' => $decimal_point, |
| 60 | ] ); |
| 61 | |
| 62 | return $decimal_point_view; |
| 63 | } |
| 64 | |
| 65 | private function get_thousands_point_setting() { |
| 66 | $setting = $this->create_element( 'text', 'number_thousands_separator' ); |
| 67 | $setting->set_attribute( 'placeholder', $this->get_default( 'number_thousands_separator' ) ); |
| 68 | $view = new View( [ |
| 69 | 'label' => __( 'Thousands separator', 'codepress-admin-columns' ), |
| 70 | 'setting' => $setting, |
| 71 | ] ); |
| 72 | |
| 73 | return $view; |
| 74 | } |
| 75 | |
| 76 | private function get_preview_setting() { |
| 77 | return new View( [ |
| 78 | 'label' => __( 'Preview', 'codepress-admin-columns' ), |
| 79 | 'setting' => '<code data-preview="">75000</code>', |
| 80 | ] ); |
| 81 | } |
| 82 | |
| 83 | public function create_view() { |
| 84 | $sections = []; |
| 85 | $select = $this->create_element( 'select' ) |
| 86 | ->set_attribute( 'data-refresh', 'column' ) |
| 87 | ->set_options( [ |
| 88 | '' => __( 'Default', 'codepress-admin-column' ), |
| 89 | 'formatted' => __( 'Formatted', 'codepress-admin-column' ), |
| 90 | ] ); |
| 91 | |
| 92 | if ( $this->get_number_format() ) { |
| 93 | $sections[] = $this->get_decimals_setting(); |
| 94 | $sections[] = $this->get_decimal_point_setting(); |
| 95 | $sections[] = $this->get_thousands_point_setting(); |
| 96 | $sections[] = $this->get_preview_setting(); |
| 97 | } |
| 98 | |
| 99 | $view = new View( [ |
| 100 | 'label' => __( 'Number Format', 'codepress-admin-columns' ), |
| 101 | 'setting' => $select, |
| 102 | 'sections' => $sections, |
| 103 | ] ); |
| 104 | |
| 105 | return $view; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return string |
| 110 | */ |
| 111 | public function get_number_format() { |
| 112 | return $this->number_format; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param string $number_format |
| 117 | * |
| 118 | * @return NumberFormat |
| 119 | */ |
| 120 | public function set_number_format( $number_format ) { |
| 121 | $this->number_format = $number_format; |
| 122 | |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * @return string |
| 128 | */ |
| 129 | public function get_number_decimals() { |
| 130 | return $this->number_decimals; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param string $number_decimals |
| 135 | * |
| 136 | * @return NumberFormat |
| 137 | */ |
| 138 | public function set_number_decimals( $number_decimals ) { |
| 139 | $this->number_decimals = $number_decimals; |
| 140 | |
| 141 | return $this; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return string |
| 146 | */ |
| 147 | public function get_number_decimal_point() { |
| 148 | return $this->number_decimal_point; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param string $number_decimal_point |
| 153 | * |
| 154 | * @return NumberFormat |
| 155 | */ |
| 156 | public function set_number_decimal_point( $number_decimal_point ) { |
| 157 | $this->number_decimal_point = $number_decimal_point; |
| 158 | |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @return string |
| 164 | */ |
| 165 | public function get_number_thousands_separator() { |
| 166 | return $this->number_thousands_separator; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * @param string $number_thousands_separator |
| 171 | * |
| 172 | * @return NumberFormat |
| 173 | */ |
| 174 | public function set_number_thousands_separator( $number_thousands_separator ) { |
| 175 | $this->number_thousands_separator = $number_thousands_separator; |
| 176 | |
| 177 | return $this; |
| 178 | } |
| 179 | |
| 180 | public function format( $value, $original_value ) { |
| 181 | if ( ! is_numeric( $value ) ) { |
| 182 | return $value; |
| 183 | } |
| 184 | |
| 185 | switch ( $this->get_number_format() ) { |
| 186 | case 'formatted' : |
| 187 | return number_format( $value, (int) $this->get_number_decimals(), $this->get_number_decimal_point(), $this->get_number_thousands_separator() ); |
| 188 | break; |
| 189 | default: |
| 190 | return $value; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | } |