Column.php
3.16 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
<?php
namespace ACA\MetaBox;
use AC;
use ACA\MetaBox\Export;
use ACP;
use Metabox\CustomTable;
class Column extends AC\Column\Meta
implements ACP\Export\Exportable, StorageAware {
/**
* @var array
*/
private $field_settings;
public function __construct() {
$this->set_group( 'metabox' );
$this->set_label( 'MetaBox' );
}
public function get_value( $id ) {
if ( $this->is_clonable() ) {
return $this->get_multiple_values( $id );
}
return $this->format_single_value( rwmb_get_value( $this->get_meta_key(), [ 'object_type' => $this->get_meta_type() ], $id ), $id );
}
public function format_single_value( $value, $id = null ) {
if ( ! $value ) {
return $this->get_empty_char();
}
return $this->get_formatted_value( $value, $id );
}
public function get_storage() {
if ( $this->get_field_setting( 'storage' ) instanceof CustomTable\Storage ) {
return self::CUSTOM_TABLE;
}
return self::META_BOX;
}
/**
* @return string|false
*/
public function get_storage_table() {
return $this->get_field_setting( 'ac_storage_table' );
}
/**
* @return string
*/
protected function get_clone_divider() {
return '<div class="ac-mb-divider"></div>';
}
public function get_multiple_values( $id ) {
$value = rwmb_get_value( $this->get_meta_key(), [ 'object_type' => $this->get_meta_type() ], $id );
if ( ! $value ) {
return $this->get_empty_char();
}
$collection = new AC\Collection( (array) rwmb_get_value( $this->get_meta_key(), [ 'object_type' => $this->get_meta_type() ], $id ) );
$result = [];
foreach ( $collection as $value ) {
$result[] = $this->format_single_value( $value );
}
return implode( $this->get_clone_divider(), $result );
}
public function get_field_setting( $key ) {
$settings = $this->get_field_settings();
if ( ! isset( $settings[ $key ] ) ) {
return false;
}
return $settings[ $key ];
}
public function get_field_settings() {
if ( ! empty( $this->field_settings ) ) {
return $this->field_settings;
}
$fields = rwmb_get_field_settings( $this->get_meta_key(), [ 'object_type' => $this->get_meta_type() ] );
if ( $fields ) {
$this->field_settings = $fields;
} else {
$this->field_settings = $this->get_meta_type_field_settings()[ $this->get_type() ];
}
return $this->field_settings;
}
/**
* @return array
*/
public function get_meta_type_field_settings() {
switch ( $this->get_meta_type() ) {
case 'user':
return rwmb_get_object_fields( 'user', 'user' );
case 'post':
return rwmb_get_object_fields( $this->get_post_type() );
case 'term':
return rwmb_get_object_fields( $this->get_list_screen()->get_taxonomy(), 'term' );
default:
return [];
}
}
/**
* @return bool
*/
public function is_multiple() {
$setting = $this->get_field_setting( 'multiple' );
return in_array( $setting, [ true, 'true', 1 ], true );
}
/**
* @return bool
*/
public function is_clonable() {
$setting = $this->get_field_setting( 'clone' );
return in_array( $setting, [ true, 'true', 1 ], true );
}
public function get_meta_key() {
return $this->get_type();
}
public function export() {
return ( new Export\Factory() )->create( $this );
}
}