Column.php
3.25 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
<?php
namespace ACA\Pods;
use AC;
use ACP;
use ACP\ConditionalFormat\FormattableConfig;
abstract class Column extends AC\Column\Meta
implements ACP\Editing\Editable, ACP\Sorting\Sortable, ACP\Filtering\Filterable, ACP\Export\Exportable, ACP\Search\Searchable, ACP\ConditionalFormat\Formattable {
/**
* @var array Pod settings
*/
private $pod;
/**
* @return string
*/
abstract protected function get_pod_name();
public function __construct() {
$this->set_type( 'column-pods' );
$this->set_label( __( 'Pods', 'codepress-admin-columns' ) );
$this->set_group( 'pods' );
}
public function get_meta_key() {
return $this->get_setting( 'pods_field' )->get_value();
}
public function get_value( $id ) {
$value = $this->get_field()->get_value( $id );
if ( $value instanceof AC\Collection ) {
$value = $value->filter()->implode( $this->get_separator() );
}
if ( ac_helper()->string->is_empty( $value ) ) {
return $this->get_empty_char();
}
return $value;
}
public function get_raw_value( $id ) {
return $this->get_field()->get_raw_value( $id );
}
public function get_separator() {
$separator = $this->get_field()->get_separator();
if ( $separator !== null ) {
return $separator;
}
return parent::get_separator();
}
public function editing() {
return $this->get_field()->editing();
}
public function sorting() {
return $this->get_field()->sorting();
}
public function filtering() {
return $this->get_field()->filtering();
}
public function export() {
return $this->get_field()->export();
}
public function search() {
return $this->get_field()->search();
}
public function conditional_format(): ?FormattableConfig {
return $this->get_field()->conditional_format();
}
protected function register_settings() {
$this->add_setting( new Setting\Field( $this ) );
}
/**
* Current Pod field settings
* @return false|array Field settings
*/
public function get_pod_field() {
$fields = $this->get_pod_fields();
return isset( $fields[ $this->get_meta_key() ] ) ? $fields[ $this->get_meta_key() ] : false;
}
/**
* @param string $property
*
* @return mixed|false
*/
public function get_pod_field_option( $property ) {
$field = $this->get_pod_field();
return isset( $field[ $property ] ) ? $field[ $property ] : false;
}
/**
* @return string|false
*/
public function get_field_type() {
return $this->get_pod_field_option( 'type' );
}
private function set_pod() {
add_filter( 'pods_error_exception', '__return_true', 12 ); // otherwise pods_error() will throw an exit
$pod = pods_api()->load_pod( [ 'name' => $this->get_pod_name() ] );
remove_filter( 'pods_error_exception', '__return_true', 12 );
$this->pod = isset( $pod['id'] ) ? $pod : false;
}
/**
* @return array
*/
public function get_pod() {
if ( null === $this->pod ) {
$this->set_pod();
}
return $this->pod;
}
/**
* @return Field
*/
public function get_field() {
$pick_object = $this->get_pod_field_option( 'pick_object' );
return ( new FieldFactory() )->create( $this->get_field_type(), $this, $pick_object ?: null );
}
/**
* @return false|array Field settings
*/
public function get_pod_fields() {
$pod = $this->get_pod();
return isset( $pod['fields'] ) ? $pod['fields'] : false;
}
}