Meta.php
4.75 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace AC\Settings\Column;
use AC\Form\Element\Select;
use AC\MetaType;
use AC\Settings\Column;
use AC\View;
abstract class Meta extends Column
{
/**
* @var string
*/
private $field;
abstract protected function get_meta_keys();
protected function define_options()
{
return ['field'];
}
/**
* @return Select
*/
protected function get_setting_field()
{
$setting = $this
->create_element('select', 'field')
->set_options($this->group_keys($this->get_cached_keys()))
->set_no_result(__('No fields available.', 'codepress-admin-columns'));
return $setting;
}
/**
* @return array|false
*/
protected function get_cached_keys()
{
$keys = $this->get_cache();
if ( ! $keys) {
$keys = $this->get_meta_keys();
$this->set_cache($keys);
}
return $keys;
}
/**
* @return string
*/
protected function get_cache_key()
{
return $this->column->get_list_key();
}
/**
* @return string
*/
protected function get_meta_type()
{
return $this->column->get_meta_type();
}
protected function get_cache_group()
{
return 'ac_settings_meta';
}
/**
* @return View
*/
public function create_view()
{
$view = new View([
'label' => __('Field', 'codepress-admin-columns'),
'setting' => $this->get_setting_field(),
]);
return $view;
}
/**
* @return string
*/
public function get_field()
{
return $this->field;
}
/**
* @param string $field
*
* @return bool
*/
public function set_field($field)
{
$this->field = $field;
return true;
}
private function get_cache()
{
return wp_cache_get($this->get_cache_key(), $this->get_cache_group());
}
/**
* @param array $data
* @param int $expire Seconds
*/
private function set_cache($data, $expire = 15)
{
wp_cache_add($this->get_cache_key(), $data, $this->get_cache_group(), $expire);
}
/**
* @return array
*/
protected function get_meta_groups()
{
global $wpdb;
$groups = [
'' => __('Public', 'codepress-admin-columns'),
'_' => __('Hidden', 'codepress-admin-columns'),
];
// User only
if (MetaType::USER === $this->get_meta_type()) {
if (is_multisite()) {
foreach (get_sites() as $site) {
$label = __('Network Site:', 'codepress-admin-columns') . ' ' . ac_helper(
)->network->get_site_option($site->blog_id, 'blogname');
if (get_current_blog_id() == $site->blog_id) {
$label .= ' (' . __('current', 'codepress-admin-columns') . ')';
}
$groups[$wpdb->get_blog_prefix($site->blog_id)] = $label;
}
} else {
$groups[$wpdb->get_blog_prefix()] = __('Site Options', 'codepress-admin-columns');
}
}
return $groups;
}
/**
* @param array $keys
*
* @return array
*/
private function group_keys($keys)
{
if ( ! $keys) {
return [];
}
$grouped = [];
$groups = $this->get_meta_groups();
// groups are ordered desc because the prefixes without a blog id ( e.g. wp_ ) should be matched last.
krsort($groups);
foreach ($groups as $prefix => $title) {
$options = [];
foreach ($keys as $k => $key) {
// Match prefix with meta key
if ($prefix && 0 === strpos($key, $prefix)) {
$options[$key] = $key;
unset($keys[$k]);
}
}
if ($options) {
$grouped[$prefix] = [
'title' => $title,
'options' => $options,
];
}
}
ksort($grouped);
// Default group
if ($keys) {
$default = [
'title' => $groups[''],
'options' => array_combine($keys, $keys),
];
array_unshift($grouped, $default);
}
// Place the hidden group at the end
if (isset($grouped['_'])) {
$grouped[] = $grouped['_'];
unset($grouped['_']);
}
// Remove groups when there is only one group
if (1 === count($grouped)) {
$grouped = array_pop($grouped);
$grouped = $grouped['options'];
}
return $grouped;
}
}