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
CustomFieldType.php
331 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Settings\Column; |
| 4 | |
| 5 | use AC; |
| 6 | use AC\Collection; |
| 7 | use AC\Settings; |
| 8 | use AC\View; |
| 9 | |
| 10 | class CustomFieldType extends Settings\Column |
| 11 | implements Settings\FormatValue { |
| 12 | |
| 13 | const NAME = 'field_type'; |
| 14 | |
| 15 | const TYPE_ARRAY = 'array'; |
| 16 | const TYPE_BOOLEAN = 'checkmark'; |
| 17 | const TYPE_COLOR = 'color'; |
| 18 | const TYPE_COUNT = 'count'; |
| 19 | const TYPE_DATE = 'date'; |
| 20 | const TYPE_IMAGE = 'image'; |
| 21 | const TYPE_MEDIA = 'library_id'; |
| 22 | const TYPE_NON_EMPTY = 'has_content'; |
| 23 | const TYPE_NUMERIC = 'numeric'; |
| 24 | const TYPE_POST = 'title_by_id'; |
| 25 | const TYPE_TEXT = 'excerpt'; |
| 26 | const TYPE_URL = 'link'; |
| 27 | const TYPE_USER = 'user_by_id'; |
| 28 | |
| 29 | /** |
| 30 | * @var string |
| 31 | */ |
| 32 | private $field_type; |
| 33 | |
| 34 | protected function define_options() { |
| 35 | return [ self::NAME ]; |
| 36 | } |
| 37 | |
| 38 | public function get_dependent_settings() { |
| 39 | $settings = []; |
| 40 | |
| 41 | switch ( $this->get_field_type() ) { |
| 42 | |
| 43 | case self::TYPE_DATE : |
| 44 | $settings[] = new Date( $this->column ); |
| 45 | |
| 46 | break; |
| 47 | case self::TYPE_IMAGE : |
| 48 | case self::TYPE_MEDIA : |
| 49 | $settings[] = new Image( $this->column ); |
| 50 | $settings[] = new MediaLink( $this->column ); |
| 51 | |
| 52 | break; |
| 53 | case self::TYPE_TEXT : |
| 54 | $settings[] = new StringLimit( $this->column ); |
| 55 | |
| 56 | break; |
| 57 | case self::TYPE_URL : |
| 58 | $settings[] = new LinkLabel( $this->column ); |
| 59 | |
| 60 | break; |
| 61 | case self::TYPE_NUMERIC : |
| 62 | $settings[] = new NumberFormat( $this->column ); |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | return $settings; |
| 67 | } |
| 68 | |
| 69 | public function create_view() { |
| 70 | $select = $this->create_element( 'select' ); |
| 71 | |
| 72 | $select->set_attribute( 'data-refresh', 'column' ) |
| 73 | ->set_options( $this->get_grouped_options() ) |
| 74 | ->set_description( $this->get_description() ); |
| 75 | |
| 76 | $tooltip = __( 'This will determine how the value will be displayed.', 'codepress-admin-columns' ); |
| 77 | |
| 78 | if ( ! in_array( $this->get_field_type(), [ null, '' ], true ) ) { |
| 79 | $tooltip .= '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->get_field_type() . '</em>'; |
| 80 | } |
| 81 | |
| 82 | return new View( [ |
| 83 | 'label' => __( 'Field Type', 'codepress-admin-columns' ), |
| 84 | 'tooltip' => $tooltip, |
| 85 | 'setting' => $select, |
| 86 | ] ); |
| 87 | } |
| 88 | |
| 89 | private function get_description_object_ids( $input ) { |
| 90 | $description = sprintf( __( "Uses one or more %s IDs to display information about it.", 'codepress-admin-columns' ), '<em>' . $input . '</em>' ); |
| 91 | $description .= ' ' . __( "Multiple IDs should be separated by commas.", 'codepress-admin-columns' ); |
| 92 | |
| 93 | return $description; |
| 94 | } |
| 95 | |
| 96 | public function get_description() { |
| 97 | $description = false; |
| 98 | |
| 99 | switch ( $this->get_field_type() ) { |
| 100 | case self::TYPE_POST : |
| 101 | $description = $this->get_description_object_ids( __( "Post Type", 'codepress-admin-columns' ) ); |
| 102 | |
| 103 | break; |
| 104 | case self::TYPE_USER : |
| 105 | $description = $this->get_description_object_ids( __( "User", 'codepress-admin-columns' ) ); |
| 106 | |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return $description; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get possible field types |
| 115 | * @return array |
| 116 | */ |
| 117 | protected function get_field_type_options() { |
| 118 | $grouped_types = [ |
| 119 | 'basic' => [ |
| 120 | self::TYPE_COLOR => __( 'Color', 'codepress-admin-columns' ), |
| 121 | self::TYPE_DATE => __( 'Date', 'codepress-admin-columns' ), |
| 122 | self::TYPE_TEXT => __( 'Text', 'codepress-admin-columns' ), |
| 123 | self::TYPE_IMAGE => __( 'Image', 'codepress-admin-columns' ), |
| 124 | self::TYPE_URL => __( 'URL', 'codepress-admin-columns' ), |
| 125 | self::TYPE_NUMERIC => __( 'Number', 'codepress-admin-columns' ), |
| 126 | ], |
| 127 | 'choice' => [ |
| 128 | self::TYPE_NON_EMPTY => __( 'Has Content', 'codepress-admin-columns' ), |
| 129 | self::TYPE_BOOLEAN => __( 'True / False', 'codepress-admin-columns' ), |
| 130 | ], |
| 131 | 'relational' => [ |
| 132 | self::TYPE_MEDIA => __( 'Media', 'codepress-admin-columns' ), |
| 133 | self::TYPE_POST => __( 'Post', 'codepress-admin-columns' ), |
| 134 | self::TYPE_USER => __( 'User', 'codepress-admin-columns' ), |
| 135 | ], |
| 136 | 'multiple' => [ |
| 137 | self::TYPE_COUNT => __( 'Number of Fields', 'codepress-admin-columns' ), |
| 138 | self::TYPE_ARRAY => __( 'Multiple Values', 'codepress-admin-columns' ), |
| 139 | ], |
| 140 | ]; |
| 141 | |
| 142 | /** |
| 143 | * Filter the available custom field types for the meta (custom field) field |
| 144 | * |
| 145 | * @param array $field_types Available custom field types ([type] => [label]) |
| 146 | * |
| 147 | * @since 3.0 |
| 148 | */ |
| 149 | $grouped_types['custom'] = apply_filters( 'ac/column/custom_field/field_types', [] ); |
| 150 | |
| 151 | foreach ( $grouped_types as $k => $fields ) { |
| 152 | natcasesort( $grouped_types[ $k ] ); |
| 153 | } |
| 154 | |
| 155 | return $grouped_types; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @return array |
| 160 | */ |
| 161 | private function get_grouped_options() { |
| 162 | $field_types = $this->get_field_type_options(); |
| 163 | |
| 164 | foreach ( $field_types as $fields ) { |
| 165 | asort( $fields ); |
| 166 | } |
| 167 | |
| 168 | $groups = [ |
| 169 | 'basic' => __( 'Basic', 'codepress-admin-columns' ), |
| 170 | 'relational' => __( 'Relational', 'codepress-admin-columns' ), |
| 171 | 'choice' => __( 'Choice', 'codepress-admin-columns' ), |
| 172 | 'multiple' => __( 'Multiple', 'codepress-admin-columns' ), |
| 173 | 'custom' => __( 'Custom', 'codepress-admin-columns' ), |
| 174 | ]; |
| 175 | |
| 176 | $grouped_options = []; |
| 177 | foreach ( $field_types as $group => $fields ) { |
| 178 | |
| 179 | if ( ! $fields ) { |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | $grouped_options[ $group ]['title'] = $groups[ $group ]; |
| 184 | $grouped_options[ $group ]['options'] = $fields; |
| 185 | } |
| 186 | |
| 187 | // Default option comes first |
| 188 | $grouped_options = array_merge( [ '' => __( 'Default', 'codepress-admin-columns' ) ], $grouped_options ); |
| 189 | |
| 190 | return $grouped_options; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * @param string|array $string |
| 195 | * |
| 196 | * @return array |
| 197 | */ |
| 198 | private function get_values_from_array_or_string( $string ) { |
| 199 | $string = ac_helper()->array->implode_recursive( ',', $string ); |
| 200 | |
| 201 | return ac_helper()->string->comma_separated_to_array( $string ); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @param string|array $string |
| 206 | * |
| 207 | * @return array |
| 208 | */ |
| 209 | private function get_ids_from_array_or_string( $string ) { |
| 210 | $string = ac_helper()->array->implode_recursive( ',', $string ); |
| 211 | |
| 212 | return ac_helper()->string->string_to_array_integers( $string ); |
| 213 | } |
| 214 | |
| 215 | public function format( $value, $original_value ) { |
| 216 | |
| 217 | switch ( $this->get_field_type() ) { |
| 218 | |
| 219 | case self::TYPE_ARRAY : |
| 220 | if ( ac_helper()->array->is_associative( $value ) ) { |
| 221 | $value = ac_helper()->array->implode_associative( $value, __( ', ' ) ); |
| 222 | } else { |
| 223 | $value = ac_helper()->array->implode_recursive( __( ', ' ), $value ); |
| 224 | } |
| 225 | |
| 226 | break; |
| 227 | case self::TYPE_DATE : |
| 228 | $timestamp = ac_helper()->date->strtotime( $value ); |
| 229 | if ( $timestamp ) { |
| 230 | $value = date( 'c', $timestamp ); |
| 231 | } |
| 232 | |
| 233 | break; |
| 234 | case self::TYPE_POST : |
| 235 | $values = []; |
| 236 | foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) { |
| 237 | $post = get_post( $id ); |
| 238 | $values[] = ac_helper()->html->link( get_edit_post_link( $post ), $post->post_title ); |
| 239 | } |
| 240 | |
| 241 | $value = implode( ac_helper()->html->divider(), $values ); |
| 242 | |
| 243 | break; |
| 244 | case self::TYPE_USER : |
| 245 | $values = []; |
| 246 | foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) { |
| 247 | $user = get_userdata( $id ); |
| 248 | $values[] = ac_helper()->html->link( get_edit_user_link( $id ), ac_helper()->user->get_display_name( $user ) ); |
| 249 | } |
| 250 | |
| 251 | $value = implode( ac_helper()->html->divider(), $values ); |
| 252 | |
| 253 | break; |
| 254 | case self::TYPE_IMAGE : |
| 255 | $value = new Collection( $this->get_values_from_array_or_string( $value ) ); |
| 256 | |
| 257 | break; |
| 258 | case self::TYPE_MEDIA : |
| 259 | $value = new Collection( $this->get_ids_from_array_or_string( $value ) ); |
| 260 | |
| 261 | break; |
| 262 | case self::TYPE_BOOLEAN : |
| 263 | $is_true = ! empty( $value ) && 'false' !== $value && '0' !== $value; |
| 264 | |
| 265 | if ( $is_true ) { |
| 266 | $value = ac_helper()->icon->dashicon( [ 'icon' => 'yes', 'class' => 'green' ] ); |
| 267 | } else { |
| 268 | $value = ac_helper()->icon->dashicon( [ 'icon' => 'no-alt', 'class' => 'red' ] ); |
| 269 | } |
| 270 | |
| 271 | break; |
| 272 | case self::TYPE_COLOR : |
| 273 | |
| 274 | if ( $value && is_scalar( $value ) ) { |
| 275 | $value = ac_helper()->string->get_color_block( $value ); |
| 276 | } else { |
| 277 | $value = false; |
| 278 | } |
| 279 | |
| 280 | break; |
| 281 | case self::TYPE_COUNT : |
| 282 | |
| 283 | if ( $this->column instanceof AC\Column\Meta ) { |
| 284 | $value = $this->column->get_meta_value( $original_value, $this->column->get_meta_key(), false ); |
| 285 | |
| 286 | if ( $value ) { |
| 287 | if ( 1 === count( $value ) && is_array( $value[0] ) ) { |
| 288 | |
| 289 | // Value contains a single serialized array with multiple values |
| 290 | $value = count( $value[0] ); |
| 291 | } else { |
| 292 | |
| 293 | // Count multiple usage of meta keys |
| 294 | $value = count( $value ); |
| 295 | } |
| 296 | } else { |
| 297 | $value = false; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | break; |
| 302 | case self::TYPE_NON_EMPTY : |
| 303 | $value = ac_helper()->icon->yes_or_no( $value, $value ); |
| 304 | |
| 305 | break; |
| 306 | default : |
| 307 | $value = ac_helper()->array->implode_recursive( __( ', ' ), $value ); |
| 308 | } |
| 309 | |
| 310 | return $value; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @return string |
| 315 | */ |
| 316 | public function get_field_type() { |
| 317 | return $this->field_type; |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * @param string $field_type |
| 322 | * |
| 323 | * @return bool |
| 324 | */ |
| 325 | public function set_field_type( $field_type ) { |
| 326 | $this->field_type = $field_type; |
| 327 | |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | } |