PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 4.4.1
Admin Columns v4.4.1
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Settings / Column / CustomFieldType.php
codepress-admin-columns / classes / Settings / Column Last commit date
BeforeAfter 4 years ago Pro 4 years ago ActionIcons.php 4 years ago AttachmentDisplay.php 4 years ago BeforeAfter.php 4 years ago CharacterLimit.php 4 years ago Comment.php 4 years ago CommentCount.php 4 years ago CommentLink.php 4 years ago CustomField.php 4 years ago CustomFieldType.php 4 years ago Date.php 4 years ago DateTimeFormat.php 4 years ago ExifData.php 4 years ago Image.php 4 years ago Images.php 4 years ago Label.php 4 years ago LinkLabel.php 4 years ago LinkToMenu.php 4 years ago MediaLink.php 4 years ago Message.php 4 years ago Meta.php 4 years ago MissingImageSize.php 4 years ago NumberFormat.php 4 years ago NumberOfItems.php 4 years ago Password.php 4 years ago PathScope.php 4 years ago Post.php 4 years ago PostFormatIcon.php 4 years ago PostLink.php 4 years ago PostStatus.php 4 years ago PostType.php 4 years ago Pro.php 4 years ago Separator.php 4 years ago StatusIcon.php 4 years ago StringLimit.php 4 years ago Taxonomy.php 4 years ago Term.php 4 years ago TermLink.php 4 years ago Time.php 4 years ago Toggle.php 4 years ago Type.php 4 years ago User.php 4 years ago UserLink.php 4 years ago Width.php 4 years ago WordLimit.php 4 years ago WordsPerMinute.php 4 years ago
CustomFieldType.php
332 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_DEFAULT = '';
16 const TYPE_ARRAY = 'array';
17 const TYPE_BOOLEAN = 'checkmark';
18 const TYPE_COLOR = 'color';
19 const TYPE_COUNT = 'count';
20 const TYPE_DATE = 'date';
21 const TYPE_IMAGE = 'image';
22 const TYPE_MEDIA = 'library_id';
23 const TYPE_NON_EMPTY = 'has_content';
24 const TYPE_NUMERIC = 'numeric';
25 const TYPE_POST = 'title_by_id';
26 const TYPE_TEXT = 'excerpt';
27 const TYPE_URL = 'link';
28 const TYPE_USER = 'user_by_id';
29
30 /**
31 * @var string
32 */
33 private $field_type;
34
35 protected function define_options() {
36 return [ self::NAME ];
37 }
38
39 public function get_dependent_settings() {
40 $settings = [];
41
42 switch ( $this->get_field_type() ) {
43
44 case self::TYPE_DATE :
45 $settings[] = new Date( $this->column );
46
47 break;
48 case self::TYPE_IMAGE :
49 case self::TYPE_MEDIA :
50 $settings[] = new Image( $this->column );
51 $settings[] = new MediaLink( $this->column );
52
53 break;
54 case self::TYPE_TEXT :
55 $settings[] = new StringLimit( $this->column );
56
57 break;
58 case self::TYPE_URL :
59 $settings[] = new LinkLabel( $this->column );
60
61 break;
62 case self::TYPE_NUMERIC :
63 $settings[] = new NumberFormat( $this->column );
64 break;
65 }
66
67 return $settings;
68 }
69
70 public function create_view() {
71 $select = $this->create_element( 'select' );
72
73 $select->set_attribute( 'data-refresh', 'column' )
74 ->set_options( $this->get_grouped_options() )
75 ->set_description( $this->get_description() );
76
77 $tooltip = __( 'This will determine how the value will be displayed.', 'codepress-admin-columns' );
78
79 if ( ! in_array( $this->get_field_type(), [ null, '' ], true ) ) {
80 $tooltip .= '<em>' . __( 'Type', 'codepress-admin-columns' ) . ': ' . $this->get_field_type() . '</em>';
81 }
82
83 return new View( [
84 'label' => __( 'Field Type', 'codepress-admin-columns' ),
85 'tooltip' => $tooltip,
86 'setting' => $select,
87 ] );
88 }
89
90 private function get_description_object_ids( $input ) {
91 $description = sprintf( __( "Uses one or more %s IDs to display information about it.", 'codepress-admin-columns' ), '<em>' . $input . '</em>' );
92 $description .= ' ' . __( "Multiple IDs should be separated by commas.", 'codepress-admin-columns' );
93
94 return $description;
95 }
96
97 public function get_description() {
98 $description = false;
99
100 switch ( $this->get_field_type() ) {
101 case self::TYPE_POST :
102 $description = $this->get_description_object_ids( __( "Post Type", 'codepress-admin-columns' ) );
103
104 break;
105 case self::TYPE_USER :
106 $description = $this->get_description_object_ids( __( "User", 'codepress-admin-columns' ) );
107
108 break;
109 }
110
111 return $description;
112 }
113
114 /**
115 * Get possible field types
116 * @return array
117 */
118 protected function get_field_type_options() {
119 $grouped_types = [
120 'basic' => [
121 self::TYPE_COLOR => __( 'Color', 'codepress-admin-columns' ),
122 self::TYPE_DATE => __( 'Date', 'codepress-admin-columns' ),
123 self::TYPE_TEXT => __( 'Text', 'codepress-admin-columns' ),
124 self::TYPE_IMAGE => __( 'Image', 'codepress-admin-columns' ),
125 self::TYPE_URL => __( 'URL', 'codepress-admin-columns' ),
126 self::TYPE_NUMERIC => __( 'Number', 'codepress-admin-columns' ),
127 ],
128 'choice' => [
129 self::TYPE_NON_EMPTY => __( 'Has Content', 'codepress-admin-columns' ),
130 self::TYPE_BOOLEAN => __( 'True / False', 'codepress-admin-columns' ),
131 ],
132 'relational' => [
133 self::TYPE_MEDIA => __( 'Media', 'codepress-admin-columns' ),
134 self::TYPE_POST => __( 'Post', 'codepress-admin-columns' ),
135 self::TYPE_USER => __( 'User', 'codepress-admin-columns' ),
136 ],
137 'multiple' => [
138 self::TYPE_COUNT => __( 'Number of Fields', 'codepress-admin-columns' ),
139 self::TYPE_ARRAY => __( 'Multiple Values', 'codepress-admin-columns' ),
140 ],
141 ];
142
143 /**
144 * Filter the available custom field types for the meta (custom field) field
145 *
146 * @param array $field_types Available custom field types ([type] => [label])
147 *
148 * @since 3.0
149 */
150 $grouped_types['custom'] = apply_filters( 'ac/column/custom_field/field_types', [] );
151
152 foreach ( $grouped_types as $k => $fields ) {
153 natcasesort( $grouped_types[ $k ] );
154 }
155
156 return $grouped_types;
157 }
158
159 /**
160 * @return array
161 */
162 private function get_grouped_options() {
163 $field_types = $this->get_field_type_options();
164
165 foreach ( $field_types as $fields ) {
166 asort( $fields );
167 }
168
169 $groups = [
170 'basic' => __( 'Basic', 'codepress-admin-columns' ),
171 'relational' => __( 'Relational', 'codepress-admin-columns' ),
172 'choice' => __( 'Choice', 'codepress-admin-columns' ),
173 'multiple' => __( 'Multiple', 'codepress-admin-columns' ),
174 'custom' => __( 'Custom', 'codepress-admin-columns' ),
175 ];
176
177 $grouped_options = [];
178 foreach ( $field_types as $group => $fields ) {
179
180 if ( ! $fields ) {
181 continue;
182 }
183
184 $grouped_options[ $group ]['title'] = $groups[ $group ];
185 $grouped_options[ $group ]['options'] = $fields;
186 }
187
188 // Default option comes first
189 $grouped_options = array_merge( [ '' => __( 'Default', 'codepress-admin-columns' ) ], $grouped_options );
190
191 return $grouped_options;
192 }
193
194 /**
195 * @param string|array $string
196 *
197 * @return array
198 */
199 private function get_values_from_array_or_string( $string ) {
200 $string = ac_helper()->array->implode_recursive( ',', $string );
201
202 return ac_helper()->string->comma_separated_to_array( $string );
203 }
204
205 /**
206 * @param string|array $string
207 *
208 * @return array
209 */
210 private function get_ids_from_array_or_string( $string ) {
211 $string = ac_helper()->array->implode_recursive( ',', $string );
212
213 return ac_helper()->string->string_to_array_integers( $string );
214 }
215
216 public function format( $value, $original_value ) {
217
218 switch ( $this->get_field_type() ) {
219
220 case self::TYPE_ARRAY :
221 if ( ac_helper()->array->is_associative( $value ) ) {
222 $value = ac_helper()->array->implode_associative( $value, __( ', ' ) );
223 } else {
224 $value = ac_helper()->array->implode_recursive( __( ', ' ), $value );
225 }
226
227 break;
228 case self::TYPE_DATE :
229 $timestamp = ac_helper()->date->strtotime( $value );
230 if ( $timestamp ) {
231 $value = date( 'c', $timestamp );
232 }
233
234 break;
235 case self::TYPE_POST :
236 $values = [];
237 foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) {
238 $post = get_post( $id );
239 $values[] = ac_helper()->html->link( get_edit_post_link( $post ), $post->post_title );
240 }
241
242 $value = implode( ac_helper()->html->divider(), $values );
243
244 break;
245 case self::TYPE_USER :
246 $values = [];
247 foreach ( $this->get_ids_from_array_or_string( $value ) as $id ) {
248 $user = get_userdata( $id );
249 $values[] = ac_helper()->html->link( get_edit_user_link( $id ), ac_helper()->user->get_display_name( $user ) );
250 }
251
252 $value = implode( ac_helper()->html->divider(), $values );
253
254 break;
255 case self::TYPE_IMAGE :
256 $value = new Collection( $this->get_values_from_array_or_string( $value ) );
257
258 break;
259 case self::TYPE_MEDIA :
260 $value = new Collection( $this->get_ids_from_array_or_string( $value ) );
261
262 break;
263 case self::TYPE_BOOLEAN :
264 $is_true = ! empty( $value ) && 'false' !== $value && '0' !== $value;
265
266 if ( $is_true ) {
267 $value = ac_helper()->icon->dashicon( [ 'icon' => 'yes', 'class' => 'green' ] );
268 } else {
269 $value = ac_helper()->icon->dashicon( [ 'icon' => 'no-alt', 'class' => 'red' ] );
270 }
271
272 break;
273 case self::TYPE_COLOR :
274
275 if ( $value && is_scalar( $value ) ) {
276 $value = ac_helper()->string->get_color_block( $value );
277 } else {
278 $value = false;
279 }
280
281 break;
282 case self::TYPE_COUNT :
283
284 if ( $this->column instanceof AC\Column\Meta ) {
285 $value = $this->column->get_meta_value( $original_value, $this->column->get_meta_key(), false );
286
287 if ( $value ) {
288 if ( 1 === count( $value ) && is_array( $value[0] ) ) {
289
290 // Value contains a single serialized array with multiple values
291 $value = count( $value[0] );
292 } else {
293
294 // Count multiple usage of meta keys
295 $value = count( $value );
296 }
297 } else {
298 $value = false;
299 }
300 }
301
302 break;
303 case self::TYPE_NON_EMPTY :
304 $value = ac_helper()->icon->yes_or_no( $value, $value );
305
306 break;
307 default :
308 $value = ac_helper()->array->implode_recursive( __( ', ' ), $value );
309 }
310
311 return $value;
312 }
313
314 /**
315 * @return string
316 */
317 public function get_field_type() {
318 return $this->field_type;
319 }
320
321 /**
322 * @param string $field_type
323 *
324 * @return bool
325 */
326 public function set_field_type( $field_type ) {
327 $this->field_type = $field_type;
328
329 return true;
330 }
331
332 }