forked from creativecommons/commoners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-cc-chapters-metabox.php
288 lines (285 loc) · 11 KB
/
class-cc-chapters-metabox.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
use Queulat\Metabox;
use Queulat\Forms\Node_Factory;
use Queulat\Forms\Element\WP_Media;
use Queulat\Forms\Element\Input_Url;
use Queulat\Forms\Element\WP_Editor;
use Queulat\Forms\Element\Input_Text;
use Queulat\Forms\Element\Input;
use Queulat\Forms\Element\Select;
use Queulat\Forms\Element\UI_Select2;
use Queulat\Forms\Element\Input_Number;
use Queulat\Forms\Element\Input_Email;
class Chapters_Metabox extends Metabox
{
public function __construct($id = '', $title = '', $post_type = '', array $args = array()) {
parent::__construct($id, $title, $post_type, $args);
add_action("wp_ajax_event-metabox__search-members", [$this, 'search_members']);
add_action("wp_ajax_event-metabox__get_countries", [$this, 'get_countries']);
}
public function get_countries() {
$search = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING);
$country_id = (array)get_post_meta(get_the_ID(), 'cc_chapter_chapter_country', false);
$countries = $countries = GF_Field_Address::get_countries();
$results = [];
foreach ($countries as $key=>$country) {
if (!empty($search)) {
if (stripos($country, $search) !== false) {
$results[] = [
'id' => $key,
'text' => $country,
'selected' => in_array($key, $country_id, true)
];
}
} else {
$results[] = [
'id' => $key,
'text' => $country,
'selected' => in_array($key, $country_id, true)
];
}
}
return wp_send_json([
'results' => $results
]);
}
public function search_members()
{
$search = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING);
$existing = (array)get_post_meta(filter_input(INPUT_GET, 'chapter_id', FILTER_SANITIZE_NUMBER_INT), 'cc_chapters_chapter_lead', false);
//$existing = array_map('absint', $existing);
$members_query = new WP_User_Query(array(
'search' => $search.'*',
'meta_key' => 'ccgn-application-state',
'meta_value' => 'accepted'
));
$people = $members_query->get_results();
$results = [];
if (!empty($people)) {
foreach ($people as $person) {
$results[] = [
'id' => $person->ID,
'text' => $person->data->display_name,
'selected' => in_array($person->ID, $existing, true)
];
}
}
return wp_send_json([
'results' => $results
]);
}
public function get_fields() : array
{
return [
Node_Factory::make(
Select::class,
[
'name' => 'chapter_status',
'label' => 'Chapter Status',
'attributes' => [
'class' => 'widefat'
],
'properties' => [
'description' => 'Choose the Status of the current chapter',
],
'options' => (function () {
$status = array(
'' => 'Choose',
'active' => 'Active',
'inactive' => 'Inactive',
'in-progress' => 'In Progress'
);
return $status;
})()
]
),
Node_Factory::make(
Input::class,
[
'name' => 'date',
'label' => 'Date founded',
'attributes' => [
'type' => 'date'
],
'properties' => [
'description' => 'Date when chapter was founded'
]
]
),
Node_Factory::make(
Input_Email::class,
[
'name' => 'email',
'label' => 'Email',
'properties' => [
'description' => 'Chapter contact email'
]
]
),
Node_Factory::make(
UI_Select2::class,
[
'name' => 'chapter_country',
'label' => 'Chapter Country',
'attributes' => [
'class' => 'widefat'
],
'properties' => [
'instance' => [
'multiple' => false,
'minimumInputLength' => 3
// 'ajax' => [
// 'url' => admin_url('admin-ajax.php?action=event-metabox__get_countries'),
// ]
],
'description' => 'Choose the Chapter Country',
],
'options' => (function () {
// $country_id = (array)get_post_meta(get_the_ID(), 'cc_chapter_chapter_country', false);
// if (!$country_id) {
// return [];
// }
$countries = GF_Field_Address::get_countries();
if (!empty($countries)) {
return $countries;
}
return [];
})()
]
),
Node_Factory::make(
UI_Select2::class,
[
'name' => 'chapter_lead',
'label' => 'Chapter Lead',
'attributes' => [
'class' => 'widefat'
],
'properties' => [
'instance' => [
'multiple' => false,
'minimumInputLength' => 3,
'ajax' => [
'url' => admin_url('admin-ajax.php?action=event-metabox__search-members&chapter_id=' . get_the_ID()),
]
],
'description' => 'Choose the Chapter Lead from the list'
],
'options' => (function () {
$author_ids = get_post_meta(get_the_ID(), 'cc_chapters_chapter_lead', false);
if (!$author_ids) {
return [];
}
$people = get_users([
'meta_key' => 'ccgn-application-state',
'meta_value' => 'accepted',
'include' => $author_ids,
'search' => $search
]);
if (!empty($people)) {
return wp_list_pluck($people, 'display_name', 'ID');
}
return [];
})()
]
),
Node_Factory::make(
UI_Select2::class,
[
'name' => 'member_gnc',
'label' => 'Global Network Council representative',
'attributes' => [
'class' => 'widefat'
],
'properties' => [
'instance' => [
'multiple' => false,
'minimumInputLength' => 3,
'ajax' => [
'url' => admin_url('admin-ajax.php?action=event-metabox__search-members&chapter_id=' . get_the_ID()),
]
],
'description' => 'Choose the Member of the representative to the network council'
],
'options' => (function () {
$author_ids = (array)get_post_meta(get_the_ID(), 'cc_chapters_member_gnc', false);
if (!$author_ids) {
return [];
}
$people = get_users([
'meta_key' => 'ccgn-application-state',
'meta_value' => 'accepted',
'include' => $author_ids,
'search' => $search
]);
if (!empty($people)) {
return wp_list_pluck($people, 'display_name', 'ID');
}
return [];
})()
]
),
Node_Factory::make(
Input_Url::class,
[
'name' => 'url',
'label' => 'URL',
'attributes' => [
'class' => 'widefat',
'placeholder' => 'Chapter URL',
'type' => 'url'
]
]
),
Node_Factory::make(
Input_Url::class,
[
'name' => 'meeting_url',
'label' => 'Meeting URL',
'attributes' => [
'class' => 'widefat',
'placeholder' => 'Chapter first meeting URL',
'type' => 'url'
],
'properties' => [
'description' => 'Chapter first meeting URL'
]
]
)
];
}
public function sanitize_data(array $data) : array
{
//Delete stats transient to update the global chapter stats
delete_transient('ccgn_global_stats');
$sanitized = [];
foreach ($data as $key => $val) {
switch ($key) {
case 'date':
$sanitized[$key] = $val;
break;
case 'email':
$sanitized[$key] = $val;
break;
case 'chapter_country':
$sanitized[$key] = $val;
case 'chapter_lead':
$sanitized[$key] = $val;
case 'member_gnc':
$sanitized[$key] = $val;
break;
case 'url':
$sanitized[$key] = esc_url_raw($val);
break;
case 'meeting_url':
$sanitized[$key] = esc_url_raw($val);
break;
case 'chapter_status':
$sanitized[$key] = $val;
break;
}
}
return $sanitized;
}
}
new Chapters_Metabox('cc_chapters', 'Chapter information', 'cc_chapters', ['context' => 'normal']);