Group.php
1.65 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
<?php
namespace ACA\MetaBox\Column;
use ACA\MetaBox\Column;
use ACA\MetaBox\Export;
use ACA\MetaBox\Setting;
use ACA\MetaBox\Value\ValueFormatterFactory;
class Group extends Column {
public function format_single_value( $value, $id = null ) {
$sub_value = $this->get_sub_field_value( $value );
$sub_field_setting = $this->get_sub_field_settings();
if ( $sub_field_setting === null || null === $sub_value ) {
return $this->get_empty_char();
}
$formatter = ( new ValueFormatterFactory() )->create( $this, $sub_field_setting );
$value = $formatter->format( $sub_value, $id );
return is_array( $value )
? implode( $this->get_separator(), $value )
: $value;
}
public function get_sub_field_value( $value ) {
$field = $this->get_sub_field();
if ( ! $field || ! is_array( $value ) ) {
return null;
}
return $value[ $field ] ?? null;
}
public function get_sub_field(): ?string {
$setting = $this->get_setting( 'group_field' );
return $setting instanceof Setting\GroupField
? $setting->get_value()
: null;
}
private function get_sub_field_settings(): ?array {
$setting = $this->get_setting( 'group_field' );
return $setting instanceof Setting\GroupField
? $setting->get_group_field_settings()
: null;
}
protected function register_settings(): void {
$this->add_setting( new Setting\GroupField( $this ) );
}
public function export() {
$type = $this->get_sub_field_settings() ?? '';
switch ( $type ) {
case 'single_image':
case 'image':
case 'image_advanced':
case 'image_upload':
return new Export\Model\Group\Image( $this );
default:
return new Export\Model\StrippedValue( $this );
}
}
}