CustomField.php
2.34 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
<?php
namespace AC\Settings\Column;
use AC;
use AC\Settings;
use AC\View;
class CustomField extends Settings\Column {
const NAME = 'custom_field';
/**
* @var string
*/
private $field;
protected function define_options() {
return [ 'field' ];
}
/**
* @return View
*/
public function create_view() {
$view = new View( [
'label' => __( 'Field', 'codepress-admin-columns' ),
'setting' => $this->get_setting_field(),
] );
return $view;
}
protected function set_name() {
$this->name = self::NAME;
}
private function use_text_field() {
return (bool) apply_filters( 'ac/column/custom_field/use_text_input', false );
}
/**
* @return AC\Form\Element\Input
*/
private function get_settings_field_text() {
return $this->create_element( 'text', 'field' )
->set_attribute( 'placeholder', 'Custom field key' );
}
/**
* @return AC\Form\Element\Select
*/
private function get_settings_field_select() {
$options = $this->get_field()
? [ $this->get_field() => $this->get_field() ]
: [];
return $this->create_element( 'select', 'field' )
->set_attribute( 'data-selected', $this->get_field() )
->set_attribute( 'data-post_type', $this->get_post_type() )
->set_attribute( 'data-type', $this->get_meta_type() )
->set_options( $options )
->set_attribute( 'class', 'custom_field' );
}
/**
* @return AC\Form\Element
*/
protected function get_setting_field() {
return $this->use_text_field()
? $this->get_settings_field_text()
: $this->get_settings_field_select();
}
public function get_dependent_settings() {
return [ new Settings\Column\CustomFieldType( $this->column ) ];
}
protected function get_meta_type() {
return $this->column->get_list_screen()->get_meta_type();
}
/**
* @return string Post type
*/
protected function get_post_type() {
return $this->column->get_post_type();
}
/**
* @return string
*/
public function get_field() {
return $this->field;
}
/**
* @param string $field
*
* @return self
*/
public function set_field( $field ) {
// Backwards compatible for WordPress Settings API not storing fields starting with _
if ( $field && 0 === strpos( $field, 'cpachidden' ) ) {
$field = substr( $field, strlen( 'cpachidden' ) );
}
$this->field = $field;
return $this;
}
}