PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 2.8.23.5
Pods – Custom Content Types and Fields v2.8.23.5
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Admin / Config / Field.php
pods / src / Pods / Admin / Config Last commit date
Base.php 1 day ago Field.php 1 day ago Group.php 1 day ago Pod.php 1 day ago
Field.php
558 lines
1 <?php
2
3 namespace Pods\Admin\Config;
4
5 use PodsForm;
6 use Tribe__Main;
7
8 /**
9 * Field configuration class.
10 *
11 * @since 2.8.0
12 */
13 class Field extends Base {
14
15 /**
16 * Get list of tabs for the Field object.
17 *
18 * @since 2.8.0
19 *
20 * @param \Pods\Whatsit\Pod $pod The pod object.
21 *
22 * @return array List of tabs for the Field object.
23 */
24 public function get_tabs( \Pods\Whatsit\Pod $pod ) {
25 $core_tabs = [
26 'basic' => __( 'Field Details', 'pods' ),
27 ];
28
29 $field_types = PodsForm::field_types();
30
31 foreach ( $field_types as $type => $field_type_data ) {
32 $core_tabs[ 'additional-field-' . $type ] = [
33 'name' => 'additional-field-' . $type,
34 /* translators: %s: Field type label. */
35 'label' => sprintf( _x( '%s Options', 'Field type options', 'pods' ), $field_type_data['label'] ),
36 'depends-on' => [
37 'type' => $type,
38 ],
39 ];
40 }
41
42 $core_tabs['advanced'] = __( 'Advanced', 'pods' );
43
44 // Only include kitchen sink if dev mode on and not running Codecept tests.
45 if ( pods_developer() && ! function_exists( 'codecept_debug' ) ) {
46 $core_tabs['kitchen-sink'] = __( 'Kitchen Sink (temp)', 'pods' );
47 }
48
49 $pod_type = $pod['type'];
50 $pod_name = $pod['name'];
51
52 $tabs = $core_tabs;
53
54 /**
55 * Filter the Pod Field option tabs for a specific pod type and name.
56 *
57 * @since 2.8.0
58 *
59 * @param array $core_tabs Tabs to set.
60 * @param \Pods\Whatsit\Pod $pod Current Pods object.
61 */
62 $tabs = (array) apply_filters( "pods_admin_setup_edit_field_tabs_{$pod_type}_{$pod_name}", $tabs, $pod );
63
64 /**
65 * Filter the Pod Field option tabs for a specific pod type.
66 *
67 * @since 2.8.0
68 *
69 * @param array $tabs Tabs to set.
70 * @param \Pods\Whatsit\Pod $pod Current Pods object.
71 */
72 $tabs = (array) apply_filters( "pods_admin_setup_edit_field_tabs_{$pod_type}", $tabs, $pod );
73
74 /**
75 * Filter the Pod Field option tabs.
76 *
77 * @since 2.8.0
78 *
79 * @param array $tabs Tabs to set.
80 * @param \Pods\Whatsit\Pod $pod Current Pods object.
81 */
82 $tabs = (array) apply_filters( 'pods_admin_setup_edit_field_tabs', $tabs, $pod );
83
84 // Sort and then enforce the core tabs to be in front.
85 uksort( $tabs, 'strnatcmp' );
86
87 $tabs = array_merge( $core_tabs, $tabs );
88
89 return $tabs;
90 }
91
92 /**
93 * Get list of fields for the Field object.
94 *
95 * @since 2.8.0
96 *
97 * @param \Pods\Whatsit\Pod $pod The pod object.
98 * @param array $tabs The list of tabs for the pod object.
99 *
100 * @return array List of fields for the Field object.
101 */
102 public function get_fields( \Pods\Whatsit\Pod $pod, array $tabs ) {
103 $field_types = PodsForm::field_types();
104 $tableless_field_types = PodsForm::tableless_field_types();
105
106 $options = [];
107
108 $options['basic'] = [
109 'label' => [
110 'name' => 'label',
111 'label' => __( 'Label', 'pods' ),
112 'type' => 'text',
113 'default' => '',
114 'help' => 'help',
115 'required' => true,
116 ],
117 'name' => [
118 'name' => 'name',
119 'label' => __( 'Name', 'pods' ),
120 'type' => 'slug',
121 'default' => '',
122 'attributes' => [
123 'maxlength' => 50,
124 ],
125 'help' => 'help',
126 'required' => true,
127 ],
128 'description' => [
129 'name' => 'description',
130 'label' => __( 'Description', 'pods' ),
131 'type' => 'text',
132 'default' => '',
133 'help' => 'help',
134 ],
135 'type' => [
136 'name' => 'type',
137 'label' => __( 'Field Type', 'pods' ),
138 'type' => 'pick',
139 'default' => 'text',
140 'required' => true,
141 'data' => [],
142 'dependency' => true,
143 'help' => 'help',
144 ],
145 'pick_object' => [
146 'name' => 'pick_object',
147 'label' => __( 'Related Type', 'pods' ),
148 'type' => 'pick',
149 'default' => '',
150 'required' => true,
151 'data' => [],
152 'pick_show_select_text' => 0,
153 'dependency' => true,
154 'depends-on' => [
155 'type' => 'pick',
156 ],
157 'help' => 'help',
158 ],
159 'pick_custom' => [
160 'name' => 'pick_custom',
161 'label' => __( 'Custom Defined Options', 'pods' ),
162 'type' => 'paragraph',
163 'default' => '',
164 'required' => true,
165 'depends-on' => [
166 'type' => 'pick',
167 'pick_object' => 'custom-simple',
168 ],
169 'help' => __( 'One option per line, use <em>value|Label</em> for separate values and labels', 'pods' ),
170 ],
171 'pick_table' => [
172 'name' => 'pick_table',
173 'label' => __( 'Related Table', 'pods' ),
174 'type' => 'pick',
175 'default' => '',
176 'data' => [],
177 'pick_show_select_text' => 0,
178 'depends-on' => [
179 'type' => 'pick',
180 'pick_object' => 'table',
181 ],
182 'help' => 'help',
183 ],
184 'sister_id' => [
185 'name' => 'sister_id',
186 'label' => __( 'Bi-directional Field', 'pods' ),
187 'type' => 'pick',
188 'default' => '',
189 'data' => [],
190 'depends-on' => [
191 'type' => 'pick',
192 'pick_object' => PodsForm::field_method( 'pick', 'bidirectional_objects' ),
193 ],
194 'help' => __( 'Bi-directional fields will update their related field for any item you select. This feature is only available for two relationships between two Pods.<br /><br />For example, when you update a Parent pod item to relate to a Child item, when you go to edit that Child item you will see the Parent pod item selected.', 'pods' ),
195 ],
196 'required' => [
197 'name' => 'required',
198 'label' => __( 'Required', 'pods' ),
199 'type' => 'boolean',
200 'default' => 0,
201 'boolean_yes_label' => '',
202 'help' => __( 'This will require a non-empty value to be entered.', 'pods' ),
203 ],
204 ];
205 $options['advanced'] = [
206 'visual' => [
207 'name' => 'visual',
208 'label' => __( 'Visual', 'pods' ),
209 'type' => 'heading',
210 ],
211 'class' => [
212 'name' => 'class',
213 'label' => __( 'Additional CSS Classes', 'pods' ),
214 'help' => __( 'help', 'pods' ),
215 'type' => 'text',
216 'default' => '',
217 ],
218 'values' => [
219 'name' => 'values',
220 'label' => __( 'Values', 'pods' ),
221 'type' => 'heading',
222 ],
223 'default_value' => [
224 'name' => 'default_value',
225 'label' => __( 'Default Value', 'pods' ),
226 'help' => __( 'help', 'pods' ),
227 'type' => 'text',
228 'default' => '',
229 'options' => [
230 'text_max_length' => - 1,
231 ],
232 ],
233 'default_value_parameter' => [
234 'name' => 'default_value_parameter',
235 'label' => __( 'Set Default Value via Parameter', 'pods' ),
236 'help' => __( 'help', 'pods' ),
237 'type' => 'text',
238 'default' => '',
239 ],
240 'visibility' => [
241 'name' => 'visibility',
242 'label' => __( 'Visibility', 'pods' ),
243 'type' => 'heading',
244 ],
245 'restrict_access' => [
246 'type' => 'boolean_group',
247 'name' => 'restrict_access',
248 'label' => __( 'Restrict Access', 'pods' ),
249 'boolean_group' => [
250 'logged_in_only' => [
251 'name' => 'logged_in_only',
252 'label' => __( 'Restrict access to Logged In Users', 'pods' ),
253 'default' => 0,
254 'type' => 'boolean',
255 'dependency' => true,
256 'help' => __( 'This field will only be able to be edited by logged in users. This is not required to be on for the other Restrict Access options to work.', 'pods' ),
257 ],
258 'admin_only' => [
259 'name' => 'admin_only',
260 'label' => __( 'Restrict access to Admins', 'pods' ),
261 'default' => 0,
262 'type' => 'boolean',
263 'dependency' => true,
264 'help' => __( 'This field will only be able to be edited by users with the ability to manage_options or delete_users, or super admins of a WordPress Multisite network', 'pods' ),
265 ],
266 'restrict_role' => [
267 'name' => 'restrict_role',
268 'label' => __( 'Restrict access by Role', 'pods' ),
269 'default' => 0,
270 'type' => 'boolean',
271 'dependency' => true,
272 ],
273 'restrict_capability' => [
274 'name' => 'restrict_capability',
275 'label' => __( 'Restrict access by Capability', 'pods' ),
276 'default' => 0,
277 'type' => 'boolean',
278 'dependency' => true,
279 ],
280 'hidden' => [
281 'name' => 'hidden',
282 'label' => __( 'Hide field from UI', 'pods' ),
283 'default' => 0,
284 'type' => 'boolean',
285 'help' => __( 'This option is overridden by access restrictions. If the user does not have access to edit this field, it will be hidden. If no access restrictions are set, this field will always be hidden.', 'pods' ),
286 ],
287 'read_only' => [
288 'name' => 'read_only',
289 'label' => __( 'Make field "Read Only" in UI', 'pods' ),
290 'default' => 0,
291 'type' => 'boolean',
292 'help' => __( 'This option is overridden by access restrictions. If the user does not have access to edit this field, it will be read only. If no access restrictions are set, this field will always be read only.', 'pods' ),
293 'depends-on' => [
294 'type' => [
295 'boolean',
296 'color',
297 'currency',
298 'date',
299 'datetime',
300 'email',
301 'number',
302 'paragraph',
303 'password',
304 'phone',
305 'slug',
306 'text',
307 'time',
308 'website',
309 ],
310 ],
311 ],
312 ],
313 ],
314 'roles_allowed' => [
315 'name' => 'roles_allowed',
316 'label' => __( 'Role(s) Allowed', 'pods' ),
317 'help' => __( 'help', 'pods' ),
318 'type' => 'pick',
319 'pick_object' => 'role',
320 'pick_format_type' => 'multi',
321 'default' => 'administrator',
322 'depends-on' => [
323 'restrict_role' => true,
324 ],
325 'help' => __( 'If none are selected, this option will be ignored.', 'pods' ),
326 ],
327 'capability_allowed' => [
328 'name' => 'capability_allowed',
329 'label' => __( 'Capability Allowed', 'pods' ),
330 'help' => __( 'Comma-separated list of capabilities, for example add_podname_item, please see the Roles and Capabilities component for the complete list and a way to add your own.', 'pods' ),
331 'type' => 'text',
332 'default' => '',
333 'depends-on' => [
334 'restrict_capability' => true,
335 ],
336 'help' => __( 'If none are selected, this option will be ignored.', 'pods' ),
337 ],
338 ];
339
340 $pick_tables = pods_transient_get( 'pods_tables' );
341
342 if ( empty( $pick_tables ) ) {
343 $pick_tables = [
344 '' => __( '-- Select Table --', 'pods' ),
345 ];
346
347 global $wpdb;
348
349 $tables = $wpdb->get_results( 'SHOW TABLES', ARRAY_N );
350
351 if ( ! empty( $tables ) ) {
352 foreach ( $tables as $table ) {
353 $pick_tables[ $table[0] ] = $table[0];
354 }
355 }
356
357 pods_transient_set( 'pods_tables', $pick_tables, WEEK_IN_SECONDS );
358 }
359
360 $field_settings = [
361 'field_types_select' => [],
362 'pick_object' => PodsForm::field_method( 'pick', 'related_objects', true ),
363 'pick_table' => $pick_tables,
364 'sister_id' => [
365 '' => __( 'No Related Fields Found', 'pods' ),
366 ],
367 ];
368
369 $pod_name = $pod['name'];
370 $pod_type = $pod['type'];
371
372 foreach ( $field_types as $type => $field_type_data ) {
373 /**
374 * @var $field_type PodsField
375 */
376 $field_type_object = PodsForm::field_loader( $type, $field_type_data['file'] );
377
378 $field_type_vars = get_class_vars( get_class( $field_type_object ) );
379
380 if ( ! isset( $field_type_vars['pod_types'] ) ) {
381 $field_type_vars['pod_types'] = true;
382 }
383
384 $options[ 'additional-field-' . $type ] = [];
385
386 // Only show supported field types
387 if ( true !== $field_type_vars['pod_types'] ) {
388 if ( empty( $field_type_vars['pod_types'] ) ) {
389 continue;
390 } elseif ( is_array( $field_type_vars['pod_types'] ) && ! in_array( $pod_type, $field_type_vars['pod_types'], true ) ) {
391 continue;
392 } elseif ( ! is_array( $field_type_vars['pod_types'] ) && $pod_type !== $field_type_vars['pod_types'] ) {
393 continue;
394 }
395 }
396
397 if ( ! empty( PodsForm::$field_group ) ) {
398 if ( ! isset( $field_settings['field_types_select'][ PodsForm::$field_group ] ) ) {
399 $field_settings['field_types_select'][ PodsForm::$field_group ] = [];
400 }
401
402 $field_settings['field_types_select'][ PodsForm::$field_group ][ $type ] = $field_type_data['label'];
403 } else {
404 if ( ! isset( $field_settings['field_types_select'][ __( 'Other', 'pods' ) ] ) ) {
405 $field_settings['field_types_select'][ __( 'Other', 'pods' ) ] = [];
406 }
407
408 $field_settings['field_types_select'][ __( 'Other', 'pods' ) ][ $type ] = $field_type_data['label'];
409 }
410
411 $type_options = PodsForm::ui_options( $type );
412
413 $dev_mode = pods_developer();
414
415 if ( ! $dev_mode ) {
416 foreach ( $type_options as $type_option => $option_data ) {
417 if ( ! empty( $option_data['developer_mode'] ) ) {
418 unset( $type_options[ $type_option ] );
419 }
420 }
421 }
422
423 /**
424 * Modify Additional Field Options tab
425 *
426 * @since 2.7.0
427 *
428 * @param array $type_options Additional field type options,
429 * @param string $type Field type,
430 * @param array $options Tabs, indexed by label,
431 * @param null|\Pods\Whatsit\Pod $pod Pods object for the Pod this UI is for.
432 */
433 $type_options = apply_filters( "pods_admin_setup_edit_{$type}_additional_field_options", $type_options, $type, $options, $pod );
434 $type_options = apply_filters( 'pods_admin_setup_edit_additional_field_options', $type_options, $type, $options, $pod );
435
436 $options[ 'additional-field-' . $type ] = $type_options;
437 }//end foreach
438
439 /**
440 * Allow filtering the field settings by pod name.
441 *
442 * @param array $field_settings List of field settings to use.
443 * @param null|\Pods\Whatsit\Pod $pod Pods object for the Pod this UI is for.
444 * @param array $tabs List of registered tabs
445 */
446 $field_settings = apply_filters( "pods_field_settings_{$pod_name}", $field_settings, $pod );
447
448 /**
449 * Allow filtering the field settings by pod name.
450 *
451 * @param array $field_settings List of field settings to use.
452 * @param null|\Pods\Whatsit\Pod $pod Pods object for the Pod this UI is for.
453 * @param array $tabs List of registered tabs
454 */
455 $field_settings = apply_filters( 'pods_field_settings', $field_settings, $pod, $tabs );
456
457 $options['basic']['type']['data'] = $field_settings['field_types_select'];
458 $options['basic']['pick_object']['data'] = $field_settings['pick_object'];
459 $options['basic']['pick_table']['data'] = $field_settings['pick_table'];
460
461 // @todo Look into supporting these in the future.
462 /*Tribe__Main::array_insert_after_key( 'visibility', $options['advanced'], [
463 'search' => [
464 'label' => __( 'Include in searches', 'pods' ),
465 'help' => __( 'help', 'pods' ),
466 'default' => 1,
467 'type' => 'boolean',
468 ],
469 ] );
470
471 Tribe__Main::array_insert_after_key( 'validation', $options['advanced'], [
472 'regex_validation' => [
473 'label' => __( 'RegEx Validation', 'pods' ),
474 'help' => __( 'help', 'pods' ),
475 'type' => 'text',
476 'default' => '',
477 ],
478 'message_regex' => [
479 'label' => __( 'Message if field does not pass RegEx', 'pods' ),
480 'help' => __( 'help', 'pods' ),
481 'type' => 'text',
482 'default' => '',
483 ],
484 'message_required' => [
485 'label' => __( 'Message if field is blank', 'pods' ),
486 'help' => __( 'help', 'pods' ),
487 'type' => 'text',
488 'default' => '',
489 'depends-on' => [ 'required' => true ],
490 ],
491 'message_unique' => [
492 'label' => __( 'Message if field is not unique', 'pods' ),
493 'help' => __( 'help', 'pods' ),
494 'type' => 'text',
495 'default' => '',
496 'depends-on' => [ 'unique' => true ],
497 ],
498 ] );*/
499
500 if ( 'table' === $pod['storage'] || 'pod' === $pod['type'] ) {
501 $options['basic']['unique'] = [
502 'name' => 'unique',
503 'label' => __( 'Unique', 'pods' ),
504 'type' => 'boolean',
505 'default' => 0,
506 'boolean_yes_label' => '',
507 'help' => __( 'This will require that the field value entered is unique and has not been saved before.', 'pods' ),
508 'excludes-on' => [
509 'type' => $tableless_field_types,
510 ],
511 ];
512 }
513
514 // Only include kitchen sink if dev mode on and not running Codecept tests.
515 if ( pods_developer() && ! function_exists( 'codecept_debug' ) ) {
516 $options['kitchen-sink'] = json_decode( file_get_contents( PODS_DIR . 'tests/codeception/_data/kitchen-sink-config.json' ), true );
517 }
518
519 $pod_type = $pod['type'];
520 $pod_name = $pod['name'];
521
522 /**
523 * Add admin fields to the Pod Fields editor for a specific Pod.
524 *
525 * @since 2.8.0
526 *
527 * @param array $options The Options fields.
528 * @param \Pods\Whatsit\Pod $pod Current Pods object.
529 * @param array $tabs List of registered tabs.
530 */
531 $options = apply_filters( "pods_admin_setup_edit_field_options_{$pod_type}_{$pod_name}", $options, $pod, $tabs );
532
533 /**
534 * Add admin fields to the Pod Fields editor for any Pod of a specific content type.
535 *
536 * @since 2.8.0
537 *
538 * @param array $options The Options fields.
539 * @param \Pods\Whatsit\Pod $pod Current Pods object.
540 * @param array $tabs List of registered tabs.
541 */
542 $options = apply_filters( "pods_admin_setup_edit_field_options_{$pod_type}", $options, $pod, $tabs );
543
544 /**
545 * Add admin fields to the Pod Fields editor for all Pods.
546 *
547 * @since 2.8.0
548 *
549 * @param array $options The Options fields.
550 * @param \Pods\Whatsit\Pod $pod Current Pods object.
551 * @param array $tabs List of registered tabs.
552 */
553 $options = apply_filters( 'pods_admin_setup_edit_field_options', $options, $pod, $tabs );
554
555 return $options;
556 }
557 }
558