GroupField.php
1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace ACA\MetaBox\Setting;
use AC;
use AC\View;
use ACA\MetaBox\Column;
use ACA\MetaBox\Column\Group;
class GroupField extends AC\Settings\Column {
public function __construct( Group $column ) {
parent::__construct( $column );
}
protected $group_field;
protected function define_options() {
return [
'group_field',
];
}
public function create_view(): View {
$select = $this->create_element( 'select' )
->set_attribute( 'data-refresh', 'column' )
->set_attribute( 'data-label', 'update' )
->set_options( $this->get_display_options() );
return new View( [
'label' => __( 'Group Field', 'codepress-admin-columns' ),
'setting' => $select,
] );
}
public function get_dependent_settings() {
$settings = $this->get_group_field_settings();
if ( ! $settings ) {
return [];
}
return ( new SubFieldSettingFactory() )->create( $settings, $this->column );
}
function get_display_options(): array {
if ( ! $this->column instanceof Column ) {
return [];
}
$fields = $this->column->get_field_setting( 'fields' );
if ( ! $fields ) {
return [];
}
$options = [];
foreach ( $fields as $field ) {
if ( in_array( $field['type'], [ 'group' ], true ) ) {
continue;
}
$options[ $field['id'] ] = $field['name'];
}
return $options;
}
public function get_group_field(): ?string {
return $this->group_field;
}
public function set_group_field( string $group_field = null ): void {
$this->group_field = $group_field;
}
public function get_group_field_settings(): ?array {
if ( ! $this->column instanceof Column ) {
return null;
}
$fields = $this->column->get_field_setting( 'fields' ) ?? [];
$key = array_search( $this->get_group_field(), array_column( $fields, 'id' ), true );
return $key === false ? null : $fields[ $key ];
}
}