Column.php
3.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace ACA\ACF;
use AC;
use AC\MetaType;
use ACA\ACF;
use ACA\ACF\Export;
use ACA\ACF\Value;
use ACP\Export\Exportable;
use InvalidArgumentException;
class Column extends AC\Column\Meta
implements Exportable, ACF\Filtering\FilteringFactoryAware, ACF\Search\SearchFactoryAware, ACF\Sorting\SortingFactoryAware, ACF\Editing\EditingFactoryAware, ACF\ConditionalFormatting\FormattableFactoryAware {
use ACF\Filtering\FilteringTrait,
ACF\Search\SearchableTrait,
ACF\Sorting\SortableTrait,
ACF\Editing\EditableTrait,
ACF\ConditionalFormatting\FormattableTrait;
/**
* @var string
*/
protected $field_hash;
/**
* @var string
*/
protected $meta_key;
/**
* @var Field
*/
protected $field;
/**
* @var string
*/
private $field_type;
public function __construct() {
$this->set_label( 'ACF Column' )
->set_group( ColumnGroup::SLUG );
}
public function set_config( array $config ) {
$this->field_hash = $config[ Configurable::FIELD_HASH ];
$this->meta_key = $config[ Configurable::META_KEY ];
$this->field = $config[ Configurable::FIELD ];
$this->field_type = $config[ Configurable::FIELD_TYPE ];
$this->validate();
}
private function validate(): void {
if ( ! is_string( $this->field_hash ) ) {
throw new InvalidArgumentException( 'Invalid field hash' );
}
if ( ! is_string( $this->meta_key ) ) {
throw new InvalidArgumentException( 'Invalid meta key' );
}
if ( ! $this->field instanceof Field ) {
throw new InvalidArgumentException( 'Invalid field' );
}
if ( ! is_string( $this->field_type ) ) {
throw new InvalidArgumentException( 'Invalid field type' );
}
}
public function get_value( $id ) {
$factory = new Value\FormatterFactory();
return $factory->create(
$this,
$this->get_field()
)->format(
$this->get_raw_value( $id ),
$id
);
}
public function get_raw_value( $id ) {
return get_field( $this->meta_key, $this->get_formatted_id( $id ), false );
}
public function get_formatted_id( $id ) {
switch ( $this->get_meta_type() ) {
case MetaType::USER:
$prefix = 'user_';
break;
case MetaType::COMMENT:
$prefix = 'comment_';
break;
case MetaType::SITE:
$prefix = 'site_';
break;
case MetaType::TERM:
$prefix = $this->get_taxonomy() . '_';
break;
default:
$prefix = '';
}
return $prefix . $id;
}
protected function register_settings() {
$setting_factory = new ACF\Settings\SettingFactory();
$settings = $setting_factory->create(
$this->get_field(),
$this
);
array_map( [ $this, 'add_setting' ], $settings );
}
public function export() {
return ( new Export\ModelFactory )->create( $this->get_field_type(), $this );
}
public function get_meta_key() {
return $this->meta_key;
}
public function get_field_hash() {
return $this->field_hash;
}
public function get_field_type() {
return $this->field_type;
}
/**
* @return Field
*/
public function get_field() {
return $this->field;
}
/**
* @return array
* @deprecated 6.0
*/
public function get_acf_field() {
_deprecated_function( __METHOD__, '6.0' );
return $this->field->get_settings();
}
/**
* @param string $name
*
* @return string
* @deprecated 6.0
*/
public function get_acf_field_option( $name ) {
_deprecated_function( __METHOD__, '6.0' );
$settings = $this->field->get_settings();
return array_key_exists( $name, $settings ) ? $settings[ $name ] : '';
}
}